180 lines
6.3 KiB
C
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;
|
||
|
|
}
|