Files
akbasic/tests/numeric_contract.c
Andrew Kesterson ecdc6b60ef
Some checks failed
akbasic CI Build / cmake_build (push) Successful in 2m52s
akbasic CI Build / sanitizers (push) Successful in 3m9s
akbasic CI Build / coverage (push) Failing after 3m12s
akbasic CI Build / akgl_build (push) Failing after 20s
akbasic CI Build / mutation_test (push) Successful in 7m27s
Delete src/convert.c: libakstdlib 0.2.0 grew the contract it existed for
That file wrapped strtoll and strtod because libakstdlib's aksl_ato* family
could not report a conversion failure at all -- atoi("not a number") returned
success with 0, turning four diagnosable errors into wrong answers. Its own note
said what to do when that changed: "When it grows it, delete src/convert.c and
switch the call sites over." 0.2.0 grew it, so it is gone.

Six call sites go straight to the library now: both literal constructors in
src/grammar.c, the line number in src/scanner.c, FunctionVAL in
src/runtime_functions.c, INPUT in src/runtime_commands.c, and GSHAPE's handle in
src/runtime_graphics.c. aksl_strtoll(str, NULL, base, &dest) rather than
aksl_atoll wherever a base is involved, because the ato* forms are base 10 and
grammar.c picks base 8 or 16 off the lexeme's prefix; the NULL endptr is what
makes trailing junk an error rather than a stopping point.

The raised status changed from AKBASIC_ERR_VALUE to AKERR_VALUE, and the message
with it -- VAL("garbage") now says `no digits in "garbage"`. Section 1.8 makes
message text part of the acceptance contract, so that was checked against the
corpus before touching anything: no golden file contained a conversion message,
which is also why section 1.9 had been asking for one. Two exist now, so the next
change to that text has to move a golden file, and one of them pins the
reference's octal-literal defect (section 6 item 10) while it is still
deliberately reproduced.

tests/convert.c became tests/numeric_contract.c rather than being deleted with
the code. The assertions did not stop being worth making when the wrapper went
away -- they became assertions about a contract this port depends on and no
longer owns, and a regression in it would make four things quietly return zero.

Repoints the CI mutation job, which was bounded to src/convert.c and
src/symtab.c. src/symtab.c alone measures 74.1% against the gate of 65.
src/audio_tables.c was measured as a replacement second file and scored 64.7%:
that is a real gap rather than a reason to skip it, since almost every survivor
is in akbasic_audio_state_init, where nothing asserts a freshly initialised audio
state is actually zeroed -- the same gap this job's history records closing for
symtab. Recorded in TODO.md so the file can earn its place back.

One thing worth knowing before reading any score here, and now written down: the
harness's ICR operator only rewrites the constants 0 and 1, so a lookup table of
other values produces no mutants and a high score over one says nothing about
whether its entries are right. tests/audio_verbs.c now asserts all 32 ADSR
entries against the datasheet anyway, because a hand-edit typo is the real
failure mode there -- but that could not and did not move the mutation number,
and claiming otherwise would be the kind of thing this file exists to stop.

72/72 core, 73/73 with libakgl, 72/72 under ASan+UBSan, coverage 93.6% line and
97.8% function, clean under -Wall -Wextra, doxygen clean.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-31 10:28:04 -04:00

103 lines
4.2 KiB
C

