Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F107811582
D32052.id95460.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
11 KB
Referenced Files
None
Subscribers
None
D32052.id95460.diff
View Options
Index: include/Makefile
===================================================================
--- include/Makefile
+++ include/Makefile
@@ -459,6 +459,9 @@
${SDESTDIR}${INCLUDEDIR}/dev/mlx5
.endif
+HAS_TESTS=
+SUBDIR.${MK_TESTS}+= tests
+
.include <bsd.prog.mk>
installincludes: ${SHARED}
Index: include/tests/Makefile
===================================================================
--- /dev/null
+++ include/tests/Makefile
@@ -0,0 +1,11 @@
+# Copyright 2021 M. Warner Losh <imp@FreeBSD.org>
+# SPDX-License-Identifier: BSD-2-Clause
+
+.include <bsd.own.mk>
+
+ATF_TESTS_C+= byteswap_test
+ATF_TESTS_C+= byteswap_endian_test
+ATF_TESTS_C+= endian_test
+ATF_TESTS_C+= sys_endian_test
+
+.include <bsd.test.mk>
Index: include/tests/byteswap_endian_test.c
===================================================================
--- /dev/null
+++ include/tests/byteswap_endian_test.c
@@ -0,0 +1,9 @@
+/*-
+ * Copyright (c) 2021 M Warner Losh <imp@FreeBSD.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+/* Make sure this still passes if both endian.h and byteswap.h included */
+#include <endian.h>
+#include "byteswap_test.c"
Index: include/tests/byteswap_test.c
===================================================================
--- /dev/null
+++ include/tests/byteswap_test.c
@@ -0,0 +1,124 @@
+/*-
+ * Copyright (c) 2021 M Warner Losh <imp@FreeBSD.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <byteswap.h>
+
+#include <atf-c.h>
+
+ATF_TC(byteswap);
+ATF_TC_HEAD(byteswap, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test swapping macros in <byteswap.h>");
+}
+
+ATF_TC_BODY(byteswap, tc)
+{
+ bool fail;
+ uint16_t ui16;
+ uint32_t ui32;
+ uint64_t ui64;
+
+ // glibc defines the {__,}bswap_{16,32,64}
+
+ fail = true;
+
+#ifdef __bswap_16
+ fail = false;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("__bswap_16 not defined");
+
+ fail = true;
+
+#ifdef bswap_16
+ fail = false;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("bswap_16 not defined");
+
+
+ fail = true;
+
+#ifdef __bswap_32
+ fail = false;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("__bswap_32 not defined");
+
+ fail = true;
+
+#ifdef bswap_32
+ fail = false;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("bswap_32 not defined");
+
+
+ fail = true;
+
+#ifdef __bswap_64
+ fail = false;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("__bswap_64 not defined");
+
+ fail = true;
+
+#ifdef bswap_64
+ fail = false;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("bswap_64 not defined");
+
+ // glibc doesn't define bswap{16,32,64}, and that matters to some programs
+
+ fail = false;
+
+#ifdef bswap16
+ fail = true;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("bswap16 improperly defined");
+
+ fail = false;
+
+#ifdef bswap32
+ fail = true;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("bswap32 improperly defined");
+
+ fail = false;
+
+#ifdef bswap64
+ fail = true;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("bswap64 improperly defined");
+
+ ui16 = 0x1234;
+ ATF_REQUIRE_MSG(0x3412 == bswap_16(ui16),
+ "bswap16(%#x) != 0x3412 instead %#x\n", ui16, bswap_16(ui16));
+
+ ui32 = 0x12345678ul;
+ ATF_REQUIRE_MSG(0x78563412ul == bswap_32(ui32),
+ "bswap32(%#lx) != 0x78563412 instead %#lx\n",
+ (unsigned long)ui32, (unsigned long)bswap_32(ui32));
+
+ ui64 = 0x123456789abcdef0ull;
+ ATF_REQUIRE_MSG(0xf0debc9a78563412ull == bswap_64(ui64),
+ "bswap64(%#llx) != 0x3412 instead %#llx\n",
+ (unsigned long long)ui64, (unsigned long long)bswap_64(ui64));
+
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+
+ ATF_TP_ADD_TC(tp, byteswap);
+
+ return atf_no_error();
+}
Index: include/tests/endian_test.c
===================================================================
--- /dev/null
+++ include/tests/endian_test.c
@@ -0,0 +1,212 @@
+/*-
+ * 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)
+{
+ bool fail;
+
+ // glibc doesn't define the {__,}bswap_{16,32,64}
+
+ fail = false;
+
+#ifdef __bswap_16
+ fail = true;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("__bswap_16 defined");
+
+ fail = false;
+
+#ifdef bswap_16
+ fail = true;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("bswap_16 defined");
+
+
+ fail = false;
+
+#ifdef __bswap_32
+ fail = true;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("__bswap_32 defined");
+
+ fail = false;
+
+#ifdef bswap_32
+ fail = true;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("bswap_32 defined");
+
+
+ fail = false;
+
+#ifdef __bswap_64
+ fail = true;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("__bswap_64 defined");
+
+ fail = false;
+
+#ifdef bswap_64
+ fail = true;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("bswap_64 defined");
+
+ // glibc doesn't define bswap{16,32,64}
+
+ fail = false;
+
+#ifdef bswap16
+ fail = true;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("bswap16 improperly defined");
+
+ fail = false;
+
+#ifdef bswap32
+ fail = true;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("bswap32 improperly defined");
+
+ fail = false;
+
+#ifdef bswap64
+ fail = true;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("bswap64 improperly defined");
+
+ fail = true;
+
+#ifdef __BIG_ENDIAN
+ fail = false;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("__BIG_ENDIAN not defined");
+
+
+ fail = true;
+
+#ifdef __LITTLE_ENDIAN
+ fail = false;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("__LITTLE_ENDIAN not defined");
+
+
+ fail = true;
+
+#ifdef __PDP_ENDIAN
+ fail = false;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("__PDP_ENDIAN not defined");
+
+
+ fail = true;
+
+#ifdef __FLOAT_WORD_ORDER
+ fail = false;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("__FLOAT_WORD_ORDER not defined");
+
+
+ fail = true;
+
+#ifdef __BYTE_ORDER
+ fail = false;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("__BYTE_ORDER not defined");
+
+ /* 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();
+}
Index: include/tests/sys_endian_test.c
===================================================================
--- /dev/null
+++ include/tests/sys_endian_test.c
@@ -0,0 +1,203 @@
+/*-
+ * Copyright (c) 2021 M Warner Losh <imp@FreeBSD.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <sys/endian.h>
+
+#include <atf-c.h>
+
+ATF_TC(sys_endian);
+ATF_TC_HEAD(sys_endian, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Test swapping macros in <byteswap.h>");
+}
+
+ATF_TC_BODY(sys_endian, tc)
+{
+ bool fail;
+
+ // FreeBSD sys/endian.h doesn't define the {__,}bswap_{16,32,64}
+
+ fail = false;
+
+#ifdef __bswap_16
+ fail = true;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("__bswap_16 defined");
+
+ fail = false;
+
+#ifdef bswap_16
+ fail = true;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("bswap_16 defined");
+
+
+ fail = false;
+
+#ifdef __bswap_32
+ fail = true;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("__bswap_32 defined");
+
+ fail = false;
+
+#ifdef bswap_32
+ fail = true;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("bswap_32 defined");
+
+
+ fail = false;
+
+#ifdef __bswap_64
+ fail = true;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("__bswap_64 defined");
+
+ fail = false;
+
+#ifdef bswap_64
+ fail = true;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("bswap_64 defined");
+
+ // FreeBSD sys/endian.h does define bswap{16,32,64}
+
+ fail = true;
+
+#ifdef bswap16
+ fail = false;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("bswap16 not defined");
+
+ fail = true;
+
+#ifdef bswap32
+ fail = false;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("bswap32 not defined");
+
+ fail = true;
+
+#ifdef bswap64
+ fail = false;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("bswap64 not defined");
+
+ fail = true;
+
+#ifdef _BIG_ENDIAN
+ fail = false;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("_BIG_ENDIAN not defined");
+
+
+ fail = true;
+
+#ifdef _LITTLE_ENDIAN
+ fail = false;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("_LITTLE_ENDIAN not defined");
+
+
+ fail = true;
+
+#ifdef _PDP_ENDIAN
+ fail = false;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("_PDP_ENDIAN not defined");
+
+
+ fail = true;
+
+#ifdef _BYTE_ORDER
+ fail = false;
+#endif
+ if (fail != false)
+ atf_tc_fail_nonfatal("_BYTE_ORDER not defined");
+
+ /* 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, sys_endian);
+
+ return atf_no_error();
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Jan 19, 9:23 AM (20 h, 26 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
15937940
Default Alt Text
D32052.id95460.diff (11 KB)
Attached To
Mode
D32052: tests: Test endian.h, byteswap.h, sys/endian.h and both endian.h and byteswap.h together
Attached
Detach File
Event Timeline
Log In to Comment