The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_util.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 2016 by Karol Nowak <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-test"
16 
17 #include <boost/test/unit_test.hpp>
18 
19 #include "util.hpp"
20 
21 #include <boost/cstdint.hpp>
22 
24 
25 BOOST_AUTO_TEST_CASE( test_lexical_cast )
26 {
27  /* First check if lexical_cast returns correct results for correct args */
28  int result = lexical_cast<int, const std::string&>(std::string("1"));
29  BOOST_CHECK( result == 1 );
30 
31  int result2 = lexical_cast<int, const char*>("2");
32  BOOST_CHECK( result2 == 2 );
33 
34  /* Check that an exception is thrown when an invalid argument is passed */
35  try {
36  lexical_cast<int, const std::string&>(std::string("iddqd"));
37 
38  /* A bad_lexical_cast should have been thrown already */
39  BOOST_CHECK( false );
40  }
41  catch( const bad_lexical_cast &) {
42  // Don't do anything, we succeeded.
43  }
44 
45  try {
46  lexical_cast<int, const char*>("idkfa");
47 
48  /* A bad_lexical_cast should have been thrown already */
49  BOOST_CHECK( false );
50  }
51  catch( const bad_lexical_cast &) {
52  // Don't do anything, we succeeded.
53  }
54 }
55 
56 BOOST_AUTO_TEST_CASE( test_lexical_cast_default )
57 {
58  /* First check if it works with correct values */
59  int result = lexical_cast_default<int, const std::string&>(std::string("1"));
60  BOOST_CHECK( result == 1 );
61 
62  int result2 = lexical_cast_default<int, const char*>("2");
63  BOOST_CHECK( result2 == 2 );
64 
65  double result3 = lexical_cast_default<double, const std::string&>(std::string("0.5"));
66  BOOST_CHECK( result3 >= 0.499 && result3 <= 0.511 );
67 
68  /* Check if default is returned when argument is empty/invalid */
69  int result4 = lexical_cast_default<int, const std::string&>(std::string(), 4);
70  BOOST_CHECK( result4 == 4 );
71 
72  int result5 = lexical_cast_default<int, const char*>("", 5);
73  BOOST_CHECK( result5 == 5 );
74 
75  double result6 = lexical_cast_default<double, const std::string&>(std::string(), 0.5);
76  BOOST_CHECK( result6 >= 0.499 && result6 <= 0.511 );
77 }
78 
79 BOOST_AUTO_TEST_CASE( test_bit_width )
80 {
81  BOOST_CHECK( bit_width<boost::uint8_t>() == 8 );
82  BOOST_CHECK( bit_width<boost::uint16_t>() == 16 );
83  BOOST_CHECK( bit_width<boost::uint32_t>() == 32 );
84  BOOST_CHECK( bit_width<boost::uint64_t>() == 64 );
85 
86  BOOST_CHECK( bit_width(static_cast<boost::uint8_t>(0)) == 8 );
87  BOOST_CHECK( bit_width(static_cast<boost::uint16_t>(0)) == 16 );
88  BOOST_CHECK( bit_width(static_cast<boost::uint32_t>(0)) == 32 );
89  BOOST_CHECK( bit_width(static_cast<boost::uint64_t>(0)) == 64 );
90 }
91 
92 BOOST_AUTO_TEST_CASE( test_count_ones )
93 {
94  BOOST_CHECK( count_ones(0) == 0 );
95  BOOST_CHECK( count_ones(1) == 1 );
96  BOOST_CHECK( count_ones(2) == 1 );
97  BOOST_CHECK( count_ones(3) == 2 );
98  BOOST_CHECK( count_ones(4) == 1 );
99  BOOST_CHECK( count_ones(5) == 2 );
100  BOOST_CHECK( count_ones(6) == 2 );
101  BOOST_CHECK( count_ones(7) == 3 );
102  BOOST_CHECK( count_ones(8) == 1 );
103  BOOST_CHECK( count_ones(9) == 2 );
104  BOOST_CHECK( count_ones(12345) == 6 );
105 }
106 
107 BOOST_AUTO_TEST_CASE( test_count_leading_zeros )
108 {
109  BOOST_CHECK( count_leading_zeros(static_cast<boost::uint8_t>(1)) == 7 );
110  BOOST_CHECK( count_leading_zeros(static_cast<boost::uint16_t>(1)) == 15 );
111  BOOST_CHECK( count_leading_zeros(static_cast<boost::uint32_t>(1)) == 31 );
112  BOOST_CHECK( count_leading_zeros(static_cast<boost::uint64_t>(1)) == 63 );
113  BOOST_CHECK( count_leading_zeros(static_cast<boost::uint8_t>(0xFF)) == 0 );
114  BOOST_CHECK( count_leading_zeros(static_cast<unsigned int>(0))
115  == bit_width<unsigned int>() );
116  BOOST_CHECK( count_leading_zeros(static_cast<unsigned long int>(0))
117  == bit_width<unsigned long int>() );
118  BOOST_CHECK( count_leading_zeros(static_cast<unsigned long long int>(0))
119  == bit_width<unsigned long long int>() );
120  BOOST_CHECK( count_leading_zeros('\0')
121  == bit_width<char>() );
122  BOOST_CHECK( count_leading_zeros('\b')
123  == bit_width<char>() - 4 );
124  BOOST_CHECK( count_leading_zeros('\033')
125  == bit_width<char>() - 5 );
126  BOOST_CHECK( count_leading_zeros(' ')
127  == bit_width<char>() - 6 );
128 }
129 
130 BOOST_AUTO_TEST_CASE( test_count_leading_ones )
131 {
132  BOOST_CHECK( count_leading_ones(0) == 0 );
133  BOOST_CHECK( count_leading_ones(1) == 0 );
134  BOOST_CHECK( count_leading_ones(static_cast<boost::uint8_t>(0xFF)) == 8 );
135  BOOST_CHECK( count_leading_ones(static_cast<boost::uint16_t>(0xFFFF)) == 16 );
136  BOOST_CHECK( count_leading_ones(static_cast<boost::uint32_t>(0xFFFFFFFF)) == 32 );
137  BOOST_CHECK( count_leading_ones(static_cast<boost::uint64_t>(0xFFFFFFFFFFFFFFFF))
138  == 64 );
139  BOOST_CHECK( count_leading_ones(static_cast<boost::uint8_t>(0xF8)) == 5 );
140  BOOST_CHECK( count_leading_ones(static_cast<boost::uint16_t>(54321)) == 2 );
141 }
142 
143 /* vim: set ts=4 sw=4: */
144 
145 BOOST_AUTO_TEST_SUITE_END()
unsigned int count_leading_ones(N n)
Returns the quantity of leading 1 bits in n ā€” i.e., the quantity of bits in n, minus the 1-based bit...
Definition: util.hpp:415
BOOST_AUTO_TEST_SUITE(test_map_location)
int lexical_cast_default< int, const char * >(const char *a, int def)
Definition: util.cpp:185
To lexical_cast(From value)
Lexical cast converts one type to another.
BOOST_AUTO_TEST_CASE(test_lexical_cast)
Definition: test_util.cpp:25
GLuint64EXT * result
Definition: glew.h:10727
unsigned int count_leading_zeros(N n)
Returns the quantity of leading 0 bits in n ā€” i.e., the quantity of bits in n, minus the 1-based bit...
Definition: util.hpp:369
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
Templates and utility-routines for strings and numbers.
std::size_t bit_width()
Returns the size, in bits, of an instance of type T, providing a convenient and self-documenting name...
Definition: util.hpp:221
unsigned int count_ones(N n)
Returns the quantity of 1 bits in n ā€” i.e., nā€™s population count.
Definition: util.hpp:260
Thrown when a lexical_cast fails.
GLsizei const GLcharARB ** string
Definition: glew.h:4503