v2 / vlib / encoding / binary / big_endian_test.v
192 lines · 162 sloc · 7.06 KB · 6aeef5e4db0c08faf968fe35bbc0951d5fa9d2ff
Raw
1module binary
2
3// Big Endian Tests
4fn test_big_endian_u16() {
5 assert big_endian_u16([u8(0), 1]) == u16(1)
6 assert big_endian_u16([u8(5), 4]) == u16(0x0504)
7 assert big_endian_u16([u8(0x35), 0x57]) == u16(0x3557)
8 assert big_endian_u16([u8(0x35), 0x57]) != u16(0x5735)
9}
10
11fn test_big_endian_u16_at() {
12 assert big_endian_u16_at([u8(0), 0, 1, 0], 1) == u16(1)
13 assert big_endian_u16_at([u8(0), 5, 4, 0], 1) == u16(0x0504)
14 assert big_endian_u16_at([u8(0), 0x35, 0x57, 0], 1) == u16(0x3557)
15 assert big_endian_u16_at([u8(0), 0x35, 0x57, 0], 1) != u16(0x5735)
16}
17
18fn test_big_endian_u16_end() {
19 assert big_endian_u16_end([u8(0), 0, 0, 1]) == u16(1)
20 assert big_endian_u16_end([u8(0), 0, 5, 4]) == u16(0x0504)
21 assert big_endian_u16_end([u8(0), 0, 0x35, 0x57]) == u16(0x3557)
22 assert big_endian_u16_end([u8(0), 0, 0x35, 0x57]) != u16(0x5735)
23}
24
25fn test_big_endian_put_u16() {
26 mut buf := []u8{len: 2}
27 big_endian_put_u16(mut buf, 0x8725)
28 assert buf == [u8(0x87), 0x25]
29 big_endian_put_u16(mut buf, 0)
30 assert buf == [u8(0), 0]
31 big_endian_put_u16(mut buf, 0xfdff)
32 assert buf == [u8(0xfd), 0xff]
33}
34
35fn test_big_endian_put_u16_at() {
36 mut buf := []u8{len: 4}
37 big_endian_put_u16_at(mut buf, 0x8725, 1)
38 assert buf == [u8(0), 0x87, 0x25, 0]
39
40 buf = []u8{len: 4}
41 big_endian_put_u16_at(mut buf, 1, 1)
42 assert buf == [u8(0), 0, 1, 0]
43
44 buf = []u8{len: 4}
45 big_endian_put_u16_at(mut buf, 0xfdff, 1)
46 assert buf == [u8(0), 0xfd, 0xff, 0]
47}
48
49fn test_big_endian_get_u16() {
50 assert big_endian_get_u16(u16(1)) == [u8(0), 1]
51 assert big_endian_get_u16(u16(0x0504)) == [u8(5), 4]
52 assert big_endian_get_u16(u16(0x3557)) == [u8(0x35), 0x57]
53 assert big_endian_get_u16(u16(0x5735)) != [u8(0x35), 0x57]
54}
55
56fn test_big_endian_u32() {
57 assert big_endian_u32([u8(0), 0, 0, 1]) == u32(1)
58 assert big_endian_u32([u8(5), 4, 9, 1]) == u32(0x05040901)
59 assert big_endian_u32([u8(0xf8), 0xa2, 0x9e, 0x21]) == u32(0xf8a29e21)
60 assert big_endian_u32([u8(0xf8), 0xa2, 0x9e, 0x21]) != u32(0x2192a2f8)
61}
62
63fn test_big_endian_u32_at() {
64 assert big_endian_u32_at([u8(0), 0, 0, 0, 1, 0, 0, 0], 1) == u32(1)
65 assert big_endian_u32_at([u8(0), 5, 4, 9, 1, 0, 0, 0], 1) == u32(0x05040901)
66 assert big_endian_u32_at([u8(0), 0xf8, 0xa2, 0x9e, 0x21, 0, 0, 0], 1) == u32(0xf8a29e21)
67 assert big_endian_u32_at([u8(0), 0xf8, 0xa2, 0x9e, 0x21, 0, 0, 0], 1) != u32(0x2192a2f8)
68}
69
70fn test_big_endian_u32_end() {
71 assert big_endian_u32_end([u8(0), 0, 0, 0, 0, 0, 1]) == u32(1)
72 assert big_endian_u32_end([u8(0), 0, 0, 0, 5, 4, 9, 1]) == u32(0x05040901)
73 assert big_endian_u32_end([u8(0), 0, 0, 0, 0xf8, 0xa2, 0x9e, 0x21]) == u32(0xf8a29e21)
74 assert big_endian_u32_end([u8(0), 0, 0, 0, 0xf8, 0xa2, 0x9e, 0x21]) != u32(0x2192a2f8)
75}
76
77fn test_big_endian_put_u32() {
78 mut buf := []u8{len: 4}
79 big_endian_put_u32(mut buf, 0x872fea95)
80 assert buf == [u8(0x87), 0x2f, 0xea, 0x95]
81 big_endian_put_u32(mut buf, 0)
82 assert buf == [u8(0), 0, 0, 0]
83 big_endian_put_u32(mut buf, 0xfdf2e68f)
84 assert buf == [u8(0xfd), 0xf2, 0xe6, 0x8f]
85}
86
87fn test_big_endian_put_u32_at() {
88 mut buf := []u8{len: 8}
89 big_endian_put_u32_at(mut buf, 0x872fea95, 1)
90 assert buf == [u8(0), 0x87, 0x2f, 0xea, 0x95, 0, 0, 0]
91
92 buf = []u8{len: 8}
93 big_endian_put_u32_at(mut buf, 1, 1)
94 assert buf == [u8(0), 0, 0, 0, 1, 0, 0, 0]
95
96 buf = []u8{len: 8}
97 big_endian_put_u32_at(mut buf, 0xfdf2e68f, 1)
98 assert buf == [u8(0), 0xfd, 0xf2, 0xe6, 0x8f, 0, 0, 0]
99}
100
101fn test_big_endian_put_u32_end() {
102 mut buf := []u8{len: 8}
103 big_endian_put_u32_end(mut buf, 0x872fea95)
104 assert buf == [u8(0), 0, 0, 0, 0x87, 0x2f, 0xea, 0x95]
105
106 buf = []u8{len: 8}
107 big_endian_put_u32_end(mut buf, 1)
108 assert buf == [u8(0), 0, 0, 0, 0, 0, 0, 1]
109
110 buf = []u8{len: 8}
111 big_endian_put_u32_end(mut buf, 0xfdf2e68f)
112 assert buf == [u8(0), 0, 0, 0, 0xfd, 0xf2, 0xe6, 0x8f]
113}
114
115fn test_big_endian_get_u32() {
116 assert big_endian_get_u32(u32(1)) == [u8(0), 0, 0, 1]
117 assert big_endian_get_u32(u32(0x05040901)) == [u8(5), 4, 9, 1]
118 assert big_endian_get_u32(u32(0xf8a29e21)) == [u8(0xf8), 0xa2, 0x9e, 0x21]
119 assert big_endian_get_u32(u32(0x2192a2f8)) != [u8(0xf8), 0xa2, 0x9e, 0x21]
120}
121
122fn test_big_endian_u64() {
123 assert big_endian_u64([u8(0), 0, 0, 0, 0, 0, 0, 1]) == u64(1)
124 assert big_endian_u64([u8(5), 4, 9, 1, 7, 3, 6, 8]) == u64(0x0504090107030608)
125 assert big_endian_u64([u8(0xf8), 0xa2, 0x9e, 0x21, 0x7f, 0x9f, 0x8e, 0x8f]) == u64(0xf8a29e217f9f8e8f)
126 assert big_endian_u64([u8(0xf8), 0xa2, 0x9e, 0x21, 0x7f, 0x9f, 0x8e, 0x8f]) != u64(0x8f8e9f7f219ea2f8)
127}
128
129fn test_big_endian_u64_at() {
130 assert big_endian_u64_at([u8(0), 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0], 1) == u64(1)
131 assert big_endian_u64_at([u8(0), 5, 4, 9, 1, 7, 3, 6, 8, 0, 0, 0, 0, 0, 0, 0], 1) == u64(0x0504090107030608)
132 assert big_endian_u64_at([u8(0), 0xf8, 0xa2, 0x9e, 0x21, 0x7f, 0x9f, 0x8e, 0x8f, 0, 0, 0, 0,
133 0, 0, 0], 1) == u64(0xf8a29e217f9f8e8f)
134 assert big_endian_u64_at([u8(0), 0xf8, 0xa2, 0x9e, 0x21, 0x7f, 0x9f, 0x8e, 0x8f, 0, 0, 0, 0,
135 0, 0, 0], 1) != u64(0x8f8e9f7f219ea2f8)
136}
137
138fn test_big_endian_u64_end() {
139 assert big_endian_u64_end([u8(0), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]) == u64(1)
140 assert big_endian_u64_end([u8(0), 0, 0, 0, 0, 0, 0, 0, 5, 4, 9, 1, 7, 3, 6, 8]) == u64(0x0504090107030608)
141 assert big_endian_u64_end([u8(0), 0, 0, 0, 0, 0, 0, 0, 0xf8, 0xa2, 0x9e, 0x21, 0x7f, 0x9f,
142 0x8e, 0x8f]) == u64(0xf8a29e217f9f8e8f)
143 assert big_endian_u64_end([u8(0), 0, 0, 0, 0, 0, 0, 0, 0xf8, 0xa2, 0x9e, 0x21, 0x7f, 0x9f,
144 0x8e, 0x8f]) != u64(0x8f8e9f7f219ea2f8)
145}
146
147fn test_big_endian_put_u64() {
148 mut buf := []u8{len: 8}
149 big_endian_put_u64(mut buf, 0x872fea95fdf2e68f)
150 assert buf == [u8(0x87), 0x2f, 0xea, 0x95, 0xfd, 0xf2, 0xe6, 0x8f]
151 big_endian_put_u64(mut buf, 0)
152 assert buf == [u8(0), 0, 0, 0, 0, 0, 0, 0]
153 big_endian_put_u64(mut buf, 0xfdf2e68f8e9f7f21)
154 assert buf == [u8(0xfd), 0xf2, 0xe6, 0x8f, 0x8e, 0x9f, 0x7f, 0x21]
155}
156
157fn test_big_endian_put_u64_at() {
158 mut buf := []u8{len: 16}
159 big_endian_put_u64_at(mut buf, 0x872fea95fdf2e68f, 1)
160 assert buf == [u8(0), 0x87, 0x2f, 0xea, 0x95, 0xfd, 0xf2, 0xe6, 0x8f, 0, 0, 0, 0, 0, 0, 0]
161
162 buf = []u8{len: 16}
163 big_endian_put_u64_at(mut buf, 1, 1)
164 assert buf == [u8(0), 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
165
166 buf = []u8{len: 16}
167 big_endian_put_u64_at(mut buf, 0xfdf2e68f8e9f7f21, 1)
168 assert buf == [u8(0), 0xfd, 0xf2, 0xe6, 0x8f, 0x8e, 0x9f, 0x7f, 0x21, 0, 0, 0, 0, 0, 0, 0]
169}
170
171fn test_big_endian_put_u64_end() {
172 mut buf := []u8{len: 16}
173 big_endian_put_u64_end(mut buf, 0x872fea95fdf2e68f)
174 assert buf == [u8(0), 0, 0, 0, 0, 0, 0, 0, 0x87, 0x2f, 0xea, 0x95, 0xfd, 0xf2, 0xe6, 0x8f]
175
176 buf = []u8{len: 16}
177 big_endian_put_u64_end(mut buf, 1)
178 assert buf == [u8(0), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
179
180 buf = []u8{len: 16}
181 big_endian_put_u64_end(mut buf, 0xfdf2e68f8e9f7f21)
182 assert buf == [u8(0), 0, 0, 0, 0, 0, 0, 0, 0xfd, 0xf2, 0xe6, 0x8f, 0x8e, 0x9f, 0x7f, 0x21]
183}
184
185fn test_big_endian_get_u64() {
186 assert big_endian_get_u64(u64(1)) == [u8(0), 0, 0, 0, 0, 0, 0, 1]
187 assert big_endian_get_u64(u64(0x0504090107030608)) == [u8(5), 4, 9, 1, 7, 3, 6, 8]
188 assert big_endian_get_u64(u64(0xf8a29e217f9f8e8f)) == [u8(0xf8), 0xa2, 0x9e, 0x21, 0x7f, 0x9f,
189 0x8e, 0x8f]
190 assert big_endian_get_u64(u64(0x8f8e9f7f219ea2f8)) != [u8(0xf8), 0xa2, 0x9e, 0x21, 0x7f, 0x9f,
191 0x8e, 0x8f]
192}
193