1+ local struct = require " struct"
2+
3+ describe (" struct test" , function ()
4+
5+ it (" test ss" , function ()
6+ local s1 = " test1"
7+ local s2 = " test2"
8+ local packed = struct .pack (" <ss" , s1 , s2 )
9+ local uns1 , uns2 = struct .unpack (" <ss" , packed )
10+ assert .same (uns1 , s1 )
11+ assert .same (uns2 , s2 )
12+ end )
13+
14+ it (" test s" , function ()
15+ local value = " test"
16+ local packed = struct .pack (" <s" , value )
17+ local unpacked = struct .unpack (" <s" , packed )
18+ assert .same (unpacked , value )
19+ end )
20+
21+ it (" test L" , function ()
22+ local value = 12345678912345678
23+ local packed = struct .pack (' <L' , value )
24+ local unpacked = struct .unpack (' <L' , packed )
25+ assert .same (unpacked , value )
26+ end )
27+
28+ it (" test I" , function ()
29+ local value = 123456789
30+ local packed = struct .pack (' <I' , value )
31+ local unpacked = struct .unpack (' <I' , packed )
32+ assert .same (unpacked , value )
33+ end )
34+
35+ it (" test h" , function ()
36+ local value = - 3200
37+ local packed = struct .pack (' <h' , value )
38+ local unpacked = struct .unpack (' <h' , packed )
39+ assert .same (unpacked , value )
40+ end )
41+
42+ it (" test B" , function ()
43+ local value = 255
44+ local packed = struct .pack (' <B' , value )
45+ local unpacked = struct .unpack (' <B' , packed )
46+ assert .same (unpacked , value )
47+ end )
48+
49+ it (" test b" , function ()
50+ local value = - 1
51+ local packed = struct .pack (' <b' , value )
52+ local unpacked = struct .unpack (' <b' , packed )
53+ assert .same (unpacked , value )
54+ end )
55+
56+ it (" test f" , function ()
57+ local value = 1.56789
58+ local packed = struct .pack (' <f' , value )
59+ local unpacked = struct .unpack (' <f' , packed )
60+ assert .is .truthy (unpacked < value )
61+ end )
62+
63+ it (" test d" , function ()
64+ local value = 1.56789
65+ local packed = struct .pack (' <d' , value )
66+ local unpacked = struct .unpack (' <d' , packed )
67+ assert .same (unpacked , value )
68+ end )
69+
70+ end )
0 commit comments