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