19 #include <boost/test/unit_test.hpp>
28 BOOST_AUTO_TEST_SUITE(common)
33 #if defined(BOOST_BIG_ENDIAN) && defined(BOOST_LITTLE_ENDIAN)
34 #error("Both BOOST_BIG_ENDIAN and BOOST_LITTLE_ENDIAN are defined.")
36 #if !defined(BOOST_BIG_ENDIAN) && !defined(BOOST_LITTLE_ENDIAN)
37 #error("Neither BOOST_BIG_ENDIAN nor BOOST_LITTLE_ENDIAN are defined.")
44 union {
char c[2]; boost::uint16_t n; } be16 = {{0x01,0x23}};
45 boost::uint16_t uint16_i_actual = be16.n;
46 #if defined(BOOST_BIG_ENDIAN)
47 boost::uint16_t uint16_h_expected = 0x0123;
48 #else // defined(BOOST_LITTLE_ENDIAN)
49 boost::uint16_t uint16_h_expected = 0x2301;
51 BOOST_CHECK_EQUAL(uint16_h_expected, uint16_i_actual);
52 boost::uint16_t uint16_n_actual = htons(uint16_i_actual);
53 boost::uint16_t uint16_n_expected = 0x0123;
54 BOOST_CHECK_EQUAL(uint16_n_expected, uint16_n_actual);
55 boost::uint16_t uint16_h_actual = ntohs(uint16_n_actual);
56 BOOST_CHECK_EQUAL(uint16_h_expected, uint16_h_actual);
62 union { uint8_t c[4]; boost::uint32_t n; } be32 = {{0x01,0x23,0x45,0x67}};
63 boost::uint32_t uint32_i_actual = be32.n;
64 #if defined(BOOST_BIG_ENDIAN)
65 boost::uint32_t uint32_h_expected = 0x01234567;
66 #else // defined(BOOST_LITTLE_ENDIAN)
67 boost::uint32_t uint32_h_expected = 0x67452301;
69 BOOST_CHECK_EQUAL(uint32_h_expected, uint32_i_actual);
70 boost::uint32_t uint32_n_actual = htonl(uint32_i_actual);
71 boost::uint32_t uint32_n_expected = 0x01234567;
72 BOOST_CHECK_EQUAL(uint32_n_expected, uint32_n_actual);
73 boost::uint32_t uint32_h_actual = ntohl(uint32_n_actual);
74 BOOST_CHECK_EQUAL(uint32_h_expected, uint32_h_actual);
80 union { uint8_t c[8]; boost::uint64_t n; } be64 = {{0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef}};
81 boost::uint64_t uint64_i_actual = be64.n;
82 #if defined(BOOST_BIG_ENDIAN)
83 boost::uint64_t uint64_h_expected = 0x0123456789abcdefll;
84 #else // defined(BOOST_LITTLE_ENDIAN)
85 boost::uint64_t uint64_h_expected = 0xefcdab8967452301ll;
87 BOOST_CHECK_EQUAL(uint64_h_expected, uint64_i_actual);
88 boost::uint64_t uint64_n_actual =
htonll(uint64_i_actual);
89 boost::uint64_t uint64_n_expected = 0x0123456789abcdefll;
90 BOOST_CHECK_EQUAL(uint64_n_expected, uint64_n_actual);
91 boost::uint64_t uint64_h_actual =
ntohll(uint64_n_actual);
92 BOOST_CHECK_EQUAL(uint64_h_expected, uint64_h_actual);
95 BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(AnnotatedUnitTest)
Unit test for the Annotated class.
boost::uint64_t ntohll(const boost::uint64_t &x)
Convert 64-bit types from network to host byte order.
boost::uint64_t htonll(const boost::uint64_t &x)
Convert 64-bit types from host to network byte order.
Header for endian conversion.