sincekit / tests /test_batch.cpp
pgg-engineering's picture
Release SinceKit 0.1.0 with native benchmarks and trace replay
ef8334d verified
Raw History Blame Contribute Delete
5.47 kB
#include "sincekit_batch.h"
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <limits>
#include <random>
#include <stdexcept>
#include <string>
#include <vector>
namespace {
void require(bool value, int line) {
if (!value) throw std::runtime_error("batch assertion at line " + std::to_string(line));
}
#define CHECK(expr) require((expr), __LINE__)
template<class Source>
void verify(const Source& index, const std::vector<std::uint64_t>& cursors) {
auto plan = sincekit::since_many(index, cursors);
CHECK(plan.revision == index.revision());
CHECK(plan.prefix_lengths.size() == cursors.size());
std::size_t longest = 0;
for (std::size_t i = 0; i < cursors.size(); ++i) {
std::size_t visited = 0;
index.since(cursors[i], [&](std::uint32_t id, std::uint64_t revision, bool deleted) {
CHECK(visited < plan.rows.size());
const auto& row = plan.rows[visited++];
CHECK(row.id == id && row.revision == revision && row.deleted == deleted);
});
CHECK(visited == plan.prefix_lengths[i]);
longest = std::max(longest, visited);
}
CHECK(longest == plan.rows.size());
}
void boundaries_and_ownership() {
sincekit::Index empty(0);
verify(empty, {});
verify(empty, {0, 0});
sincekit::Index index(4);
index.touch(0);
index.erase(1);
index.touch(2);
index.erase(0);
index.touch(0);
verify(index, {});
verify(index, {5, 3, 1, 4, 0, 2, 0, 3, 5});
verify(index, std::vector<std::uint64_t>(8192, 5));
verify(index, std::vector<std::uint64_t>(8192, 1));
auto saved = sincekit::since_many(index, {0, 4});
index.erase(0);
CHECK(saved.revision == 5 && saved.rows.size() == 3);
CHECK(saved.rows[0].id == 0 && saved.rows[0].revision == 5 && !saved.rows[0].deleted);
CHECK(saved.rows[2].id == 1 && saved.rows[2].deleted);
CHECK(saved.prefix_lengths == std::vector<std::size_t>({3, 1}));
for (const auto& cursors : {std::vector<std::uint64_t>{7}, {0, 0, 7},
{std::numeric_limits<std::uint64_t>::max(), 0}}) {
bool threw = false;
try { (void)sincekit::since_many(index, cursors); }
catch (const std::out_of_range&) { threw = true; }
CHECK(threw);
}
}
void exact_ranks() {
sincekit::Index index(260);
for (std::uint32_t id = 0; id < 260; ++id) index.touch(id);
std::vector<std::uint64_t> cursors(261);
for (std::size_t i = 0; i < cursors.size(); ++i) cursors[i] = i;
std::mt19937_64 rng(1299);
std::shuffle(cursors.begin(), cursors.end(), rng);
verify(index, cursors); // Direct arithmetic for contiguous revisions.
for (int i = 0; i < 16; ++i) index.erase(static_cast<std::uint32_t>(rng() % 9));
for (auto i = std::uint64_t{261}; i <= index.revision(); ++i) cursors.push_back(i);
verify(index, cursors); // Includes exact and missing ranks around word boundaries.
for (int i = 0; i < 22000; ++i) index.touch(7);
cursors.clear();
for (std::uint64_t i = 0; i <= index.revision(); i += 29) cursors.push_back(i);
cursors.push_back(index.revision());
verify(index, cursors); // A long gap forces the bounded-memory fallback.
}
void sparse_bucket_boundaries() {
sincekit::Index index(32);
for (std::uint32_t id = 0; id < 32; ++id) index.touch(id);
for (unsigned i = 0; i < 3000; ++i) index.erase(i % 9);
index.touch(18);
for (unsigned i = 0; i < 7000; ++i) index.touch(i % 9);
std::vector<std::uint64_t> cursors;
for (std::uint64_t i = 0; i <= index.revision(); ++i) cursors.push_back(i);
verify(index, cursors); // Both exact boundaries of both idle intervals.
}
struct FullRevisionRange {
std::vector<sincekit::BatchRow> rows;
std::size_t capacity() const { return rows.size(); }
std::uint64_t revision() const { return rows.front().revision; }
template<class Fn>
std::uint64_t since(std::uint64_t cursor, Fn&& fn) const {
for (const auto& row : rows) {
if (row.revision <= cursor) break;
fn(row.id, row.revision, row.deleted);
}
return revision();
}
};
void generic_source_near_wrap() {
const auto max = std::numeric_limits<std::uint64_t>::max();
FullRevisionRange timeline{{{max, 0, false}, {max - 2, 1, true},
{max / 2, 2, false}, {1, 3, true}}};
std::vector<std::uint64_t> cursors{0, 1, 2, max / 2 - 1, max / 2, max / 2 + 1,
max - 3, max - 2, max - 1, max};
std::mt19937_64 rng(593);
for (int i = 0; i < 200; ++i) cursors.push_back(rng());
verify(timeline, cursors);
}
void differential_workload() {
std::mt19937_64 rng(379022);
sincekit::Index index(193);
for (int round = 0; round < 160; ++round) {
for (int i = 0; i < 33; ++i) {
auto id = static_cast<std::uint32_t>(rng() % index.capacity());
if (rng() % 3 == 0) index.erase(id);
else index.touch(id);
}
std::vector<std::uint64_t> cursors(static_cast<std::size_t>(rng() % 300));
for (auto& cursor : cursors) cursor = rng() % (index.revision() + 1);
verify(index, cursors);
}
}
} // namespace
int main() {
boundaries_and_ownership();
exact_ranks();
sparse_bucket_boundaries();
generic_source_near_wrap();
differential_workload();
std::cout << "batch prefix equivalence tests passed\n";
}