1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "memory-util.h"
4 #include "sparse-endian.h"
5 #include "tests.h"
6 #include "unaligned.h"
7
8 static uint8_t data[] = {
9 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
10 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
11 };
12
TEST(be)13 TEST(be) {
14 uint8_t scratch[16];
15
16 assert_se(unaligned_read_be16(&data[0]) == 0x0001);
17 assert_se(unaligned_read_be16(&data[1]) == 0x0102);
18
19 assert_se(unaligned_read_be32(&data[0]) == 0x00010203);
20 assert_se(unaligned_read_be32(&data[1]) == 0x01020304);
21 assert_se(unaligned_read_be32(&data[2]) == 0x02030405);
22 assert_se(unaligned_read_be32(&data[3]) == 0x03040506);
23
24 assert_se(unaligned_read_be64(&data[0]) == 0x0001020304050607);
25 assert_se(unaligned_read_be64(&data[1]) == 0x0102030405060708);
26 assert_se(unaligned_read_be64(&data[2]) == 0x0203040506070809);
27 assert_se(unaligned_read_be64(&data[3]) == 0x030405060708090a);
28 assert_se(unaligned_read_be64(&data[4]) == 0x0405060708090a0b);
29 assert_se(unaligned_read_be64(&data[5]) == 0x05060708090a0b0c);
30 assert_se(unaligned_read_be64(&data[6]) == 0x060708090a0b0c0d);
31 assert_se(unaligned_read_be64(&data[7]) == 0x0708090a0b0c0d0e);
32
33 zero(scratch);
34 unaligned_write_be16(&scratch[0], 0x0001);
35 assert_se(memcmp(&scratch[0], &data[0], sizeof(uint16_t)) == 0);
36 zero(scratch);
37 unaligned_write_be16(&scratch[1], 0x0102);
38 assert_se(memcmp(&scratch[1], &data[1], sizeof(uint16_t)) == 0);
39
40 zero(scratch);
41 unaligned_write_be32(&scratch[0], 0x00010203);
42 assert_se(memcmp(&scratch[0], &data[0], sizeof(uint32_t)) == 0);
43 zero(scratch);
44 unaligned_write_be32(&scratch[1], 0x01020304);
45 assert_se(memcmp(&scratch[1], &data[1], sizeof(uint32_t)) == 0);
46 zero(scratch);
47 unaligned_write_be32(&scratch[2], 0x02030405);
48 assert_se(memcmp(&scratch[2], &data[2], sizeof(uint32_t)) == 0);
49 zero(scratch);
50 unaligned_write_be32(&scratch[3], 0x03040506);
51 assert_se(memcmp(&scratch[3], &data[3], sizeof(uint32_t)) == 0);
52
53 zero(scratch);
54 unaligned_write_be64(&scratch[0], 0x0001020304050607);
55 assert_se(memcmp(&scratch[0], &data[0], sizeof(uint64_t)) == 0);
56 zero(scratch);
57 unaligned_write_be64(&scratch[1], 0x0102030405060708);
58 assert_se(memcmp(&scratch[1], &data[1], sizeof(uint64_t)) == 0);
59 zero(scratch);
60 unaligned_write_be64(&scratch[2], 0x0203040506070809);
61 assert_se(memcmp(&scratch[2], &data[2], sizeof(uint64_t)) == 0);
62 zero(scratch);
63 unaligned_write_be64(&scratch[3], 0x030405060708090a);
64 assert_se(memcmp(&scratch[3], &data[3], sizeof(uint64_t)) == 0);
65 zero(scratch);
66 unaligned_write_be64(&scratch[4], 0x0405060708090a0b);
67 assert_se(memcmp(&scratch[4], &data[4], sizeof(uint64_t)) == 0);
68 zero(scratch);
69 unaligned_write_be64(&scratch[5], 0x05060708090a0b0c);
70 assert_se(memcmp(&scratch[5], &data[5], sizeof(uint64_t)) == 0);
71 zero(scratch);
72 unaligned_write_be64(&scratch[6], 0x060708090a0b0c0d);
73 assert_se(memcmp(&scratch[6], &data[6], sizeof(uint64_t)) == 0);
74 zero(scratch);
75 unaligned_write_be64(&scratch[7], 0x0708090a0b0c0d0e);
76 assert_se(memcmp(&scratch[7], &data[7], sizeof(uint64_t)) == 0);
77 }
78
TEST(le)79 TEST(le) {
80 uint8_t scratch[16];
81
82 assert_se(unaligned_read_le16(&data[0]) == 0x0100);
83 assert_se(unaligned_read_le16(&data[1]) == 0x0201);
84
85 assert_se(unaligned_read_le32(&data[0]) == 0x03020100);
86 assert_se(unaligned_read_le32(&data[1]) == 0x04030201);
87 assert_se(unaligned_read_le32(&data[2]) == 0x05040302);
88 assert_se(unaligned_read_le32(&data[3]) == 0x06050403);
89
90 assert_se(unaligned_read_le64(&data[0]) == 0x0706050403020100);
91 assert_se(unaligned_read_le64(&data[1]) == 0x0807060504030201);
92 assert_se(unaligned_read_le64(&data[2]) == 0x0908070605040302);
93 assert_se(unaligned_read_le64(&data[3]) == 0x0a09080706050403);
94 assert_se(unaligned_read_le64(&data[4]) == 0x0b0a090807060504);
95 assert_se(unaligned_read_le64(&data[5]) == 0x0c0b0a0908070605);
96 assert_se(unaligned_read_le64(&data[6]) == 0x0d0c0b0a09080706);
97 assert_se(unaligned_read_le64(&data[7]) == 0x0e0d0c0b0a090807);
98
99 zero(scratch);
100 unaligned_write_le16(&scratch[0], 0x0100);
101 assert_se(memcmp(&scratch[0], &data[0], sizeof(uint16_t)) == 0);
102 zero(scratch);
103 unaligned_write_le16(&scratch[1], 0x0201);
104 assert_se(memcmp(&scratch[1], &data[1], sizeof(uint16_t)) == 0);
105
106 zero(scratch);
107 unaligned_write_le32(&scratch[0], 0x03020100);
108
109 assert_se(memcmp(&scratch[0], &data[0], sizeof(uint32_t)) == 0);
110 zero(scratch);
111 unaligned_write_le32(&scratch[1], 0x04030201);
112 assert_se(memcmp(&scratch[1], &data[1], sizeof(uint32_t)) == 0);
113 zero(scratch);
114 unaligned_write_le32(&scratch[2], 0x05040302);
115 assert_se(memcmp(&scratch[2], &data[2], sizeof(uint32_t)) == 0);
116 zero(scratch);
117 unaligned_write_le32(&scratch[3], 0x06050403);
118 assert_se(memcmp(&scratch[3], &data[3], sizeof(uint32_t)) == 0);
119
120 zero(scratch);
121 unaligned_write_le64(&scratch[0], 0x0706050403020100);
122 assert_se(memcmp(&scratch[0], &data[0], sizeof(uint64_t)) == 0);
123 zero(scratch);
124 unaligned_write_le64(&scratch[1], 0x0807060504030201);
125 assert_se(memcmp(&scratch[1], &data[1], sizeof(uint64_t)) == 0);
126 zero(scratch);
127 unaligned_write_le64(&scratch[2], 0x0908070605040302);
128 assert_se(memcmp(&scratch[2], &data[2], sizeof(uint64_t)) == 0);
129 zero(scratch);
130 unaligned_write_le64(&scratch[3], 0x0a09080706050403);
131 assert_se(memcmp(&scratch[3], &data[3], sizeof(uint64_t)) == 0);
132 zero(scratch);
133 unaligned_write_le64(&scratch[4], 0x0B0A090807060504);
134 assert_se(memcmp(&scratch[4], &data[4], sizeof(uint64_t)) == 0);
135 zero(scratch);
136 unaligned_write_le64(&scratch[5], 0x0c0b0a0908070605);
137 assert_se(memcmp(&scratch[5], &data[5], sizeof(uint64_t)) == 0);
138 zero(scratch);
139 unaligned_write_le64(&scratch[6], 0x0d0c0b0a09080706);
140 assert_se(memcmp(&scratch[6], &data[6], sizeof(uint64_t)) == 0);
141 zero(scratch);
142 unaligned_write_le64(&scratch[7], 0x0e0d0c0b0a090807);
143 assert_se(memcmp(&scratch[7], &data[7], sizeof(uint64_t)) == 0);
144 }
145
TEST(ne)146 TEST(ne) {
147 uint16_t x = 4711;
148 uint32_t y = 123456;
149 uint64_t z = 9876543210;
150
151 /* Note that we don't bother actually testing alignment issues in this function, after all the _ne() functions
152 * are just aliases for the _le() or _be() implementations, which we test extensively above. Hence, in this
153 * function, just ensure that they map to the right version on the local architecture. */
154
155 assert_se(unaligned_read_ne16(&x) == 4711);
156 assert_se(unaligned_read_ne32(&y) == 123456);
157 assert_se(unaligned_read_ne64(&z) == 9876543210);
158
159 unaligned_write_ne16(&x, 1);
160 unaligned_write_ne32(&y, 2);
161 unaligned_write_ne64(&z, 3);
162
163 assert_se(x == 1);
164 assert_se(y == 2);
165 assert_se(z == 3);
166 }
167
168 DEFINE_TEST_MAIN(LOG_INFO);
169