xcdat/test/vectors_test.cpp

103 lines
1.9 KiB
C++
Raw Normal View History

2016-12-03 07:51:00 +00:00
#undef NDEBUG
#include <cassert>
#include <iostream>
#include <random>
2017-11-12 11:49:13 +00:00
#include "xcdat/BitVector.hpp"
#include "xcdat/FitVector.hpp"
2016-12-03 07:51:00 +00:00
using namespace xcdat;
namespace {
2017-11-12 11:49:13 +00:00
constexpr size_t SIZE = 1U << 10;
void test_vector() {
std::vector<int> orig_vec(SIZE);
{
std::random_device rnd;
for (size_t i = 0; i < SIZE; ++i) {
orig_vec[i] = rnd();
}
}
auto copied_vec = orig_vec; // copy
Vector<int> vec(copied_vec);
assert(copied_vec.empty());
for (size_t i = 0; i < SIZE; ++i) {
assert(orig_vec[i] == vec[i]);
}
Vector<int> swapped_vec;
vec.swap(swapped_vec);
assert(vec.is_empty());
for (size_t i = 0; i < SIZE; ++i) {
assert(orig_vec[i] == swapped_vec[i]);
}
}
2016-12-03 07:51:00 +00:00
void test_bit_vector() {
std::vector<bool> orig_bit_vector;
{
std::random_device rnd;
2017-11-12 11:49:13 +00:00
for (size_t i = 0; i < SIZE; ++i) {
2016-12-03 07:51:00 +00:00
orig_bit_vector.push_back(rnd() % 2 == 0);
}
}
BitVector bit_vector;
{
BitVectorBuilder builder;
2017-11-12 11:49:13 +00:00
for (size_t i = 0; i < SIZE; ++i) {
2016-12-03 07:51:00 +00:00
builder.push_back(orig_bit_vector[i]);
}
2017-07-12 06:48:49 +00:00
bit_vector = BitVector(builder, true, true);
2016-12-03 07:51:00 +00:00
}
2017-11-12 11:49:13 +00:00
assert(bit_vector.size() == SIZE);
2016-12-03 07:51:00 +00:00
2017-11-12 11:49:13 +00:00
id_type sum = 0;
for (id_type i = 0; i < SIZE; ++i) {
2016-12-03 07:51:00 +00:00
assert(bit_vector[i] == orig_bit_vector[i]);
if (bit_vector[i]) {
assert(sum == bit_vector.rank(i));
assert(i == bit_vector.select(sum));
++sum;
}
}
assert(bit_vector.num_1s() == sum);
2017-11-12 11:49:13 +00:00
assert(bit_vector.num_0s() == SIZE - sum);
2016-12-03 07:51:00 +00:00
}
void test_small_vector() {
2017-03-29 06:01:06 +00:00
std::vector<id_type> orig_vector;
2016-12-03 07:51:00 +00:00
{
std::random_device rnd;
2017-11-12 11:49:13 +00:00
for (size_t i = 0; i < SIZE; ++i) {
2016-12-03 07:51:00 +00:00
orig_vector.push_back(rnd() & UINT16_MAX);
}
}
2017-03-29 06:01:06 +00:00
FitVector small_vector(orig_vector);
2016-12-03 07:51:00 +00:00
assert(orig_vector.size() == small_vector.size());
2017-11-12 11:49:13 +00:00
for (size_t i = 0; i < SIZE; ++i) {
2016-12-03 07:51:00 +00:00
assert(orig_vector[i] == small_vector[i]);
}
}
} // namespace
int main() {
2017-11-12 11:49:13 +00:00
test_vector();
2016-12-03 07:51:00 +00:00
test_bit_vector();
test_small_vector();
return 0;
}