a gleam implementation of a CS assignment originally written in cpp

feat: add a timeout to the tests

Changed files
+38 -19
test
+38 -19
test/test.sh
···
#!/bin/bash
+
# Test script with configurable timeout support
+
# Usage: ./test.sh [timeout_seconds]
+
# Example: ./test.sh 5 (uses 5 second timeout)
+
# Default: 1 second timeout
+
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
···
# Test configuration
temp_file="lab66.output"
program="../build/lab66"
+
test_timeout=${1:-1}
# Test counters
total_tests=0
···
local test_name="$1"
local result="$2"
local message="$3"
-
+
total_tests=$((total_tests + 1))
-
+
if [ "$result" = "PASS" ]; then
echo -e "${GREEN}[PASS]${NC} $test_name: $message"
passed_tests=$((passed_tests + 1))
+
elif [ "$result" = "TIMEOUT" ]; then
+
echo -e "${YELLOW}[TIMEOUT]${NC} $test_name: $message"
else
echo -e "${RED}[FAIL]${NC} $test_name: $message"
fi
···
# Test with known valid verse
basic_input="Genesis\n1\n1"
-
resp=$(echo -e "$basic_input" | "$program" 2>&1)
+
resp=$(timeout "$test_timeout" bash -c "echo -e '$basic_input' | '$program'" 2>&1)
exit_code=$?
# Print the output
···
fi
echo
-
if [ $exit_code -ne 0 ]; then
+
if [ $exit_code -eq 124 ]; then
+
print_result "Basic Execution" "TIMEOUT" "Program timed out after ${test_timeout}s"
+
exit 1
+
elif [ $exit_code -ne 0 ]; then
print_result "Basic Execution" "FAIL" "Program exited with code $exit_code"
exit 1
else
···
local verse="$4"
local expected_pattern="$5"
local should_create_file="$6"
-
+
# Clean up previous test
rm -f verses.txt
-
+
input="$book\n$chapter\n$verse"
-
output=$(echo -e "$input" | "$program" 2>&1)
+
output=$(timeout "$test_timeout" bash -c "echo -e '$input' | '$program'" 2>&1)
exit_code=$?
-
-
if [ $exit_code -ne 0 ]; then
+
+
if [ $exit_code -eq 124 ]; then
+
print_result "$test_name" "TIMEOUT" "Program timed out after ${test_timeout}s"
+
return
+
elif [ $exit_code -ne 0 ]; then
print_result "$test_name" "FAIL" "Program crashed (exit code $exit_code)"
return
fi
-
+
# Check if expected pattern is found in output
pattern_found=false
if [[ "$output" == *"$expected_pattern"* ]]; then
pattern_found=true
fi
-
+
# For tests expecting specific output, pattern must be found
if [ -n "$expected_pattern" ] && [ "$pattern_found" = "false" ]; then
print_result "$test_name" "FAIL" "Expected '$expected_pattern' not found in output"
return
fi
-
+
# Check file creation expectations
if [ "$should_create_file" = "true" ]; then
if [ -f "verses.txt" ] && [ "$pattern_found" = "true" ]; then
···
local file_path="$2"
local expected_pattern="$3"
local should_succeed="$4"
-
+
# Clean up previous test
rm -f verses.txt
-
+
# Provide verse lookup input for file path tests
input="Genesis\n1\n1"
-
+
if [ -n "$file_path" ]; then
-
output=$(echo -e "$input" | "$program" "$file_path" 2>&1)
+
output=$(timeout "$test_timeout" bash -c "echo -e '$input' | '$program' '$file_path'" 2>&1)
else
-
output=$(echo -e "$input" | "$program" 2>&1)
+
output=$(timeout "$test_timeout" bash -c "echo -e '$input' | '$program'" 2>&1)
fi
exit_code=$?
-
+
+
if [ $exit_code -eq 124 ]; then
+
print_result "$test_name" "TIMEOUT" "Program timed out after ${test_timeout}s"
+
return
+
fi
+
if [ "$should_succeed" = "true" ]; then
if [ $exit_code -eq 0 ]; then
print_result "$test_name" "PASS" "Program accepted file path successfully"
···
else
echo -e "${RED}Some tests failed!${NC}"
exit 1
-
fi
+
fi