#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"
/root/.cargo/bin/cargo build --release
BIN="$ROOT/target/release/check-ap-bound"
SDP_BIN="$ROOT/target/release/check-sdp-lower"
BOX_BIN="$ROOT/target/release/check-box-sos-lower"
BEAD_BIN="$ROOT/target/release/check-interval-beads"
CONT_BIN="$ROOT/target/release/check-continuum-rlt"

"$BIN" tests/alternating-k3.txt | grep -q '^PASS$'
"$BIN" tests/allzero-k4.txt | grep -q '^PASS$'
if "$BIN" tests/bad-claim.txt > /tmp/check-ap-bound-bad.out 2>&1; then
  echo "negative test unexpectedly passed" >&2
  exit 1
fi
grep -q '^FAIL:' /tmp/check-ap-bound-bad.out
rm -f /tmp/check-ap-bound-bad.out
python3 tests/crosscheck.py

"$SDP_BIN" tests/sdp-l256.txt | grep -q '^PASS: delta_3'
cp tests/sdp-l256.txt /tmp/sdp-lower-bad.txt
python3 - <<'PY'
p='/tmp/sdp-lower-bad.txt'
s=open(p).read().replace('claimed_diag_sum 5445570182', 'claimed_diag_sum 5445570183')
open(p,'w').write(s)
PY
if "$SDP_BIN" /tmp/sdp-lower-bad.txt > /tmp/check-sdp-lower-bad.out 2>&1; then
  echo "negative SDP test unexpectedly passed" >&2
  exit 1
fi
grep -q '^FAIL:' /tmp/check-sdp-lower-bad.out
rm -f /tmp/sdp-lower-bad.txt /tmp/check-sdp-lower-bad.out
python3 tests/crosscheck_sdp.py

"$BOX_BIN" tests/box-sos-l128.txt | grep -q '^PASS: delta_3'
cp tests/box-sos-l128.txt /tmp/box-sos-bad.txt
python3 - <<'PY'
p='/tmp/box-sos-bad.txt'
s=open(p).read().replace('claimed_multiplier_sum 119613313008', 'claimed_multiplier_sum 119613313009')
open(p,'w').write(s)
PY
if "$BOX_BIN" /tmp/box-sos-bad.txt > /tmp/check-box-sos-bad.out 2>&1; then
  echo "negative box-SOS test unexpectedly passed" >&2
  exit 1
fi
grep -q '^FAIL:' /tmp/check-box-sos-bad.out
rm -f /tmp/box-sos-bad.txt /tmp/check-box-sos-bad.out
python3 tests/crosscheck_box_sos.py

"$BEAD_BIN" tests/interval-beads-q548.txt | grep -q '^PASS: delta_3'
python3 tests/crosscheck_interval_beads.py

"$CONT_BIN" tests/continuum-rlt-548.txt | grep -q '^PASS: continuum lower bound'
cp tests/continuum-rlt-548.txt /tmp/continuum-rlt-bad.txt
python3 - <<'PY'
p='/tmp/continuum-rlt-bad.txt'
s=open(p).read().replace('U\n6665619 ', 'U\n1006665619 ')
open(p,'w').write(s)
PY
if "$CONT_BIN" /tmp/continuum-rlt-bad.txt > /tmp/check-continuum-rlt-bad.out 2>&1; then
  echo "negative continuum-RLT test unexpectedly passed" >&2
  exit 1
fi
grep -q '^FAIL:' /tmp/check-continuum-rlt-bad.out
rm -f /tmp/continuum-rlt-bad.txt /tmp/check-continuum-rlt-bad.out
python3 tests/crosscheck_continuum_rlt.py

echo "all checker tests PASS"
