philosophers_prep/testers/test_10_philosophers_args.sh

237 lines
7.1 KiB
Bash
Executable File

#!/bin/bash
# Tester for philosophers_args exercise
# Tests complete philosophers implementation with arguments
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
EXERCISE="philosophers_args"
PASSED=0
FAILED=0
echo "========================================"
echo "Testing: $EXERCISE (Complete Philosophers)"
echo "========================================"
if [ ! -f "./$EXERCISE" ]; then
echo -e "${RED}✗ Executable ./$EXERCISE not found${NC}"
echo "Please compile: gcc -Wall -Wextra -Werror -pthread philosophers_args.c -o philosophers_args"
exit 1
fi
# Test 1: Basic execution (no one should die)
echo -n "Test 1: Basic case - 5 800 200 200... "
timeout 10 ./$EXERCISE 5 800 200 200 > /tmp/philo_test1.txt 2>&1
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
OUTPUT=$(cat /tmp/philo_test1.txt)
if ! echo "$OUTPUT" | grep -qiE "die"; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "A philosopher died when they shouldn't have"
((FAILED++))
fi
else
echo -e "${RED}✗ FAILED (timeout or crash)${NC}"
((FAILED++))
fi
# Test 2: Death scenario (should die)
echo -n "Test 2: Death detection - 4 310 200 100... "
timeout 5 ./$EXERCISE 4 310 200 100 > /tmp/philo_test2.txt 2>&1
OUTPUT=$(cat /tmp/philo_test2.txt)
if echo "$OUTPUT" | grep -qiE "die"; then
echo -e "${GREEN}✓ PASSED${NC} (death detected)"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "A philosopher should have died but didn't"
((FAILED++))
fi
# Test 3: Meals limit
echo -n "Test 3: Meal limit - 5 800 200 200 7... "
timeout 15 ./$EXERCISE 5 800 200 200 7 > /tmp/philo_test3.txt 2>&1
EXIT_CODE=$?
OUTPUT=$(cat /tmp/philo_test3.txt)
if [ $EXIT_CODE -eq 0 ]; then
# Check if program terminated (should stop after all ate 7 times)
if echo "$OUTPUT" | grep -qiE "(done|finish|complete|all.*eat)" || [ $(echo "$OUTPUT" | wc -l) -lt 1000 ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${YELLOW}⚠ PARTIAL${NC} (program completed but unclear if meal limit worked)"
((PASSED++))
fi
else
echo -e "${RED}✗ FAILED${NC}"
((FAILED++))
fi
# Test 4: Single philosopher (should die)
echo -n "Test 4: Single philosopher - 1 800 200 200... "
timeout 3 ./$EXERCISE 1 800 200 200 > /tmp/philo_test4.txt 2>&1
OUTPUT=$(cat /tmp/philo_test4.txt)
if echo "$OUTPUT" | grep -qiE "die"; then
echo -e "${GREEN}✓ PASSED${NC} (died as expected)"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Single philosopher should die (no fork available)"
((FAILED++))
fi
# Test 5: Invalid arguments
echo -n "Test 5: Invalid argument handling... "
timeout 2 ./$EXERCISE 0 800 200 200 > /dev/null 2>&1
EXIT1=$?
timeout 2 ./$EXERCISE 5 -100 200 200 > /dev/null 2>&1
EXIT2=$?
timeout 2 ./$EXERCISE abc def ghi > /dev/null 2>&1
EXIT3=$?
if [ $EXIT1 -ne 0 ] && [ $EXIT2 -ne 0 ] && [ $EXIT3 -ne 0 ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Program should reject invalid arguments"
((FAILED++))
fi
# Test 6: No data races
echo -n "Test 6: All actions for correct philosopher... "
OUTPUT=$(cat /tmp/philo_test1.txt)
# Check that philosopher IDs are valid (0 to 4 for 5 philosophers)
INVALID_ID=$(echo "$OUTPUT" | grep -oE "Philosopher [0-9]+" | grep -vE "Philosopher [0-4]" | wc -l)
if [ "$INVALID_ID" -eq 0 ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Found invalid philosopher IDs"
((FAILED++))
fi
# Test 7: Timestamp ordering
echo -n "Test 7: Timestamps are increasing... "
TIMESTAMPS=$(echo "$OUTPUT" | grep -oE "\[[0-9]+\]" | tr -d '[]' | head -20)
DECREASING=false
PREV=0
for TS in $TIMESTAMPS; do
if [ "$TS" -lt "$PREV" ]; then
DECREASING=true
break
fi
PREV=$TS
done
if [ "$DECREASING" = false ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Timestamps should be monotonically increasing"
((FAILED++))
fi
# Test 8: Death detection timing
echo -n "Test 8: Death detection is timely... "
timeout 5 ./$EXERCISE 4 410 200 200 > /tmp/philo_test8.txt 2>&1
OUTPUT=$(cat /tmp/philo_test8.txt)
DEATH_LINE=$(echo "$OUTPUT" | grep -iE "die" | head -1)
if [ -n "$DEATH_LINE" ]; then
DEATH_TIME=$(echo "$DEATH_LINE" | grep -oE "\[[0-9]+\]" | tr -d '[]')
if [ "$DEATH_TIME" -le 430 ]; then
echo -e "${GREEN}✓ PASSED${NC} (died at ${DEATH_TIME}ms)"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Death detected too late: ${DEATH_TIME}ms (should be ~410ms)"
((FAILED++))
fi
else
echo -e "${YELLOW}⚠ UNCLEAR${NC} (no death detected)"
((PASSED++))
fi
# Test 9: Thread sanitizer
if command -v gcc &> /dev/null; then
echo -n "Test 9: Data race detection... "
gcc -Wall -Wextra -Werror -pthread -fsanitize=thread -g philosophers_args.c -o philosophers_args_tsan 2>/dev/null
if [ $? -eq 0 ]; then
TSAN_OUTPUT=$(timeout 8 ./philosophers_args_tsan 5 800 200 200 5 2>&1)
if echo "$TSAN_OUTPUT" | grep -q "WARNING: ThreadSanitizer: data race"; then
echo -e "${RED}✗ FAILED${NC}"
echo "Data race detected!"
((FAILED++))
else
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
fi
rm -f philosophers_args_tsan
else
echo -e "${YELLOW}⊘ Could not compile with thread sanitizer${NC}"
fi
else
echo -e "${YELLOW}⊘ Test 9: gcc not available, skipping${NC}"
fi
# Test 10: Stress test - odd number
echo -n "Test 10: Odd philosophers (3) - 3 410 200 200... "
timeout 8 ./$EXERCISE 3 410 200 200 > /tmp/philo_test10.txt 2>&1
OUTPUT=$(cat /tmp/philo_test10.txt)
if ! echo "$OUTPUT" | grep -qiE "die"; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Philosophers died when they shouldn't (check timing logic)"
((FAILED++))
fi
# Test 11: Memory leak check
if command -v valgrind &> /dev/null; then
echo -n "Test 11: Memory leak check... "
VALGRIND_OUTPUT=$(timeout 10 valgrind --leak-check=full --error-exitcode=42 ./$EXERCISE 5 800 200 200 3 2>&1)
VALGRIND_EXIT=$?
if [ $VALGRIND_EXIT -eq 0 ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
elif [ $VALGRIND_EXIT -eq 124 ]; then
echo -e "${YELLOW}⚠ TIMEOUT${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Memory leaks detected!"
((FAILED++))
fi
else
echo -e "${YELLOW}⊘ Test 11: Valgrind not installed, skipping${NC}"
fi
# Cleanup
rm -f /tmp/philo_test*.txt
# Summary
echo "========================================"
echo -e "Results: ${GREEN}$PASSED passed${NC}, ${RED}$FAILED failed${NC}"
echo "========================================"
echo ""
echo "Additional tests you should run manually:"
echo " - ./philosophers_args 2 410 200 200 (edge case)"
echo " - ./philosophers_args 4 310 200 100 (tight timing)"
echo " - helgrind: valgrind --tool=helgrind ./$EXERCISE 5 800 200 200"
[ $FAILED -eq 0 ] && exit 0 || exit 1