xcdat/tests/test_bc_vector.cpp

82 lines
2.4 KiB
C++
Raw Normal View History

2021-06-25 22:12:17 +00:00
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <algorithm>
#include <random>
#include "doctest/doctest.h"
2021-06-26 14:48:29 +00:00
#include "test_common.hpp"
2021-07-02 23:12:35 +00:00
#include "xcdat/bc_vector_15.hpp"
#include "xcdat/bc_vector_16.hpp"
2021-06-27 05:06:08 +00:00
#include "xcdat/bc_vector_7.hpp"
2021-06-27 04:12:35 +00:00
#include "xcdat/bc_vector_8.hpp"
2021-06-25 22:12:17 +00:00
2021-06-29 01:09:49 +00:00
#ifdef BC_VECTOR_7
2021-06-27 05:06:08 +00:00
using bc_vector_type = xcdat::bc_vector_7;
2021-06-29 01:09:49 +00:00
#elif BC_VECTOR_8
using bc_vector_type = xcdat::bc_vector_8;
2021-07-02 23:12:35 +00:00
#elif BC_VECTOR_15
using bc_vector_type = xcdat::bc_vector_15;
#elif BC_VECTOR_16
using bc_vector_type = xcdat::bc_vector_16;
2021-06-29 01:09:49 +00:00
#endif
2021-06-27 05:06:08 +00:00
2021-06-25 22:12:17 +00:00
struct bc_unit {
std::uint64_t base;
std::uint64_t check;
};
2021-06-27 05:06:08 +00:00
std::vector<bc_unit> make_random_units(std::uint64_t n, std::uint64_t maxv, std::uint64_t seed = 13) {
2021-06-25 22:12:17 +00:00
std::mt19937_64 engine(seed);
2021-06-27 05:06:08 +00:00
std::uniform_int_distribution<std::uint64_t> dist(0, maxv);
2021-06-25 22:12:17 +00:00
std::vector<bc_unit> bc_units(n);
for (std::uint64_t i = 0; i < n; i++) {
bc_units[i].base = dist(engine);
bc_units[i].check = dist(engine);
}
return bc_units;
}
xcdat::bit_vector::builder to_bit_vector_builder(const std::vector<bool>& bits) {
xcdat::bit_vector::builder bvb(bits.size());
for (std::uint64_t i = 0; i < bits.size(); i++) {
bvb.set_bit(i, bits[i]);
}
return bvb;
}
std::uint64_t get_num_ones(const std::vector<bool>& bits) {
return std::accumulate(bits.begin(), bits.end(), 0ULL);
}
2021-06-27 05:06:08 +00:00
void test_bc_vector(const std::vector<bc_unit>& bc_units, const std::vector<bool>& leaves) {
bc_vector_type bc(bc_units, to_bit_vector_builder(leaves));
2021-06-25 22:12:17 +00:00
REQUIRE_EQ(bc.num_units(), bc_units.size());
2021-06-27 05:06:08 +00:00
REQUIRE_EQ(bc.num_leaves(), get_num_ones(leaves));
2021-06-25 22:12:17 +00:00
for (std::uint64_t i = 0; i < bc.num_units(); i++) {
2021-06-27 05:06:08 +00:00
REQUIRE_EQ(bc.is_leaf(i), leaves[i]);
if (leaves[i]) {
2021-06-25 22:12:17 +00:00
REQUIRE_EQ(bc.link(i), bc_units[i].base);
} else {
REQUIRE_EQ(bc.base(i), bc_units[i].base);
}
REQUIRE_EQ(bc.check(i), bc_units[i].check);
}
2021-06-27 05:06:08 +00:00
}
TEST_CASE("Test bc_vector 10K in [0,10K)") {
const std::uint64_t size = 10000;
auto bc_units = make_random_units(size, size - 1);
auto leaves = xcdat::test::make_random_bits(size, 0.2);
test_bc_vector(bc_units, leaves);
}
TEST_CASE("Test bc_vector 10K in [0,UINT64_MAX)") {
const std::uint64_t size = 10000;
auto bc_units = make_random_units(size, UINT64_MAX);
auto leaves = xcdat::test::make_random_bits(size, 0.2);
test_bc_vector(bc_units, leaves);
}