The Battle for Wesnoth  1.17.0-dev
test_util.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 2021
3  by Karol Nowak <grywacz@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-test"
17 
18 #include <boost/test/unit_test.hpp>
19 
20 #include "utils/math.hpp"
21 
22 #include <cstdint>
23 
25 
26 BOOST_AUTO_TEST_CASE( test_bit_width )
27 {
28  BOOST_CHECK( bit_width<uint8_t>() == 8 );
29  BOOST_CHECK( bit_width<uint16_t>() == 16 );
30  BOOST_CHECK( bit_width<uint32_t>() == 32 );
31  BOOST_CHECK( bit_width<uint64_t>() == 64 );
32 
33  BOOST_CHECK( bit_width(static_cast<uint8_t>(0)) == 8 );
34  BOOST_CHECK( bit_width(static_cast<uint16_t>(0)) == 16 );
35  BOOST_CHECK( bit_width(static_cast<uint32_t>(0)) == 32 );
36  BOOST_CHECK( bit_width(static_cast<uint64_t>(0)) == 64 );
37 }
38 
39 BOOST_AUTO_TEST_CASE( test_count_ones )
40 {
41  BOOST_CHECK( count_ones(0) == 0 );
42  BOOST_CHECK( count_ones(1) == 1 );
43  BOOST_CHECK( count_ones(2) == 1 );
44  BOOST_CHECK( count_ones(3) == 2 );
45  BOOST_CHECK( count_ones(4) == 1 );
46  BOOST_CHECK( count_ones(5) == 2 );
47  BOOST_CHECK( count_ones(6) == 2 );
48  BOOST_CHECK( count_ones(7) == 3 );
49  BOOST_CHECK( count_ones(8) == 1 );
50  BOOST_CHECK( count_ones(9) == 2 );
51  BOOST_CHECK( count_ones(12345) == 6 );
52 }
53 
54 BOOST_AUTO_TEST_CASE( test_count_leading_zeros )
55 {
56  BOOST_CHECK( count_leading_zeros(static_cast<uint8_t>(1)) == 7 );
57  BOOST_CHECK( count_leading_zeros(static_cast<uint16_t>(1)) == 15 );
58  BOOST_CHECK( count_leading_zeros(static_cast<uint32_t>(1)) == 31 );
59  BOOST_CHECK( count_leading_zeros(static_cast<uint64_t>(1)) == 63 );
60  BOOST_CHECK( count_leading_zeros(static_cast<uint8_t>(0xFF)) == 0 );
61  BOOST_CHECK( count_leading_zeros(static_cast<unsigned int>(0))
62  == bit_width<unsigned int>() );
63  BOOST_CHECK( count_leading_zeros(static_cast<unsigned long int>(0))
64  == bit_width<unsigned long int>() );
65  BOOST_CHECK( count_leading_zeros(static_cast<unsigned long long int>(0))
66  == bit_width<unsigned long long int>() );
67  BOOST_CHECK( count_leading_zeros('\0')
68  == bit_width<char>() );
69  BOOST_CHECK( count_leading_zeros('\b')
70  == bit_width<char>() - 4 );
71  BOOST_CHECK( count_leading_zeros('\033')
72  == bit_width<char>() - 5 );
73  BOOST_CHECK( count_leading_zeros(' ')
74  == bit_width<char>() - 6 );
75 }
76 
77 BOOST_AUTO_TEST_CASE( test_count_leading_ones )
78 {
79  BOOST_CHECK( count_leading_ones(0) == 0 );
80  BOOST_CHECK( count_leading_ones(1) == 0 );
81  BOOST_CHECK( count_leading_ones(static_cast<uint8_t>(0xFF)) == 8 );
82  BOOST_CHECK( count_leading_ones(static_cast<uint16_t>(0xFFFF)) == 16 );
83  BOOST_CHECK( count_leading_ones(0xFFFFFFFFU) == 32 );
84  BOOST_CHECK( count_leading_ones(0xFFFFFFFFFFFFFFFFULL) == 64 );
85  BOOST_CHECK( count_leading_ones(static_cast<uint8_t>(0xF8)) == 5 );
86  BOOST_CHECK( count_leading_ones(static_cast<uint16_t>(54321)) == 2 );
87 }
88 
89 /* vim: set ts=4 sw=4: */
90 
91 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:143
Definition: optimer.hpp:22
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:298
BOOST_AUTO_TEST_SUITE(filesystem)
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:106
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:252
BOOST_AUTO_TEST_CASE(test_bit_width)
Definition: test_util.cpp:26