v2 / vlib / encoding / binary / big_endian_fixed_test.v
53 lines · 47 sloc · 1.97 KB · 729a1cbc9fc4a2b8879d19bc3fa28036e09e4d9e
Raw
1module binary
2
3// Big Endian Tests
4fn test_big_endian_u16_fixed() {
5 assert big_endian_u16_fixed([u8(0), 1]!) == u16(1)
6 assert big_endian_u16_fixed([u8(5), 4]!) == u16(0x0504)
7 assert big_endian_u16_fixed([u8(0x35), 0x57]!) == u16(0x3557)
8 assert big_endian_u16_fixed([u8(0x35), 0x57]!) != u16(0x5735)
9}
10
11fn test_big_endian_put_u16_fixed() {
12 mut buf := [2]u8{}
13 big_endian_put_u16_fixed(mut buf, 0x8725)
14 assert buf == [u8(0x87), 0x25]!
15 big_endian_put_u16_fixed(mut buf, 0)
16 assert buf == [u8(0), 0]!
17 big_endian_put_u16_fixed(mut buf, 0xfdff)
18 assert buf == [u8(0xfd), 0xff]!
19}
20
21fn test_big_endian_u32_fixed() {
22 assert big_endian_u32_fixed([u8(0), 0, 0, 1]!) == u32(1)
23 assert big_endian_u32_fixed([u8(5), 4, 9, 1]!) == u32(0x05040901)
24 assert big_endian_u32_fixed([u8(0xf8), 0xa2, 0x9e, 0x21]!) == u32(0xf8a29e21)
25 assert big_endian_u32_fixed([u8(0xf8), 0xa2, 0x9e, 0x21]!) != u32(0x2192a2f8)
26}
27
28fn test_big_endian_put_u32_fixed() {
29 mut buf := [4]u8{}
30 big_endian_put_u32_fixed(mut buf, 0x872fea95)
31 assert buf == [u8(0x87), 0x2f, 0xea, 0x95]!
32 big_endian_put_u32_fixed(mut buf, 0)
33 assert buf == [u8(0), 0, 0, 0]!
34 big_endian_put_u32_fixed(mut buf, 0xfdf2e68f)
35 assert buf == [u8(0xfd), 0xf2, 0xe6, 0x8f]!
36}
37
38fn test_big_endian_u64_fixed() {
39 assert big_endian_u64_fixed([u8(0), 0, 0, 0, 0, 0, 0, 1]!) == u64(1)
40 assert big_endian_u64_fixed([u8(5), 4, 9, 1, 7, 3, 6, 8]!) == u64(0x0504090107030608)
41 assert big_endian_u64_fixed([u8(0xf8), 0xa2, 0x9e, 0x21, 0x7f, 0x9f, 0x8e, 0x8f]!) == u64(0xf8a29e217f9f8e8f)
42 assert big_endian_u64_fixed([u8(0xf8), 0xa2, 0x9e, 0x21, 0x7f, 0x9f, 0x8e, 0x8f]!) != u64(0x8f8e9f7f219ea2f8)
43}
44
45fn test_big_endian_put_u64_fixed() {
46 mut buf := [8]u8{}
47 big_endian_put_u64_fixed(mut buf, 0x872fea95fdf2e68f)
48 assert buf == [u8(0x87), 0x2f, 0xea, 0x95, 0xfd, 0xf2, 0xe6, 0x8f]!
49 big_endian_put_u64_fixed(mut buf, 0)
50 assert buf == [u8(0), 0, 0, 0, 0, 0, 0, 0]!
51 big_endian_put_u64_fixed(mut buf, 0xfdf2e68f8e9f7f21)
52 assert buf == [u8(0xfd), 0xf2, 0xe6, 0x8f, 0x8e, 0x9f, 0x7f, 0x21]!
53}
54