sincekit / tests /test_sincekit.cpp
pgg-engineering's picture
Release SinceKit 0.1.0 with native benchmarks and trace replay
ef8334d verified
Raw History Blame Contribute Delete
7.32 kB
#include "sincekit.h"
#include <algorithm>
#include <cstdint>
#include <iostream>
#include <limits>
#include <random>
#include <stdexcept>
#include <string>
#include <vector>
namespace {
using sincekit::Index;
void check(bool condition, const char* expression, int line) {
if (!condition) {
throw std::runtime_error(std::string("line ") + std::to_string(line) + ": " + expression);
}
}
#define CHECK(expression) check(static_cast<bool>(expression), #expression, __LINE__)
template <typename Exception, typename F>
void expect_exception(F&& fn) {
try {
fn();
} catch (const Exception&) {
return;
}
throw std::runtime_error("expected exception was not thrown");
}
struct Change {
std::uint32_t id;
std::uint64_t revision;
bool deleted;
};
bool operator==(const Change& lhs, const Change& rhs) {
return lhs.id == rhs.id && lhs.revision == rhs.revision && lhs.deleted == rhs.deleted;
}
std::vector<Change> read(const Index& index, std::uint64_t cursor) {
std::vector<Change> changes;
const auto watermark = index.since(cursor, [&](std::uint32_t id, std::uint64_t revision, bool deleted) {
changes.push_back({id, revision, deleted});
});
CHECK(watermark == index.revision());
return changes;
}
void test_boundaries() {
if constexpr (std::numeric_limits<std::size_t>::digits > 32) {
expect_exception<std::length_error>([] {
const auto max_id_count = std::numeric_limits<std::uint32_t>::max();
Index too_large(static_cast<std::size_t>(max_id_count) + 1);
});
}
Index empty(0);
CHECK(empty.capacity() == 0);
CHECK(empty.revision() == 0);
CHECK(read(empty, 0).empty());
expect_exception<std::out_of_range>([&] { empty.touch(0); });
expect_exception<std::out_of_range>([&] { empty.erase(0); });
expect_exception<std::out_of_range>([&] { read(empty, 1); });
CHECK(empty.revision() == 0);
Index item(1);
CHECK(item.capacity() == 1);
CHECK(item.touch(0) == 1);
CHECK(item.erase(0) == 2);
CHECK(item.erase(0) == 3);
CHECK(item.touch(0) == 4);
for (auto id : {std::uint32_t{1}, std::numeric_limits<std::uint32_t>::max()}) {
expect_exception<std::out_of_range>([&] { item.touch(id); });
expect_exception<std::out_of_range>([&] { item.erase(id); });
}
bool visited = false;
expect_exception<std::out_of_range>([&] {
item.since(std::numeric_limits<std::uint64_t>::max(), [&](auto, auto, auto) { visited = true; });
});
CHECK(!visited);
CHECK(item.revision() == 4);
CHECK((read(item, 3) == std::vector<Change>{{0, 4, false}}));
CHECK(read(item, 4).empty());
}
void test_latest_only() {
Index index(10);
CHECK(index.erase(9) == 1); // Publishing a tombstone is meaningful even on a fresh index.
CHECK(index.touch(0) == 2);
CHECK(index.touch(3) == 3);
CHECK(index.erase(0) == 4);
CHECK(index.erase(9) == 5);
CHECK(index.touch(0) == 6);
CHECK(index.touch(0) == 7);
const std::vector<Change> expected{{0, 7, false}, {9, 5, true}, {3, 3, false}};
CHECK(read(index, 0) == expected);
CHECK(read(index, 2) == expected);
CHECK((read(index, 3) == std::vector<Change>{{0, 7, false}, {9, 5, true}}));
CHECK((read(index, 5) == std::vector<Change>{{0, 7, false}}));
CHECK(read(index, 7).empty());
CHECK(read(index, 0) == expected);
}
struct ConsumerFailure {};
void test_callbacks() {
Index index(3);
Index other(1);
index.touch(0);
index.touch(1);
index.erase(2);
const auto original = read(index, 0);
unsigned delivered = 0;
try {
index.since(0, [&](auto, auto, auto) {
if (++delivered == 2) {
throw ConsumerFailure{};
}
});
CHECK(false);
} catch (const ConsumerFailure&) {
CHECK(delivered == 2);
}
CHECK(read(index, 0) == original);
// A nested read must leave the outer traversal's write guard active.
std::vector<Change> outer;
index.since(0, [&](auto id, auto revision, auto deleted) {
expect_exception<ConsumerFailure>([&] {
index.since(1, [](auto, auto, auto) { throw ConsumerFailure{}; });
});
CHECK(read(index, 0) == original);
expect_exception<std::logic_error>([&] { index.touch(0); });
expect_exception<std::logic_error>([&] { index.erase(2); });
other.touch(0);
other.since(0, [&](auto, auto, auto) {
expect_exception<std::logic_error>([&] { index.erase(0); });
expect_exception<std::logic_error>([&] { other.touch(0); });
});
outer.push_back({id, revision, deleted});
});
CHECK(outer == original);
CHECK(other.revision() == 3);
CHECK(index.revision() == 3);
expect_exception<std::logic_error>([&] {
index.since(0, [&](auto id, auto, auto) { index.touch(id); });
});
CHECK(index.erase(1) == 4); // Guard cleared after exception escaped the outermost callback.
CHECK((read(index, 3) == std::vector<Change>{{1, 4, true}}));
}
// The oracle deliberately scans all IDs at read time. It shares no traversal
// structure with the implementation and handles arbitrary historical cursors.
std::vector<Change> expected_since(const std::vector<Change>& latest, std::uint64_t cursor) {
std::vector<Change> result;
for (const auto& change : latest) {
if (change.revision > cursor) {
result.push_back(change);
}
}
std::sort(result.begin(), result.end(), [](const auto& lhs, const auto& rhs) {
return lhs.revision > rhs.revision;
});
return result;
}
void differential_test(std::uint32_t seed, std::uint32_t capacity) {
std::mt19937 rng(seed);
Index index(capacity);
std::vector<Change> latest(capacity);
std::vector<std::uint64_t> cursors(19);
std::uint64_t head = 0;
for (unsigned step = 0; step < 12000; ++step) {
if (rng() % 5 < 3) {
// Mix a hot subset with the full range, including the tail ID.
const auto range = rng() % 2 ? capacity : std::min<std::uint32_t>(capacity, 7);
const auto id = static_cast<std::uint32_t>(rng() % range);
const bool deleted = rng() % 3 == 0;
const auto revision = deleted ? index.erase(id) : index.touch(id);
latest[id] = {id, ++head, deleted};
CHECK(revision == head);
} else {
auto& cursor = cursors[rng() % cursors.size()];
if (rng() % 3 == 0) {
cursor = rng() % (head + 1);
}
CHECK(read(index, cursor) == expected_since(latest, cursor));
if (rng() % 2 == 0) {
cursor = head;
}
}
CHECK(index.revision() == head);
}
CHECK(read(index, 0) == expected_since(latest, 0));
CHECK(read(index, head).empty());
}
} // namespace
int main() {
try {
test_boundaries();
test_latest_only();
test_callbacks();
differential_test(1801, 1);
differential_test(77611, 37);
differential_test(90210, 253);
} catch (const std::exception& e) {
std::cerr << e.what() << '\n';
return 1;
}
std::cout << "sincekit tests passed\n";
}