blob: 860406861977f96d923dc88fa9e47d869ee756f0 [file] [log] [blame]
Norman James6a58a272015-10-07 14:34:16 -05001#include <ccan/endian/endian.h>
2#include <stdlib.h>
3#include <stddef.h>
4#include <ccan/tap/tap.h>
5
6int main(int argc, char *argv[])
7{
8 union {
9 uint64_t u64;
10 unsigned char u64_bytes[8];
11 } u64;
12 union {
13 uint32_t u32;
14 unsigned char u32_bytes[4];
15 } u32;
16 union {
17 uint16_t u16;
18 unsigned char u16_bytes[2];
19 } u16;
20
21 plan_tests(48);
22
23 /* Straight swap tests. */
24 u64.u64_bytes[0] = 0x00;
25 u64.u64_bytes[1] = 0x11;
26 u64.u64_bytes[2] = 0x22;
27 u64.u64_bytes[3] = 0x33;
28 u64.u64_bytes[4] = 0x44;
29 u64.u64_bytes[5] = 0x55;
30 u64.u64_bytes[6] = 0x66;
31 u64.u64_bytes[7] = 0x77;
32 u64.u64 = bswap_64(u64.u64);
33 ok1(u64.u64_bytes[7] == 0x00);
34 ok1(u64.u64_bytes[6] == 0x11);
35 ok1(u64.u64_bytes[5] == 0x22);
36 ok1(u64.u64_bytes[4] == 0x33);
37 ok1(u64.u64_bytes[3] == 0x44);
38 ok1(u64.u64_bytes[2] == 0x55);
39 ok1(u64.u64_bytes[1] == 0x66);
40 ok1(u64.u64_bytes[0] == 0x77);
41
42 u32.u32_bytes[0] = 0x00;
43 u32.u32_bytes[1] = 0x11;
44 u32.u32_bytes[2] = 0x22;
45 u32.u32_bytes[3] = 0x33;
46 u32.u32 = bswap_32(u32.u32);
47 ok1(u32.u32_bytes[3] == 0x00);
48 ok1(u32.u32_bytes[2] == 0x11);
49 ok1(u32.u32_bytes[1] == 0x22);
50 ok1(u32.u32_bytes[0] == 0x33);
51
52 u16.u16_bytes[0] = 0x00;
53 u16.u16_bytes[1] = 0x11;
54 u16.u16 = bswap_16(u16.u16);
55 ok1(u16.u16_bytes[1] == 0x00);
56 ok1(u16.u16_bytes[0] == 0x11);
57
58 /* Endian tests. */
59 u64.u64 = cpu_to_le64(0x0011223344556677ULL);
60 ok1(u64.u64_bytes[0] == 0x77);
61 ok1(u64.u64_bytes[1] == 0x66);
62 ok1(u64.u64_bytes[2] == 0x55);
63 ok1(u64.u64_bytes[3] == 0x44);
64 ok1(u64.u64_bytes[4] == 0x33);
65 ok1(u64.u64_bytes[5] == 0x22);
66 ok1(u64.u64_bytes[6] == 0x11);
67 ok1(u64.u64_bytes[7] == 0x00);
68 ok1(le64_to_cpu(u64.u64) == 0x0011223344556677ULL);
69
70 u64.u64 = cpu_to_be64(0x0011223344556677ULL);
71 ok1(u64.u64_bytes[7] == 0x77);
72 ok1(u64.u64_bytes[6] == 0x66);
73 ok1(u64.u64_bytes[5] == 0x55);
74 ok1(u64.u64_bytes[4] == 0x44);
75 ok1(u64.u64_bytes[3] == 0x33);
76 ok1(u64.u64_bytes[2] == 0x22);
77 ok1(u64.u64_bytes[1] == 0x11);
78 ok1(u64.u64_bytes[0] == 0x00);
79 ok1(be64_to_cpu(u64.u64) == 0x0011223344556677ULL);
80
81 u32.u32 = cpu_to_le32(0x00112233);
82 ok1(u32.u32_bytes[0] == 0x33);
83 ok1(u32.u32_bytes[1] == 0x22);
84 ok1(u32.u32_bytes[2] == 0x11);
85 ok1(u32.u32_bytes[3] == 0x00);
86 ok1(le32_to_cpu(u32.u32) == 0x00112233);
87
88 u32.u32 = cpu_to_be32(0x00112233);
89 ok1(u32.u32_bytes[3] == 0x33);
90 ok1(u32.u32_bytes[2] == 0x22);
91 ok1(u32.u32_bytes[1] == 0x11);
92 ok1(u32.u32_bytes[0] == 0x00);
93 ok1(be32_to_cpu(u32.u32) == 0x00112233);
94
95 u16.u16 = cpu_to_le16(0x0011);
96 ok1(u16.u16_bytes[0] == 0x11);
97 ok1(u16.u16_bytes[1] == 0x00);
98 ok1(le16_to_cpu(u16.u16) == 0x0011);
99
100 u16.u16 = cpu_to_be16(0x0011);
101 ok1(u16.u16_bytes[1] == 0x11);
102 ok1(u16.u16_bytes[0] == 0x00);
103 ok1(be16_to_cpu(u16.u16) == 0x0011);
104
105 exit(exit_status());
106}