summaryrefslogtreecommitdiffstats
path: root/tcllib/modules/crc/crc32.test
blob: f2366ebdcb646abffb77990426a889b00787f120 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# crc32.test - Copyright (C) 2002 Pat Thoyts <patthoyts@users.sourceforge.net>
#
# Tests for the crc32 commands
#
# -------------------------------------------------------------------------
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# -------------------------------------------------------------------------
# RCS: @(#) $Id: crc32.test,v 1.12 2009/03/04 01:01:42 patthoyts Exp $

# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.2
testsNeedTcltest 1.0

testing {
    useLocal crc32.tcl crc32 ::crc
}

# -------------------------------------------------------------------------

if {[::crc::LoadAccelerator critcl]} {
    puts "> critcl based"
}
if {[::crc::LoadAccelerator trf]} {
    puts "> Trf based"
}
puts "> pure Tcl"

# -------------------------------------------------------------------------
# Handle multiple implementation testing
#

array set preserve [array get ::crc::accel]

proc implementations {} {
    variable ::crc::accel
    foreach {a v} [array get accel] {if {$v} {lappend r $a}}
    lappend r tcl; set r
}

proc select_implementation {impl} {
    variable ::crc::accel
    foreach e [array names accel] { set accel($e) 0 }
    if {[string compare "tcl" $impl] != 0} {
        set accel($impl) 1
    }
}

proc reset_implementation {} {
    variable ::crc::accel
    array set accel [array get ::preserve]
}

# -------------------------------------------------------------------------

test crc32-1.0 {crc32 with no parameters } {
    catch {::crc::crc32} result
    string match "wrong # args: *" $result
} {1}

# -------------------------------------------------------------------------

set tests {
    1    ""
    "0"
    2    "a"
    "3904355907"
    3    "abc"
    "891568578"
    4    "message digest"
    "538287487"
    5    "abcdefghijklmnopqrstuvwxyz"
    "1277644989"
    6    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    "532866770"
    7    "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
    "2091469426"
    8    "\uFFFE\u0000\u0001\u0002"
    "2968055525"
    9    "-"
    "2547889144"
    10   "--"
    "606868581"
}
foreach impl [implementations] {
    select_implementation $impl
    foreach {n msg expected} $tests {
        test crc32-$impl-2.$n "crc32 as unsigned integer ($impl)" {
            list [catch {::crc::crc32 $msg} err] $err
        } [list 0 $expected]
    }
    reset_implementation
}

# -------------------------------------------------------------------------

set tests {
    1    ""
    "0x0"
    2    "a"
    "0xE8B7BE43"
    3    "abc"
    "0x352441C2"
    4    "message digest"
    "0x20159D7F"
    5    "abcdefghijklmnopqrstuvwxyz"
    "0x4C2750BD"
    6    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    "0x1FC2E6D2"
    7    "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
    "0x7CA94A72"
    8    "\uFFFE\u0000\u0001\u0002"
    "0xB0E8EEE5"
    9    "-"
    "0x97DDB3F8"
    10   "--"
    "0x242C1465"
}

foreach impl [implementations] {
    select_implementation $impl
    foreach {n msg expected} $tests {
        test crc32-$impl-3.$n "crc32 as hexadecimal string ($impl)" {
            list [catch {::crc::crc32 -format 0x%X $msg} err] $err
        } [list 0 $expected]
    }
    reset_implementation
}

# -------------------------------------------------------------------------

set crc::testfile [info script]

proc crc::loaddata {filename} {
    set f [open $filename r]
    fconfigure $f -translation binary
    set data [read $f]
    close $f
    return $data
}

foreach impl [implementations] {
    select_implementation $impl
    test crc32-$impl-4.0 "crc32 file option ($impl)" {
        set r1 [::crc::crc32 -file $crc::testfile]
        set r2 [::crc::crc32 [crc::loaddata $crc::testfile]]
        if {$r1 != $r2} {
            set r "differing results: $r1 != $r2"
        } else {
            set r ok
        }
    } {ok}
    reset_implementation
}

# -------------------------------------------------------------------------

set tests {
    1    0  ""
    "4294967295"
    2    1  ""
    "4294967294"
    3    0  "Hello, World!"
    "482441901"
    4    1  "Hello, World!"
    "3243746088"
    5    0  "-"
    "3122701194"
}
foreach impl [implementations] {
    select_implementation $impl
    foreach {n seed msg expected} $tests {
        test crc32-$impl-5.$n "crc32 initial seed option ($impl)" {
            list [catch {::crc::crc32 -seed $seed $msg} err] $err
        } [list 0 $expected]
    }
    reset_implementation
}

# -------------------------------------------------------------------------

set tests {
    1    "a" "bc"
    "891568578"
    2    "message" " digest"
    "538287487"
    3    "abcdefghijkl" "mnopqrstuvwxyz"
    "1277644989"
    4    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345678" "9"
    "532866770"
    5    "1234567890" 
    "1234567890123456789012345678901234567890123456789012345678901234567890"
    "2091469426"
    6    "\uFFFE\u0000" "\u0001\u0002"
    "2968055525"
}
foreach impl [implementations] {
    select_implementation $impl
    foreach {n msgA msgB expected} $tests {
        test crc32-$impl-6.$n "crc32 using -seed ($impl)" {
            list [catch {
                ::crc::crc32 -seed [expr {[::crc::crc32 $msgA] ^ 0xffffffff}] $msgB
            } err] $err
        } [list 0 $expected]
    }
    reset_implementation
}

# -------------------------------------------------------------------------

catch {unset crc::filename}
testsuiteCleanup

# Local Variables:
#  mode: tcl
#  indent-tabs-mode: nil
# End: