From 294777178f9203283364f3e672569becb4dd1e7c Mon Sep 17 00:00:00 2001 From: hachem Date: Sun, 30 Aug 2026 21:21:53 +0200 Subject: test: add automated docker testing for the whole test suite --- scripts/docker_test.sh | 18 ++++++++++++ scripts/test_examples.sh | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100755 scripts/docker_test.sh create mode 100755 scripts/test_examples.sh (limited to 'scripts') diff --git a/scripts/docker_test.sh b/scripts/docker_test.sh new file mode 100755 index 0000000..93f0bca --- /dev/null +++ b/scripts/docker_test.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +# Convenience wrapper: build hdass and run the full test suite (unit tests plus +# the end-to-end example tests) inside the amd64 Linux Docker environment. +# Run this from the host; it brings the container up if it is not already. +set -euo pipefail + +root="$(cd "$(dirname "$0")/.." && pwd)" +cd "$root" + +docker compose up -d + +docker compose exec -T hdass bash -c ' + set -e + premake5 gmake + make config=debug + ./bin/debug-linux/tests + ./scripts/test_examples.sh +' diff --git a/scripts/test_examples.sh b/scripts/test_examples.sh new file mode 100755 index 0000000..2fb4e5f --- /dev/null +++ b/scripts/test_examples.sh @@ -0,0 +1,76 @@ +#!/usr/bin/env bash +# End-to-end example tests: transpile each program with hdass, assemble it with +# nasm, link it with ld, run it, and compare its stdout and exit status against +# the expected values below. +# +# The generated programs use Linux x86-64 syscalls, so this must run in the +# amd64 Linux environment (see the Docker setup in the README), not on macOS. +set -u + +root="$(cd "$(dirname "$0")/.." && pwd)" +cd "$root" + +hdass="${HDASS:-./bin/debug-linux/hdass}" +if [ ! -x "$hdass" ]; then + echo "error: '$hdass' not found; build it first with:" >&2 + echo " premake5 gmake && make config=debug" >&2 + exit 1 +fi + +for tool in nasm ld; do + if ! command -v "$tool" >/dev/null 2>&1; then + echo "error: '$tool' not found; run this inside the Docker environment" >&2 + exit 1 + fi +done + +work="$(mktemp -d)" +trap 'rm -rf "$work"' EXIT + +pass=0 +fail=0 + +# check +# expected_stdout is compared after trailing newlines are stripped (as $() does). +check() +{ + local name="$1" source="$2" expected_exit="$3" expected_stdout="$4" + local asm="$work/$name.asm" obj="$work/$name.o" bin="$work/$name" + + if ! "$hdass" "$source" -o "$asm" 2>"$work/err"; then + echo "FAIL $name (transpile)"; cat "$work/err"; fail=$((fail + 1)); return + fi + if ! nasm -f elf64 "$asm" -o "$obj" 2>"$work/err"; then + echo "FAIL $name (assemble)"; cat "$work/err"; fail=$((fail + 1)); return + fi + if ! ld "$obj" -o "$bin" 2>"$work/err"; then + echo "FAIL $name (link)"; cat "$work/err"; fail=$((fail + 1)); return + fi + + local actual_stdout actual_exit + actual_stdout="$("$bin")" + actual_exit=$? + + if [ "$actual_exit" != "$expected_exit" ] || [ "$actual_stdout" != "$expected_stdout" ]; then + echo "FAIL $name" + echo " expected: exit=$expected_exit stdout=$(printf '%q' "$expected_stdout")" + echo " actual: exit=$actual_exit stdout=$(printf '%q' "$actual_stdout")" + fail=$((fail + 1)) + return + fi + + echo "PASS $name" + pass=$((pass + 1)) +} + +check hello_world examples/hello_world.hdass 0 "Hello, World!" +check greet examples/greet.hdass 0 "hdass works!" +check exit_code examples/exit_code.hdass 42 "" +check arithmetic examples/arithmetic.hdass 15 "" +check loop_sum examples/loop_sum.hdass 15 "" +check branch examples/branch.hdass 8 "" +check call examples/call.hdass 21 "" + +echo +echo "$pass passed, $fail failed" +[ "$fail" -eq 0 ] -- cgit v1.3