Spaces:
Running
Running
File size: 5,473 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 | #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";
}
|