2020-02-23 19:35:25 +00:00
|
|
|
#include <catch2/catch.hpp>
|
2023-01-09 17:47:41 +00:00
|
|
|
#include <llarp/util/bits.hpp>
|
2019-02-07 00:22:54 +00:00
|
|
|
|
2020-02-24 15:26:35 +00:00
|
|
|
using namespace llarp::bits;
|
|
|
|
|
2020-02-23 19:35:25 +00:00
|
|
|
TEST_CASE("test bit counting, 8-bit", "[bits]") {
|
2020-02-24 18:04:03 +00:00
|
|
|
// Workaround for gcc 5's stdlib; we can drop this crap (and drop all the `T`'s below) once we
|
|
|
|
// stop supporting it.
|
|
|
|
using T = std::tuple<unsigned char, size_t>;
|
2020-02-24 15:26:35 +00:00
|
|
|
auto x = GENERATE(table<unsigned char, size_t>({
|
2020-02-24 18:04:03 +00:00
|
|
|
T{0b00000000, 0},
|
|
|
|
T{0b00000001, 1},
|
|
|
|
T{0b00000010, 1},
|
|
|
|
T{0b00000100, 1},
|
|
|
|
T{0b00001000, 1},
|
|
|
|
T{0b00010000, 1},
|
|
|
|
T{0b00100000, 1},
|
|
|
|
T{0b01000000, 1},
|
|
|
|
T{0b10000000, 1},
|
|
|
|
T{0b11111111, 8},
|
2020-02-23 19:35:25 +00:00
|
|
|
}));
|
2020-02-24 15:26:35 +00:00
|
|
|
std::array<unsigned char, 1> arr{{std::get<0>(x)}};
|
|
|
|
auto expected = std::get<1>(x);
|
|
|
|
REQUIRE( count_array_bits(arr) == expected );
|
2020-02-23 19:35:25 +00:00
|
|
|
}
|
2019-02-07 00:22:54 +00:00
|
|
|
|
2020-02-23 19:35:25 +00:00
|
|
|
TEST_CASE("test bit counting, 20 x 8-bit", "[bits]") {
|
2020-02-24 15:26:35 +00:00
|
|
|
std::array<unsigned char, 20> x{{
|
|
|
|
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
|
|
|
|
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
|
|
|
|
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
|
|
|
|
0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000}};
|
|
|
|
REQUIRE( count_array_bits(x) == 0 );
|
2020-02-23 19:35:25 +00:00
|
|
|
|
2020-02-24 15:26:35 +00:00
|
|
|
x = {{
|
|
|
|
0b11111111, 0b00000100, 0b00000100, 0b00000100, 0b00000100,
|
|
|
|
0b11111111, 0b00000100, 0b00000100, 0b00000100, 0b00000100,
|
|
|
|
0b11111111, 0b00000100, 0b00000100, 0b00000100, 0b00000100,
|
|
|
|
0b11111111, 0b00000100, 0b00000100, 0b00000100, 0b00000100}};
|
|
|
|
REQUIRE( count_array_bits(x) == 48 );
|
2020-02-23 19:35:25 +00:00
|
|
|
}
|
2019-02-07 00:22:54 +00:00
|
|
|
|
2020-02-23 19:35:25 +00:00
|
|
|
TEST_CASE("test bit counting, unsigned int", "[bits]") {
|
2020-02-24 18:04:03 +00:00
|
|
|
using T = std::tuple<unsigned int, size_t>; // gcc 5 workaround
|
2020-02-24 15:26:35 +00:00
|
|
|
auto x = GENERATE(table<unsigned int, size_t>({
|
2020-02-24 18:04:03 +00:00
|
|
|
T{0b00000000000000000000000000000000, 0},
|
|
|
|
T{0b00101010101010101010101010101010, 15},
|
|
|
|
T{0b10101010101010101010101010101010, 16},
|
|
|
|
T{0b01010101010101010101010101010101, 16},
|
|
|
|
T{0b11111111111111111111111111111111, 32},
|
2020-02-23 19:35:25 +00:00
|
|
|
}));
|
2019-02-07 00:22:54 +00:00
|
|
|
|
2020-02-24 15:26:35 +00:00
|
|
|
std::array<unsigned int, 1> arr{{std::get<0>(x)}};
|
|
|
|
auto expected = std::get<1>(x);
|
|
|
|
REQUIRE( llarp::bits::count_array_bits(arr) == expected );
|
2019-02-07 00:22:54 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 19:35:25 +00:00
|
|
|
TEST_CASE("test bit counting, unsigned long long", "[bits]") {
|
2020-02-24 18:04:03 +00:00
|
|
|
using T = std::tuple<unsigned long long, size_t>; // gcc 5 workaround
|
2020-02-24 15:26:35 +00:00
|
|
|
auto x = GENERATE(table<unsigned long long, size_t>({
|
2020-02-24 18:04:03 +00:00
|
|
|
T{0b0000000000000000000000000000000000000000000000000000000000000000, 0},
|
|
|
|
T{0b0010101010101010101010101010101000101010101010101010101010101010, 30},
|
|
|
|
T{0b1010101010101010101010101010101010101010101010101010101010101010, 32},
|
|
|
|
T{0b0101010101010101010101010101010101010101010101010101010101010101, 32},
|
|
|
|
T{0b1111111111111111111111111111111111111111111111111111111111111111, 64},
|
2020-02-23 19:35:25 +00:00
|
|
|
}));
|
2019-02-07 00:22:54 +00:00
|
|
|
|
2020-02-24 15:26:35 +00:00
|
|
|
std::array<unsigned long long, 1> arr{{std::get<0>(x)}};
|
|
|
|
auto expected = std::get<1>(x);
|
|
|
|
REQUIRE( llarp::bits::count_array_bits(arr) == expected );
|
2020-02-23 19:35:25 +00:00
|
|
|
}
|