torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EndianUnitTest.cpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Unit test for endian conversion.
18 
19 #include <boost/test/unit_test.hpp>
20 #include "torc/common/Endian.hpp"
21 
22 namespace torc {
23 namespace common {
24 
27 
28 BOOST_AUTO_TEST_SUITE(common)
29 
30 /// \brief Unit test for endian conversion.
31 BOOST_AUTO_TEST_CASE(EndianUnitTest) {
32  // ensure that BOOST_BIG_ENDIAN or BOOST_LITTLE_ENDIAN are properly defined
33 #if defined(BOOST_BIG_ENDIAN) && defined(BOOST_LITTLE_ENDIAN)
34  #error("Both BOOST_BIG_ENDIAN and BOOST_LITTLE_ENDIAN are defined.")
35 #endif
36 #if !defined(BOOST_BIG_ENDIAN) && !defined(BOOST_LITTLE_ENDIAN)
37  #error("Neither BOOST_BIG_ENDIAN nor BOOST_LITTLE_ENDIAN are defined.")
38 #endif
39 
40  // functions tested:
41  // htons(uint16_t);
42  // ntohs(uint16_t);
43  // verify 16-bit conversion to and from network order
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;
50 #endif
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);
57 
58  // functions tested:
59  // htons(uint32_t);
60  // ntohs(uint32_t);
61  // verify 32-bit conversion to and from network order
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;
68 #endif
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);
75 
76  // functions tested:
77  // htons(uint64_t);
78  // ntohs(uint64_t);
79  // verify 64-bit conversion to and from network order
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;
86 #endif
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);
93 }
94 
95 BOOST_AUTO_TEST_SUITE_END()
96 
97 } // namespace common
98 } // namespace torc
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.
Definition: Endian.hpp:40
boost::uint64_t htonll(const boost::uint64_t &x)
Convert 64-bit types from host to network byte order.
Definition: Endian.hpp:61
Header for endian conversion.