The old test.r8asm was a three round hash: `clb_add_nc` + `clb_mul`, nothing
else. It proved "add and multiply are enough for a hash" and it was the file
every session was told to check, but a regression in, say, clb_divmod or raw_sub
could not show up there.
It is now the library's kitchen sink instead: one call per standard macro,
grouped from the smallest to the biggest
A lib set_0 set_1 erase rol step back .anchor copy bit_copy
copy_to_buffer bit_copy_to_buffer clb_bit_mov
B bit slices clb_bit_add_nc clb_bit_inc_nc bit_raw_add/inc/and/or/xor
clb_bit_and/or/xor
C raw_* raw_add raw_inc raw_and raw_or raw_xor raw_sub (operands kept)
D clb_* clb_add_nc clb_inc_nc clb_neg_nc clb_sub_nc clb_not_ip
clb_inc_ip clb_neg_ip clb_dec_ip clb_nonzero clb_add_ip
E big clb_ones + loop/endloop, clb_divmod, clb_digits
F language a user %macro that uses %rep and %for, .var arithmetic,
two inline %synth blocks (map and stat, both cache hits)
G the old hash, now two rounds
Each call is followed by `output`, so the OUT stream is the fingerprint.
examples_test now runs this file too and compares it with the `; out` line in
the header (it already did that for examples/), which makes the file an actual
assertion instead of a length in the docs: an example that stops printing what
its header says fails the test.
Numbers: 58722 characters, 215169 steps, 100 values. r8forge_derive,
r8forge_tape_run and ROT-8-viz's rot8_test produce the identical stream (the
last six values are the hash: 1322 5 82 0 1786 94).
Two layout decisions come from that cross check:
* The tape has to stay under 65536 instructions: that is ROT-8-viz's
INS_SIZE (include/rot8/rot8.h) and read_tape truncates silently past it.
An earlier version of this file was 76047 and the viz stopped after 99 of
its 104 outputs, which is how the limit was found.
* So the hash keeps two rounds and the two rounds share one clb_mul scratch
block (erased in between) instead of carrying three separate ones.
The hash's own rules are unchanged: the multiplier stays 141 because it is the
historical value, not because small operands must sit in MUL any more.
SPEC 10.2, the AGENTS baseline/self-check/table and the README build snippet
are updated to the new numbers and to the 65536 limit.
209 lines
6.4 KiB
C++
209 lines
6.4 KiB
C++
// Runs every program in examples/ and compares its OUT stream with the one
|
|
// written in the file header:
|
|
//
|
|
// ; out 254 0 1024 ...
|
|
//
|
|
// The marker is part of the example's documentation -- a reader sees at a
|
|
// glance what the program prints -- and this test is what keeps the doc true:
|
|
// an example that stops printing what its header says is a broken example, and
|
|
// one that does not assemble at all would otherwise rot silently.
|
|
//
|
|
// It is also the only place that runs examples/*.r8asm: the %synth ones are
|
|
// covered combination by combination by r8forge_synth_test, but every file in
|
|
// examples/ is still a stand alone program, so it has to keep running.
|
|
//
|
|
// r8forge_examples_test [examples dir] default: examples
|
|
#include "r8forge/r8asm.hh"
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
struct machine {
|
|
std::vector<uint16_t> cells = std::vector<uint16_t>(kDefaultMemoryCells, 0);
|
|
std::vector<uint16_t> out;
|
|
size_t dp = 0;
|
|
size_t steps = 0;
|
|
bool finished = true; // false: the step limit ran out
|
|
|
|
static uint16_t ror(uint16_t v) {
|
|
return uint16_t((v >> 1) | ((v & 1u) << (kDefaultCellSize - 1)));
|
|
}
|
|
|
|
void run(const std::string &tape, size_t limit = 5'000'000) {
|
|
for (size_t pc = 0; pc < tape.size(); pc++) {
|
|
if (++steps > limit) {
|
|
finished = false;
|
|
return;
|
|
}
|
|
switch (tape[pc]) {
|
|
case '>':
|
|
dp = (dp + 1) % cells.size();
|
|
break;
|
|
case '<':
|
|
dp = (dp + cells.size() - 1) % cells.size();
|
|
break;
|
|
case '-': // flb
|
|
cells[dp] ^= 1;
|
|
break;
|
|
case '+': // ror
|
|
cells[dp] = ror(cells[dp]);
|
|
break;
|
|
// biz / rnz test the LOW BIT, not "is the cell zero" (ROT-8-viz
|
|
// src/rot8.c). Reading them as a Brainfuck style zero test makes every
|
|
// byte level program look like an endless loop.
|
|
case '[':
|
|
if ((cells[dp] & 1) == 0) {
|
|
int depth = 1;
|
|
while (depth) {
|
|
pc++;
|
|
if (tape[pc] == '[')
|
|
depth++;
|
|
else if (tape[pc] == ']')
|
|
depth--;
|
|
}
|
|
}
|
|
break;
|
|
case ']':
|
|
if ((cells[dp] & 1) == 1) {
|
|
int depth = 1;
|
|
while (depth) {
|
|
pc--;
|
|
if (tape[pc] == ']')
|
|
depth++;
|
|
else if (tape[pc] == '[')
|
|
depth--;
|
|
}
|
|
}
|
|
break;
|
|
case '.':
|
|
out.push_back(cells[dp]);
|
|
break;
|
|
default: // ',' (input) is not modelled
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
// import_file() only reports what the parser found; an unknown macro is found
|
|
// while the tree is expanded, and a truncated tape still runs and prints
|
|
// something plausible. Same order as r8forge's own main.
|
|
bool assemble(const std::filesystem::path &file, std::string &tape) {
|
|
r8asm assembler;
|
|
if (!assembler.import_file(file.string(), kDefaultCellSize)) {
|
|
std::cerr << " could not assemble " << file.string() << "\n";
|
|
return false;
|
|
}
|
|
tape = assembler.export_rot8();
|
|
if (assembler.failed()) {
|
|
std::cerr << " could not assemble " << file.string() << "\n";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// The `; out 1 2 3` line in the header. Returns false when it is missing or
|
|
// carries no numbers -- the marker is required, not optional.
|
|
bool expected_out(const std::filesystem::path &file, std::vector<int> &want) {
|
|
std::ifstream in(file);
|
|
std::string line;
|
|
while (std::getline(in, line)) {
|
|
if (!line.empty() && line.back() == '\r')
|
|
line.pop_back();
|
|
const size_t at = line.find_first_not_of(" \t");
|
|
if (at == std::string::npos || line[at] != ';')
|
|
continue;
|
|
std::istringstream rest(line.substr(at + 1));
|
|
std::string word;
|
|
if (!(rest >> word) || word != "out")
|
|
continue;
|
|
std::vector<int> values;
|
|
int v;
|
|
while (rest >> v)
|
|
values.push_back(v);
|
|
if (values.empty())
|
|
continue;
|
|
want = values;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
std::string join(const std::vector<int> &v) {
|
|
std::ostringstream os;
|
|
for (size_t i = 0; i < v.size(); i++)
|
|
os << (i ? " " : "") << v[i];
|
|
return os.str();
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char **argv) {
|
|
const std::filesystem::path dir = argc > 1 ? argv[1] : "examples";
|
|
std::error_code ec;
|
|
std::vector<std::filesystem::path> files;
|
|
for (const auto &entry : std::filesystem::directory_iterator(dir, ec)) {
|
|
if (entry.path().extension() == ".r8asm")
|
|
files.push_back(entry.path());
|
|
}
|
|
if (ec || files.empty()) {
|
|
std::cerr << "cannot read " << dir.string() << " (" << ec.message() << ")\n";
|
|
return 2;
|
|
}
|
|
std::sort(files.begin(), files.end());
|
|
|
|
int failures = 0;
|
|
auto check = [&](const std::filesystem::path &file, const std::string &name) {
|
|
std::vector<int> want;
|
|
if (!expected_out(file, want)) {
|
|
std::cout << name << ": no '; out ...' line in the header\n";
|
|
failures++;
|
|
return;
|
|
}
|
|
std::string tape;
|
|
if (!assemble(file, tape)) {
|
|
failures++;
|
|
return;
|
|
}
|
|
machine m;
|
|
m.run(tape);
|
|
std::vector<int> got;
|
|
for (uint16_t v : m.out)
|
|
got.push_back(int(v));
|
|
const bool ok = m.finished && got == want;
|
|
std::cout << name << ": " << tape.size() << " characters, " << m.steps
|
|
<< " steps, out " << join(got) << (ok ? " ok" : " FAILED");
|
|
if (!ok) {
|
|
std::cout << " (want " << join(want) << (m.finished ? "" : ", HANG") << ")";
|
|
failures++;
|
|
}
|
|
std::cout << "\n";
|
|
};
|
|
|
|
for (const std::filesystem::path &file : files)
|
|
check(file, file.filename().string());
|
|
|
|
// asm/test.r8asm is the flagship: it calls every macro in the library, so it
|
|
// gets exactly the same header check. It does not live in examples/ because
|
|
// it is a test, not a demo. The path is relative to the examples directory
|
|
// so this works whatever the working directory is.
|
|
const std::filesystem::path flagship = dir / ".." / "asm" / "test.r8asm";
|
|
const bool have_flagship = std::filesystem::exists(flagship);
|
|
if (have_flagship)
|
|
check(flagship, "asm/test.r8asm");
|
|
|
|
std::cout << (failures ? "FAILED: " + std::to_string(failures) + " file(s)\n"
|
|
: "all " + std::to_string(files.size()) + " examples" +
|
|
(have_flagship ? " and asm/test.r8asm" : "") +
|
|
" print what their headers say\n");
|
|
return failures ? 1 : 0;
|
|
}
|