#!/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" 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 (Complete Philosophers)" 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 philosophers_args.c -o $EXERCISE" exit 1 fi echo -e "${YELLOW}Found executable at: $EXE_PATH${NC}" # Test 1: Basic execution (no one should die) echo -n "Test 1: Basic case - 5 800 200 200... " timeout 10 "$EXE_PATH" 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 "$EXE_PATH" 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 "$EXE_PATH" 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 "$EXE_PATH" 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 "$EXE_PATH" 0 800 200 200 > /dev/null 2>&1 EXIT1=$? timeout 2 "$EXE_PATH" 5 -100 200 200 > /dev/null 2>&1 EXIT2=$? timeout 2 "$EXE_PATH" 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: Check for memory leaks with valgrind if command -v valgrind &> /dev/null; then echo -n "Test 6: Memory leak check... " VALGRIND_OUTPUT=$(valgrind --leak-check=full --error-exitcode=42 "$EXE_PATH" 4 410 200 200 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: Thread sanitizer check if command -v gcc &> /dev/null; then echo -n "Test 7: Data race detection... " # Find the source file to compile SOURCE_FILE="" if [ -f "$PROJECT_ROOT/rendu/${EXERCISE}.c" ]; then SOURCE_FILE="$PROJECT_ROOT/rendu/${EXERCISE}.c" elif [ -f "$PROJECT_ROOT/${EXERCISE}.c" ]; then SOURCE_FILE="$PROJECT_ROOT/${EXERCISE}.c" fi if [ -n "$SOURCE_FILE" ]; then TSAN_EXE="$PROJECT_ROOT/$(basename "$EXE_PATH")_tsan" gcc -Wall -Wextra -Werror -pthread -fsanitize=thread -g "$SOURCE_FILE" -o "$TSAN_EXE" 2>/dev/null if [ $? -eq 0 ]; then TSAN_OUTPUT=$(timeout 15 "$TSAN_EXE" 4 410 200 200 2>&1) if echo "$TSAN_OUTPUT" | grep -q "WARNING: ThreadSanitizer: data race"; then echo -e "${RED}✗ FAILED${NC}" echo "Data race detected!" echo "$TSAN_OUTPUT" | head -20 ((FAILED++)) else echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) fi rm -f "$TSAN_EXE" else echo -e "${YELLOW}⊘ Could not compile with ThreadSanitizer, skipping test${NC}" fi else echo -e "${YELLOW}⊘ Source file ${EXERCISE}.c not found, skipping ThreadSanitizer test${NC}" fi else echo -e "${YELLOW}⊘ Test 7: gcc not found, skipping data race test${NC}" fi # Cleanup rm -f /tmp/philo_test*.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