The Battle for Wesnoth  1.15.1+dev
test_util.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 2018 by Karol Nowak <grywacz@gmail.com>
3  Part of the Battle for Wesnoth Project https://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 "utils/math.hpp"
20 
21 #include <cstdint>
22 
23 BOOST_AUTO_TEST_SUITE( util )
24 
25 BOOST_AUTO_TEST_CASE( test_bit_width )
26 {
27  BOOST_CHECK( bit_width<uint8_t>() == 8 );
28  BOOST_CHECK( bit_width<uint16_t>() == 16 );
29  BOOST_CHECK( bit_width<uint32_t>() == 32 );
30  BOOST_CHECK( bit_width<uint64_t>() == 64 );
31 
32  BOOST_CHECK( bit_width(static_cast<uint8_t>(0)) == 8 );
33  BOOST_CHECK( bit_width(static_cast<uint16_t>(0)) == 16 );
34  BOOST_CHECK( bit_width(static_cast<uint32_t>(0)) == 32 );
35  BOOST_CHECK( bit_width(static_cast<uint64_t>(0)) == 64 );
36 }
37 
38 BOOST_AUTO_TEST_CASE( test_count_ones )
39 {
40  BOOST_CHECK( count_ones(0) == 0 );
41  BOOST_CHECK( count_ones(1) == 1 );
42  BOOST_CHECK( count_ones(2) == 1 );
43  BOOST_CHECK( count_ones(3) == 2 );
44  BOOST_CHECK( count_ones(4) == 1 );
45  BOOST_CHECK( count_ones(5) == 2 );
46  BOOST_CHECK( count_ones(6) == 2 );
47  BOOST_CHECK( count_ones(7) == 3 );
48  BOOST_CHECK( count_ones(8) == 1 );
49  BOOST_CHECK( count_ones(9) == 2 );
50  BOOST_CHECK( count_ones(12345) == 6 );
51 }
52 
53 BOOST_AUTO_TEST_CASE( test_count_leading_zeros )
54 {
55  BOOST_CHECK( count_leading_zeros(static_cast<uint8_t>(1)) == 7 );
56  BOOST_CHECK( count_leading_zeros(static_cast<uint16_t>(1)) == 15 );
57  BOOST_CHECK( count_leading_zeros(static_cast<uint32_t>(1)) == 31 );
58  BOOST_CHECK( count_leading_zeros(static_cast<uint64_t>(1)) == 63 );
59  BOOST_CHECK( count_leading_zeros(static_cast<uint8_t>(0xFF)) == 0 );
60  BOOST_CHECK( count_leading_zeros(static_cast<unsigned int>(0))
61  == bit_width<unsigned int>() );
62  BOOST_CHECK( count_leading_zeros(static_cast<unsigned long int>(0))
63  == bit_width<unsigned long int>() );
64  BOOST_CHECK( count_leading_zeros(static_cast<unsigned long long int>(0))
65  == bit_width<unsigned long long int>() );
66  BOOST_CHECK( count_leading_zeros('\0')
67  == bit_width<char>() );
68  BOOST_CHECK( count_leading_zeros('\b')
69  == bit_width<char>() - 4 );
70  BOOST_CHECK( count_leading_zeros('\033')
71  == bit_width<char>() - 5 );
72  BOOST_CHECK( count_leading_zeros(' ')
73  == bit_width<char>() - 6 );
74 }
75 
76 BOOST_AUTO_TEST_CASE( test_count_leading_ones )
77 {
78  BOOST_CHECK( count_leading_ones(0) == 0 );
79  BOOST_CHECK( count_leading_ones(1) == 0 );
80  BOOST_CHECK( count_leading_ones(static_cast<uint8_t>(0xFF)) == 8 );
81  BOOST_CHECK( count_leading_ones(static_cast<uint16_t>(0xFFFF)) == 16 );
82  BOOST_CHECK( count_leading_ones(0xFFFFFFFFU) == 32 );
83  BOOST_CHECK( count_leading_ones(0xFFFFFFFFFFFFFFFFULL) == 64 );
84  BOOST_CHECK( count_leading_ones(static_cast<uint8_t>(0xF8)) == 5 );
85  BOOST_CHECK( count_leading_ones(static_cast<uint16_t>(54321)) == 2 );
86 }
87 
88 /* vim: set ts=4 sw=4: */
89 
90 BOOST_AUTO_TEST_SUITE_END()
unsigned int count_ones(N n)
Returns the quantity of 1 bits in n ā€” i.e., nā€™s population count.
Definition: math.hpp:122
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: math.hpp:277
General math utility functions.
std::size_t bit_width()
Returns the size, in bits, of an instance of type T, providing a convenient and self-documenting name...
Definition: math.hpp:85
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: math.hpp:231
BOOST_AUTO_TEST_CASE(test_bit_width)
Definition: test_util.cpp:25