Files
JIANT/tests/math_user_test.c
T
Claude 7198f8f88d V10.8.75: math_user_test names the FLOAT formula over 64 bytes right (F32, 125 B)
The V10.8.75 commit message says "B16 (65) and F43 (125) don't fit; every
other of BYTE's 96 factory texts does": the one is F32, and BYTE's F05 and
F22 (its kick and drum loop) use ph(), a state per voice, so they are
refused too. 93 of BYTE's 96 factory formulas run in JIANT FM's MATH USER.
The test's label only; nothing else changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
2026-11-30 11:13:42 +00:00

320 lines
16 KiB
C

/* SPDX-License-Identifier: GPL-3.0-only
* Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant) */
/* (V10.8.75) MATH USER (firmware/src/math_user.c): a formula of one's own on a MATH track, JIANT BYTE's formula machine.
* BYTE every formula of tests/math_user_gold.h (BYTE's 48 + 48 factory formulas and formulas of every
* operator, compiled and run by BYTE's own fvm.c) gives here BYTE's results, value for value (a hash of
* 4160 times at three a's); one over 64 bytes is refused
* factory each of MATH's own 48 bytebeats as BYTE's bytecode: lofi_byte's values, every a, 2^17 times and random
* DIVF MULF against the 64-bit arithmetic BYTE writes, random operands
* check what is refused: an unknown or stateful op, eu(), a push without a value, the stack under 1 or over 12,
* two left, a number cut short, a table out of its pool, a header bit, 0 or 65 bytes
* fuzz random bytes and mutated formulas: the check never crashes, the machine never leaves its stack or code
* (ASan), whatever it is given, checked or not; its time bounded (one pass over at most 64 bytes)
* voice ALGO USER through MATH's BYTE voice: B01's bytecode renders as B01 bit for bit at C4 (t under the 11 kHz
* cap); the cap holds at C7; FLOAT plays an INT formula as BYTE's fvm_sample; four USER voices at most
* storage a project keeps each track's formula ('F' 'U', the last block) and ALGO USER (-4); one without the block
* loads none; ALGO 128 .. 191 (B33 .. B48) is saved and loaded (was SAVE FORMAT ERROR 4), a user
* preset's too; the knob: USER first only on a track that holds a formula
* cc -O1 -g -w -Wno-implicit-function-declaration -fsanitize=address,bounds -Ibuild/gen -Ifirmware/src -Itests \
* tests/math_user_test.c -lm -o build/math-user && build/math-user */
#define UI_TEST_NO_MAIN 1
#include "ui_test.c"
#include "math_user_gold.h"
#include <time.h>
static int fails;
static uint8_t buf[1u + MU_MAX + 8u]; /* (a code as mu_c holds it, and room past it) */
static uint32_t run1(const uint8_t *c, uint32_t n, uint32_t t, uint32_t w, uint32_t a)
{
uint32_t u[3] = {a, 60u, 100u};
buf[0] = (uint8_t)n;
memcpy(buf + 1, c, n);
return mu_run(buf, t, w, u);
}
static uint32_t rs = 12345u;
static uint32_t rnd32(void) { rs ^= rs << 13; rs ^= rs >> 17; rs ^= rs << 5; return rs; }
static void golden(void)
{
uint32_t g, k, x, bad = 0, run = 0, refused = 0;
static const uint32_t AS[3] = {1, 7, 16};
for (g = 0; g < NELEM(MU_GOLD); g++) {
const mu_gold_t *m = &MU_GOLD[g];
if (m->n > MU_MAX) {
refused += !mu_check(m->code, m->n);
continue;
}
if (!mu_check(m->code, m->n) || (m->code[0] & 1u) != m->ty) {
printf("math-user: not taken: %s\n", m->src);
bad++;
continue;
}
for (k = 0; k < 3u; k++) {
uint32_t h = 2166136261u;
for (x = 0; x < 4160u; x++) {
uint32_t t = x < 4096u ? x * (k + 1u) : mu_hash(x * 7919u + k);
h = (h ^ run1(m->code, m->n, t, t * 2654435761u, AS[k])) * 16777619u;
}
if (h != m->h[k]) {
printf("math-user: %s (a %u): %08x, BYTE's %08x\n", m->src, AS[k], h, m->h[k]);
bad++;
break;
}
}
run++;
}
fails += check("BYTE: every formula of the golden set (BYTE's 96 factory, every operator) runs as BYTE's fvm runs it", !bad && run > 120u);
fails += check("BYTE: the ones over 64 bytes (B16 65, F32 125) refused", refused == NELEM(MU_GOLD) - run && refused >= 2u);
printf("math-user: %u formulas as BYTE's, %u over 64 bytes refused, %u of BYTE's not in this machine (ph lp hp eu)\n",
run, refused, (uint32_t)NELEM(MU_GOLD_NO));
}
static void factory(void)
{
uint32_t f, a, i, bad = 0, n = 0;
for (f = 0; f < 48u; f++) {
const mu_gold_t *m = &MU_GOLD[f];
if (m->n > MU_MAX)
continue;
n++;
for (a = 1; a <= 16u && !bad; a++)
for (i = 0; i < 140000u; i++) {
uint32_t t = i < 131072u ? i * 3u + a : rnd32();
if ((lofi_byte(f, t, a) & 255u) != (run1(m->code, m->n, t, 0, a) & 255u)) {
printf("math-user: B%02u a %u t %u differs\n", f + 1u, a, t);
bad++;
break;
}
}
}
fails += check("factory: MATH's own bytebeats as BYTE's bytecode give lofi_byte's values (47 of 48; every a, 2^17 + random t)", !bad && n == 47u);
}
static void arith(void)
{
uint32_t i, bad = 0;
for (i = 0; i < 2000000u; i++) {
int32_t a = (int32_t)rnd32(), b = (int32_t)rnd32();
if (i & 1u) b >>= rnd32() & 31u; /* (small divisors too) */
if (i % 7u == 0u) a >>= rnd32() & 31u;
if (!b) continue;
if (mu_op(MO_DIVF, (uint32_t)a, (uint32_t)b) != (uint32_t)(int32_t)(((int64_t)a * 65536) / b)) bad++;
if (mu_op(MO_MULF, (uint32_t)a, (uint32_t)b) != (uint32_t)(int32_t)(((int64_t)a * b) >> 16)) bad++;
}
fails += check("DIVF and MULF: BYTE's 64-bit arithmetic, 2M random operands (the divider and 16 steps, no 64-bit division)",
!bad && mu_op(MO_DIVF, 5, 0) == 0u && mu_op(MO_DIVU, 5, 0) == 0u && mu_op(MO_MODU, 5, 0) == 0u &&
mu_op(MO_SHL, 1, 33) == 2u && mu_op(MO_DIVFI, 0x80000000u, 0xFFFFFFFFu) == 0x80000000u);
fails += check("BYTE's operator numbers (fvm.c FO_*): ADD 0 .. EU 31, NBIN 32, NEG 33 .. SAT 50, PUSH 52 SEL 53 TAB 54",
MO_ADD == 0 && MO_GES == 19 && MO_MODF == 30 && MO_EU == 31 && MO_NBIN == 32 && MO_NEG == 33 && MO_SAT == 50 &&
MO_NUN == 51 && MO_PUSH == 52 && MO_SEL == 53 && MO_TAB == 54);
}
static void refusals(void)
{
static const struct { uint8_t n, c[16]; const char *why; } NO[] = {
{2, {0, 0xF4 | 0}, "PUSH t alone is fine: (the control)"},
{3, {0, 0xF4, 56 | 1 << 6}, "lp (56)"}, {3, {0, 0xF4, 55}, "ph (55)"}, {4, {0, 0xF4, 0xF4, 31}, "eu (31)"},
{2, {0, 52}, "a push without a value"}, {2, {0, 0}, "ADD on an empty stack"}, {3, {0, 0xF4, 0}, "ADD with one"},
{3, {0, 0xF4, 0xF4}, "two left"}, {3, {0, 0x74, 0x80}, "a number cut short"}, {1, {0}, "no instruction"},
{2, {4, 0xF4}, "a header bit"}, {5, {2, 1, 'a', 0xF4, 54}, "TAB without its table"},
{6, {2, 1, 'a', 0xF4, 54, 0x02}, "a table past the pool (offset 0, length 2)"}, {3, {0, 0xF4, 33 | 64}, "NEG with an operand"},
{3, {0, 0x74, 0x05}, "PUSH 5 (fine: the control)"}, {7, {0, 0x74, 0xFF, 0xFF, 0xFF, 0xFF, 0x81}, "a number of six bytes"},
};
uint32_t i, bad = 0;
uint8_t deep[MU_MAX], c65[65];
for (i = 0; i < NELEM(NO); i++) {
int want = i == 0u || i == 14u;
if (mu_check(NO[i].c, NO[i].n) != want) { printf("math-user: check wrong: %s\n", NO[i].why); bad++; }
}
deep[0] = 0;
for (i = 1; i <= 13u; i++) deep[i] = 0xF4; /* 13 pushes: one over the stack */
for (; i < 26u; i++) deep[i] = 0; /* .. 12 ADDs */
bad += mu_check(deep, 26) != 0;
bad += mu_check(deep + 1, 24) != 0; /* (deep + 1: header 0xF4: refused) */
deep[13] = 0; /* 12 pushes, 11 ADDs: one left */
bad += mu_check(deep, 24) != 1;
memset(c65, 0, sizeof c65); c65[1] = 0xF4;
for (i = 2; i + 1u < 65u; i += 2) { c65[i] = 0xC0; c65[i + 1u] = 0; }
bad += mu_check(c65, 65) != 0 || mu_check(c65, 0) != 0;
fails += check("check: lp ph eu, a push without a value, the stack under 1 / over 12 / two left, a number cut short or of "
"six bytes, a header bit, a table out of its pool, 0 and 65 bytes refused", !bad);
{
uint8_t ok1[3] = {0, 0xF4, 0};
track_t *t = &trk[3];
uint8_t before[1u + MU_MAX];
mu_set(3, deep, 24);
memcpy(before, mu_c[3], sizeof before);
fails += check("mu_set: a refused formula leaves the track's as it was; 0 bytes clears", mu_set(3, ok1, 3) == 1 &&
!memcmp(before, mu_c[3], sizeof before) && mu_set(3, 0, 0) == 0 && !mu_c[3][0]);
(void)t;
}
}
static void fuzz(void)
{
static uint8_t c[80];
uint32_t i, k, n, acc = 0, taken = 0;
clock_t t0 = clock();
double secs;
for (i = 0; i < 400000u; i++) {
if (i & 1u) { /* random bytes */
n = rnd32() % 70u;
for (k = 0; k < n; k++) c[k] = (uint8_t)rnd32();
if (n) c[0] &= 3u;
} else { /* a golden formula mutated */
const mu_gold_t *m = &MU_GOLD[rnd32() % NELEM(MU_GOLD)];
n = m->n > MU_MAX ? MU_MAX : m->n;
memcpy(c, m->code, n);
for (k = 1u + rnd32() % 3u; k--; ) c[rnd32() % n] = (uint8_t)rnd32();
}
if (mu_check(c, n)) {
taken++;
acc += run1(c, n, rnd32(), rnd32(), 1u + rnd32() % 16u);
}
{ /* unchecked: a block reading a half-written one */
uint32_t u[3] = {5, 60, 100};
buf[0] = (uint8_t)rnd32(); /* (any length, up to 255: read to MU_MAX) */
memcpy(buf + 1, c, n > MU_MAX ? MU_MAX : n);
acc += mu_run(buf, rnd32(), rnd32(), u);
}
}
secs = (double)(clock() - t0) / CLOCKS_PER_SEC;
printf("math-user: fuzz: 400000 inputs, %u checked and run, 800000 runs unchecked, %.2f s (acc %08x)\n", taken, secs, acc);
fails += check("fuzz: random and mutated bytes: no crash, checked or not (ASan), each run one pass (< 1 s / 800k runs here)",
taken > 1000u && secs < 30.0);
}
static uint32_t voice_hash(uint32_t user, uint32_t note, uint32_t wave)
{
static voice_t v;
static int32_t o[CTL];
vmod_t m;
track_t *t = &trk[0];
uint32_t k, i, h = 2166136261u;
memset(&v, 0, sizeof v); memset(&m, 0, sizeof m);
t->eng_req = t->engine = ENGI_LOFI;
t->p[P_E0] = 2; t->p[P_E1] = (int16_t)wave; t->p[P_E2] = user ? (int16_t)MU_ALGO : 0; t->p[P_E3] = 0; t->p[P_E4] = 0;
t->p[P_E5] = 0; t->p[P_E6] = 0; t->p[P_E7] = 0; t->p[P_FCUT] = 127; t->p[P_FRES] = 50;
m.amp0 = m.amp1 = 32767; m.shape = 64 << 8; m.pitch16 = (int32_t)note << 4;
vh_sh_c[0] = 1;
lofi_note_on(t, &v);
v.note = (uint8_t)note; v.vel = 100;
for (k = 0; k < 300u; k++) {
memset(o, 0, sizeof o);
lofi_render(t, &v, o, CTL / 2u, &m);
for (i = 0; i < CTL / 2u; i++) h = (h ^ (uint32_t)o[i]) * 16777619u;
}
vh_sh_c[0] = 0;
return h;
}
static void voices(void)
{
const mu_gold_t *b01 = &MU_GOLD[0];
uint32_t i, k, n;
mu_set(0, b01->code, b01->n);
fails += check("voice: ALGO USER with B01's bytecode, BYTE wave at C4 (t ~8 kHz, under the cap): B01 bit for bit",
voice_hash(1, 60, 5) == voice_hash(0, 60, 5));
fails += check("voice: at C7 (t past 11 kHz) the cap holds its reads: not B01's every sample, a sound all the same",
voice_hash(1, 96, 5) != voice_hash(0, 96, 5) && voice_hash(1, 96, 5) == voice_hash(1, 96, 5));
fails += check("voice: FLOAT plays it too (an INT formula as BYTE's fvm_sample), not silent", voice_hash(1, 60, 6) != voice_hash(1, 60, 6) + 1u &&
voice_hash(1, 60, 6) != 2166136261u);
/* four USER voices at most, every track's together */
ui_power_on();
for (k = 0; k < 3u; k++) {
trk[k].eng_req = trk[k].engine = ENGI_LOFI;
trk[k].p[P_E1] = 5; trk[k].p[P_E2] = MU_ALGO; trk[k].p[P_VOICE] = V_POLY;
mu_set(k, b01->code, b01->n);
}
for (k = 0; k < 3u; k++)
for (i = 0; i < 3u; i++)
trk_note_on(&trk[k], 48u + k * 7u + i * 2u, 100);
for (n = 0, k = 0; k < 3u; k++)
for (i = 0; i < NVOICE; i++)
n += trk[k].v[i].active && trk[k].v[i].stage != 4u;
fails += check("voice: nine USER notes on three tracks: four sound, the oldest faded (voice.c mu_cap)", n == MU_VOICES);
for (k = 0; k < 3u; k++) {
trk_all_off(&trk[k]);
trk[k].p[P_E2] = 0;
}
}
static void storage(void)
{
static project_t q;
static project_store_t st;
static uint8_t keep[NTRK][1u + MU_MAX];
const mu_gold_t *f20 = 0, *b03 = &MU_GOLD[2];
uint32_t g, k;
char v[8];
const char *u;
for (g = 0; g < NELEM(MU_GOLD); g++)
if (MU_GOLD[g].n == 63u && MU_GOLD[g].ty) f20 = &MU_GOLD[g];
ui_power_on();
memset(mu_c, 0, sizeof mu_c);
set_engine_of(&trk[0], ENGI_LOFI); set_engine_of(&trk[1], ENGI_LOFI); set_engine_of(&trk[2], ENGI_LOFI);
trk[0].p[P_E2] = MU_ALGO; trk[1].p[P_E2] = 150; trk[2].p[P_E1] = RW_FLOAT; trk[2].p[P_E2] = MU_ALGO;
mu_set(0, b03->code, b03->n);
mu_set(2, f20->code, f20->n);
memcpy(keep, mu_c, sizeof keep);
project_capture(&q);
proj_why = 0;
fails += check("storage: a project with two formulas, ALGO USER and ALGO 150 (B38) packs (was SAVE FORMAT ERROR 4 at 150)",
proj_pack(&st, &q) && proj_import(&q, &st, sizeof st));
fails += check("storage: unpacked, ALGO -4 and 150 as saved", q.t[0].p[P_E2] == MU_ALGO && q.t[1].p[P_E2] == 150 && q.t[2].p[P_E2] == MU_ALGO);
memset(mu_c, 0, sizeof mu_c);
trk[1].p[P_E2] = 0;
fails += check("storage: restored, the formulas byte for byte, ALGO as saved", !project_restore_runtime(&q) &&
!memcmp(keep, mu_c, sizeof keep) && trk[0].p[P_E2] == MU_ALGO && trk[1].p[P_E2] == 150);
{
uint32_t n = proj_packed_size(&q), at = n - (2u + NTRK + b03->n + f20->n);
fails += check("storage: 'F' 'U' the last block (an older firmware stops before it), its size counted",
st.raw[at] == 'F' && st.raw[at + 1u] == 'U' && st.raw[at + 2u] == b03->n);
}
memset(proj_mu, 0, sizeof proj_mu); /* a project of before: no block */
fails += check("storage: a project without the block packs as before and loads no formula", proj_pack(&st, &q) &&
proj_import(&q, &st, sizeof st) && !project_restore_runtime(&q) && !mu_c[0][0] && !mu_c[2][0]);
/* a user preset keeps ALGO 150 (its byte 214, read as -42 before: B01) and USER (the track's formula with it) */
song.sel = 1;
trk[1].p[P_E2] = 150;
k = (uint32_t)up_store(5, "ALGO150"); /* (0 stored; 3: in RAM, the host has no flash) */
trk[1].p[P_E2] = 3;
fails += check("storage: a user preset with ALGO 150 loads ALGO 150 (was B01)", (k == 0u || k == 3u) && !up_load(5) &&
trk[1].p[P_E2] == 150);
trk[1].p[P_E2] = MU_ALGO;
k = (uint32_t)up_store(6, "USER");
trk[1].p[P_E2] = 3;
fails += check("storage: .. and one with ALGO USER, USER", (k == 0u || k == 3u) && !up_load(6) && trk[1].p[P_E2] == MU_ALGO);
/* the knob: USER only where a formula is */
trk[1].p[P_E1] = RW_BYTE; trk[1].p[P_E2] = 0;
mu_set(1, 0, 0);
fails += check("knob: no formula: ALGO 0 .. 191, B01 .. B48 (as V10.8.74)", track_desc(&trk[1], P_E2)->min == 0 &&
track_desc(&trk[1], P_E2)->max == 191);
mu_set(1, b03->code, b03->n);
param_format(track_desc(&trk[1], P_E2), MU_ALGO, v, &u);
fails += check("knob: with a formula, USER first (-4), then B01 at 0, B48 at 191", track_desc(&trk[1], P_E2)->min == MU_ALGO &&
!strcmp(v, "USER") && (param_format(track_desc(&trk[1], P_E2), 0, v, &u), !strcmp(v, "B01")) &&
(param_format(track_desc(&trk[1], P_E2), 191, v, &u), !strcmp(v, "B48")));
trk[1].p[P_E1] = RW_FLOAT;
param_format(track_desc(&trk[1], P_E2), MU_ALGO, v, &u);
fails += check("knob: FLOAT's too (USER, F01 ..)", !strcmp(v, "USER") && (param_format(track_desc(&trk[1], P_E2), 4, v, &u), !strcmp(v, "F02")));
mu_set(1, 0, 0);
trk[1].p[P_E2] = 0;
for (k = 0; k < NTRK; k++) mu_set(k, 0, 0);
}
int main(void)
{
int bad0;
golden();
factory();
arith();
refusals();
fuzz();
voices();
storage();
bad0 = fails;
printf(bad0 ? "math-user: %d FAILED\n" : "math-user: all ok\n", bad0);
return bad0 != 0;
}