Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/tests/include/endian_test.c
39475 views
1
/*-
2
* Copyright (c) 2021 M. Warner Losh <[email protected]>
3
*
4
* SPDX-License-Identifier: BSD-2-Clause
5
*/
6
7
#include <endian.h>
8
9
#include <atf-c.h>
10
11
ATF_TC(endian);
12
ATF_TC_HEAD(endian, tc)
13
{
14
atf_tc_set_md_var(tc, "descr", "Test swapping macros in <byteswap.h>");
15
}
16
17
ATF_TC_BODY(endian, tc)
18
{
19
/* glibc doesn't define the {__,}bswap_{16,32,64} */
20
#ifdef __bswap_16
21
atf_tc_fail_nonfatal("__bswap_16 improperly defined");
22
#endif
23
#ifdef bswap_16
24
atf_tc_fail_nonfatal("bswap_16 improperly defined");
25
#endif
26
#ifdef __bswap_32
27
atf_tc_fail_nonfatal("__bswap_32 improperly defined");
28
#endif
29
#ifdef bswap_32
30
atf_tc_fail_nonfatal("bswap_32 improperly defined");
31
#endif
32
#ifdef __bswap_64
33
atf_tc_fail_nonfatal("__bswap_64 improperly defined");
34
#endif
35
#ifdef bswap_64
36
atf_tc_fail_nonfatal("bswap_64 improperly defined");
37
#endif
38
39
/* glibc doesn't define bswap{16,32,64} */
40
#ifdef bswap16
41
atf_tc_fail_nonfatal("bswap16 improperly defined");
42
#endif
43
#ifdef bswap32
44
atf_tc_fail_nonfatal("bswap32 improperly defined");
45
#endif
46
#ifdef bswap64
47
atf_tc_fail_nonfatal("bswap64 improperly defined");
48
#endif
49
50
/*
51
* glibc defines with two underscores. We don't test for only one since
52
* that doesn't interfere.
53
*/
54
#ifndef __BIG_ENDIAN
55
atf_tc_fail_nonfatal("__BIG_ENDIAN not defined");
56
#endif
57
#ifndef __LITTLE_ENDIAN
58
atf_tc_fail_nonfatal("__LITTLE_ENDIAN not defined");
59
#endif
60
#ifndef __PDP_ENDIAN
61
atf_tc_fail_nonfatal("__PDP_ENDIAN not defined");
62
#endif
63
#ifndef __FLOAT_WORD_ORDER
64
atf_tc_fail_nonfatal("__FLOAT_WORD_ORDER not defined");
65
#endif
66
#ifndef __BYTE_ORDER
67
atf_tc_fail_nonfatal("__BYTE_ORDER not defined");
68
#endif
69
70
/* order to host */
71
#ifdef __BYTE_ORDER
72
#if __BYTE_ORDER == __BIG_ENDIAN
73
#define H16(x) be16toh(x)
74
#define H32(x) be32toh(x)
75
#define H64(x) be64toh(x)
76
#define O16(x) le16toh(x)
77
#define O32(x) le32toh(x)
78
#define O64(x) le64toh(x)
79
#else
80
#define H16(x) le16toh(x)
81
#define H32(x) le32toh(x)
82
#define H64(x) le64toh(x)
83
#define O16(x) be16toh(x)
84
#define O32(x) be32toh(x)
85
#define O64(x) be64toh(x)
86
#endif
87
#endif
88
ATF_REQUIRE(H16(0x1234) == 0x1234);
89
ATF_REQUIRE(H32(0x12345678ul) == 0x12345678ul);
90
ATF_REQUIRE(H64(0x123456789abcdef0ull) == 0x123456789abcdef0ull);
91
ATF_REQUIRE(O16(0x1234) == __bswap16(0x1234));
92
ATF_REQUIRE(O32(0x12345678ul) == __bswap32(0x12345678ul));
93
ATF_REQUIRE(O64(0x123456789abcdef0ull) == __bswap64(0x123456789abcdef0ull));
94
#undef H16
95
#undef H32
96
#undef H64
97
#undef O16
98
#undef O32
99
#undef O64
100
101
/* host to order */
102
#ifdef __BYTE_ORDER
103
#if __BYTE_ORDER == __BIG_ENDIAN
104
#define H16(x) htobe16(x)
105
#define H32(x) htobe32(x)
106
#define H64(x) htobe64(x)
107
#define O16(x) htole16(x)
108
#define O32(x) htole32(x)
109
#define O64(x) htole64(x)
110
#else
111
#define H16(x) htole16(x)
112
#define H32(x) htole32(x)
113
#define H64(x) htole64(x)
114
#define O16(x) htobe16(x)
115
#define O32(x) htobe32(x)
116
#define O64(x) htobe64(x)
117
#endif
118
#endif
119
ATF_REQUIRE(H16(0x1234) == 0x1234);
120
ATF_REQUIRE(H32(0x12345678ul) == 0x12345678ul);
121
ATF_REQUIRE(H64(0x123456789abcdef0ull) == 0x123456789abcdef0ull);
122
ATF_REQUIRE(O16(0x1234) == __bswap16(0x1234));
123
ATF_REQUIRE(O32(0x12345678ul) == __bswap32(0x12345678ul));
124
ATF_REQUIRE(O64(0x123456789abcdef0ull) == __bswap64(0x123456789abcdef0ull));
125
#undef H16
126
#undef H32
127
#undef H64
128
#undef O16
129
#undef O32
130
#undef O64
131
}
132
133
ATF_TP_ADD_TCS(tp)
134
{
135
136
ATF_TP_ADD_TC(tp, endian);
137
138
return atf_no_error();
139
}
140
141