#include "sincekit.h" #include #include #include #include #include #include #include #include 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(expression), #expression, __LINE__) template 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 read(const Index& index, std::uint64_t cursor) { std::vector 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::digits > 32) { expect_exception([] { const auto max_id_count = std::numeric_limits::max(); Index too_large(static_cast(max_id_count) + 1); }); } Index empty(0); CHECK(empty.capacity() == 0); CHECK(empty.revision() == 0); CHECK(read(empty, 0).empty()); expect_exception([&] { empty.touch(0); }); expect_exception([&] { empty.erase(0); }); expect_exception([&] { 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::max()}) { expect_exception([&] { item.touch(id); }); expect_exception([&] { item.erase(id); }); } bool visited = false; expect_exception([&] { item.since(std::numeric_limits::max(), [&](auto, auto, auto) { visited = true; }); }); CHECK(!visited); CHECK(item.revision() == 4); CHECK((read(item, 3) == std::vector{{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 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{{0, 7, false}, {9, 5, true}})); CHECK((read(index, 5) == std::vector{{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 outer; index.since(0, [&](auto id, auto revision, auto deleted) { expect_exception([&] { index.since(1, [](auto, auto, auto) { throw ConsumerFailure{}; }); }); CHECK(read(index, 0) == original); expect_exception([&] { index.touch(0); }); expect_exception([&] { index.erase(2); }); other.touch(0); other.since(0, [&](auto, auto, auto) { expect_exception([&] { index.erase(0); }); expect_exception([&] { other.touch(0); }); }); outer.push_back({id, revision, deleted}); }); CHECK(outer == original); CHECK(other.revision() == 3); CHECK(index.revision() == 3); expect_exception([&] { 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{{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 expected_since(const std::vector& latest, std::uint64_t cursor) { std::vector 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 latest(capacity); std::vector 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(capacity, 7); const auto id = static_cast(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"; }