SilverSight/c/test_avm.c
allaun 3b6baec64e wip: durability snapshot of local working tree (pre-existing, uncommitted)
Snapshot of previously-uncommitted local work so nothing is lost after the
power outage. NOT reviewed for correctness — a WIP checkpoint, not a feature:
- multi-language hachimoji encoders (c/cpp/fortran/julia/octave/r/scala/go/rust/coq)
- formal Lean WIP (BraidTree, Eisenstein, HachimojiCapture, MathlibConnect,
  ModularFormBridge, ClusterManifold) + lakefile + E8Sidon edit
- docs/, experiments/ (epyc oisc benches), deploy/, scripts, test scaffolding
- .gitignore: exclude **/target/ and Coq build artifacts

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-02 20:49:53 -05:00

90 lines
3.8 KiB
C

/* AVM ISA v1 — C Test Harness */
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "avm_types.h"
extern void init_state(State *s, int n_locals);
extern int step(State *s, const Instr *prog, int prog_len);
extern int run(State *s, const Instr *prog, int prog_len, int fuel);
extern AnyVal eval_prim(PrimCode p, AnyVal a, AnyVal b);
#define Q16_SCALE 65536
#define AVM_CLAMP_MAX 2147483647
void test_basic_add() {
Instr prog[] = {{OP_PUSH_Q16, 5 * Q16_SCALE, 0},{OP_PUSH_Q16, 3 * Q16_SCALE, 0},{OP_PRIM, PRIM_ADD_Q16, 0},{OP_HALT, 0, 0}};
State s; init_state(&s, 0);
int err = run(&s, prog, 4, 100);
assert(err == 0); assert(s.halted); assert(s.stack[0].val.i == 8 * Q16_SCALE);
printf(" ✅ basic_add: 5 + 3 = 8\n");
}
void test_div_q16() {
Instr prog[] = {{OP_PUSH_Q16, 3 * Q16_SCALE, 0},{OP_PUSH_Q16, 5 * Q16_SCALE, 0},{OP_PRIM, PRIM_DIV_Q16, 0},{OP_HALT, 0, 0}};
State s; init_state(&s, 0);
int err = run(&s, prog, 4, 100);
assert(err == 0); assert(s.stack[0].val.i == (3 * Q16_SCALE) / 5);
printf(" ✅ div_q16: 3/5 = 0.6\n");
}
void test_saturation() {
Instr prog[] = {{OP_PUSH_Q16, AVM_CLAMP_MAX - 1, 0},{OP_PUSH_Q16, 2, 0},{OP_PRIM, PRIM_ADD_Q16, 0},{OP_HALT, 0, 0}};
State s; init_state(&s, 0);
int err = run(&s, prog, 4, 100);
assert(err == 0); assert(s.stack[0].val.i == AVM_CLAMP_MAX);
printf(" ✅ saturation: max-1+2 = max\n");
}
void test_v6_lt() {
struct { int a, b; int exp; } cases[] = {{-5*Q16_SCALE, -3*Q16_SCALE, 1},{-3*Q16_SCALE, -5*Q16_SCALE, 0},{5*Q16_SCALE, 3*Q16_SCALE, 0},{3*Q16_SCALE, 5*Q16_SCALE, 1},{-1*Q16_SCALE, 2*Q16_SCALE, 1}};
for (int i = 0; i < 5; i++) {
Instr prog[] = {{OP_PUSH_Q16, cases[i].a, 0},{OP_PUSH_Q16, cases[i].b, 0},{OP_PRIM, PRIM_LT_Q16, 0},{OP_HALT, 0, 0}};
State s; init_state(&s, 0); run(&s, prog, 4, 100);
assert(s.stack[0].val.b == cases[i].exp);
}
printf(" ✅ v6_lt: 5 cases pass\n");
}
void test_type_mismatch() {
Instr prog[] = {{OP_PUSH_BOOL, 0, 1},{OP_PUSH_Q16, Q16_SCALE, 0},{OP_PRIM, PRIM_ADD_Q16, 0}};
State s; init_state(&s, 0);
int err = run(&s, prog, 3, 100);
assert(err == 0); assert(s.sp == 1 && s.stack[0].ty != TY_Q16);
printf(" ✅ type_mismatch: handled (default value)\n");
}
void test_div_by_zero() {
Instr prog[] = {{OP_PUSH_Q16, Q16_SCALE, 0},{OP_PUSH_Q16, 0, 0},{OP_PRIM, PRIM_DIV_Q16, 0}};
State s; init_state(&s, 0);
int err = run(&s, prog, 3, 100);
assert(err != 0);
printf(" ✅ div_by_zero: rejected\n");
}
void test_stack_overflow() {
Instr prog[1026];
for (int i = 0; i < 1025; i++) prog[i] = (Instr){OP_PUSH_Q16, 0, 0};
prog[1025] = (Instr){OP_HALT, 0, 0};
State s; init_state(&s, 0);
int err = run(&s, prog, 1026, 2000);
assert(err != 0);
printf(" ✅ stack_overflow: rejected\n");
}
void test_control_flow() {
Instr prog[] = {{OP_PUSH_BOOL, 0, 1},{OP_JUMP_IF, 4, 0},{OP_PUSH_Q16, 0, 0},{OP_HALT, 0, 0},{OP_PUSH_Q16, Q16_SCALE, 0},{OP_HALT, 0, 0}};
State s; init_state(&s, 0);
int err = run(&s, prog, 6, 100);
assert(err == 0); assert(s.stack[0].val.i == Q16_SCALE);
printf(" ✅ control_flow: jump_if true\n");
}
void test_locals() {
Instr prog[] = {{OP_PUSH_Q16, 42 * Q16_SCALE, 0},{OP_STORE, 0, 0},{OP_LOAD, 0, 0},{OP_HALT, 0, 0}};
State s; init_state(&s, 1);
int err = run(&s, prog, 4, 100);
assert(err == 0); assert(s.stack[0].val.i == 42 * Q16_SCALE);
printf(" ✅ locals: store+load\n");
}
int main() {
setbuf(stdout, NULL);
printf("AVM C Port — Test Harness\n=========================\n");
test_basic_add(); test_div_q16(); test_saturation(); test_v6_lt();
test_type_mismatch(); test_div_by_zero(); test_stack_overflow();
test_control_flow(); test_locals();
printf("\nAll C tests passed.\n");
return 0;
}