rlm-oolong-reproduction / ctxstream /tests /test_ctxstream.cpp
Rickesh's picture
Upload folder using huggingface_hub
5afdbb2 verified
Raw
History Blame Contribute Delete
7.03 kB
// Self-checks for everything that runs without a model server. The whole point
// of ctxstream is that planning and reduction are deterministic code, so they
// are testable with no GPU, no network and no tokens.
#include "../src/ctxstream.hpp"
#include <cassert>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <numeric>
#include <string>
namespace fs = std::filesystem;
using namespace ctxstream;
static int checks = 0;
#define CHECK(cond, msg) \
do { \
++checks; \
if (!(cond)) { \
std::fprintf(stderr, "FAIL %s:%d %s\n", __FILE__, __LINE__, msg); \
return 1; \
} \
} while (0)
static int test_manifest_covers_everything() {
std::string text;
for (int i = 0; i < 5000; ++i) {
text += "row " + std::to_string(i) + " || status=alpha\n";
}
ManifestOptions opt;
opt.segment_chars = 4000;
opt.overlap_chars = 0;
const auto segs = plan(text, opt);
CHECK(!segs.empty(), "no segments planned");
// Every byte must land in some segment: a planner that silently drops the
// tail produces a confident answer over partial data.
std::size_t covered = 0;
for (const auto& s : segs) covered += s.length;
CHECK(covered >= text.size(), "segments do not cover the whole text");
CHECK(segs.front().offset == 0, "first segment must start at 0");
CHECK(segs.back().offset + segs.back().length == text.size(),
"last segment must reach the end");
// No segment may cut a row in half.
for (const auto& s : segs) {
if (s.offset + s.length >= text.size()) continue;
CHECK(text[s.offset + s.length - 1] == '\n',
"segment ends mid-record");
}
return 0;
}
static int test_manifest_pathological() {
// A single record longer than a segment must still make progress, not loop.
const std::string text(20000, 'x');
ManifestOptions opt;
opt.segment_chars = 1000;
opt.overlap_chars = 0;
const auto segs = plan(text, opt);
CHECK(segs.size() >= 20, "no progress on a record larger than a segment");
CHECK(plan("", opt).empty(), "empty text must yield no segments");
return 0;
}
static int test_parse_records() {
// The output contract, and the shapes a small model actually emits.
auto r = parse_records("alpha\t12\nbeta\t7\n");
CHECK(r.size() == 2, "tab-separated records not parsed");
CHECK(r[0].key == "alpha" && r[0].value == 12, "wrong tab parse");
// Keys with spaces must survive: "Spatial Relationship" truncated to
// "Spatial" is a real failure this replaces.
r = parse_records("Spatial Relationship\t109\n");
CHECK(r.size() == 1 && r[0].key == "Spatial Relationship",
"key with spaces truncated");
r = parse_records("alpha: 4846\n");
CHECK(r.size() == 1 && r[0].value == 4846, "colon form not parsed");
r = parse_records("gamma\t1,905\n");
CHECK(r.size() == 1 && r[0].value == 1905, "thousands separator not handled");
CHECK(parse_records("NONE\n").empty(), "NONE must yield no records");
CHECK(parse_records("I analysed the fragment and found several statuses.\n").empty(),
"prose must not parse as a record");
return 0;
}
static int test_tally_and_construct() {
std::vector<SegmentResult> rs(3);
rs[0] = {0, "alpha\t10\nbeta\t5\ngamma\t2\n", true, false, "", 0, 0, 0};
rs[1] = {1, "alpha\t20\nbeta\t5\ndelta\t1\n", true, false, "", 0, 0, 0};
// A failed segment must not be treated as empty-but-fine.
rs[2] = {2, "", false, false, "boom", 0, 0, 0};
const Tally t = tally(rs);
CHECK(t.sorted.front().first == "delta", "least-common wrong");
CHECK(t.sorted.back().first == "alpha", "most-common wrong");
CHECK(t.total == 43, "total wrong");
// This is the exact question the 4B model answered with a list of all four
// labels instead of a selection.
CHECK(construct(t, Answer::Least, "Status") == "Status: delta",
"construct(Least) wrong");
CHECK(construct(t, Answer::Most, "Status") == "Status: alpha",
"construct(Most) wrong");
return 0;
}
static int test_truncation_guard() {
CHECK(looks_truncated(350000, 16387), "gross clipping not detected");
CHECK(!looks_truncated(100000, 30000), "normal ratio flagged as truncated");
CHECK(!looks_truncated(100000, 0), "unreported token count must not flag");
return 0;
}
static int test_codegraph() {
const fs::path root = fs::temp_directory_path() / "ctxstream_gtest";
fs::remove_all(root);
fs::create_directories(root / "sub");
fs::create_directories(root / ".git");
{
std::ofstream a(root / "a.py");
a << "import helper\n\ndef alpha():\n return 1\n\nclass Beta:\n pass\n";
std::ofstream h(root / "sub" / "helper.py");
h << "def helper_fn():\n return 2\n";
std::ofstream junk(root / ".git" / "ignored.py");
junk << "def should_not_appear():\n pass\n";
}
ScanOptions so;
const CodeGraph g = scan_repo(root.string(), so);
CHECK(g.files.size() == 2, "excluded dir was scanned, or files missed");
bool saw_alpha = false, saw_beta = false, saw_helper = false;
for (const auto& s : g.symbols) {
if (s.name == "alpha" && s.kind == NodeKind::Function) saw_alpha = true;
if (s.name == "Beta" && s.kind == NodeKind::Class) saw_beta = true;
if (s.name == "helper_fn") saw_helper = true;
CHECK(s.name != "should_not_appear", "symbol from excluded dir");
}
CHECK(saw_alpha && saw_beta && saw_helper, "symbols missed");
CHECK(!g.edges.empty(), "import edge a.py -> helper.py not found");
std::string packed;
ManifestOptions mo;
mo.segment_chars = 100000;
const auto segs = plan_codebase(g, mo, &packed);
CHECK(!segs.empty(), "no codebase segments");
CHECK(packed.find("===== FILE") != std::string::npos,
"packed text missing file headers");
// A dependency must be packed before its dependent.
CHECK(packed.find("helper.py") < packed.find("a.py"),
"topological order not applied");
std::size_t covered = 0;
for (const auto& s : segs) covered += s.length;
CHECK(covered >= packed.size(), "codebase segments do not cover packed text");
fs::remove_all(root);
return 0;
}
int main() {
if (test_manifest_covers_everything()) return 1;
if (test_manifest_pathological()) return 1;
if (test_parse_records()) return 1;
if (test_tally_and_construct()) return 1;
if (test_truncation_guard()) return 1;
if (test_codegraph()) return 1;
std::printf("ok: %d checks passed\n", checks);
return 0;
}