lib: Add CCAN headers for endian helpers
[petitboot] / lib / ccan / endian / test / run.c
1 #include <ccan/endian/endian.h>
2 #include <stdlib.h>
3 #include <stddef.h>
4 #include <ccan/tap/tap.h>
5
6 int 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         (void)argc;
22         (void)argv;
23
24         plan_tests(48);
25
26         /* Straight swap tests. */
27         u64.u64_bytes[0] = 0x00;
28         u64.u64_bytes[1] = 0x11;
29         u64.u64_bytes[2] = 0x22;
30         u64.u64_bytes[3] = 0x33;
31         u64.u64_bytes[4] = 0x44;
32         u64.u64_bytes[5] = 0x55;
33         u64.u64_bytes[6] = 0x66;
34         u64.u64_bytes[7] = 0x77;
35         u64.u64 = bswap_64(u64.u64);
36         ok1(u64.u64_bytes[7] == 0x00);
37         ok1(u64.u64_bytes[6] == 0x11);
38         ok1(u64.u64_bytes[5] == 0x22);
39         ok1(u64.u64_bytes[4] == 0x33);
40         ok1(u64.u64_bytes[3] == 0x44);
41         ok1(u64.u64_bytes[2] == 0x55);
42         ok1(u64.u64_bytes[1] == 0x66);
43         ok1(u64.u64_bytes[0] == 0x77);
44
45         u32.u32_bytes[0] = 0x00;
46         u32.u32_bytes[1] = 0x11;
47         u32.u32_bytes[2] = 0x22;
48         u32.u32_bytes[3] = 0x33;
49         u32.u32 = bswap_32(u32.u32);
50         ok1(u32.u32_bytes[3] == 0x00);
51         ok1(u32.u32_bytes[2] == 0x11);
52         ok1(u32.u32_bytes[1] == 0x22);
53         ok1(u32.u32_bytes[0] == 0x33);
54
55         u16.u16_bytes[0] = 0x00;
56         u16.u16_bytes[1] = 0x11;
57         u16.u16 = bswap_16(u16.u16);
58         ok1(u16.u16_bytes[1] == 0x00);
59         ok1(u16.u16_bytes[0] == 0x11);
60
61         /* Endian tests. */
62         u64.u64 = cpu_to_le64(0x0011223344556677ULL);
63         ok1(u64.u64_bytes[0] == 0x77);
64         ok1(u64.u64_bytes[1] == 0x66);
65         ok1(u64.u64_bytes[2] == 0x55);
66         ok1(u64.u64_bytes[3] == 0x44);
67         ok1(u64.u64_bytes[4] == 0x33);
68         ok1(u64.u64_bytes[5] == 0x22);
69         ok1(u64.u64_bytes[6] == 0x11);
70         ok1(u64.u64_bytes[7] == 0x00);
71         ok1(le64_to_cpu(u64.u64) == 0x0011223344556677ULL);
72
73         u64.u64 = cpu_to_be64(0x0011223344556677ULL);
74         ok1(u64.u64_bytes[7] == 0x77);
75         ok1(u64.u64_bytes[6] == 0x66);
76         ok1(u64.u64_bytes[5] == 0x55);
77         ok1(u64.u64_bytes[4] == 0x44);
78         ok1(u64.u64_bytes[3] == 0x33);
79         ok1(u64.u64_bytes[2] == 0x22);
80         ok1(u64.u64_bytes[1] == 0x11);
81         ok1(u64.u64_bytes[0] == 0x00);
82         ok1(be64_to_cpu(u64.u64) == 0x0011223344556677ULL);
83
84         u32.u32 = cpu_to_le32(0x00112233);
85         ok1(u32.u32_bytes[0] == 0x33);
86         ok1(u32.u32_bytes[1] == 0x22);
87         ok1(u32.u32_bytes[2] == 0x11);
88         ok1(u32.u32_bytes[3] == 0x00);
89         ok1(le32_to_cpu(u32.u32) == 0x00112233);
90
91         u32.u32 = cpu_to_be32(0x00112233);
92         ok1(u32.u32_bytes[3] == 0x33);
93         ok1(u32.u32_bytes[2] == 0x22);
94         ok1(u32.u32_bytes[1] == 0x11);
95         ok1(u32.u32_bytes[0] == 0x00);
96         ok1(be32_to_cpu(u32.u32) == 0x00112233);
97
98         u16.u16 = cpu_to_le16(0x0011);
99         ok1(u16.u16_bytes[0] == 0x11);
100         ok1(u16.u16_bytes[1] == 0x00);
101         ok1(le16_to_cpu(u16.u16) == 0x0011);
102
103         u16.u16 = cpu_to_be16(0x0011);
104         ok1(u16.u16_bytes[1] == 0x11);
105         ok1(u16.u16_bytes[0] == 0x00);
106         ok1(be16_to_cpu(u16.u16) == 0x0011);
107
108         exit(exit_status());
109 }