-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_crc32.lua
executable file
·98 lines (74 loc) · 2.25 KB
/
test_crc32.lua
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
#!/usr/bin/env lua
local _G,arg,assert,getmetatable,package,require,string,type
= _G,arg,assert,getmetatable,package,require,string,type
local C32 = require'crc32'
if arg[-1] ~= '-lcrc32' then -- work around debian dh_lua test calling with '-lcrc32', which sets _G.crc32
assert(_G.crc32==nil, 'do not pollute global env')
end
assert(type(C32.version)=='string')
assert(type(C32.crc32)=='function')
do
local crc32=C32.crc32
assert(crc32(0, 'egil') == 0x901FF815)
assert(crc32(0, 'eg') == 0x8A8579E6)
assert(crc32(0x8A8579E6, 'il') == 0x901FF815)
-- test with null byte
assert(crc32(0, string.char (1,0) ) == 0x58C223BE)
-- test with CRC as string
assert ( crc32('', 'egil') == string.char (0x90, 0x1F, 0xF8, 0x15))
local c1 = crc32('', 'eg')
assert( c1 == string.char (0x8A, 0x85, 0x79, 0xE6))
assert ( crc32(c1, 'il') == string.char (0x90, 0x1F, 0xF8, 0x15))
end
do -- test .newcrc32()
local crccalc = C32.newcrc32()
--assert(type(crccalc) == 'userdata')
local crccalc_mt = getmetatable(crccalc)
assert(crccalc_mt.reset)
assert(crccalc:tonumber() == 0)
assert(crccalc:tostring() == string.char (0, 0, 0, 0))
assert(crccalc:tohex() == '00000000')
assert(crccalc:update('egil' , 'surplussparameter') == crccalc )
assert(crccalc:tonumber() == 0x901FF815)
assert(crccalc:tostring() == string.char (0x90, 0x1F, 0xF8, 0x15))
assert(crccalc:tohex() == '901ff815')
assert(crccalc:reset('surplussparameter') == crccalc )
assert(crccalc:tonumber() == 0)
assert(crccalc:tohex() == '00000000')
crccalc:update'eg':update'il'
assert(crccalc:tohex() == '901ff815')
end
--[[ -- test performance
local S = require'socket'
local now = S.gettime()
local d ={}
for i = 1, 1000 do
d[i] = ('%d'):format(i+1000)
end
--------------------------
local now = S.gettime()
crc = 0
for i = 1, 1000 do
crc = crc32(crc, d[i])
end
print( (S.gettime()- now))
print(('%8.8x'):format(crc))
print()
--------------------------
local now = S.gettime()
crc = ''
for i = 1, 1000 do
crc = crc32(crc, d[i])
end
print( (S.gettime()- now))
print(crc:gsub('.', function(b) return ('%2.2x'):format(b:byte()) end) )
print()
--------------------------
local now = S.gettime()
crc = C32.newcrc32()
for i = 1, 1000 do
crc:update( d[i])
end
print( (S.gettime()- now))
print(crc:tohex() )
--]]