#!/bin/bash # Tester for simple_philosophers exercise # Tests basic philosophers implementation RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' EXERCISE="simple_philosophers" 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 or compile the executable --- # Priority: # 1. rendu/executable # 2. root/executable # 3. rendu/source -> compile # 4. root/source -> compile # Define potential paths for executable and source EXE_IN_RENDU="$PROJECT_ROOT/rendu/$EXERCISE" EXE_IN_ROOT="$PROJECT_ROOT/$EXERCISE" SRC_IN_RENDU="$PROJECT_ROOT/rendu/${EXERCISE}.c" SRC_IN_ROOT="$PROJECT_ROOT/${EXERCISE}.c" # Check for existing executable if [ -f "$EXE_IN_RENDU" ]; then EXE_PATH="$EXE_IN_RENDU" echo -e "${YELLOW}Found executable in 'rendu' directory.${NC}" elif [ -f "$EXE_IN_ROOT" ]; then EXE_PATH="$EXE_IN_ROOT" echo -e "${YELLOW}Found executable in root directory.${NC}" else echo -e "${YELLOW}Executable not found. Attempting to compile from source...${NC}" COMPILE_CMD="gcc -Wall -Wextra -Werror -pthread" # Check for source file and compile if [ -f "$SRC_IN_RENDU" ]; then echo "Found source file in 'rendu'. Compiling..." if $COMPILE_CMD "$SRC_IN_RENDU" -o "$EXE_IN_RENDU"; then EXE_PATH="$EXE_IN_RENDU" echo -e "${GREEN}Compilation successful.${NC}" else echo -e "${RED}✗ Compilation failed for '$SRC_IN_RENDU'.${NC}" exit 1 fi elif [ -f "$SRC_IN_ROOT" ]; then echo "Found source file in root. Compiling..." if $COMPILE_CMD "$SRC_IN_ROOT" -o "$EXE_IN_ROOT"; then EXE_PATH="$EXE_IN_ROOT" echo -e "${GREEN}Compilation successful.${NC}" else echo -e "${RED}✗ Compilation failed for '$SRC_IN_ROOT'.${NC}" exit 1 fi else echo -e "${RED}✗ Neither executable nor source file found for '$EXERCISE'.${NC}" echo "Looked for:" echo " - $EXE_IN_RENDU" echo " - $EXE_IN_ROOT" echo " - $SRC_IN_RENDU" echo " - $SRC_IN_ROOT" exit 1 fi fi echo -e "${YELLOW}Using executable at: $EXE_PATH${NC}" # Test 1: Program runs for approximately 10 seconds echo -n "Test 1: Program duration (~10 seconds)... " START_TIME=$(date +%s) timeout 15 "$EXE_PATH" > /tmp/simple_philo_output.txt 2>&1 EXIT_CODE=$? END_TIME=$(date +%s) DURATION=$((END_TIME - START_TIME)) if [ $EXIT_CODE -eq 0 ] && [ $DURATION -ge 9 ] && [ $DURATION -le 12 ]; then echo -e "${GREEN}✓ PASSED${NC} (ran for ${DURATION}s)" ((PASSED++)) elif [ $EXIT_CODE -eq 0 ]; then echo -e "${YELLOW}⚠ PARTIAL${NC} (ran for ${DURATION}s, expected ~10s)" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Program should run for approximately 10 seconds" ((FAILED++)) fi OUTPUT=$(cat /tmp/simple_philo_output.txt 2>/dev/null) # Test 2: Three philosophers present echo -n "Test 2: Three philosophers... " PHILO_COUNT=$(echo "$OUTPUT" | grep -oE "Philosopher [0-9]+" | sort -u | wc -l) if [ "$PHILO_COUNT" -eq 3 ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Expected 3 philosophers, found: $PHILO_COUNT" ((FAILED++)) fi # Test 3: Thinking action present echo -n "Test 3: Thinking actions... " THINKING_COUNT=$(echo "$OUTPUT" | grep -ci "thinking") if [ "$THINKING_COUNT" -gt 0 ]; then echo -e "${GREEN}✓ PASSED${NC} ($THINKING_COUNT occurrences)" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "No thinking actions detected" ((FAILED++)) fi # Test 4: Eating action present echo -n "Test 4: Eating actions... " EATING_COUNT=$(echo "$OUTPUT" | grep -ci "eating") if [ "$EATING_COUNT" -gt 0 ]; then echo -e "${GREEN}✓ PASSED${NC} ($EATING_COUNT occurrences)" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "No eating actions detected" ((FAILED++)) fi # Test 5: Timestamps present echo -n "Test 5: Timestamps... " if echo "$OUTPUT" | grep -qE "\[[0-9]+\]"; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Expected timestamps in format [timestamp]" ((FAILED++)) fi # Test 6: Each philosopher eats multiple times echo -n "Test 6: All philosophers eat... " # Extract philosopher IDs from eating lines. Support both formats like: # "[123] Philosopher 1 is eating." and any line containing "eating" followed by a number. # Extract philosopher IDs from eating lines by capturing the number after 'Philosopher' # Supports lines like: [123] Philosopher 1 is eating. EAT_IDS=$(echo "$OUTPUT" | grep -i "eating" | sed -n 's/.*Philosopher \([0-9]\+\).*/\1/p') # Fallback: some logs may format differently; take the 3rd whitespace field which is the id in # the format "[ts] Philosopher is " if [ -z "$EAT_IDS" ]; then EAT_IDS=$(echo "$OUTPUT" | grep -i "eating" | awk '{print $3}' | sed 's/[^0-9]*//g') fi # Normalize IDs to unique sorted list UNIQUE_EATERS=$(echo "$EAT_IDS" | sort -n | uniq) # Determine expected philosopher ids from output (Philosopher X occurrences) FOUND_PHILOS=$(echo "$OUTPUT" | grep -oE "Philosopher [0-9]+" | sed 's/Philosopher //g' | sort -n | uniq) ALL_ATE=true MISSING_LIST="" for ph in $FOUND_PHILOS; do if ! echo "$UNIQUE_EATERS" | grep -qx "$ph"; then ALL_ATE=false MISSING_LIST="$MISSING_LIST $ph" fi done if [ "$ALL_ATE" = true ]; then # Build counts per philosopher for reporting REPORT="" for ph in $FOUND_PHILOS; do CNT=$(echo "$EAT_IDS" | grep -cx "$ph" || true) REPORT="$REPORT P$ph:$CNT" done echo -e "${GREEN}✓ PASSED${NC} ($REPORT)" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Not all philosophers ate. Missing ids:$MISSING_LIST" ((FAILED++)) fi # Test 7: No philosopher should eat twice in a row echo -n "Test 7: No philosopher eats twice in a row... " NO_CONSECUTIVE_EATING=true LAST_EATER=-1 # Extract just the philosopher number from lines containing "eating" EATING_ORDER=$(echo "$OUTPUT" | grep "eating" | grep -oE "[0-9]+" | head -n 20) for philo in $EATING_ORDER; do if [ "$philo" -eq "$LAST_EATER" ]; then NO_CONSECUTIVE_EATING=false break fi LAST_EATER=$philo done if [ "$NO_CONSECUTIVE_EATING" = true ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "A philosopher ate twice in a row. Order: $EATING_ORDER" ((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_OUTPUT=$(valgrind --leak-check=full --error-exitcode=42 "$EXE_PATH" 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 8: Valgrind not installed, skipping memory test${NC}" fi # Cleanup rm -f /tmp/simple_philo_output.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