File: crc16.test

package info (click to toggle)
tcllib 2.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,560 kB
  • sloc: tcl: 306,798; ansic: 14,272; sh: 3,035; xml: 1,766; yacc: 1,157; pascal: 881; makefile: 124; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (292 lines) | stat: -rw-r--r-- 8,495 bytes parent folder | download | duplicates (2)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# crc16.test - Copyright (C) 2002 Pat Thoyts <patthoyts@users.sourceforge.net>
#
# Tests for the crc16 commands
#
# -------------------------------------------------------------------------
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# -------------------------------------------------------------------------

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

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

testsNeedTcl     8.5
testsNeedTcltest 1.0

testing {
    useLocal crc16.tcl crc16 ::crc
}

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

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

test crc16-1.1 {crc16 with single parameter} {
    list [catch {::crc::crc16 abc} err] $err
} {0 38712}

test crc16-1.2 {crc16 with "--" parameter} {
    list [catch {::crc::crc16 -- abc} err] $err
} {0 38712}

test crc16-1.3 {crc16 with leading hyphen data} {
    list [catch {::crc::crc16 -abc} err] $err
} {0 64305}

test crc16-1.4 {crc16 with leading hyphen data and option separator} {
    list [catch {::crc::crc16 -- -abc} err] $err
} {0 64305}

test crc16-1.5 {crc16 with leading hyphen data and format option} {
    list [catch {::crc::crc16 -format %04x -abc} err] $err
} {0 fb31}

test crc16-1.6 {crc16 with leading hyphen data, format option separator} {
    list [catch {::crc::crc16 -format %04x -- -abc} err] $err
} {0 fb31}

test crc16-1.7 {crc-ccitt with leading hyphen data} {
    list [catch {::crc::crc-ccitt -abc} err] $err
} {0 6110}

test crc16-1.8 {crc-ccitt with leading hyphen data and option separator} {
    list [catch {::crc::crc-ccitt -- -abc} err] $err
} {0 6110}


# -------------------------------------------------------------------------
# CRC16 tests
# -------------------------------------------------------------------------

foreach {n msg expected} {
    1    ""
    "0"
    2    "123456789"
    "47933"
    3    "abc"
    "38712"
    4    "ABC"
    "17697"
    5    "This is a string"
    "19524"
} {
    test crc16-2.$n {crc16 and unsigned integer} {
        list [catch {::crc::crc16 $msg} res] $res
    } [list 0 $expected]
}

test crc16-2.6 {crc16 and unsigned integer} tcl8 {
    list [catch {::crc::crc16 "\uFFFE\u0000\u0001\u0002"} res] $res
} [list 0 "47537"]

test crc16-2.6 {crc16 and unsigned integer} tcl9plus {
    list [catch {::crc::crc16 "\uFFFE\u0000\u0001\u0002"} res] $res
} [list 1 "expected byte sequence but character 0 was '\U0000FFFE\x00\x01\x02' (U+00FFFE)"]

foreach {n msg expected} {
    1    ""
    "0x0"
    2    "123456789"
    "0xBB3D"
    3    "abc"
    "0x9738"
    4    "ABC"
    "0x4521"
    5    "This is a string"
    "0x4C44"
} {
    test crc16-3.$n {crc16 as hexadecimal string} {
        list [catch {::crc::crc16 -format 0x%X $msg} res] $res
    } [list 0 $expected]
}

test crc16-3.6 {crc16 as hexadecimal string} tcl8 {
    list [catch {::crc::crc16 -format 0x%X "\uFFFE\u0000\u0001\u0002"} res] $res
} [list 0 "0xB9B1"]

test crc16-3.6 {crc16 as hexadecimal string} tcl9plus {
    list [catch {::crc::crc16 -format 0x%X "\uFFFE\u0000\u0001\u0002"} res] $res
} [list 1 "expected byte sequence but character 0 was '\U0000FFFE\x00\x01\x02' (U+00FFFE)"]

# -------------------------------------------------------------------------
# Implementation tests
# -------------------------------------------------------------------------

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
}

test crc16-4.0 {crc16 file option} {
    set r1 [::crc::crc16 -file [info script]]
    list [catch {
        set r2 [::crc::crc16 [crc::loaddata [info script]]]
        if {$r1 != $r2} {
            set r "differing results: $r1 != $r2"
        } else {
            set r ok
        }
    } result] $result
} {0 ok}

test crc16-4.1 {crc16 channel option} {
    set r1 [::crc::crc16 [crc::loaddata $crc::testfile]]
    list [catch {
        set f [open $crc::testfile r]
        set r2 [::crc::crc16 -channel $f]
        close $f
        if {$r1 != $r2} {
            set r "differing results: $r1 != $r2"
        } else {
            set r ok
        }
        set r
    } result] $result
} {0 ok}

