]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-unaligned.c
2 This file is part of systemd
4 Copyright 2014 Tom Gundersen
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "sparse-endian.h"
21 #include "unaligned.h"
24 static uint8_t data
[] = {
25 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
26 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
29 static void test_be(void) {
32 assert_se(unaligned_read_be16(&data
[0]) == 0x0001);
33 assert_se(unaligned_read_be16(&data
[1]) == 0x0102);
35 assert_se(unaligned_read_be32(&data
[0]) == 0x00010203);
36 assert_se(unaligned_read_be32(&data
[1]) == 0x01020304);
37 assert_se(unaligned_read_be32(&data
[2]) == 0x02030405);
38 assert_se(unaligned_read_be32(&data
[3]) == 0x03040506);
40 assert_se(unaligned_read_be64(&data
[0]) == 0x0001020304050607);
41 assert_se(unaligned_read_be64(&data
[1]) == 0x0102030405060708);
42 assert_se(unaligned_read_be64(&data
[2]) == 0x0203040506070809);
43 assert_se(unaligned_read_be64(&data
[3]) == 0x030405060708090a);
44 assert_se(unaligned_read_be64(&data
[4]) == 0x0405060708090a0b);
45 assert_se(unaligned_read_be64(&data
[5]) == 0x05060708090a0b0c);
46 assert_se(unaligned_read_be64(&data
[6]) == 0x060708090a0b0c0d);
47 assert_se(unaligned_read_be64(&data
[7]) == 0x0708090a0b0c0d0e);
50 unaligned_write_be16(&scratch
[0], 0x0001);
51 assert_se(memcmp(&scratch
[0], &data
[0], sizeof(uint16_t)) == 0);
53 unaligned_write_be16(&scratch
[1], 0x0102);
54 assert_se(memcmp(&scratch
[1], &data
[1], sizeof(uint16_t)) == 0);
57 unaligned_write_be32(&scratch
[0], 0x00010203);
58 assert_se(memcmp(&scratch
[0], &data
[0], sizeof(uint32_t)) == 0);
60 unaligned_write_be32(&scratch
[1], 0x01020304);
61 assert_se(memcmp(&scratch
[1], &data
[1], sizeof(uint32_t)) == 0);
63 unaligned_write_be32(&scratch
[2], 0x02030405);
64 assert_se(memcmp(&scratch
[2], &data
[2], sizeof(uint32_t)) == 0);
66 unaligned_write_be32(&scratch
[3], 0x03040506);
67 assert_se(memcmp(&scratch
[3], &data
[3], sizeof(uint32_t)) == 0);
70 unaligned_write_be64(&scratch
[0], 0x0001020304050607);
71 assert_se(memcmp(&scratch
[0], &data
[0], sizeof(uint64_t)) == 0);
73 unaligned_write_be64(&scratch
[1], 0x0102030405060708);
74 assert_se(memcmp(&scratch
[1], &data
[1], sizeof(uint64_t)) == 0);
76 unaligned_write_be64(&scratch
[2], 0x0203040506070809);
77 assert_se(memcmp(&scratch
[2], &data
[2], sizeof(uint64_t)) == 0);
79 unaligned_write_be64(&scratch
[3], 0x030405060708090a);
80 assert_se(memcmp(&scratch
[3], &data
[3], sizeof(uint64_t)) == 0);
82 unaligned_write_be64(&scratch
[4], 0x0405060708090a0b);
83 assert_se(memcmp(&scratch
[4], &data
[4], sizeof(uint64_t)) == 0);
85 unaligned_write_be64(&scratch
[5], 0x05060708090a0b0c);
86 assert_se(memcmp(&scratch
[5], &data
[5], sizeof(uint64_t)) == 0);
88 unaligned_write_be64(&scratch
[6], 0x060708090a0b0c0d);
89 assert_se(memcmp(&scratch
[6], &data
[6], sizeof(uint64_t)) == 0);
91 unaligned_write_be64(&scratch
[7], 0x0708090a0b0c0d0e);
92 assert_se(memcmp(&scratch
[7], &data
[7], sizeof(uint64_t)) == 0);
95 static void test_le(void) {
98 assert_se(unaligned_read_le16(&data
[0]) == 0x0100);
99 assert_se(unaligned_read_le16(&data
[1]) == 0x0201);
101 assert_se(unaligned_read_le32(&data
[0]) == 0x03020100);
102 assert_se(unaligned_read_le32(&data
[1]) == 0x04030201);
103 assert_se(unaligned_read_le32(&data
[2]) == 0x05040302);
104 assert_se(unaligned_read_le32(&data
[3]) == 0x06050403);
106 assert_se(unaligned_read_le64(&data
[0]) == 0x0706050403020100);
107 assert_se(unaligned_read_le64(&data
[1]) == 0x0807060504030201);
108 assert_se(unaligned_read_le64(&data
[2]) == 0x0908070605040302);
109 assert_se(unaligned_read_le64(&data
[3]) == 0x0a09080706050403);
110 assert_se(unaligned_read_le64(&data
[4]) == 0x0b0a090807060504);
111 assert_se(unaligned_read_le64(&data
[5]) == 0x0c0b0a0908070605);
112 assert_se(unaligned_read_le64(&data
[6]) == 0x0d0c0b0a09080706);
113 assert_se(unaligned_read_le64(&data
[7]) == 0x0e0d0c0b0a090807);
116 unaligned_write_le16(&scratch
[0], 0x0100);
117 assert_se(memcmp(&scratch
[0], &data
[0], sizeof(uint16_t)) == 0);
119 unaligned_write_le16(&scratch
[1], 0x0201);
120 assert_se(memcmp(&scratch
[1], &data
[1], sizeof(uint16_t)) == 0);
123 unaligned_write_le32(&scratch
[0], 0x03020100);
125 assert_se(memcmp(&scratch
[0], &data
[0], sizeof(uint32_t)) == 0);
127 unaligned_write_le32(&scratch
[1], 0x04030201);
128 assert_se(memcmp(&scratch
[1], &data
[1], sizeof(uint32_t)) == 0);
130 unaligned_write_le32(&scratch
[2], 0x05040302);
131 assert_se(memcmp(&scratch
[2], &data
[2], sizeof(uint32_t)) == 0);
133 unaligned_write_le32(&scratch
[3], 0x06050403);
134 assert_se(memcmp(&scratch
[3], &data
[3], sizeof(uint32_t)) == 0);
137 unaligned_write_le64(&scratch
[0], 0x0706050403020100);
138 assert_se(memcmp(&scratch
[0], &data
[0], sizeof(uint64_t)) == 0);
140 unaligned_write_le64(&scratch
[1], 0x0807060504030201);
141 assert_se(memcmp(&scratch
[1], &data
[1], sizeof(uint64_t)) == 0);
143 unaligned_write_le64(&scratch
[2], 0x0908070605040302);
144 assert_se(memcmp(&scratch
[2], &data
[2], sizeof(uint64_t)) == 0);
146 unaligned_write_le64(&scratch
[3], 0x0a09080706050403);
147 assert_se(memcmp(&scratch
[3], &data
[3], sizeof(uint64_t)) == 0);
149 unaligned_write_le64(&scratch
[4], 0x0B0A090807060504);
150 assert_se(memcmp(&scratch
[4], &data
[4], sizeof(uint64_t)) == 0);
152 unaligned_write_le64(&scratch
[5], 0x0c0b0a0908070605);
153 assert_se(memcmp(&scratch
[5], &data
[5], sizeof(uint64_t)) == 0);
155 unaligned_write_le64(&scratch
[6], 0x0d0c0b0a09080706);
156 assert_se(memcmp(&scratch
[6], &data
[6], sizeof(uint64_t)) == 0);
158 unaligned_write_le64(&scratch
[7], 0x0e0d0c0b0a090807);
159 assert_se(memcmp(&scratch
[7], &data
[7], sizeof(uint64_t)) == 0);
162 int main(int argc
, const char *argv
[]) {