#!/bin/sh
set -eu
cd "$(dirname "$0")"
T=firmware/固件/lamp_target
C=firmware/固件/lamp_control
F="-std=c11 -g -O1 -Wall -Wextra -Werror -fsanitize=address,undefined -fno-omit-frame-pointer"
I="-Ihost -I$T/include -I$C"
cc $F $I test_clock.c "$T/src/lamp_i2c_owner.c" "$T/src/lamp_i2c_clock.c" "$T/src/lamp_i2c_read.c" -o host/clock
./host/clock
cc $F $I test_read.c "$T/src/lamp_i2c_owner.c" "$T/src/lamp_i2c_clock.c" "$T/src/lamp_i2c_read.c" -o host/read
./host/read
cc $F $I test_integrated.c "$T/src/lamp_i2c_owner.c" "$T/src/lamp_i2c_clock.c" "$T/src/lamp_i2c_read.c" "$T/src/lamp_driver_diagnostics.c" "$C/pca9956b_async_bank.c" "$C/pca9956b_fault_monitor.c" -o host/integrated
./host/integrated
for n in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23; do ./host/integrated "$n"; done
./host/integrated -1 slow
cc $F $I test_runtime.c "$T/src/lamp_i2c_owner.c" "$T/src/lamp_i2c_clock.c" "$T/src/lamp_i2c_read.c" "$T/src/lamp_driver_diagnostics.c" "$T/src/lamp_diagnostic_runtime.c" "$T/src/lamp_dark_init.c" "$T/src/lamp_i2c_write.c" "$C/pca9956b_async_bank.c" "$C/pca9956b_fault_monitor.c" -o host/runtime
./host/runtime
for phase in 100 200 300; do
 n=0; while [ "$n" -lt 30 ]; do ./host/runtime "$((phase+n))"; n=$((n+1)); done
done
for n in 400 401 402 403 404; do ./host/runtime "$n"; done
n=500; while [ "$n" -lt 524 ]; do ./host/runtime "$n"; n=$((n+1)); done
printf '%s\n' 'PASS 120 diagnostic runtime scenarios: two scans, 90 GPIO/config/alias faults across three phases, four scheduling/ownership faults, tick wrap, 24 NACK positions.'
cc $F $I test_write.c "$T/src/lamp_i2c_owner.c" "$T/src/lamp_i2c_clock.c" "$T/src/lamp_i2c_read.c" "$T/src/lamp_i2c_write.c" -o host/write
./host/write
cc $F $I test_dark.c "$T/src/lamp_i2c_owner.c" "$T/src/lamp_i2c_clock.c" "$T/src/lamp_i2c_read.c" "$T/src/lamp_i2c_write.c" "$T/src/lamp_dark_init.c" -o host/dark
./host/dark
./host/dark 1
for scenario in 2 3 4; do
 n=0; while [ "$n" -lt 174 ]; do ./host/dark "$scenario" "$n"; n=$((n+1)); done
done
./host/dark 5
./host/dark 6
./host/dark 7
./host/dark 8
printf '%s\n' 'PASS 528 dark initialization scenarios: normal/wrap, 174 write NACKs, 174 read NACKs, 174 mismatches, poll gap, MODE2 fixed bits, terminal abort, total deadline.'
cc $F $I test_frame.c "$T/src/lamp_i2c_owner.c" "$T/src/lamp_i2c_clock.c" "$T/src/lamp_i2c_read.c" "$T/src/lamp_i2c_write.c" "$T/src/lamp_frame_stage.c" -o host/frame
./host/frame
./host/frame 1
for scenario in 2 3 4; do
 n=0; while [ "$n" -lt 174 ]; do ./host/frame "$scenario" "$n"; n=$((n+1)); done
done
n=0; while [ "$n" -lt 20 ]; do ./host/frame 5 "$n"; n=$((n+1)); done
n=0; while [ "$n" -lt 25 ]; do ./host/frame 6 "$n"; n=$((n+1)); done
./host/frame 7
./host/frame 8
./host/frame 9
./host/frame 10
./host/frame 11
./host/frame 12
printf '%s\n' 'PASS 575 frame stage scenarios: normal/wrap, 174 write NACKs, 174 read NACKs, 174 mismatches, 20 initial authorization rejections, 25 mid-transaction revocations, terminal latch, nonzero PWM with zero IREF rejected, stale guard, total deadline, poll gap, clock loss.'
