Files
R8Forge/tests/byte_add_test.cc
T
dsh afd56e7a8c Fix the ROT-8 interpreters: biz / rnz test the low bit
Four interpreters here (tests/synth_test.cc, src/derive.cc and the two I added
earlier: tests/tape_run.cc, tests/byte_add_test.cc) implemented '[' and ']' as
a Brainfuck style "is this whole cell zero" test.  The reference implementation
does not:

    ROT-8-viz src/rot8.c
    rot8_biz: if ((*mem & 1) == 0) skip to just past the matching ']'
    rot8_rnz: if ((*mem & 1) == 1) jump back to the matching '['

That one difference is why every byte level program in the library looked like
an endless loop.  It is invisible in the bit level world -- for a cell holding 0
or 1 the low bit test and the zero test agree -- which is why the synth tables,
asm/test.r8asm and full_add never noticed anything.

Cross checked against the reference before touching anything: the tape our
assembler produces for "A = 128, set_0, B = 3, raw_add" prints 128 3 0 131 0 on
ROT-8-viz/build/rot8_test.exe and hung in our derive.

With the fix, measured:

    raw_add   65536/65536 pairs ok  1647 chars
    raw_and   65536/65536            639
    raw_or    65536/65536            575
    raw_xor   65536/65536            463
    raw_inc   512/512               1214
    asm/fibonacci.r8asm prints 1 1 2 3 5 8 13 21 34 55, exactly what its
    comment claims

So the byte level layer was never broken.  Two commits ago it was declared dead
-- set_0 "only converges on 0 and 1", the bits of a packed byte "unreadable",
raw_add "only survives 0+0" -- entirely on the strength of the broken
interpreters.  AGENTS.md and README.md now say the opposite and keep the wrong
diagnosis as a worked example: when byte level code looks hung, check the
interpreter first.

New:
  tests/byte_op_test.cc + drv/raw_{add,and,or,xor,inc}_byte.r8asm
      exhaustive runtime proof for the byte level library, 0.5 s
  tests/tape_run.cc + r8forge_tape_run
      a second interpreter that runs a .rot8 file directly -- cross checking
      derive is now one command

verify_set_0.r8asm now demonstrates what set_0 really does instead of claiming
it hangs: 255 -> 254, 5 -> 4, 128 -> 128 (it clears the low bit, not the cell).

The one bit per cell layer (byte_add / pack_byte) stays as a documented
alternative: it is correct under the real semantics too, just redundant.
2026-11-07 00:01:25 +08:00

170 lines
4.9 KiB
C++

// Assembles the byte-level adders (examples/add8*.r8asm) and runs them on a tiny
// ROT-8 machine for every input combination. This is the runtime check the
// library never had: pass/fail comes from executing the tape, not from looking
// at the character count.
//
// r8forge_byte_add_test [repo root]
//
// The paths in the sources are relative to the repo root, so run it from there.
#include "r8forge/r8asm.hh"
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
namespace {
struct machine {
std::vector<uint8_t> cells = std::vector<uint8_t>(256, 0);
std::vector<uint8_t> out;
size_t dp = 0;
size_t steps = 0;
bool finished = true; // false: the step limit ran out (a loop that never ends)
static uint8_t ror(uint8_t v) { return uint8_t((v >> 1) | (v << 7)); }
void run(const std::string &tape, size_t limit) {
for (size_t pc = 0; pc < tape.size(); pc++) {
if (++steps > limit) {
finished = false;
return;
}
switch (tape[pc]) {
case '>':
dp++;
break;
case '<':
dp--;
break;
case '-': // flb
cells[dp] ^= 1;
break;
case '+': // ror
cells[dp] = ror(cells[dp]);
break;
// biz / rnz test the LOW BIT of the cell (ROT-8-viz src/rot8.c), not
// "is the cell zero".
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;
}
}
}
};
// The examples lay bit i out in cell 4i + off: off 0 is A, 1 is B, 3 is scratch.
void put_bits(machine &m, int off, int value) {
for (int b = 0; b < 8; b++)
if (value & (1 << b))
m.cells[size_t(4 * b + off)] ^= 1;
}
std::string assemble(const std::string &file) {
r8asm assembler;
if (!assembler.import_file(file, 8) || assembler.failed()) {
std::cerr << "could not assemble " << file << "\n";
std::exit(1);
}
return assembler.export_rot8();
}
int failures = 0;
void report(const std::string &what, int a, int b, const machine &m, int sum,
int carry) {
if (failures++ < 5)
std::cerr << " WRONG " << what << " a=" << a << " b=" << b << " out";
if (failures <= 5) {
for (uint8_t v : m.out)
std::cerr << " " << int(v);
std::cerr << " (expect low " << (sum & 0xff) << ", carry " << carry
<< "), finished=" << m.finished << " steps=" << m.steps << "\n";
}
}
// Every (a, b) pair through examples/add8.r8asm.
int test_add(const std::string &file) {
const std::string tape = assemble(file);
std::cout << "add8: " << tape.size() << " characters\n";
int bad = 0;
for (int a = 0; a < 256; a++)
for (int b = 0; b < 256; b++) {
machine m;
put_bits(m, 0, a);
put_bits(m, 1, b);
m.run(tape, 200'000);
const int sum = a + b;
const bool ok = m.finished && m.out.size() == 2 &&
m.out[0] == (sum & 0xff) && m.out[1] == (sum >> 8);
if (!ok) {
bad++;
if (bad <= 3)
report("add8", a, b, m, sum, sum >> 8);
}
}
std::cout << " " << (bad ? "FAILED" : "all 65536 combinations ok")
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
return bad;
}
// examples/add8_inc.r8asm adds 1 three times, so the carry is only the carry of
// that last add: earlier carries are dropped (the accumulator is 8 bits).
int test_inc(const std::string &file) {
const std::string tape = assemble(file);
std::cout << "add8_inc: " << tape.size() << " characters\n";
int bad = 0;
for (int a = 0; a < 256; a++) {
machine m;
put_bits(m, 0, a);
m.run(tape, 400'000);
const int low = (a + 3) & 0xff;
const int carry = ((a + 2) & 0xff) == 0xff ? 1 : 0;
const bool ok = m.finished && m.out.size() == 2 && m.out[0] == low &&
m.out[1] == carry;
if (!ok) {
bad++;
if (bad <= 3)
report("add8_inc", a, 3, m, a + 3, carry);
}
}
std::cout << " " << (bad ? "FAILED" : "all 256 combinations ok")
<< (bad ? " (" + std::to_string(bad) + " wrong)" : "") << "\n";
return bad;
}
} // namespace
int main(int argc, char **argv) {
const std::string root = argc > 1 ? argv[1] : ".";
const int bad = test_add(root + "/examples/add8.r8asm") +
test_inc(root + "/examples/add8_inc.r8asm");
std::cout << (bad ? "FAILED\n" : "byte level adders ok\n");
return bad ? 1 : 0;
}