Files
akbasic/tests/for_next.c

194 lines
6.3 KiB
C
Raw Permalink Normal View History

Finish the language: every remaining verb group, and the defects that blocked them Closes groups A, C, D, E, F, H and J of TODO.md section 4, plus RESTORE and RENUMBER, and closes section 6 -- all seventeen reference defects. Seven of those turned out to have been fixed or never ported and nobody had written it down; the audit records the evidence for each. Two of the seventeen were real. math_plus mutated its left operand when the operand was mutable, so A# + 1 could modify A#; it was gated on FOR/NEXT coverage because NEXT relied on the mutation, so tests/for_next.c came first and NEXT now writes the counter back itself. And the binary operators summed both numeric fields of their right operand, which no BASIC program can reach -- that one needed a test written against the value API. Writing the tests turned up eight defects nobody had listed. Seven are fixed: IF A = 2 THEN was a parse error; only == worked IF ... AND ... was a parse error, because a condition parsed as one relation IF A = 1 OR B = 2 THEN was silently always false, and so was IF A THEN EXIT before any NEXT restarted the program and exhausted the variable pool READ never found a DATA line above it, and swallowed the lines between PRINT 2 + 2 at the prompt was filed as program text instead of answering a short read discarded its bytes, so COPY produced empty files every verb taking an argument list said "peek() returned nil token!" on none The eighth is not fixed and cannot be quietly: a FOR whose step overshoots runs its body one extra time, and FOR I = 1 TO 1 runs it zero times. The two errors cancel for a step of 1, which is why neither was noticed. Correcting them changes the expected output of a checked-in acceptance file, and tests/reference/README.md forbids editing one to suit this interpreter. It is tests/for_semantics.c in AKBASIC_KNOWN_FAILING_TESTS, asserting the correct contract, and TODO.md items 19 and 20. Sprites are real libakgl actors with a renderfunc of their own, because akgl_actor_render draws every sprite square and an actor has no per-axis scale. Both are filed upstream. SPRSAV takes an image file, an SSHAPE handle or a 63-element integer array -- a string here cannot hold a zero byte. Verbs that need hardware that does not exist are refused by name with the reason rather than faked: SYS, HEADER, COLLECT, BACKUP, BOOT, FILTER, and DIRECTORY, which is refused for a missing libakstdlib wrapper filed upstream. 94 tests in the default build, 93 with SDL, 94 under ASan and UBSan, doxygen clean. The Go acceptance corpus stayed green throughout. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-31 21:50:37 -04:00
/**
* @file for_next.c
* @brief Full coverage of FOR/NEXT, which TODO.md section 6 item 4 is gated on.
*
* That item is `math_plus` mutating its left operand in place when the operand
* is mutable, where every other operator clones. It is a real defect -- `A# + 1`
* modifying `A#` depending on where the value came from -- and it was left alone
* because `NEXT`'s loop increment *relied* on the mutation: fixing one without
* the other silently breaks every FOR loop in the language.
*
* The golden corpus covers a plain loop, a nested pair and the
* `waitingForCommand` case. What it does not cover is `STEP`, a negative step, a
* float counter, `EXIT`, or a body that assigns to the loop variable -- and
* those are the cases where an increment that writes to the wrong storage shows
* up. This file is what made the fix safe to make.
*/
#include <string.h>
#include <akbasic/error.h>
#include <akbasic/runtime.h>
#include "harness.h"
#include "testutil.h"
/** @brief Run a program to completion in RUN mode, from a string. */
static akerr_ErrorContext AKERR_NOIGNORE *run_program(const char *source)
{
PREPARE_ERROR(errctx);
PASS(errctx, harness_start(NULL));
PASS(errctx, akbasic_runtime_load(&HARNESS_RUNTIME, source));
PASS(errctx, akbasic_runtime_start(&HARNESS_RUNTIME, AKBASIC_MODE_RUN));
PASS(errctx, akbasic_runtime_run(&HARNESS_RUNTIME, 0));
SUCCEED_RETURN(errctx);
}
/** @brief The counter advances by one and stops *after* the limit, not on it. */
static void test_plain_loop(void)
{
TEST_REQUIRE_OK(run_program("10 FOR I# = 1 TO 4\n"
"20 PRINT I#\n"
"30 NEXT I#\n"));
/*
* Every value from the start to the limit, once each. That the body sees the
* *incremented* value at all is what says the increment reached the variable
* rather than a scratch copy of it -- the whole point of this file.
*
* What the counter reads after the loop is a separate question, and this
* interpreter answers it differently from a C128: see tests/for_semantics.c.
*/
TEST_REQUIRE_STR(HARNESS_OUTPUT, "1\n2\n3\n4\n");
harness_stop();
}
/** @brief STEP advances by what it says, and the limit is still a limit. */
static void test_step(void)
{
/*
* A step that lands exactly on the limit. The overshooting case is a known
* defect and lives in tests/for_semantics.c; what is asserted here is only
* that STEP is read and applied.
*/
TEST_REQUIRE_OK(run_program("10 FOR I# = 0 TO 9 STEP 3\n"
"20 PRINT I#\n"
"30 NEXT I#\n"));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "0\n3\n6\n9\n");
harness_stop();
}
/** @brief A negative step counts down, and the comparison flips with it. */
static void test_negative_step(void)
{
TEST_REQUIRE_OK(run_program("10 FOR I# = 5 TO 1 STEP -2\n"
"20 PRINT I#\n"
"30 NEXT I#\n"));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "5\n3\n1\n");
harness_stop();
}
/** @brief A float counter accumulates in the float field, not the integer one. */
static void test_float_counter(void)
{
TEST_REQUIRE_OK(run_program("10 FOR I% = 1.0 TO 2.0 STEP 0.5\n"
"20 PRINT I%\n"
"30 NEXT I%\n"));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "1.000000\n1.500000\n2.000000\n");
harness_stop();
}
/**
* @brief A body that assigns to the loop variable changes where the loop goes.
*
* The counter is ordinary storage, so writing to it is legal and the loop reads
* the written value. This is the case that tells an increment landing in the
* variable apart from one landing in a scratch value that happens to be read
* back: with a scratch increment the assignment on line 20 would be overwritten
* rather than built on.
*/
static void test_body_assigns_to_counter(void)
{
TEST_REQUIRE_OK(run_program("10 FOR I# = 1 TO 10\n"
"20 I# = I# + 3\n"
"30 PRINT I#\n"
"40 NEXT I#\n"));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "4\n8\n12\n");
harness_stop();
}
/** @brief EXIT leaves the loop and lands after its NEXT, with the wait cleared. */
static void test_exit(void)
{
TEST_REQUIRE_OK(run_program("10 FOR I# = 1 TO 10\n"
"20 PRINT I#\n"
"30 IF I# = 2 THEN EXIT\n"
"40 NEXT I#\n"
"50 PRINT \"OUT\"\n"));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "1\n2\nOUT\n");
harness_stop();
/* And a second loop afterwards still runs, so the wait really was cleared. */
TEST_REQUIRE_OK(run_program("10 FOR I# = 1 TO 10\n"
"20 IF I# = 1 THEN EXIT\n"
"30 NEXT I#\n"
"40 FOR J# = 1 TO 2\n"
"50 PRINT J#\n"
"60 NEXT J#\n"));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "1\n2\n");
harness_stop();
}
/** @brief Nested loops each advance their own counter and unwind in order. */
static void test_nested(void)
{
TEST_REQUIRE_OK(run_program("10 FOR I# = 1 TO 2\n"
"20 FOR J# = 1 TO 2\n"
"30 PRINT I# * 10 + J#\n"
"40 NEXT J#\n"
"50 NEXT I#\n"));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "11\n12\n21\n22\n");
harness_stop();
}
/**
* @brief `A# + 1` does not modify `A#`.
*
* The defect itself, stated as a program. Addition on a variable read out of the
* environment used to update the variable in place, so this printed `2` and then
* `2` -- the first PRINT changing what the second one saw.
*/
static void test_addition_does_not_mutate(void)
{
TEST_REQUIRE_OK(run_program("10 A# = 1\n"
"20 PRINT A# + 1\n"
"30 PRINT A#\n"
"40 PRINT A# + 1\n"
"50 PRINT A#\n"));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "2\n1\n2\n1\n");
harness_stop();
/* The same for a float, and for a string, which concatenates. */
TEST_REQUIRE_OK(run_program("10 A% = 1.5\n"
"20 PRINT A% + 1.0\n"
"30 PRINT A%\n"));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "2.500000\n1.500000\n");
harness_stop();
TEST_REQUIRE_OK(run_program("10 A$ = \"X\"\n"
"20 PRINT A$ + \"Y\"\n"
"30 PRINT A$\n"));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "XY\nX\n");
harness_stop();
/* And inside an expression used twice on one line. */
TEST_REQUIRE_OK(run_program("10 A# = 5\n"
"20 PRINT (A# + 1) + (A# + 1)\n"
"30 PRINT A#\n"));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "12\n5\n");
harness_stop();
}
int main(void)
{
test_plain_loop();
test_step();
test_negative_step();
test_float_counter();
test_body_assigns_to_counter();
test_exit();
test_nested();
test_addition_does_not_mutate();
return akbasic_test_failures;
}