philosophers_prep/testers/test_11_race_detector.sh

185 lines
6.1 KiB
Bash
Executable File

#!/bin/bash
# Tester for race_detector exercise
# Tests data race detection and optimization strategies
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
EXERCISE="race_detector"
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 race_detector.c -o $EXERCISE"
exit 1
fi
echo -e "${YELLOW}Found executable at: $EXE_PATH${NC}"
# Test 1: Basic execution
echo -n "Test 1: Basic execution - 4 410 200 200... "
timeout 120 "$EXE_PATH" 4 410 200 200 > /tmp/race_detector_output.txt 2>&1
EXIT_CODE=$?
if [ $EXIT_CODE -eq 0 ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED (timeout or crash)${NC}"
((FAILED++))
fi
OUTPUT=$(cat /tmp/race_detector_output.txt 2>/dev/null)
# Test 2: Multiple iterations/strategies tested
echo -n "Test 2: Multiple test iterations... "
if echo "$OUTPUT" | grep -qiE "(iteration|run|test|strategy)"; then
ITERATIONS=$(echo "$OUTPUT" | grep -ciE "(iteration|run|test)")
if [ "$ITERATIONS" -ge 3 ]; then
echo -e "${GREEN}✓ PASSED${NC} ($ITERATIONS iterations)"
((PASSED++))
else
echo -e "${YELLOW}⚠ PARTIAL${NC} (found $ITERATIONS iterations)"
((PASSED++))
fi
else
echo -e "${RED}✗ FAILED${NC}"
echo "Expected multiple test iterations"
((FAILED++))
fi
# Test 3: Reports statistics
echo -n "Test 3: Statistics reporting... "
HAS_STATS=false
if echo "$OUTPUT" | grep -qiE "(race|death|time|performance)"; then
HAS_STATS=true
fi
if [ "$HAS_STATS" = true ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Expected statistics about races, deaths, or performance"
((FAILED++))
fi
# Test 4: Different strategies mentioned
echo -n "Test 4: Multiple strategies... "
STRATEGY_COUNT=$(echo "$OUTPUT" | grep -ciE "(strategy|approach|method|optimization)")
if [ "$STRATEGY_COUNT" -ge 2 ]; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${YELLOW}⚠ WARNING${NC}"
echo "Expected different optimization strategies to be tested"
((PASSED++))
fi
# Test 5: Helgrind check (if available)
if command -v valgrind &> /dev/null; then
echo -n "Test 5: Helgrind data race check... "
# Run a single iteration with helgrind
timeout 60 valgrind --tool=helgrind "$EXE_PATH" 4 410 200 200 > /tmp/helgrind_output.txt 2>&1
HELGRIND_OUTPUT=$(cat /tmp/helgrind_output.txt 2>/dev/null)
if echo "$HELGRIND_OUTPUT" | grep -q "ERROR SUMMARY: 0 errors"; then
echo -e "${GREEN}✓ PASSED (no races detected by helgrind)${NC}"
((PASSED++))
elif echo "$HELGRIND_OUTPUT" | grep -q "Possible data race"; then
echo -e "${RED}✗ FAILED${NC}"
echo "Helgrind detected data races!"
echo "$HELGRIND_OUTPUT" | grep -A 2 "Possible data race" | head -10
((FAILED++))
else
echo -e "${YELLOW}⚠ WARNING${NC} (Helgrind test inconclusive)"
((PASSED++))
fi
else
echo -e "${YELLOW}⊘ Test 5: Valgrind not installed, skipping Helgrind test${NC}"
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 "$EXE_PATH" 4 410 200 200 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
# Test 7: Thread sanitizer check
if command -v gcc &> /dev/null; then
echo -n "Test 7: Data race detection (ThreadSanitizer)... "
# Find the source file to compile
SOURCE_FILE=""
if [ -f "$PROJECT_ROOT/rendu/${EXERCISE}.c" ]; then
SOURCE_FILE="$PROJECT_ROOT/rendu/${EXERCISE}.c"
elif [ -f "$PROJECT_ROOT/${EXERCISE}.c" ]; then
SOURCE_FILE="$PROJECT_ROOT/${EXERCISE}.c"
fi
if [ -n "$SOURCE_FILE" ]; then
TSAN_EXE="$PROJECT_ROOT/$(basename "$EXE_PATH")_tsan"
gcc -Wall -Wextra -Werror -pthread -fsanitize=thread -g "$SOURCE_FILE" -o "$TSAN_EXE" 2>/dev/null
if [ $? -eq 0 ]; then
TSAN_OUTPUT=$(timeout 60 "$TSAN_EXE" 4 410 200 200 2>&1)
if echo "$TSAN_OUTPUT" | grep -q "WARNING: ThreadSanitizer: data race"; then
echo -e "${RED}✗ FAILED${NC}"
echo "Data race detected by ThreadSanitizer!"
echo "$TSAN_OUTPUT" | head -20
((FAILED++))
else
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
fi
rm -f "$TSAN_EXE"
else
echo -e "${YELLOW}⊘ Could not compile with ThreadSanitizer, skipping test${NC}"
fi
else
echo -e "${YELLOW}⊘ Source file ${EXERCISE}.c not found, skipping ThreadSanitizer test${NC}"
fi
else
echo -e "${YELLOW}⊘ Test 7: gcc not found, skipping data race test${NC}"
fi
# Cleanup
rm -f /tmp/race_detector_output.txt /tmp/helgrind_output.txt
# Final summary
echo "----------------------------------------"
if [ $FAILED -eq 0 ]; then
echo -e "${GREEN}Summary: All $PASSED tests passed for $EXERCISE!${NC}"
exit 0
else
echo -e "${RED}Summary: $FAILED out of $((PASSED + FAILED)) tests failed for $EXERCISE.${NC}"
exit 1
fi