philosophers_prep/testers/test_13_semaphore_basics.sh

177 lines
5.5 KiB
Bash
Executable File

#!/bin/bash
# Tester for semaphore_basics exercise
# Tests POSIX named semaphores usage
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
EXERCISE="semaphore_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 with -pthread flag for semaphores
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 ---
# Clean up any leftover semaphores before testing
sem_unlink /test_semaphore 2>/dev/null || true
# Test 1: Program creates and cleans up semaphore
echo -n "Test 1: Basic execution... "
OUTPUT=$(timeout 10 "$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 properly (exit code: $EXIT_CODE)"
((FAILED++))
fi
# Test 2: Creates 5 child processes
echo -n "Test 2: Creates 5 child processes... "
CHILD_COUNT=$(echo "$OUTPUT" | grep -oE "Child [0-9]+" | sort -u | wc -l)
if [ "$CHILD_COUNT" -eq 5 ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Expected 5 unique children, found: $CHILD_COUNT"
((FAILED++))
fi
# Test 3: Resource acquisition and release
echo -n "Test 3: Resource acquisition/release... "
ACQUIRED_COUNT=$(echo "$OUTPUT" | grep -c "Acquired resource")
RELEASED_COUNT=$(echo "$OUTPUT" | grep -c "Released resource")
if [ "$ACQUIRED_COUNT" -eq 5 ] && [ "$RELEASED_COUNT" -eq 5 ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Expected 5 acquire and 5 release, got: $ACQUIRED_COUNT acquire, $RELEASED_COUNT release"
((FAILED++))
fi
# Test 4: Semaphore limits access (max 3 concurrent)
echo -n "Test 4: Semaphore limits concurrent access... "
# Check that we see "Waiting for resource" messages (proof that not all can access at once)
WAITING_COUNT=$(echo "$OUTPUT" | grep -c "Waiting for resource")
if [ "$WAITING_COUNT" -gt 0 ]; then
echo -e "${GREEN}✓ PASSED${NC} ($WAITING_COUNT wait events)"
((PASSED++))
else
echo -e "${YELLOW}⚠ PARTIAL${NC}"
echo "Expected some processes to wait (semaphore initialized to 3, but 5 processes)"
((PASSED++))
fi
# Test 5: Proper semaphore cleanup (sem_unlink called)
echo -n "Test 5: Semaphore cleanup... "
# Run the program again - if semaphore wasn't cleaned up, it might fail to create
OUTPUT2=$(timeout 10 "$EXE_PATH" 2>&1)
EXIT_CODE2=$?
if [ $EXIT_CODE2 -eq 0 ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Second execution failed - semaphore may not have been properly cleaned up"
((FAILED++))
fi
# Test 6: No zombie processes
echo -n "Test 6: No zombie processes... "
"$EXE_PATH" > /dev/null 2>&1 &
PARENT_PID=$!
sleep 1
ZOMBIE_COUNT=$(ps aux | grep "$PARENT_PID" | grep "defunct" | wc -l)
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 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 --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 7: Valgrind not installed, skipping memory test${NC}"
fi
# Cleanup
sem_unlink /test_semaphore 2>/dev/null || true
# 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