philosophers_prep/testers/test_12_process_basics.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

173 lines
5.3 KiB
Bash
Executable File

#!/bin/bash
# Tester for process_basics exercise
# Tests basic process creation with fork and waitpid
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
EXERCISE="process_basics"
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}"
# Compile into the root directory
gcc -Wall -Wextra -Werror "$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 creates 3 child processes
echo -n "Test 1: Creates 3 child processes... "
OUTPUT=$(timeout 5 "$EXE_PATH" 2>&1)
if [ $? -eq 0 ]; then
CHILD_COUNT=$(echo "$OUTPUT" | grep -c "Child [123]")
if [ "$CHILD_COUNT" -ge 3 ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Expected output from 3 children, found: $CHILD_COUNT"
((FAILED++))
fi
else
echo -e "${RED}✗ FAILED (timeout or crash)${NC}"
((FAILED++))
fi
# Test 2: Each child prints PID
echo -n "Test 2: Each child prints PID... "
PID_COUNT=$(echo "$OUTPUT" | grep -oE "PID: [0-9]+" | wc -l)
if [ "$PID_COUNT" -ge 15 ]; then # 3 children * 5 messages each
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Expected PIDs in output, found: $PID_COUNT references"
((FAILED++))
fi
# Test 3: Each child prints 5 messages
echo -n "Test 3: Each child prints 5 messages... "
for i in 1 2 3; do
COUNT=$(echo "$OUTPUT" | grep "Child $i" | grep -c "Message")
if [ "$COUNT" -ne 5 ]; then
echo -e "${RED}✗ FAILED${NC}"
echo "Child $i printed $COUNT messages instead of 5"
((FAILED++))
break
fi
done
if [ $? -eq 0 ] && [ "$COUNT" -eq 5 ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
fi
# Test 4: Parent waits for all children
echo -n "Test 4: Parent waits for all children... "
if echo "$OUTPUT" | grep -q "Parent: All 3 children have finished"; then
# Verify parent message comes after all child messages
LAST_CHILD_LINE=$(echo "$OUTPUT" | grep -n "Child" | tail -1 | cut -d: -f1)
PARENT_LINE=$(echo "$OUTPUT" | grep -n "Parent: All 3 children have finished" | cut -d: -f1)
if [ "$PARENT_LINE" -gt "$LAST_CHILD_LINE" ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Parent message appeared before all children finished"
((FAILED++))
fi
else
echo -e "${RED}✗ FAILED${NC}"
echo "Parent summary message not found"
((FAILED++))
fi
# Test 5: No zombie processes (check process cleanup)
echo -n "Test 5: Process cleanup (no zombies)... "
# Run the program in background and check for zombie processes
"$EXE_PATH" > /dev/null 2>&1 &
PARENT_PID=$!
sleep 1
ZOMBIE_COUNT=$(ps aux | grep "$PARENT_PID" | grep -c "defunct")
wait $PARENT_PID
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 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 --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 6: 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