/**
* @file numeric_contract.c
* @brief Pins the string-to-number contract this port depends on libakstdlib for.
*
* `src/convert.c` used to live here in spirit: it wrapped `strtoll`/`strtod`
* because `libakstdlib`'s `aksl_ato*` family could not report a conversion
* failure at all, and every assertion below is one that family used to get
* wrong. As of `libakstdlib` 0.2.0 it gets them right, so the wrapper is gone
* and the call sites go straight to the library.
*
* The assertions did not go with it. Four things in this interpreter turn a
* string into a number -- an integer literal, a float literal, a line number in
* the scanner, and `VAL()` at runtime -- and every one of them relies on a bad
* string being *refused* rather than quietly becoming zero. That is a contract
* belonging to another library now, which is exactly why it is worth a test on
* this side: if it ever regresses, `VAL("garbage")` starts returning 0 and
* nothing else in the suite would say so.
*
* Same reasoning `libakstdlib`'s own `tests/test_status_registry.c` gives for
* pinning that it reserves no status range: a contract you depend on and do not
* own is worth asserting where you depend on it.
*/
#include <errno.h>
#include <akstdlib.h>
#include <akbasic/error.h>
#include "testutil.h"
int main(void)
{
long long i = 0;
double d = 0.0;
TEST_REQUIRE_OK(akbasic_error_register());
/* The shapes the scanner and the parser actually produce. */
TEST_REQUIRE_OK(aksl_atoll("1234", &i));
TEST_REQUIRE_INT(i, 1234);
TEST_REQUIRE_OK(aksl_atoll("-256", &i));
TEST_REQUIRE_INT(i, -256);
/*
* Hex goes through aksl_strtoll with an explicit base, because the ato*
* forms are base 10 and would stop at the 'x'. src/grammar.c picks the base
* from the lexeme's prefix and passes it here, so both halves matter: base
* 16 must accept the 0x, and base 8 must read a leading zero as octal --
* which is the reference's behaviour that TODO.md section 6 item 10 records
* as a defect and section 5 says to reproduce until it is fixed on purpose.
*/
TEST_REQUIRE_OK(aksl_strtoll("0xff", NULL, 16, &i));
TEST_REQUIRE_INT(i, 255);
TEST_REQUIRE_OK(aksl_strtoll("010", NULL, 8, &i));
TEST_REQUIRE_INT(i, 8);
/*
* The refusals. Every one of these returned success with 0 before
* libakstdlib 0.2.0, which is the whole reason src/convert.c existed: a
* silent 0 turns four diagnosable errors into wrong answers.
*/
TEST_REQUIRE_STATUS(aksl_atoll("", &i), AKERR_VALUE);
TEST_REQUIRE_STATUS(aksl_atoll("not a number", &i), AKERR_VALUE);
TEST_REQUIRE_STATUS(aksl_atoll("12abc", &i), AKERR_VALUE);
TEST_REQUIRE_STATUS(aksl_atoll("12 ", &i), AKERR_VALUE);
TEST_REQUIRE_STATUS(aksl_atoll("99999999999999999999999", &i), ERANGE);
/* A NULL endptr is what makes trailing junk an error rather than a stop. */
TEST_REQUIRE_STATUS(aksl_strtoll("12abc", NULL, 10, &i), AKERR_VALUE);
TEST_REQUIRE_STATUS(aksl_atoll(NULL, &i), AKERR_NULLPOINTER);
TEST_REQUIRE_STATUS(aksl_atoll("1", NULL), AKERR_NULLPOINTER);
/* The float half, which is what VAL() and a float literal both reach. */
TEST_REQUIRE_OK(aksl_atof("123.456", &d));
TEST_REQUIRE_FEQ(d, 123.456);
TEST_REQUIRE_OK(aksl_atof("-2.5", &d));
TEST_REQUIRE_FEQ(d, -2.5);
TEST_REQUIRE_OK(aksl_atof("32", &d));
TEST_REQUIRE_FEQ(d, 32.0);
TEST_REQUIRE_STATUS(aksl_atof("", &d), AKERR_VALUE);
TEST_REQUIRE_STATUS(aksl_atof("garbage", &d), AKERR_VALUE);
TEST_REQUIRE_STATUS(aksl_atof("1.5x", &d), AKERR_VALUE);
/*
* errno hygiene, kept from the wrapper's own tests because the hazard did
* not move: strtoll and strtod do not clear errno on success, so a stale
* ERANGE left by an earlier call must not make a valid conversion raise.
* Mutation testing found this when the wrapper owned it; asserting it here
* is what says libakstdlib carries the same guarantee.
*/
errno = ERANGE;
TEST_REQUIRE_OK(aksl_atoll("42", &i));
TEST_REQUIRE_INT(i, 42);
errno = ERANGE;
TEST_REQUIRE_OK(aksl_atof("2.5", &d));
TEST_REQUIRE_FEQ(d, 2.5);
return akbasic_test_failures;
}