v / vlib / encoding / binary / little_endian_fixed_test.v
54 lines · 47 sloc · 2.06 KB · 729a1cbc9fc4a2b8879d19bc3fa28036e09e4d9e
Raw
1module binary
2
3// Little Endian Tests
4
5fn test_little_endian_u16_fixed() {
6 assert little_endian_u16_fixed([u8(0), 0]!) == u16(0)
7 assert little_endian_u16_fixed([u8(5), 4]!) == u16(0x0405)
8 assert little_endian_u16_fixed([u8(0x35), 0x57]!) == u16(0x5735)
9 assert little_endian_u16_fixed([u8(0x35), 0x57]!) != u16(0x3557)
10}
11
12fn test_little_endian_put_u16_fixed() {
13 mut buf := [2]u8{}
14 little_endian_put_u16_fixed(mut buf, 0x8725)
15 assert buf == [u8(0x25), 0x87]!
16 little_endian_put_u16_fixed(mut buf, 0)
17 assert buf == [u8(0), 0]!
18 little_endian_put_u16_fixed(mut buf, 0xfdff)
19 assert buf == [u8(0xff), 0xfd]!
20}
21
22fn test_little_endian_u32_fixed() {
23 assert little_endian_u32_fixed([u8(0), 0, 0, 0]!) == u32(0)
24 assert little_endian_u32_fixed([u8(5), 4, 9, 1]!) == u32(0x01090405)
25 assert little_endian_u32_fixed([u8(0xf8), 0xa2, 0x9e, 0x21]!) == u32(0x219ea2f8)
26 assert little_endian_u32_fixed([u8(0xf8), 0xa2, 0x9e, 0x21]!) != u32(0xf8a29e21)
27}
28
29fn test_little_endian_put_u32_fixed() {
30 mut buf := [4]u8{}
31 little_endian_put_u32_fixed(mut buf, 0x872fea95)
32 assert buf == [u8(0x95), 0xea, 0x2f, 0x87]!
33 little_endian_put_u32_fixed(mut buf, 0)
34 assert buf == [u8(0), 0, 0, 0]!
35 little_endian_put_u32_fixed(mut buf, 0xfdf2e68f)
36 assert buf == [u8(0x8f), 0xe6, 0xf2, 0xfd]!
37}
38
39fn test_little_endian_u64_fixed() {
40 assert little_endian_u64_fixed([u8(0), 0, 0, 0, 0, 0, 0, 0]!) == u64(0)
41 assert little_endian_u64_fixed([u8(5), 4, 9, 1, 7, 3, 6, 8]!) == u64(0x0806030701090405)
42 assert little_endian_u64_fixed([u8(0xf8), 0xa2, 0x9e, 0x21, 0x7f, 0x9f, 0x8e, 0x8f]!) == u64(0x8f8e9f7f219ea2f8)
43 assert little_endian_u64_fixed([u8(0xf8), 0xa2, 0x9e, 0x21, 0x7f, 0x9f, 0x8e, 0x8f]!) != u64(0xf8a29e217f9f8e8f)
44}
45
46fn test_little_endian_put_u64_fixed() {
47 mut buf := [8]u8{}
48 little_endian_put_u64_fixed(mut buf, 0x872fea95fdf2e68f)
49 assert buf == [u8(0x8f), 0xe6, 0xf2, 0xfd, 0x95, 0xea, 0x2f, 0x87]!
50 little_endian_put_u64_fixed(mut buf, 0)
51 assert buf == [u8(0), 0, 0, 0, 0, 0, 0, 0]!
52 little_endian_put_u64_fixed(mut buf, 0xfdf2e68f8e9f7f21)
53 assert buf == [u8(0x21), 0x7f, 0x9f, 0x8e, 0x8f, 0xe6, 0xf2, 0xfd]!
54}
55