philosophers_prep/testers/test_1_thread_basics.sh
Rui Ribeiro 8779aa24a6 Increase tester functionality
- Tester now checks if executable exists in `rendu` filder, if not, compiles the source file and run the test on it
2025-10-07 20:57:32 +01:00

149 lines
4.6 KiB
Bash
Executable File

#!/bin/bash
# Tester for thread_basics exercise
# Tests basic thread creation and joining
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
EXERCISE="thread_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 -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: Basic execution with 1 message
echo -n "Test 1: Basic execution (1 message)... "
OUTPUT=$(timeout 5 "$EXE_PATH" 1 2>&1)
if [ $? -eq 0 ]; then
if echo "$OUTPUT" | grep -q "Thread 1: Hello from thread 1" && \
echo "$OUTPUT" | grep -q "Thread 2: Hello from thread 2"; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Expected output to contain both thread messages"
echo "Got: $OUTPUT"
((FAILED++))
fi
else
echo -e "${RED}✗ FAILED (timeout or crash)${NC}"
((FAILED++))
fi
# Test 2: Multiple messages (5)
echo -n "Test 2: Multiple messages (5)... "
OUTPUT=$(timeout 5 "$EXE_PATH" 5 2>&1)
if [ $? -eq 0 ]; then
COUNT_T1=$(echo "$OUTPUT" | grep -c "Thread 1: Hello from thread 1")
COUNT_T2=$(echo "$OUTPUT" | grep -c "Thread 2: Hello from thread 2")
if [ "$COUNT_T1" -eq 5 ] && [ "$COUNT_T2" -eq 5 ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Expected 5 messages from each thread"
echo "Got: Thread 1: $COUNT_T1, Thread 2: $COUNT_T2"
((FAILED++))
fi
else
echo -e "${RED}✗ FAILED (timeout or crash)${NC}"
((FAILED++))
fi
# Test 3: Large number (10)
echo -n "Test 3: Large number (10)... "
OUTPUT=$(timeout 5 "$EXE_PATH" 10 2>&1)
if [ $? -eq 0 ]; then
COUNT_T1=$(echo "$OUTPUT" | grep -c "Thread 1: Hello from thread 1")
COUNT_T2=$(echo "$OUTPUT" | grep -c "Thread 2: Hello from thread 2")
if [ "$COUNT_T1" -eq 10 ] && [ "$COUNT_T2" -eq 10 ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Expected 10 messages from each thread"
echo "Got: Thread 1: $COUNT_T1, Thread 2: $COUNT_T2"
((FAILED++))
fi
else
echo -e "${RED}✗ FAILED (timeout or crash)${NC}"
((FAILED++))
fi
# Test 4: Check for memory leaks with valgrind
if command -v valgrind &> /dev/null; then
echo -n "Test 4: Memory leak check... "
VALGRIND_OUTPUT=$(valgrind --leak-check=full --error-exitcode=42 "$EXE_PATH" 3 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 4: 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