123 lines
3.7 KiB
Bash
Executable File
123 lines
3.7 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 "========================================"
|
|
|
|
# Find the executable in the project root or the 'rendu' subdirectory.
|
|
if [ -f "$PROJECT_ROOT/$EXERCISE" ]; then
|
|
EXE_PATH="$PROJECT_ROOT/$EXERCISE"
|
|
elif [ -f "$PROJECT_ROOT/rendu/$EXERCISE" ]; then
|
|
EXE_PATH="$PROJECT_ROOT/rendu/$EXERCISE"
|
|
else
|
|
echo -e "${RED}✗ Executable '$EXERCISE' not found in '$PROJECT_ROOT' or '$PROJECT_ROOT/rendu'${NC}"
|
|
echo "Please compile your program first. Example:"
|
|
echo "gcc -Wall -Wextra -Werror -pthread thread_basics.c -o $EXERCISE"
|
|
exit 1
|
|
fi
|
|
|
|
echo -e "${YELLOW}Found executable at: $EXE_PATH${NC}"
|
|
|
|
# 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
|