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