philosophers_prep/testers/test_14_process_communication.sh
Rui Ribeiro 2128325237 Added new exercices to better cover philosophers_bonus requirements
- Added exercices:
  - Added process basics.
  - Added semaphores basics.
  - Added process communication
  - Added process termination
  - Added process_philosophers
  - Renamed and reordered philosophers_bonus
2025-10-14 16:37:24 +01:00

191 lines
6.3 KiB
Bash
Executable File

#!/bin/bash
# Tester for process_communication exercise
# Tests producer-consumer pattern with processes and semaphores
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
EXERCISE="process_communication"
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 "========================================"
# --- Compilation and Executable Finding Logic ---
EXE_PATH="$PROJECT_ROOT/$EXERCISE"
RENDU_EXE_PATH="$PROJECT_ROOT/rendu/$EXERCISE"
SOURCE_FILE="$PROJECT_ROOT/${EXERCISE}.c"
RENDU_SOURCE_FILE="$PROJECT_ROOT/rendu/${EXERCISE}.c"
# Prefer executable in 'rendu' directory
if [ -f "$RENDU_EXE_PATH" ]; then
EXE_PATH="$RENDU_EXE_PATH"
# Fallback to root directory executable
elif [ -f "$EXE_PATH" ]; then
: # EXE_PATH is already set correctly
# If no executable, try to compile from source
else
COMPILE_CANDIDATE=""
if [ -f "$RENDU_SOURCE_FILE" ]; then
COMPILE_CANDIDATE="$RENDU_SOURCE_FILE"
elif [ -f "$SOURCE_FILE" ]; then
COMPILE_CANDIDATE="$SOURCE_FILE"
fi
if [ -n "$COMPILE_CANDIDATE" ]; then
echo -e "${YELLOW}Executable not found, attempting to compile from $COMPILE_CANDIDATE...${NC}"
gcc -Wall -Wextra -Werror -pthread "$COMPILE_CANDIDATE" -o "$PROJECT_ROOT/$EXERCISE"
if [ $? -eq 0 ]; then
echo -e "${GREEN}✓ Compilation successful.${NC}"
EXE_PATH="$PROJECT_ROOT/$EXERCISE" # Use the newly compiled executable
else
echo -e "${RED}✗ Compilation failed.${NC}"
exit 1
fi
else
echo -e "${RED}✗ Executable and source file for '$EXERCISE' not found.${NC}"
exit 1
fi
fi
echo -e "${YELLOW}Using executable at: $EXE_PATH${NC}"
# --- End of Logic ---
# Test 1: Program executes successfully
echo -n "Test 1: Basic execution... "
OUTPUT=$(timeout 15 "$EXE_PATH" 2>&1)
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Program failed to execute (exit code: $EXIT_CODE)"
((FAILED++))
fi
# Test 2: Creates 2 producers and 2 consumers
echo -n "Test 2: Creates 2 producers and 2 consumers... "
PRODUCER_COUNT=$(echo "$OUTPUT" | grep -oE "Producer [0-9]+" | sort -u | wc -l)
CONSUMER_COUNT=$(echo "$OUTPUT" | grep -oE "Consumer [0-9]+" | sort -u | wc -l)
if [ "$PRODUCER_COUNT" -eq 2 ] && [ "$CONSUMER_COUNT" -eq 2 ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Expected 2 producers and 2 consumers, got: $PRODUCER_COUNT producers, $CONSUMER_COUNT consumers"
((FAILED++))
fi
# Test 3: Each producer produces 10 items
echo -n "Test 3: Producers produce items... "
PRODUCED_TOTAL=$(echo "$OUTPUT" | grep -c "Produced item")
if [ "$PRODUCED_TOTAL" -ge 15 ]; then # 2 producers * 10 items = 20, allow some margin
echo -e "${GREEN}✓ PASSED${NC} ($PRODUCED_TOTAL items produced)"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Expected ~20 produced items, got: $PRODUCED_TOTAL"
((FAILED++))
fi
# Test 4: Each consumer consumes 10 items
echo -n "Test 4: Consumers consume items... "
CONSUMED_TOTAL=$(echo "$OUTPUT" | grep -c "Consumed item")
if [ "$CONSUMED_TOTAL" -ge 15 ]; then # 2 consumers * 10 items = 20, allow some margin
echo -e "${GREEN}✓ PASSED${NC} ($CONSUMED_TOTAL items consumed)"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Expected ~20 consumed items, got: $CONSUMED_TOTAL"
((FAILED++))
fi
# Test 5: Synchronization - no deadlock
echo -n "Test 5: No deadlock... "
# If the program completed successfully, there was no deadlock
if [ $EXIT_CODE -eq 0 ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Program may have deadlocked (timeout or abnormal exit)"
((FAILED++))
fi
# Test 6: Proper semaphore usage (interleaved production/consumption)
echo -n "Test 6: Production/consumption interleaving... "
# Extract sequence of produce/consume actions
FIRST_PRODUCE=$(echo "$OUTPUT" | grep -n "Produced item" | head -1 | cut -d: -f1)
FIRST_CONSUME=$(echo "$OUTPUT" | grep -n "Consumed item" | head -1 | cut -d: -f1)
LAST_PRODUCE=$(echo "$OUTPUT" | grep -n "Produced item" | tail -1 | cut -d: -f1)
LAST_CONSUME=$(echo "$OUTPUT" | grep -n "Consumed item" | tail -1 | cut -d: -f1)
# Check that production and consumption are interleaved (not all production then all consumption)
if [ -n "$FIRST_PRODUCE" ] && [ -n "$FIRST_CONSUME" ] && [ -n "$LAST_PRODUCE" ] && [ -n "$LAST_CONSUME" ]; then
if [ "$FIRST_CONSUME" -lt "$LAST_PRODUCE" ] && [ "$FIRST_PRODUCE" -lt "$LAST_CONSUME" ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${YELLOW}⚠ PARTIAL${NC}"
echo "Production and consumption may not be properly synchronized"
((PASSED++))
fi
else
echo -e "${RED}✗ FAILED${NC}"
echo "Could not verify interleaving"
((FAILED++))
fi
# Test 7: No zombie processes
echo -n "Test 7: No zombie processes... "
"$EXE_PATH" > /dev/null 2>&1 &
PARENT_PID=$!
sleep 1
ZOMBIE_COUNT=$(ps aux | grep "$PARENT_PID" | grep -c "defunct" || echo "0")
wait $PARENT_PID 2>/dev/null
if [ "$ZOMBIE_COUNT" -eq 0 ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Found $ZOMBIE_COUNT zombie processes"
((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 --trace-children=yes "$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
# Summary
echo "========================================"
echo -e "Results: ${GREEN}$PASSED passed${NC}, ${RED}$FAILED failed${NC}"
echo "========================================"
if [ $FAILED -eq 0 ]; then
exit 0
else
exit 1
fi