#!/bin/bash # Tester for state_monitor exercise # Tests state monitoring and thread synchronization RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' EXERCISE="state_monitor" 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 reasonable time echo -n "Test 1: Program execution... " timeout 15 "$EXE_PATH" > /tmp/state_monitor_output.txt 2>&1 & PID=$! sleep 8 kill $PID 2>/dev/null wait $PID 2>/dev/null if [ -f /tmp/state_monitor_output.txt ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" ((FAILED++)) fi # Test 2: Check for state transitions echo -n "Test 2: State transitions present... " OUTPUT=$(cat /tmp/state_monitor_output.txt 2>/dev/null) if echo "$OUTPUT" | grep -qi "WORKING" && \ echo "$OUTPUT" | grep -qi "RESTING" && \ echo "$OUTPUT" | grep -qi "THINKING"; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Expected to see WORKING, RESTING, and THINKING states" ((FAILED++)) fi # Test 3: Check for timestamps echo -n "Test 3: Timestamps present... " 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 4: Check for worker identification echo -n "Test 4: Worker identification... " WORKER_COUNT=$(echo "$OUTPUT" | grep -oE "Worker [0-9]+" | sort -u | wc -l) if [ "$WORKER_COUNT" -ge 3 ]; then echo -e "${GREEN}✓ PASSED${NC} (found $WORKER_COUNT workers)" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Expected at least 3 workers, found: $WORKER_COUNT" ((FAILED++)) fi # Test 5: Check state duration logic echo -n "Test 5: State duration verification... " # WORKING should last ~2s, RESTING ~1s, THINKING ~1.5s # Extract timestamps and check intervals WORKING_LINES=$(echo "$OUTPUT" | grep -i "WORKING" | head -10) if [ -n "$WORKING_LINES" ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${YELLOW}⚠ WARNING${NC} - Could not verify state durations" ((PASSED++)) fi # Test 6: Monitor thread functionality echo -n "Test 6: Monitor warnings (if applicable)... " if echo "$OUTPUT" | grep -qi "WARNING"; then echo -e "${GREEN}✓ PASSED${NC} (monitor detected stuck worker)" ((PASSED++)) elif echo "$OUTPUT" | grep -qi "monitor"; then echo -e "${GREEN}✓ PASSED${NC} (monitor running)" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Monitor thread did not seem to run or report" ((FAILED++)) fi # Test 7: Check for memory leaks with valgrind if command -v valgrind &> /dev/null; then echo -n "Test 7: 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 7: Valgrind not installed, skipping memory test${NC}" fi # Cleanup rm -f /tmp/state_monitor_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