#!/bin/bash # Tester for philosophers_bonus exercise # Tests process-based philosophers with semaphores RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' EXERCISE="philosophers_bonus" 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 (Bonus)" 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_bonus.c -o $EXERCISE" exit 1 fi echo -e "${YELLOW}Found executable at: $EXE_PATH${NC}" # Clean up any leftover semaphores from previous runs echo "Cleaning up old semaphores..." # Use a loop to be thorough, as semaphore names can vary for i in {0..255}; do sem_unlink "/philo_forks" &>/dev/null sem_unlink "/philo_print" &>/dev/null sem_unlink "/philo_main" &>/dev/null sem_unlink "/philo_sem_$i" &>/dev/null done # Common names from other implementations sem_unlink "/forks" &>/dev/null sem_unlink "/print" &>/dev/null sem_unlink "forks" &>/dev/null sem_unlink "print" &>/dev/null # Test 1: Basic execution (no death) echo -n "Test 1: Basic case - 5 800 200 200... " timeout 12 "$EXE_PATH" 5 800 200 200 > /tmp/bonus_test1.txt 2>&1 EXIT_CODE=$? if [ $EXIT_CODE -eq 0 ]; then OUTPUT=$(cat /tmp/bonus_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: Check for process creation (not threads) echo -n "Test 2: Uses processes (not threads)... " 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" ] && grep -q "fork()" "$SOURCE_FILE" 2>/dev/null; then echo -e "${GREEN}✓ PASSED${NC} (fork() found in source)" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Bonus should use fork() for processes. Source file not found or fork() missing." ((FAILED++)) fi # Test 3: Check for semaphore usage echo -n "Test 3: Uses semaphores... " if [ -n "$SOURCE_FILE" ] && grep -qE "sem_(open|wait|post)" "$SOURCE_FILE" 2>/dev/null; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Bonus should use POSIX semaphores. Source file not found or sem_ calls missing." ((FAILED++)) fi # Test 4: Death scenario echo -n "Test 4: Death detection - 4 310 200 100... " timeout 5 "$EXE_PATH" 4 310 200 100 > /tmp/bonus_test4.txt 2>&1 OUTPUT=$(cat /tmp/bonus_test4.txt) if echo "$OUTPUT" | grep -qiE "die"; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Expected a philosopher to die" ((FAILED++)) fi # Test 5: Meal limit functionality echo -n "Test 5: Meal limit - 5 800 200 200 7... " timeout 20 "$EXE_PATH" 5 800 200 200 7 > /tmp/bonus_test5.txt 2>&1 EXIT_CODE=$? if [ $EXIT_CODE -eq 0 ]; then OUTPUT=$(cat /tmp/bonus_test5.txt) if echo "$OUTPUT" | grep -qiE "meal limit"; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Meal limit not reached or reported" ((FAILED++)) fi else echo -e "${RED}✗ FAILED${NC}" echo "Program failed or timed out with meal limit" ((FAILED++)) fi # Test 6: Single philosopher (should die) echo -n "Test 6: Single philosopher - 1 800 200 200... " timeout 3 "$EXE_PATH" 1 800 200 200 > /tmp/bonus_test6.txt 2>&1 OUTPUT=$(cat /tmp/bonus_test6.txt) if echo "$OUTPUT" | grep -qiE "die"; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Single philosopher should die" ((FAILED++)) fi # Test 7: Invalid arguments echo -n "Test 7: 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=$? if [ $EXIT1 -ne 0 ] && [ $EXIT2 -ne 0 ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Program should exit with error on invalid arguments" ((FAILED++)) fi # Test 8: Check for memory leaks with valgrind if command -v valgrind &> /dev/null; then echo -n "Test 8: Memory leak check... " # Valgrind does not work well with fork() and semaphores in this context # It often reports false positives or hangs. echo -e "${YELLOW}⊘ SKIPPED (Valgrind not suitable for process/semaphore bonus)${NC}" else echo -e "${YELLOW}⊘ Test 8: Valgrind not installed, skipping memory test${NC}" fi # Cleanup rm -f /tmp/bonus_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