]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-unaligned.c
00445fa302ab400086109d64b6201da587830b6e
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd
5 Copyright 2014 Tom Gundersen
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include "sparse-endian.h"
22 #include "unaligned.h"
25 static uint8_t data
[] = {
26 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
27 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
30 static void test_be(void) {
33 assert_se(unaligned_read_be16(&data
[0]) == 0x0001);
34 assert_se(unaligned_read_be16(&data
[1]) == 0x0102);
36 assert_se(unaligned_read_be32(&data
[0]) == 0x00010203);
37 assert_se(unaligned_read_be32(&data
[1]) == 0x01020304);
38 assert_se(unaligned_read_be32(&data
[2]) == 0x02030405);
39 assert_se(unaligned_read_be32(&data
[3]) == 0x03040506);
41 assert_se(unaligned_read_be64(&data
[0]) == 0x0001020304050607);
42 assert_se(unaligned_read_be64(&data
[1]) == 0x0102030405060708);
43 assert_se(unaligned_read_be64(&data
[2]) == 0x0203040506070809);
44 assert_se(unaligned_read_be64(&data
[3]) == 0x030405060708090a);
45 assert_se(unaligned_read_be64(&data
[4]) == 0x0405060708090a0b);
46 assert_se(unaligned_read_be64(&data
[5]) == 0x05060708090a0b0c);
47 assert_se(unaligned_read_be64(&data
[6]) == 0x060708090a0b0c0d);
48 assert_se(unaligned_read_be64(&data
[7]) == 0x0708090a0b0c0d0e);
51 unaligned_write_be16(&scratch
[0], 0x0001);
52 assert_se(memcmp(&scratch
[0], &data
[0], sizeof(uint16_t)) == 0);
54 unaligned_write_be16(&scratch
[1], 0x0102);
55 assert_se(memcmp(&scratch
[1], &data
[1], sizeof(uint16_t)) == 0);
58 unaligned_write_be32(&scratch
[0], 0x00010203);
59 assert_se(memcmp(&scratch
[0], &data
[0], sizeof(uint32_t)) == 0);
61 unaligned_write_be32(&scratch
[1], 0x01020304);
62 assert_se(memcmp(&scratch
[1], &data
[1], sizeof(uint32_t)) == 0);
64 unaligned_write_be32(&scratch
[2], 0x02030405);
65 assert_se(memcmp(&scratch
[2], &data
[2], sizeof(uint32_t)) == 0);
67 unaligned_write_be32(&scratch
[3], 0x03040506);
68 assert_se(memcmp(&scratch
[3], &data
[3], sizeof(uint32_t)) == 0);
71 unaligned_write_be64(&scratch
[0], 0x0001020304050607);
72 assert_se(memcmp(&scratch
[0], &data
[0], sizeof(uint64_t)) == 0);
74 unaligned_write_be64(&scratch
[1], 0x0102030405060708);
75 assert_se(memcmp(&scratch
[1], &data
[1], sizeof(uint64_t)) == 0);
77 unaligned_write_be64(&scratch
[2], 0x0203040506070809);
78 assert_se(memcmp(&scratch
[2], &data
[2], sizeof(uint64_t)) == 0);
80 unaligned_write_be64(&scratch
[3], 0x030405060708090a);
81 assert_se(memcmp(&scratch
[3], &data
[3], sizeof(uint64_t)) == 0);
83 unaligned_write_be64(&scratch
[4], 0x0405060708090a0b);
84 assert_se(memcmp(&scratch
[4], &data
[4], sizeof(uint64_t)) == 0);
86 unaligned_write_be64(&scratch
[5], 0x05060708090a0b0c);
87 assert_se(memcmp(&scratch
[5], &data
[5], sizeof(uint64_t)) == 0);
89 unaligned_write_be64(&scratch
[6], 0x060708090a0b0c0d);
90 assert_se(memcmp(&scratch
[6], &data
[6], sizeof(uint64_t)) == 0);
92 unaligned_write_be64(&scratch
[7], 0x0708090a0b0c0d0e);
93 assert_se(memcmp(&scratch
[7], &data
[7], sizeof(uint64_t)) == 0);
96 static void test_le(void) {
99 assert_se(unaligned_read_le16(&data
[0]) == 0x0100);
100 assert_se(unaligned_read_le16(&data
[1]) == 0x0201);
102 assert_se(unaligned_read_le32(&data
[0]) == 0x03020100);
103 assert_se(unaligned_read_le32(&data
[1]) == 0x04030201);
104 assert_se(unaligned_read_le32(&data
[2]) == 0x05040302);
105 assert_se(unaligned_read_le32(&data
[3]) == 0x06050403);
107 assert_se(unaligned_read_le64(&data
[0]) == 0x0706050403020100);
108 assert_se(unaligned_read_le64(&data
[1]) == 0x0807060504030201);
109 assert_se(unaligned_read_le64(&data
[2]) == 0x0908070605040302);
110 assert_se(unaligned_read_le64(&data
[3]) == 0x0a09080706050403);
111 assert_se(unaligned_read_le64(&data
[4]) == 0x0b0a090807060504);
112 assert_se(unaligned_read_le64(&data
[5]) == 0x0c0b0a0908070605);
113 assert_se(unaligned_read_le64(&data
[6]) == 0x0d0c0b0a09080706);
114 assert_se(unaligned_read_le64(&data
[7]) == 0x0e0d0c0b0a090807);
117 unaligned_write_le16(&scratch
[0], 0x0100);
118 assert_se(memcmp(&scratch
[0], &data
[0], sizeof(uint16_t)) == 0);
120 unaligned_write_le16(&scratch
[1], 0x0201);
121 assert_se(memcmp(&scratch
[1], &data
[1], sizeof(uint16_t)) == 0);
124 unaligned_write_le32(&scratch
[0], 0x03020100);
126 assert_se(memcmp(&scratch
[0], &data
[0], sizeof(uint32_t)) == 0);
128 unaligned_write_le32(&scratch
[1], 0x04030201);
129 assert_se(memcmp(&scratch
[1], &data
[1], sizeof(uint32_t)) == 0);
131 unaligned_write_le32(&scratch
[2], 0x05040302);
132 assert_se(memcmp(&scratch
[2], &data
[2], sizeof(uint32_t)) == 0);
134 unaligned_write_le32(&scratch
[3], 0x06050403);
135 assert_se(memcmp(&scratch
[3], &data
[3], sizeof(uint32_t)) == 0);
138 unaligned_write_le64(&scratch
[0], 0x0706050403020100);
139 assert_se(memcmp(&scratch
[0], &data
[0], sizeof(uint64_t)) == 0);
141 unaligned_write_le64(&scratch
[1], 0x0807060504030201);
142 assert_se(memcmp(&scratch
[1], &data
[1], sizeof(uint64_t)) == 0);
144 unaligned_write_le64(&scratch
[2], 0x0908070605040302);
145 assert_se(memcmp(&scratch
[2], &data
[2], sizeof(uint64_t)) == 0);
147 unaligned_write_le64(&scratch
[3], 0x0a09080706050403);
148 assert_se(memcmp(&scratch
[3], &data
[3], sizeof(uint64_t)) == 0);
150 unaligned_write_le64(&scratch
[4], 0x0B0A090807060504);
151 assert_se(memcmp(&scratch
[4], &data
[4], sizeof(uint64_t)) == 0);
153 unaligned_write_le64(&scratch
[5], 0x0c0b0a0908070605);
154 assert_se(memcmp(&scratch
[5], &data
[5], sizeof(uint64_t)) == 0);
156 unaligned_write_le64(&scratch
[6], 0x0d0c0b0a09080706);
157 assert_se(memcmp(&scratch
[6], &data
[6], sizeof(uint64_t)) == 0);
159 unaligned_write_le64(&scratch
[7], 0x0e0d0c0b0a090807);
160 assert_se(memcmp(&scratch
[7], &data
[7], sizeof(uint64_t)) == 0);
163 static void test_ne(void) {
166 uint64_t z
= 9876543210;
168 /* Note that we don't bother actually testing alignment issues in this function, after all the _ne() functions
169 * are just aliases for the _le() or _be() implementations, which we test extensively above. Hence, in this
170 * function, just ensure that they map to the right version on the local architecture. */
172 assert_se(unaligned_read_ne16(&x
) == 4711);
173 assert_se(unaligned_read_ne32(&y
) == 123456);
174 assert_se(unaligned_read_ne64(&z
) == 9876543210);
176 unaligned_write_ne16(&x
, 1);
177 unaligned_write_ne32(&y
, 2);
178 unaligned_write_ne64(&z
, 3);
185 int main(int argc
, const char *argv
[]) {