summaryrefslogtreecommitdiff
path: root/tests/include/endian_test.c
blob: 2431b54535a237a1fee6f26f5e7a1b9881be2d50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*-
 * Copyright (c) 2021 M. Warner Losh <imp@FreeBSD.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <endian.h>

#include <atf-c.h>

ATF_TC(endian);
ATF_TC_HEAD(endian, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test swapping macros in <byteswap.h>");
}

ATF_TC_BODY(endian, tc)
{
	/* glibc doesn't define the {__,}bswap_{16,32,64} */
#ifdef __bswap_16
	atf_tc_fail_nonfatal("__bswap_16 improperly defined");
#endif
#ifdef bswap_16
	atf_tc_fail_nonfatal("bswap_16 improperly defined");
#endif
#ifdef __bswap_32
	atf_tc_fail_nonfatal("__bswap_32 improperly defined");
#endif
#ifdef bswap_32
	atf_tc_fail_nonfatal("bswap_32 improperly defined");
#endif
#ifdef __bswap_64
	atf_tc_fail_nonfatal("__bswap_64 improperly defined");
#endif
#ifdef bswap_64
	atf_tc_fail_nonfatal("bswap_64 improperly defined");
#endif

	/* glibc doesn't define bswap{16,32,64} */
#ifdef bswap16
	atf_tc_fail_nonfatal("bswap16 improperly defined");
#endif
#ifdef bswap32
	atf_tc_fail_nonfatal("bswap32 improperly defined");
#endif
#ifdef bswap64
	atf_tc_fail_nonfatal("bswap64 improperly defined");
#endif

	/*
	 * glibc defines with two underscores. We don't test for only one since
	 * that doesn't interfere.
	 */
#ifndef __BIG_ENDIAN
	atf_tc_fail_nonfatal("__BIG_ENDIAN not defined");
#endif
#ifndef __LITTLE_ENDIAN
	atf_tc_fail_nonfatal("__LITTLE_ENDIAN not defined");
#endif
#ifndef __PDP_ENDIAN
	atf_tc_fail_nonfatal("__PDP_ENDIAN not defined");
#endif
#ifndef __FLOAT_WORD_ORDER
	atf_tc_fail_nonfatal("__FLOAT_WORD_ORDER not defined");
#endif
#ifndef __BYTE_ORDER
	atf_tc_fail_nonfatal("__BYTE_ORDER not defined");
#endif

	/* order to host */
#ifdef __BYTE_ORDER
#if __BYTE_ORDER == __BIG_ENDIAN
#define H16(x) be16toh(x)
#define H32(x) be32toh(x)
#define H64(x) be64toh(x)
#define O16(x) le16toh(x)
#define O32(x) le32toh(x)
#define O64(x) le64toh(x)
#else
#define H16(x) le16toh(x)
#define H32(x) le32toh(x)
#define H64(x) le64toh(x)
#define O16(x) be16toh(x)
#define O32(x) be32toh(x)
#define O64(x) be64toh(x)
#endif
#endif
	ATF_REQUIRE(H16(0x1234) == 0x1234);
	ATF_REQUIRE(H32(0x12345678ul) == 0x12345678ul);
	ATF_REQUIRE(H64(0x123456789abcdef0ull) == 0x123456789abcdef0ull);
	ATF_REQUIRE(O16(0x1234) == __bswap16(0x1234));
	ATF_REQUIRE(O32(0x12345678ul) == __bswap32(0x12345678ul));
	ATF_REQUIRE(O64(0x123456789abcdef0ull) == __bswap64(0x123456789abcdef0ull));
#undef H16
#undef H32
#undef H64
#undef O16
#undef O32
#undef O64

	/* host to order */
#ifdef __BYTE_ORDER
#if __BYTE_ORDER == __BIG_ENDIAN
#define H16(x) htobe16(x)
#define H32(x) htobe32(x)
#define H64(x) htobe64(x)
#define O16(x) htole16(x)
#define O32(x) htole32(x)
#define O64(x) htole64(x)
#else
#define H16(x) htole16(x)
#define H32(x) htole32(x)
#define H64(x) htole64(x)
#define O16(x) htobe16(x)
#define O32(x) htobe32(x)
#define O64(x) htobe64(x)
#endif
#endif
	ATF_REQUIRE(H16(0x1234) == 0x1234);
	ATF_REQUIRE(H32(0x12345678ul) == 0x12345678ul);
	ATF_REQUIRE(H64(0x123456789abcdef0ull) == 0x123456789abcdef0ull);
	ATF_REQUIRE(O16(0x1234) == __bswap16(0x1234));
	ATF_REQUIRE(O32(0x12345678ul) == __bswap32(0x12345678ul));
	ATF_REQUIRE(O64(0x123456789abcdef0ull) == __bswap64(0x123456789abcdef0ull));
#undef H16
#undef H32
#undef H64
#undef O16
#undef O32
#undef O64
}

ATF_TP_ADD_TCS(tp)
{

	ATF_TP_ADD_TC(tp, endian);

	return atf_no_error();
}