1 /*-
2 * Copyright (c) 2021 M. Warner Losh <imp@FreeBSD.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7 #include <byteswap.h>
8
9 #include <atf-c.h>
10
11 ATF_TC(byteswap);
ATF_TC_HEAD(byteswap,tc)12 ATF_TC_HEAD(byteswap, tc)
13 {
14 atf_tc_set_md_var(tc, "descr", "Test swapping macros in <byteswap.h>");
15 }
16
ATF_TC_BODY(byteswap,tc)17 ATF_TC_BODY(byteswap, tc)
18 {
19 uint16_t ui16;
20 uint32_t ui32;
21 uint64_t ui64;
22
23 /* glibc defines the {__,}bswap_{16,32,64} */
24 #ifndef __bswap_16
25 atf_tc_fail_nonfatal("__bswap_16 not defined");
26 #endif
27 #ifndef bswap_16
28 atf_tc_fail_nonfatal("bswap_16 not defined");
29 #endif
30 #ifndef __bswap_32
31 atf_tc_fail_nonfatal("__bswap_32 not defined");
32 #endif
33 #ifndef bswap_32
34 atf_tc_fail_nonfatal("bswap_32 not defined");
35 #endif
36 #ifndef __bswap_64
37 atf_tc_fail_nonfatal("__bswap_64 not defined");
38 #endif
39 #ifndef bswap_64
40 atf_tc_fail_nonfatal("bswap_64 not defined");
41 #endif
42
43 /* glibc does not define bswap{16,32,64} */
44 #ifdef bswap16
45 atf_tc_fail_nonfatal("bswap16 improperly defined");
46 #endif
47 #ifdef bswap32
48 atf_tc_fail_nonfatal("bswap32 improperly defined");
49 #endif
50 #ifdef bswap64
51 atf_tc_fail_nonfatal("bswap64 improperly defined");
52 #endif
53
54 ui16 = 0x1234;
55 ATF_REQUIRE_MSG(0x3412 == bswap_16(ui16),
56 "bswap16(%#x) != 0x3412 instead %#x\n", ui16, bswap_16(ui16));
57
58 ui32 = 0x12345678ul;
59 ATF_REQUIRE_MSG(0x78563412ul == bswap_32(ui32),
60 "bswap32(%#lx) != 0x78563412 instead %#lx\n",
61 (unsigned long)ui32, (unsigned long)bswap_32(ui32));
62
63 ui64 = 0x123456789abcdef0ull;
64 ATF_REQUIRE_MSG(0xf0debc9a78563412ull == bswap_64(ui64),
65 "bswap64(%#llx) != 0x3412 instead %#llx\n",
66 (unsigned long long)ui64, (unsigned long long)bswap_64(ui64));
67
68 }
69
ATF_TP_ADD_TCS(tp)70 ATF_TP_ADD_TCS(tp)
71 {
72
73 ATF_TP_ADD_TC(tp, byteswap);
74
75 return atf_no_error();
76 }
77