56 lines
1.5 KiB
Bash
Executable File
56 lines
1.5 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Exit immediately if a command exits with a non-zero status.
|
|
set -e
|
|
|
|
# --- Helper Function ---
|
|
run_test() {
|
|
local src_file="$1"
|
|
local expected_code="$2"
|
|
local base_name
|
|
base_name=$(basename "$src_file" .src)
|
|
local obj_file="tests/$base_name.o"
|
|
local exec_file="tests/$base_name"
|
|
|
|
echo "--- Running test: $src_file ---"
|
|
|
|
# 1. Compile the source file using our compiler
|
|
echo " [1/4] Compiling..."
|
|
cargo run --release -- "$src_file" -o "$obj_file" > /dev/null 2>&1
|
|
|
|
# 2. Link the object file with the system linker (gcc)
|
|
echo " [2/4] Linking with gcc..."
|
|
gcc "$obj_file" -o "$exec_file"
|
|
|
|
# 3. Run the executable
|
|
echo " [3/4] Running..."
|
|
set +e
|
|
./"$exec_file"
|
|
local actual_code=$?
|
|
set -e
|
|
|
|
# 4. Check the exit code
|
|
echo " [4/4] Verifying exit code..."
|
|
if [ "$actual_code" -eq "$expected_code" ]; then
|
|
echo "SUCCESS: Exit code is $actual_code as expected."
|
|
else
|
|
echo "FAILURE: Expected exit code $expected_code, but got $actual_code."
|
|
exit 1
|
|
fi
|
|
|
|
# Clean up generated files
|
|
rm "$obj_file" "$exec_file"
|
|
echo "------------------------------------"
|
|
echo
|
|
}
|
|
|
|
# --- Test Cases ---
|
|
|
|
# Test a simple positive return value.
|
|
run_test "tests/return_42.src" 42
|
|
|
|
# Test a negative return value. Shell exit codes are unsigned 8-bit integers,
|
|
# so -69 (i8) wraps around to 187.
|
|
run_test "tests/return_neg_69.src" 187
|
|
|
|
echo "All end-to-end tests passed!" |