The Battle for Wesnoth  1.19.6+dev
test_util.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 2024
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 "utils/math.hpp"
19 
20 #include <cstdint>
21 
22 static_assert(bit_width<uint8_t>() == 8);
23 static_assert(bit_width<uint16_t>() == 16);
24 static_assert(bit_width<uint32_t>() == 32);
25 static_assert(bit_width<uint64_t>() == 64);
26 
27 static_assert(bit_width(static_cast<uint8_t>(0)) == 8);
28 static_assert(bit_width(static_cast<uint16_t>(0)) == 16);
29 static_assert(bit_width(static_cast<uint32_t>(0)) == 32);
30 static_assert(bit_width(static_cast<uint64_t>(0)) == 64);
31 
32 static_assert(count_leading_zeros(static_cast<uint8_t>(1)) == 7);
33 static_assert(count_leading_zeros(static_cast<uint16_t>(1)) == 15);
34 static_assert(count_leading_zeros(static_cast<uint32_t>(1)) == 31);
35 static_assert(count_leading_zeros(static_cast<uint64_t>(1)) == 63);
36 static_assert(count_leading_zeros(static_cast<uint8_t>(0xFF)) == 0);
37 static_assert(count_leading_zeros(static_cast<unsigned int>(0)) == bit_width<unsigned int>());
38 static_assert(count_leading_zeros(static_cast<unsigned long int>(0)) == bit_width<unsigned long int>());
39 static_assert(count_leading_zeros(static_cast<unsigned long long int>(0)) == bit_width<unsigned long long int>());
40 static_assert(count_leading_zeros(static_cast<uint16_t>(12345)) == 2); // 12345 == 0x3039
41 static_assert(count_leading_zeros(static_cast<int16_t>(12345)) == 2); // 12345 == 0x3039
42 static_assert(count_leading_zeros(uint8_t{0xff}) == 0);
43 static_assert(count_leading_zeros('\0') == bit_width<char>());
44 static_assert(count_leading_zeros('\b') == bit_width<char>() - 4);
45 static_assert(count_leading_zeros('\033') == bit_width<char>() - 5);
46 static_assert(count_leading_zeros(' ') == bit_width<char>() - 6);
47 
48 static_assert(count_leading_ones(0) == 0);
49 static_assert(count_leading_ones(1) == 0);
50 static_assert(count_leading_ones(0u) == 0);
51 static_assert(count_leading_ones(1u) == 0);
52 static_assert(count_leading_ones(static_cast<uint8_t>(0xFF)) == 8);
53 static_assert(count_leading_ones(static_cast<uint16_t>(0xFFFF)) == 16);
54 static_assert(count_leading_ones(0xFFFFFFFFU) == 32);
55 static_assert(count_leading_ones(0xFFFFFFFFFFFFFFFFULL) == 64);
56 static_assert(count_leading_ones(static_cast<uint8_t>(0xF8)) == 5);
57 static_assert(count_leading_ones(static_cast<uint16_t>(54321)) == 2);
General math utility functions.
constexpr 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:179
constexpr std::enable_if_t< std::is_integral_v< N >, 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:146
constexpr 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