Two defects that the same mechanism produced, both found by making the examples in docs/ actually run. DLOAD scans the file it reads through the runtime's own scanner, so by the time it returns, the tokens of the line that invoked it are gone and hadlinenumber and environment->lineno describe the last line of the *file*. The REPL carried on parsing what it believed was still its own buffer, concluded the leftovers were program text because hadlinenumber was now true, and filed the DLOAD command itself under that line number -- silently replacing the last line of every program loaded from a prompt. It now abandons the rest of the line, which is the only thing that can sensibly follow replacing the whole program. The second is the same class of oversight one layer up. process_line_run() has always swallowed the error context after interpret() puts the BASIC-level line on the sink, with a comment saying that is what goal 3 requires. The direct-mode branch of process_line_repl() used a bare PASS instead, so `VERIFY` against a file that did not match -- an ordinary user answer, not a fault -- came back as a stack trace and terminated the driver. An embedding host would have gone with it. Both tests fail when their fix is reverted. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-Authored-By: Andrew Kesterson <andrew@aklabs.net>
328 lines
11 KiB
C
328 lines
11 KiB
C
/**
|
|
* @file disk_verbs.c
|
|
* @brief Tests the group F verbs: channels, files, and the ones that are refused.
|
|
*
|
|
* These touch the filesystem, so they work in a temporary directory of their own
|
|
* and clean up after themselves. What is asserted is the seam between BASIC and
|
|
* `aksl_f*` -- that a verb reaches the right call with the right arguments --
|
|
* plus, for the five that need a real 1541, that they refuse *by name* rather
|
|
* than doing something plausible and wrong.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.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 Read a whole file, for checking what a verb wrote. */
|
|
static bool file_contents(const char *name, char *dest, size_t len)
|
|
{
|
|
FILE *fp = fopen(name, "rb");
|
|
size_t got = 0;
|
|
|
|
if ( fp == NULL ) {
|
|
return false;
|
|
}
|
|
got = fread(dest, 1, len - 1, fp);
|
|
dest[got] = '\0';
|
|
fclose(fp);
|
|
return true;
|
|
}
|
|
|
|
/** @brief A channel written and then read back gives the same lines. */
|
|
static void test_channel_round_trip(void)
|
|
{
|
|
char contents[256];
|
|
|
|
remove("t_chan.txt");
|
|
TEST_REQUIRE_OK(run_program("10 DOPEN 1, \"t_chan.txt\", W\n"
|
|
"20 PRINT #1, \"FIRST\"\n"
|
|
"30 PRINT #1, \"SECOND\"\n"
|
|
"40 DCLOSE 1\n"
|
|
"50 DOPEN 2, \"t_chan.txt\"\n"
|
|
"60 INPUT #2, A$\n"
|
|
"70 INPUT #2, B$\n"
|
|
"80 DCLOSE 2\n"
|
|
"90 PRINT A$\n"
|
|
"100 PRINT B$\n"));
|
|
TEST_REQUIRE_STR(HARNESS_OUTPUT, "FIRST\nSECOND\n");
|
|
TEST_REQUIRE(file_contents("t_chan.txt", contents, sizeof(contents)),
|
|
"the file should exist");
|
|
TEST_REQUIRE_STR(contents, "FIRST\nSECOND\n");
|
|
harness_stop();
|
|
remove("t_chan.txt");
|
|
}
|
|
|
|
/** @brief A numeric variable reads a numeric line, and end of file gives zero. */
|
|
static void test_channel_numeric(void)
|
|
{
|
|
remove("t_num.txt");
|
|
TEST_REQUIRE_OK(run_program("10 DOPEN 1, \"t_num.txt\", W\n"
|
|
"20 PRINT #1, 42\n"
|
|
"30 DCLOSE 1\n"
|
|
"40 DOPEN 2, \"t_num.txt\"\n"
|
|
"50 INPUT #2, A#\n"
|
|
"60 INPUT #2, B#\n"
|
|
"70 DCLOSE 2\n"
|
|
"80 PRINT A#\n"
|
|
"90 PRINT B#\n"));
|
|
TEST_REQUIRE_STR(HARNESS_OUTPUT, "42\n0\n");
|
|
harness_stop();
|
|
remove("t_num.txt");
|
|
}
|
|
|
|
/** @brief APPEND adds to a file rather than replacing it. */
|
|
static void test_append(void)
|
|
{
|
|
char contents[256];
|
|
|
|
remove("t_app.txt");
|
|
TEST_REQUIRE_OK(run_program("10 DOPEN 1, \"t_app.txt\", W\n"
|
|
"20 PRINT #1, \"ONE\"\n"
|
|
"30 DCLOSE 1\n"
|
|
"40 APPEND 1, \"t_app.txt\"\n"
|
|
"50 PRINT #1, \"TWO\"\n"
|
|
"60 DCLOSE 1\n"));
|
|
TEST_REQUIRE(file_contents("t_app.txt", contents, sizeof(contents)),
|
|
"the file should exist");
|
|
TEST_REQUIRE_STR(contents, "ONE\nTWO\n");
|
|
harness_stop();
|
|
remove("t_app.txt");
|
|
}
|
|
|
|
/** @brief Reading a channel opened for writing is refused, and the reverse too. */
|
|
static void test_channel_direction(void)
|
|
{
|
|
remove("t_dir.txt");
|
|
TEST_REQUIRE_OK(run_program("10 DOPEN 1, \"t_dir.txt\", W\n"
|
|
"20 INPUT #1, A$\n"));
|
|
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "opened for writing") != NULL,
|
|
"reading a write channel should be refused, got \"%s\"", HARNESS_OUTPUT);
|
|
harness_stop();
|
|
|
|
TEST_REQUIRE_OK(run_program("10 DOPEN 1, \"t_dir.txt\"\n"
|
|
"20 PRINT #1, \"NO\"\n"));
|
|
TEST_REQUIRE(strstr(HARNESS_OUTPUT, "opened for reading") != NULL,
|
|
"writing a read channel should be refused, got \"%s\"", HARNESS_OUTPUT);
|
|
harness_stop();
|
|
remove("t_dir.txt"); |