1# sum.test - Copyright (C) 2002 Pat Thoyts <patthoyts@users.sourceforge.net>
2#
3# Tests for the Tcllib sum command
4#
5# -------------------------------------------------------------------------
6# See the file "license.terms" for information on usage and redistribution
7# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
8# -------------------------------------------------------------------------
9# RCS: @(#) $Id: sum.test,v 1.8 2006/10/09 21:41:40 andreas_kupries Exp $
10
11# -------------------------------------------------------------------------
12
13source [file join \
14	[file dirname [file dirname [file join [pwd] [info script]]]] \
15	devtools testutilities.tcl]
16
17testsNeedTcl     8.4
18testsNeedTcltest 2.0
19
20testing {
21    useLocal sum.tcl sum ::crc
22}
23
24# -------------------------------------------------------------------------
25
26if {[info commands ::crc::SumBsd_c] == {}} {
27    puts "> pure tcl"
28} else {
29    puts "> critcl based"
30}
31
32# -------------------------------------------------------------------------
33
34test sum-1.0 {sum with no parameters} -body {
35   ::crc::sum
36} -returnCodes error -result {wrong # args: should be  "sum ?-bsd|-sysv? ?-format string? ?-chunksize size?  ?-timeout ms? -file name | -channel chan | data"}
37
38test sum-1.1 {sum with incorrect parameters} -body {
39   ::crc::sum -zxcv
40} -returnCodes error -result {bad option -zxcv: must be one of -bsd, -channel, -chunksize, -filename, -format, -sysv, -timeout}
41
42# -------------------------------------------------------------------------
43
44foreach {n msg expected} {
45    1    ""
46    "0"
47    2    "a"
48    "97"
49    3    "abc"
50    "16556"
51    4    "cba"
52    "49322"
53    5    "message digest"
54    "26423"
55    6    "abcdefghijklmnopqrstuvwxyz"
56    "53553"
57    7    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
58    "25587"
59    8    "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
60    "21845"
61    9    "\uFFFE\u0000\u0001\u0002"
62    "16418"
63} {
64    test sum-2.$n {sum using BSD algorithm and unsigned integer} -body {
65	::crc::sum -bsd $msg
66    } -result $expected
67}
68
69# -------------------------------------------------------------------------
70foreach {n msg expected} {
71    1    ""
72    "0"
73    2    "a"
74    "97"
75    3    "abc"
76    "294"
77    4    "cba"
78    "294"
79    5    "message digest"
80    "1413"
81    6    "abcdefghijklmnopqrstuvwxyz"
82    "2847"
83    7    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
84    "5387"
85    8    "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
86    "4200"
87    9    "\uFFFE\u0000\u0001\u0002"
88    "257"
89} {
90    test sum-3.$n {sum using SysV algorithm and unsigned integer} -body {
91	::crc::sum -sysv $msg
92    } -result $expected
93}
94
95# -------------------------------------------------------------------------
96
97set crc::testfile [info script]
98
99proc ::crc::loaddata {filename} {
100    set f [open $filename r]
101    fconfigure $f -translation binary
102    set data [read $f]
103    close $f
104    return $data
105}
106
107test sum-4.0 {sum file option (BSD)} -body {
108    set r1 [::crc::sum -bsd -file $::crc::testfile]
109    set r2 [::crc::sum -bsd [::crc::loaddata $::crc::testfile]]
110    if {$r1 != $r2} {
111        set r "differing results: $r1 != $r2"
112    } else {
113        set r ok
114    }
115} -result ok
116
117test sum-4.1 {sum file option (SysV)} -body {
118    set r1 [::crc::sum -sysv -file $::crc::testfile]
119    set r2 [::crc::sum -sysv [::crc::loaddata $crc::testfile]]
120    if {$r1 != $r2} {
121        set r "differing results: $r1 != $r2"
122    } else {
123        set r ok
124    }
125} -result ok
126
127test sum-4.2 {sum -channel option (BSD)} -body {
128    set r1 [::crc::sum -bsd [::crc::loaddata $::crc::testfile]]
129    set f [open $::crc::testfile r]
130    fconfigure $f -translation binary
131    set r2 [::crc::sum -bsd -channel $f]
132    close $f
133    if {$r1 != $r2} {
134        set r "differing results: $r1 != $r2"
135    } else {
136        set r ok
137    }
138} -result ok
139
140test sum-4.3 {sum -channel option (SysV)} -body {
141    set r1 [::crc::sum -sysv -file $::crc::testfile]
142    set f [open $::crc::testfile r]
143    fconfigure $f -translation binary
144    set r2 [::crc::sum -sysv -channel $f]
145    close $f
146    if {$r1 != $r2} {
147        set r "differing results: $r1 != $r2"
148    } else {
149        set r ok
150    }
151} -result ok
152
153# -------------------------------------------------------------------------
154
155test sum-5.0 {sum format option (BSD)} -body {
156    ::crc::sum -bsd -format 0x%X [string repeat x 200]
157} -result 0xF8EE
158
159test sum-5.1 {sum format option (SysV)} -body {
160    ::crc::sum -sysv -format 0x%X [string repeat x 200]
161} -result 0x5DC0
162
163# -------------------------------------------------------------------------
164# ticket a80e60deb1 vectors - data over 1 KB length.
165
166foreach {n expected base count} {
167    0 58625 a    1280
168    1 11010 fx    640
169    2 62980 \xfe 1280
170} {
171    test sum-6.$n {sum (sysv) over 1 K} -body {
172        crc::sum -sysv -- [string repeat $base $count]
173    } -result $expected
174}
175
176# -------------------------------------------------------------------------
177# ticket 0a3d5dfe52
178
179foreach {n expected base count suffix} {
180    0 65535 X 1489 &
181} {
182    test sum-7.$n "sum (sysv) ${base}*${count}" -body {
183        crc::sum -sysv -- [string repeat $base $count]$suffix
184    } -result $expected
185}
186
187# -------------------------------------------------------------------------
188
189catch {unset ::crc::testfile}
190testsuiteCleanup
191
192# Local Variables:
193#  mode: tcl
194#  indent-tabs-mode: nil
195# End:
196
197