Files
akbasic/tests/console_verbs.c
Andrew Kesterson 9845e77a5c 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

180 lines
6.3 KiB
C

/**
* @file console_verbs.c
* @brief Tests the group E verbs: SLEEP, WAIT, KEY, WINDOW and the TI clock.
*
* The two that wait are the interesting ones, and what they have to prove is
* that they *do not block*: a held step still returns, so a bounded
* akbasic_runtime_run() comes back on time and a host keeps its frame rate.
* Section 1.6 is the rule and this is where it is checked for group E.
*/
#include <string.h>
#include <akbasic/error.h>
#include <akbasic/runtime.h>
#include "harness.h"
#include "testutil.h"
/** @brief Load a program and put the runtime in RUN mode without stepping it. */
static akerr_ErrorContext AKERR_NOIGNORE *load_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));
SUCCEED_RETURN(errctx);
}
/** @brief Run a program to completion in RUN mode. */
static akerr_ErrorContext AKERR_NOIGNORE *run_program(const char *source)
{
PREPARE_ERROR(errctx);
PASS(errctx, load_program(source));
PASS(errctx, akbasic_runtime_run(&HARNESS_RUNTIME, 0));
SUCCEED_RETURN(errctx);
}
/** @brief SLEEP holds the step loop until the host's clock passes its deadline. */
static void test_sleep(void)
{
TEST_REQUIRE_OK(load_program("1 PRINT \"BEFORE\"\n"
"2 SLEEP 5\n"
"3 PRINT \"AFTER\"\n"));
TEST_REQUIRE_OK(akbasic_runtime_settime(&HARNESS_RUNTIME, 1000));
/* Line 0, line 1, line 2 -- the SLEEP arms and the program stops advancing. */
TEST_REQUIRE_OK(akbasic_runtime_run(&HARNESS_RUNTIME, 3));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "BEFORE\n");
TEST_REQUIRE(HARNESS_RUNTIME.console_state.sleeping, "SLEEP should be holding");
/*
* Twenty more steps with the clock standing still change nothing -- and,
* crucially, they *return*. A SLEEP that blocked would never get here.
*/
TEST_REQUIRE_OK(akbasic_runtime_run(&HARNESS_RUNTIME, 20));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "BEFORE\n");
/* Past the deadline, it wakes. */
TEST_REQUIRE_OK(akbasic_runtime_settime(&HARNESS_RUNTIME, 6500));
TEST_REQUIRE_OK(akbasic_runtime_run(&HARNESS_RUNTIME, 2));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "BEFORE\nAFTER\n");
TEST_REQUIRE(!HARNESS_RUNTIME.console_state.sleeping, "SLEEP should have finished");
harness_stop();
}
/** @brief A host that never sets a clock does not hang on SLEEP. */
static void test_sleep_without_a_clock(void)
{
TEST_REQUIRE_OK(run_program("1 SLEEP 10\n2 PRINT \"THROUGH\"\n"));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "THROUGH\n");
harness_stop();
}
/** @brief SLEEP refuses a negative interval rather than waiting forever. */
static void test_sleep_refuses_negative(void)
{
TEST_REQUIRE_OK(run_program("10 SLEEP -1\n"));
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "negative number of seconds") != NULL,
"SLEEP -1 should be refused, got \"%s\"", HARNESS_OUTPUT);
harness_stop();
}
/**
* @brief WAIT polls a byte and holds until it matches, without blocking.
*
* The address is a real one -- this test's own variable -- which is the only
* honest way to exercise it: on a C128 the byte is a hardware register, and here
* the only thing that can change it is something outside the program.
*/
static void test_wait(void)
{
static volatile uint8_t watched = 0;
char source[256];
watched = 0;
snprintf(source, sizeof(source),
"1 WAIT %llu, 1\n2 PRINT \"RELEASED\"\n",
(unsigned long long)(uintptr_t)&watched);
TEST_REQUIRE_OK(load_program(source));
TEST_REQUIRE_OK(akbasic_runtime_run(&HARNESS_RUNTIME, 4));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "");
TEST_REQUIRE(HARNESS_RUNTIME.console_state.waiting, "WAIT should be holding");
/* Still returning, which is the whole point. */
TEST_REQUIRE_OK(akbasic_runtime_run(&HARNESS_RUNTIME, 20));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "");
watched = 1;
TEST_REQUIRE_OK(akbasic_runtime_run(&HARNESS_RUNTIME, 2));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "RELEASED\n");
harness_stop();
}
/** @brief KEY stores a macro per key, and bare KEY lists all eight. */
static void test_key(void)
{
TEST_REQUIRE_OK(run_program("10 KEY 1, \"RUN\"\n"
"20 KEY 3, \"LIST\"\n"
"30 KEY\n"));
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "KEY 1, \"RUN\"") != NULL,
"KEY should list its macros, got \"%s\"", HARNESS_OUTPUT);
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "KEY 3, \"LIST\"") != NULL,
"KEY should list its macros, got \"%s\"", HARNESS_OUTPUT);
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "KEY 8, \"\"") != NULL,
"KEY should list undefined keys too, got \"%s\"", HARNESS_OUTPUT);
harness_stop();
TEST_REQUIRE_OK(run_program("10 KEY 9, \"NOPE\"\n"));
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "outside 1..8") != NULL,
"KEY 9 should be refused, got \"%s\"", HARNESS_OUTPUT);
harness_stop();
}
/** @brief WINDOW refuses against a sink with no character grid, which is stdio. */
static void test_window_needs_a_grid(void)
{
TEST_REQUIRE_OK(run_program("10 WINDOW 1, 1, 20, 10\n"));
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "character grid") != NULL,
"WINDOW against a stdio sink should refuse by name, got \"%s\"",
HARNESS_OUTPUT);
harness_stop();
/* An inside-out rectangle is refused before the device is even consulted. */
TEST_REQUIRE_OK(run_program("10 WINDOW 20, 10, 1, 1\n"));
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "bottom right") != NULL,
"an inverted WINDOW should be refused, got \"%s\"", HARNESS_OUTPUT);
harness_stop();
}
/** @brief TI# counts jiffies and TI$ formats them, both from the host's clock. */
static void test_ti(void)
{
TEST_REQUIRE_OK(load_program("1 PRINT TI#\n2 PRINT TI$\n"));
/* 3661 seconds is 01:01:01, and 3661 * 60 jiffies. */
TEST_REQUIRE_OK(akbasic_runtime_settime(&HARNESS_RUNTIME, 3661000));
TEST_REQUIRE_OK(akbasic_runtime_run(&HARNESS_RUNTIME, 3));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "219660\n010101\n");
harness_stop();
/* A host with no clock has a stopped one rather than a wrong one. */
TEST_REQUIRE_OK(run_program("1 PRINT TI$\n"));
TEST_REQUIRE_STR(HARNESS_OUTPUT, "000000\n");
harness_stop();
}
int main(void)
{
test_sleep();
test_sleep_without_a_clock();
test_sleep_refuses_negative();
test_wait();
test_key();
test_window_needs_a_grid();
test_ti();
return akbasic_test_failures;
}