philosophers_prep/testers/test_6_deadlock_demo.sh

163 lines
4.9 KiB
Bash
Executable File

#!/bin/bash
# Tester for deadlock_demo exercise
# Tests deadlock demonstration and solution
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
EXERCISE="deadlock_demo"
EXE_PATH=""
PASSED=0
FAILED=0
# Determine the project root directory, which is one level up from the 'testers' directory.
PROJECT_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
echo "========================================"
echo "Testing: $EXERCISE"
echo "========================================"
# Find the executable in the project root or the 'rendu' subdirectory.
if [ -f "$PROJECT_ROOT/$EXERCISE" ]; then
EXE_PATH="$PROJECT_ROOT/$EXERCISE"
elif [ -f "$PROJECT_ROOT/rendu/$EXERCISE" ]; then
EXE_PATH="$PROJECT_ROOT/rendu/$EXERCISE"
else
echo -e "${RED}✗ Executable '$EXERCISE' not found in '$PROJECT_ROOT' or '$PROJECT_ROOT/rendu'${NC}"
echo "Please compile your program first. Example:"
echo "gcc -Wall -Wextra -Werror -pthread deadlock_demo.c -o $EXERCISE"
exit 1
fi
echo -e "${YELLOW}Found executable at: $EXE_PATH${NC}"
# Test 1: Deadlock mode (should hang or timeout)
echo -n "Test 1: Deadlock demonstration (mode 0)... "
timeout 3 "$EXE_PATH" 0 > /tmp/deadlock_output.txt 2>&1
EXIT_CODE=$?
if [ $EXIT_CODE -eq 124 ]; then
# Timeout occurred - this is expected for deadlock
echo -e "${GREEN}✓ PASSED${NC} (program deadlocked as expected)"
((PASSED++))
else
OUTPUT=$(cat /tmp/deadlock_output.txt 2>/dev/null)
if echo "$OUTPUT" | grep -qi "deadlock"; then
echo -e "${GREEN}✓ PASSED${NC} (deadlock detected/explained)"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Program should demonstrate deadlock (hang) or explain it"
echo "Exit code: $EXIT_CODE"
((FAILED++))
fi
fi
# Test 2: Solution mode (should complete)
echo -n "Test 2: Deadlock solution (mode 1)... "
timeout 5 "$EXE_PATH" 1 > /tmp/deadlock_solution.txt 2>&1
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
echo -e "${GREEN}✓ PASSED${NC} (completed successfully)"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Solution mode should complete without hanging"
echo "Exit code: $EXIT_CODE"
((FAILED++))
fi
# Test 3: Check solution output
echo -n "Test 3: Solution produces output... "
OUTPUT=$(cat /tmp/deadlock_solution.txt 2>/dev/null)
if [ -n "$OUTPUT" ] && [ $(echo "$OUTPUT" | wc -l) -gt 2 ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Expected meaningful output from solution mode"
((FAILED++))
fi
# Test 4: Verify both threads complete in solution mode
echo -n "Test 4: Both threads complete (solution)... "
if echo "$OUTPUT" | grep -qiE "(thread 1|t1)" && \
echo "$OUTPUT" | grep -qiE "(thread 2|t2)"; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${YELLOW}⚠ WARNING${NC}"
echo "Could not clearly identify both threads in output"
echo "Output: $OUTPUT"
((PASSED++))
fi
# Test 5: Multiple runs of solution (should always work)
echo -n "Test 5: Solution consistency (5 runs)... "
ALL_SUCCESS=true
for i in {1..5}; do
timeout 5 "$EXE_PATH" 1 > /dev/null 2>&1
if [ $? -ne 0 ]; then
ALL_SUCCESS=false
break
fi
done
if [ "$ALL_SUCCESS" = true ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Solution mode failed on iteration $i"
((FAILED++))
fi
# Test 6: Check for memory leaks with valgrind
if command -v valgrind &> /dev/null; then
echo -n "Test 6: Memory leak check (solution mode)... "
VALGRIND_OUTPUT=$(valgrind --leak-check=full --error-exitcode=42 "$EXE_PATH" 1 2>&1)
if [ $? -ne 42 ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Memory leaks detected!"
echo "$VALGRIND_OUTPUT" | grep -A 5 "LEAK SUMMARY"
((FAILED++))
fi
else
echo -e "${YELLOW}⊘ Test 6: Valgrind not installed, skipping memory test${NC}"
fi
# Test 7: Helgrind deadlock detection
if command -v valgrind &> /dev/null; then
echo -n "Test 7: Helgrind deadlock detection (mode 0)... "
HELGRIND_OUTPUT=$(valgrind --tool=helgrind "$EXE_PATH" 0 2>&1)
if echo "$HELGRIND_OUTPUT" | grep -q "deadlock"; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${YELLOW}⚠ WARNING${NC} - Helgrind did not report a deadlock"
((PASSED++))
fi
else
echo -e "${YELLOW}⊘ Test 7: Valgrind not installed, skipping Helgrind test${NC}"
fi
# Cleanup
rm -f /tmp/deadlock_output.txt /tmp/deadlock_solution.txt
# Final summary
echo "----------------------------------------"
if [ $FAILED -eq 0 ]; then
echo -e "${GREEN}Summary: All $PASSED tests passed for $EXERCISE!${NC}"
exit 0
else
echo -e "${RED}Summary: $FAILED out of $((PASSED + FAILED)) tests failed for $EXERCISE.${NC}"
exit 1
fi