Spaces:
Running
Running
File size: 7,321 Bytes
ef8334d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | #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";
}
|