Files
akbasic/tests/testutil.h

110 lines
3.5 KiB
C
Raw Permalink Normal View History

Port the BASIC interpreter from Go to C Reproduces deps/basicinterpret in C, in the idiom of the ak* libraries. All 41 .bas files in the reference's corpus produce byte-identical stdout, including the trailing double newline on an error line -- that comes from basicError building a string ending in \n and handing it to Println, and array_outofbounds.txt encodes it. The corpus is driven in place from the submodule as 41 individual CTest cases rather than copied, so it cannot drift from upstream. Eighteen unit tests cover what the corpus cannot reach. Three structural changes carry most of the work. Go's three reflection lookups (Command*, Function*, ParseCommand*) become one sorted dispatch table in src/verbs.c searched with bsearch; adding a verb is a row and two functions. The five Go maps become one fixed open-addressed table over aksl_strhash_djb2. And run(), which owned the process until MODE_QUIT, splits into step() plus a bounded run() -- goal 3 requires a host game to be able to bound execution, and nothing in the library now terminates the process or touches SDL. Output goes through an akbasic_TextSink vtable. src/sink_stdio.c is what makes the corpus runnable with no SDL present; the akgl-backed sink is still to come and is blocked on libakgl having no text-measurement call. src/convert.c exists because libakstdlib's aksl_ato* family cannot report a conversion failure (its TODO.md 2.1.5). The reference checks strconv's error at four sites and turns it into a BASIC error; routing those through aksl_atoi would have turned four diagnosable errors into wrong answers, with VAL("garbage") quietly returning 0. TODO.md 1.9 records which libakstdlib calls are cleared for use here and which are not. Reference defects are reproduced, not fixed: the golden files encode the observed behaviour and a silent correction is a behaviour change. TODO.md section 6 lists sixteen, and tests/known_reference_defects.c asserts the *correct* contract for six of them under AKBASIC_KNOWN_FAILING_TESTS, so a fix shows up as "unexpectedly passed". Five of the sixteen were found by this port and are new: subtraction stops after one operator so 1-2-3 computes 1-2 and abandons the rest of the line (a wrong answer, not a refused one); a unary-minus argument inflates a function's arity so ABS(-9) is rejected; a comparison operator in a line's final column is dropped; hex literals never survive the scanner; and the "Reserved word in variable name" check is dead code. Where the reference reaches undefined behaviour by a route that is defined in Go -- an out-of-range shift, a negative string multiplier, integer division by zero -- this raises instead of inheriting the UB. No golden case exercises any of them. The top-level CMakeLists shadows add_test, set_tests_properties and add_custom_target around all three add_subdirectory calls. Without it libakerror's tests land in our suite as Not Run, and its un-namespaced `coverage` target stops a coverage build from configuring at all. Test targets are akbasic_test_<name>: bare test_<name> collides with libakstdlib's, which is what broke libakgl's configure in c2b16d3. ctest 59/59; ASan+UBSan 59/59; 92.3% line and 96.9% function coverage; no warnings under -Wall -Wextra. Branch coverage is not a target, for the reason libakstdlib and libakgl both record: the akerror macros expand into large branch trees at every call site. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-30 23:53:56 -04:00
/**
* @file testutil.h
* @brief Shared assertions for the akbasic CTest programs.
*
* A test passes by exiting zero and fails by exiting non-zero with something
* informative on stderr.
*
* TEST_ASSERT and friends expand to a `break`, so they belong directly inside an
* ATTEMPT block, never inside a loop nested in one -- the same rule that governs
* CATCH and the FAIL_*_BREAK macros. TEST_REQUIRE is the return-based form for
* use inside loops and outside ATTEMPT blocks.
*/
#ifndef _AKBASIC_TESTUTIL_H_
#define _AKBASIC_TESTUTIL_H_
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <akerror.h>
/** Count of failures recorded by TEST_REQUIRE; main() returns it. */
static int akbasic_test_failures = 0;
/*
* Mark an error handled and hand it back to the pool. Assigning the result is
* what satisfies AKERR_NOIGNORE -- a (void) cast does not, and libakerror's
* IGNORE() would log to stderr and pollute the test output.
*/
static void test_discard_error(akerr_ErrorContext *e)
{
akerr_ErrorContext *released = NULL;
if ( e == NULL ) {
return;
}
e->handled = true;
released = akerr_release_error(e);
(void)released;
}
#define TEST_REQUIRE(__cond, ...) \
do { \
if ( !(__cond) ) { \
fprintf(stderr, "%s:%d: FAIL: ", __FILE__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
akbasic_test_failures += 1; \
} \
} while ( 0 )
#define TEST_REQUIRE_STR(__got, __want) \
TEST_REQUIRE(strcmp((__got), (__want)) == 0, \
"expected \"%s\", got \"%s\"", (__want), (__got))
#define TEST_REQUIRE_INT(__got, __want) \
TEST_REQUIRE((long long)(__got) == (long long)(__want), \
"expected %lld, got %lld", \
(long long)(__want), (long long)(__got))
#define TEST_REQUIRE_FEQ(__got, __want) \
TEST_REQUIRE(fabs((double)(__got) - (double)(__want)) < 1e-9, \
"expected %f, got %f", (double)(__want), (double)(__got))
/*
* Assert that a call succeeded. Releases the context on failure so the pool does
* not drain across a long test.
*/
#define TEST_REQUIRE_OK(__call) \
do { \
akerr_ErrorContext *__e = (__call); \
if ( __e != NULL && __e->status != 0 ) { \
fprintf(stderr, "%s:%d: FAIL: %s: unexpected error %d (%s): %s\n", \
__FILE__, __LINE__, #__call, __e->status, \
akerr_name_for_status(__e->status, NULL), __e->message); \
akbasic_test_failures += 1; \
} \
test_discard_error(__e); \
} while ( 0 )
/** Assert that a call failed with a specific status. */
#define TEST_REQUIRE_STATUS(__call, __status) \
do { \
akerr_ErrorContext *__e = (__call); \
if ( __e == NULL || __e->status != (__status) ) { \
fprintf(stderr, "%s:%d: FAIL: %s: expected status %d (%s), got %d\n", \
__FILE__, __LINE__, #__call, (int)(__status), \
akerr_name_for_status((__status), NULL), \
(__e == NULL ? 0 : __e->status)); \
akbasic_test_failures += 1; \
} \
test_discard_error(__e); \
} while ( 0 )
/** Assert that a call failed, without caring which status. */
#define TEST_REQUIRE_ANY_ERROR(__call) \
do { \
akerr_ErrorContext *__e = (__call); \
if ( __e == NULL || __e->status == 0 ) { \
fprintf(stderr, "%s:%d: FAIL: %s: expected an error, got success\n", \
__FILE__, __LINE__, #__call); \
akbasic_test_failures += 1; \
} \
test_discard_error(__e); \
} while ( 0 )
#endif // _AKBASIC_TESTUTIL_H_