torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EncapsulatedIntegerUnitTest.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 EncapsulatedInteger class.
18 
19 #include <boost/test/unit_test.hpp>
20 #include <boost/cstdint.hpp>
21 #include <boost/integer_traits.hpp>
23 
24 namespace torc {
25 namespace common {
26 
27 BOOST_AUTO_TEST_SUITE(common)
28 
29 /// \brief Unit test for standard integer sizes.
30 BOOST_AUTO_TEST_CASE(EncapsulatedIntegerSizeUnitTest) {
31  // types tested:
32  // int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t
33  // verify the sizes of 8-, 16-, 32-, and 64-bit integers
34  BOOST_REQUIRE_EQUAL((size_t) 1, sizeof(boost::int8_t));
35  BOOST_REQUIRE_EQUAL((size_t) 1, sizeof(boost::uint8_t));
36  BOOST_REQUIRE_EQUAL((size_t) 2, sizeof(boost::int16_t));
37  BOOST_REQUIRE_EQUAL((size_t) 2, sizeof(boost::uint16_t));
38  BOOST_REQUIRE_EQUAL((size_t) 4, sizeof(boost::int32_t));
39  BOOST_REQUIRE_EQUAL((size_t) 4, sizeof(boost::uint32_t));
40  BOOST_REQUIRE_EQUAL((size_t) 8, sizeof(boost::int64_t));
41  BOOST_REQUIRE_EQUAL((size_t) 8, sizeof(boost::uint64_t));
42 
43  // types tested:
44  // char, short, int, long, long long
45  // verify the sizes of standard integers
46  BOOST_WARN_EQUAL((size_t) 1, sizeof(char));
47  BOOST_WARN_EQUAL((size_t) 2, sizeof(short));
48  BOOST_WARN_EQUAL((size_t) 4, sizeof(int));
49  BOOST_WARN_EQUAL((size_t) 8, sizeof(long));
50  BOOST_WARN_EQUAL((size_t) 8, sizeof(long long));
51 }
52 
53 /// \brief Standard macro to be used for each of the encapsulated integer types.
54 #define STANDARD_ENCAPSULATED_INTEGER_BLOCK(TYPE) \
55 { \
56  typedef TYPE type; \
57  typedef EncapsulatedInteger<type> EInt; \
58  \
59  /* functions tested: */ \
60  /* EncapsulatedInteger(const T& rhs); */ \
61  /* EncapsulatedInteger(const type& rhs); */ \
62  /* EncapsulatedInteger(void); */ \
63  EInt eint1 = boost::integer_traits<TYPE>::const_min; /* primitive constructor */ \
64  EInt eint2 = eint1; /* object constructor */ \
65  EInt eint3, eint4, eint5; /* null constructors */ \
66  (void) eint2; /* be sure to test the constructor even if the object is unused */ \
67  \
68  /* functions tested: */ \
69  /* type& operator =(const type& rhs); */ \
70  /* type& operator =(const T& rhs); */ \
71  eint3 = eint1; /* object assignment */ \
72  eint4 = boost::integer_traits<TYPE>::const_max; /* primitive assignment */ \
73  \
74  /* functions tested: */ \
75  /* operator T&(void); */ \
76  type int1 = eint3; /* non-const primitive reference typecast */ \
77  type int2 = eint4; /* non-const primitive reference typecast */ \
78  \
79  /* functions tested: */ \
80  /* bool operator ==(const T& rhs) const; */ \
81  /* bool operator ==(const type& rhs) const; */ \
82  /* operator const T&(void) const; */ \
83  BOOST_CHECK_EQUAL(boost::integer_traits<TYPE>::const_min, int1); /* minimum value */ \
84  BOOST_CHECK_EQUAL(boost::integer_traits<TYPE>::const_max, int2); /* maximum value */ \
85  BOOST_CHECK_EQUAL(boost::integer_traits<TYPE>::const_min, eint3); /* minimum value */ \
86  BOOST_CHECK_EQUAL(boost::integer_traits<TYPE>::const_max, eint4); /* maximum value */ \
87 }
88 
89 /// \brief Unit test for encapsulated integers.
90 BOOST_AUTO_TEST_CASE(EncapsulatedIntegerLimitsUnitTest) {
91  // 8-bit unsigned
93  // 8-bit signed
95  // 16-bit unsigned
96  STANDARD_ENCAPSULATED_INTEGER_BLOCK(boost::uint16_t);
97  // 16-bit signed
99  // 32-bit unsigned
100  STANDARD_ENCAPSULATED_INTEGER_BLOCK(boost::uint32_t);
101  // 32-bit signed
102  STANDARD_ENCAPSULATED_INTEGER_BLOCK(boost::int32_t);
103  // 64-bit unsigned
104  STANDARD_ENCAPSULATED_INTEGER_BLOCK(boost::uint64_t);
105  // 64-bit signed
106  STANDARD_ENCAPSULATED_INTEGER_BLOCK(boost::int64_t);
107 }
108 
109 BOOST_AUTO_TEST_SUITE_END()
110 
111 } // namespace common
112 } // namespace torc
#define STANDARD_ENCAPSULATED_INTEGER_BLOCK(TYPE)
Standard macro to be used for each of the encapsulated integer types.
BOOST_AUTO_TEST_CASE(AnnotatedUnitTest)
Unit test for the Annotated class.
Header for the EncapsulatedInteger template.