1 /* Copyright (C) 2010 Wildfire Games.
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #include "lib/self_test.h"
27 //#define EQUALS(actual, expected) ENSURE((actual) == (expected))
28 #define EQUALS TS_ASSERT_EQUALS
30 class TestBits
: public CxxTest::TestSuite
35 EQUALS(Bit
<unsigned>(0), 1u);
36 EQUALS(Bit
<unsigned>(8), 0x100u
);
37 EQUALS(Bit
<u32
>(31), u32(0x80000000ul
));
38 EQUALS(Bit
<u64
>(1), u64(2));
39 EQUALS(Bit
<u64
>(32), u64(0x100000000ull
));
40 EQUALS(Bit
<u64
>(63), u64(0x8000000000000000ull
));
45 EQUALS(IsBitSet(0u, 1), false);
46 EQUALS(IsBitSet(1u, 1), false);
47 EQUALS(IsBitSet(2u, 1), true);
48 EQUALS(IsBitSet
<u32
>(0xFFFFFFFFul
, 0), true);
49 EQUALS(IsBitSet
<u32
>(0xFFFFFFFFul
, 31), true);
50 EQUALS(IsBitSet
<u64
>(0xFFFFFFFFFFFFFFFFull
, 0), true);
51 EQUALS(IsBitSet
<u64
>(0xFFFFFFFFFFFFFFFFull
, 31), true);
52 EQUALS(IsBitSet
<u64
>(0xFFFFFFFFFFFFFFFFull
, 32), true);
53 EQUALS(IsBitSet
<u64
>(0xFFFFFFFFFFFFFFFFull
, 63), true);
58 EQUALS(bit_mask
<u16
>(0), 0);
59 EQUALS(bit_mask
<u16
>(2), 0x3);
60 EQUALS(bit_mask
<u16
>(16), 0xFFFF);
61 EQUALS(bit_mask
<u32
>(0), 0u);
62 EQUALS(bit_mask
<u32
>(2), 0x3u
);
63 EQUALS(bit_mask
<u32
>(32), 0xFFFFFFFFul
);
64 EQUALS(bit_mask
<u64
>(0), 0u);
65 EQUALS(bit_mask
<u64
>(2), 0x3u
);
66 EQUALS(bit_mask
<u64
>(32), 0xFFFFFFFFull
);
67 EQUALS(bit_mask
<u64
>(64), 0xFFFFFFFFFFFFFFFFull
);
72 EQUALS(bits
<u16
>(0xFFFF, 0, 15), 0xFFFF);
73 EQUALS(bits
<u16
>(0xFFFF, 0, 7), 0xFF);
74 EQUALS(bits
<u16
>(0xFFFF, 8, 15), 0xFF);
75 EQUALS(bits
<u16
>(0xFFFF, 14, 15), 0x3);
76 EQUALS(bits
<u16
>(0xAA55, 4, 11), 0xA5);
77 EQUALS(bits
<u16
>(0xAA55, 14, 15), 0x2);
78 EQUALS(bits
<u32
>(0ul, 0, 31), 0ul);
79 EQUALS(bits
<u32
>(0xFFFFFFFFul
, 0, 31), 0xFFFFFFFFul
);
80 EQUALS(bits
<u64
>(0ull, 0, 63), 0ull);
81 EQUALS(bits
<u64
>(0xFFFFFFFFull
, 0, 31), 0xFFFFFFFFull
);
82 EQUALS(bits
<u64
>(0x0000FFFFFFFF0000ull
, 16, 47), 0xFFFFFFFFull
);
83 EQUALS(bits
<u64
>(0xFFFFFFFFFFFFFFFFull
, 0, 63), 0xFFFFFFFFFFFFFFFFull
);
84 EQUALS(bits
<u64
>(0xA5A5A5A5A5A5A5A5ull
, 32, 63), 0xA5A5A5A5ull
);
87 void test_PopulationCount()
89 EQUALS(PopulationCount
<u8
>(0), 0u);
90 EQUALS(PopulationCount
<u8
>(4), 1u);
91 EQUALS(PopulationCount
<u8
>(0x28), 2u);
92 EQUALS(PopulationCount
<u8
>(0xFF), 8u);
93 EQUALS(PopulationCount
<u32
>(0x0ul
), 0u);
94 EQUALS(PopulationCount
<u32
>(0x8ul
), 1u);
95 EQUALS(PopulationCount
<u32
>(0xFFFFul
), 16u);
96 EQUALS(PopulationCount
<u32
>(0xFFFFFFFFul
), 32u);
97 EQUALS(PopulationCount
<u64
>(0x0ull
), 0u);
98 EQUALS(PopulationCount
<u64
>(0x10ull
), 1u);
99 EQUALS(PopulationCount
<u64
>(0xFFFFull
), 16u);
100 EQUALS(PopulationCount
<u64
>(0xFFFFFFFFull
), 32u);
101 EQUALS(PopulationCount
<u64
>(0xFFFFFFFFFFFFFFFEull
), 63u);
102 EQUALS(PopulationCount
<u64
>(0xFFFFFFFFFFFFFFFFull
), 64u);
107 EQUALS(is_pow2(0u), false);
108 EQUALS(is_pow2(~0u), false);
109 EQUALS(is_pow2(0x80000001), false);
110 EQUALS(is_pow2(1), true);
111 EQUALS(is_pow2(1u << 31), true);
114 void test_ceil_log2()
116 EQUALS(ceil_log2(3u), 2u);
117 EQUALS(ceil_log2(0xffffffffu
), 32u);
118 EQUALS(ceil_log2(1u), 0u);
119 EQUALS(ceil_log2(256u), 8u);
120 EQUALS(ceil_log2(0x80000000u
), 31u);
123 void test_floor_log2()
125 EQUALS(floor_log2(1.f
), 0);
126 EQUALS(floor_log2(3.f
), 1);
127 EQUALS(floor_log2(256.f
), 8);
130 void test_round_up_to_pow2()
132 EQUALS(round_up_to_pow2(0u), 1u);
133 EQUALS(round_up_to_pow2(1u), 1u);
134 EQUALS(round_up_to_pow2(127u), 128u);
135 EQUALS(round_up_to_pow2(128u), 128u);
136 EQUALS(round_up_to_pow2(129u), 256u);
139 void test_round_down_to_pow2()
141 EQUALS(round_down_to_pow2(1u), 1u);
142 EQUALS(round_down_to_pow2(127u), 64u);
143 EQUALS(round_down_to_pow2(128u), 128u);
144 EQUALS(round_down_to_pow2(129u), 128u);
149 EQUALS(round_up( 0u, 16u), 0u);
150 EQUALS(round_up( 4u, 16u), 16u);
151 EQUALS(round_up(15u, 16u), 16u);
152 EQUALS(round_up(20u, 32u), 32u);
153 EQUALS(round_up(29u, 32u), 32u);
154 EQUALS(round_up(0x1000u
, 0x1000u
), 0x1000u
);
155 EQUALS(round_up(0x1001u
, 0x1000u
), 0x2000u
);
156 EQUALS(round_up(0x1900u
, 0x1000u
), 0x2000u
);
159 void test_round_down()
161 EQUALS(round_down( 0u, 16u), 0u);
162 EQUALS(round_down( 4u, 16u), 0u);
163 EQUALS(round_down(15u, 16u), 0u);
164 EQUALS(round_down(20u, 16u), 16u);
165 EQUALS(round_down(29u, 16u), 16u);
166 EQUALS(round_down(0x1900u
, 0x1000u
), 0x1000u
);
167 EQUALS(round_down(0x2001u
, 0x2000u
), 0x2000u
);