-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest.lua
142 lines (102 loc) · 2.66 KB
/
test.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
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
local data = require'data'
-- create a new data object
d1 = data.new{0x0f}
-- with no layout applied, __index and __newindex should return nil
d1.no = 0xdead
assert(d1.no == nil)
-- create and apply a data layout
d1:layout{byte = {0, 8}, lsb = {7, 1}}
-- access the whole byte
assert(d1.byte == 0x0f)
-- set 0 to the least significant bit
d1.lsb = 0
-- access the whole byte again
assert(d1.byte == 0x0e)
-- create a new layout
l = data.layout{
uint16be = {0, 16},
uint16le = {0, 16, 'number', 'le'},
uint4 = {16, 4},
uint9 = {20, 9},
overflow = {32, 1},
}
-- create a new data object
d2 = data.new{0xaa, 0xbb, 0xcc, 0xdd}
-- check byte length of d2
assert(#d2 == 4)
-- apply the layout 'l' to data 'd2'
d2:layout(l)
-- access 2 bytes using big-endian ordering
assert(d2.uint16be == 0xaabb)
-- access 2 bytes using little-endian ordering
assert(d2.uint16le == 0xbbaa)
-- access 4 bits
assert(d2.uint4 == 0xc)
-- access out of bounds
assert(d2.overflow == nil)
-- create a new data segment, where offset = 1, length = 3
d3 = d2:segment(1, 3)
-- check bit length of d3
assert(#d3 == 3)
-- apply the layout 'l' into data 'd3'
d3:layout(l)
-- access the first 16 bits of 'd3' segment
assert(d3.uint16be == 0xbbcc)
-- create a new data segment, where offset = 1, length is unbounded
d4 = d3:segment(1)
-- apply the layout 'l' into data 'd4'
d4:layout(l)
-- access the first 16 bits of 'd4' segment
assert(d4.uint16be == 0xccdd)
-- create a new data segment comprehending the whole original data
d5 = d2:segment()
-- apply the layout 'l' into data 'd5'
d5:layout(l)
-- set 0xf into 'uint4' position
d5.uint4 = 0xf
-- check that it was set
assert(d5.uint4 == 0xf)
-- check that d5 points to the same raw data of d2
assert(d2.uint4 == 0xf)
-- force the collecting of d2, d3, d4
d2 = nil
d3 = nil
d4 = nil
collectgarbage()
-- check that d5 is still valid
assert(d5.uint4 == 0xf)
-- create a new data object with 2 bytes
d = data.new(2)
d:layout(l)
d.uint16be = 0xBEEF
assert(d.uint16be == 0xBEEF)
d = data.new(128)
l = data.layout{
toobig = {0, 65},
uint64 = {0, 64},
}
d:layout(l)
d.uint64 = -1
assert(d.toobig == nil)
assert(d.uint64 == -1)
-- create a new data object from a string
d = data.new'\a'
d:layout{ascii = {1, 7}}
assert(d.ascii == 7)
-- create a string field in a layout
d6 = data.new("abcdef")
d6:layout{str = {0, 6, 's'}, overflow = {1, 6, 's'}}
assert(d6.str == "abcdef")
assert(d6.overflow == nil)
d6.str = "hij"
assert(d6.str == "hijdef")
-- check invalid data creation
d = data.new()
assert(d == nil)
d = data.new(0)
assert(d == nil)
d = data.new{}
assert(d == nil)
d = data.new('')
assert(d == nil)
print("test passed ;-)")