test crc16-5.0 {crc implementation option} {
    proc crc::junk {s seed} {
        return 0
    }

    list [catch {::crc::crc16 -impl crc::junk {Hello, World!}} res] $res
} {0 0}

# -------------------------------------------------------------------------
# CRC-CCITT tests
# -------------------------------------------------------------------------

foreach {n msg expected} {
    1    ""
    "0xFFFF"
    2    "123456789"
    "0x29B1"
    3    "abc"
    "0x514A"
    4    "ABC"
    "0xF508"
    5    "This is a string"
    "0x4BE9"
} {
    test crc16-6.$n {crc-ccitt and unsigned integer} {
        list [catch {::crc::crc-ccitt -format 0x%X $msg} res] $res
    } [list 0 $expected]
}

test crc16-6.6 {crc-ccitt and unsigned integer} tcl8 {
    list [catch {::crc::crc-ccitt -format 0x%X "\uFFFE\u0000\u0001\u0002"} res] $res
} [list 0 "0xAAA4"]

test crc16-6.6 {crc-ccitt and unsigned integer} tcl9plus {
    list [catch {::crc::crc-ccitt -format 0x%X "\uFFFE\u0000\u0001\u0002"} res] $res
} [list 1 "expected byte sequence but character 0 was '\U0000FFFE\x00\x01\x02' (U+00FFFE)"]

# -------------------------------------------------------------------------
# CRC32 tests
# -------------------------------------------------------------------------

foreach {n msg expected} {
    1    ""
    "0x0"
    2    "123456789"
    "0xCBF43926"
    3    "abc"
    "0x352441C2"
    4    "ABC"
    "0xA3830348"
    5    "This is a string"
    "0x876633F"
} {
    test crc16-7.$n {crc-32 from the crc16 algorithms} {
        list [catch {::crc::crc-32 -format 0x%X $msg} res] $res
    } [list 0 $expected]
}

test crc16-7.6 {crc-32 from the crc16 algorithms} tcl8 {
    list [catch {::crc::crc-32 -format 0x%X "\uFFFE\u0000\u0001\u0002"} res] $res
} [list 0 "0xB0E8EEE5"]

test crc16-7.6 {crc-32 from the crc16 algorithms} tcl9plus {
    list [catch {::crc::crc-32 -format 0x%X "\uFFFE\u0000\u0001\u0002"} res] $res
} [list 1 "expected byte sequence but character 0 was '\U0000FFFE\x00\x01\x02' (U+00FFFE)"]

# -------------------------------------------------------------------------
# XMODEM CRC tests
# -------------------------------------------------------------------------

foreach {n msg expected} {
    1    ""
    "0x0"
    2    "T"
    "0x1A71"
    3    "123456789"
    "0x31C3"
    4    "abc"
    "0x9DD6"
    5    "ABC"
    "0x3994"
    6    "This is a string"
    "0x21E3"
} {
    test crc16-8.$n {XMODEM CRCs as hexadecimal string} {
        list [catch {::crc::xmodem -format 0x%X $msg} res] $res
    } [list 0 $expected]
}

test crc16-8.6 {XMODEM CRCs as hexadecimal string} tcl8 {
    list [catch {::crc::xmodem -format 0x%X "\uFFFE\u0000\u0001\u0002"} res] $res
} [list 0 "0x2E64"]

test crc16-8.6 {XMODEM CRCs as hexadecimal string} tcl9plus {
    list [catch {::crc::xmodem -format 0x%X "\uFFFE\u0000\u0001\u0002"} res] $res
} [list 1 "expected byte sequence but character 0 was '\U0000FFFE\x00\x01\x02' (U+00FFFE)"]

# -------------------------------------------------------------------------
# General tests based on the test vectors listed in the implementation
# -------------------------------------------------------------------------

foreach {n cmd expected} {
    1  crc16     0xBB3D
    2  crc-ccitt 0x29B1
    3  xmodem    0x31C3
    4  crc-32    0xCBF43926
    5  kermit    0x2189
    6  modbus    0x4B37
    7  mcrf4xx   0x6F91
    8  genibus   0xD64E
    9  crc-x25   0x906E
    10 crc-sdlc  0x906E
    11 crc-usb   0xB4C8
    12 buypass   0xFEE8
    13 umts      0xFEE8
    14 gsm       0xCE3C
    15 unknown2  0xDE76
    16 maxim     0x44C2
    17 unknown3   0x117
    18 unknown4  0x5118
    19 cms       0xAEE7
} {
    test crc16-9.$n "$cmd CRC as hexadecimal string" {
        list [catch {::crc::$cmd -format 0x%X "123456789"} res] $res
    } [list 0 $expected]
}

# -------------------------------------------------------------------------
catch {unset crc::filename}
testsuiteCleanup

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