#!/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" PASSED=0 FAILED=0 echo "========================================" echo "Testing: $EXERCISE" echo "========================================" if [ ! -f "./$EXERCISE" ]; then echo -e "${RED}✗ Executable ./$EXERCISE not found${NC}" echo "Please compile: gcc -Wall -Wextra -Werror -pthread race_detector.c -o race_detector" exit 1 fi # Test 1: Basic execution echo -n "Test 1: Basic execution - 4 410 200 200... " timeout 120 ./$EXERCISE 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 ./$EXERCISE 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}⚠ UNCLEAR${NC} (helgrind ran but results unclear)" ((PASSED++)) fi rm -f /tmp/helgrind_output.txt else echo -e "${YELLOW}⊘ Test 5: Valgrind not installed, skipping helgrind${NC}" fi # Test 6: Edge case - odd number of philosophers echo -n "Test 6: Odd philosophers (5)... " timeout 120 ./$EXERCISE 5 800 200 200 > /tmp/race_detector_odd.txt 2>&1 if [ $? -eq 0 ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" ((FAILED++)) fi # Test 7: Edge case - tight timing echo -n "Test 7: Tight timing (4 310 200 100)... " timeout 120 ./$EXERCISE 4 310 200 100 > /tmp/race_detector_tight.txt 2>&1 if [ $? -eq 0 ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" ((FAILED++)) fi # Test 8: Performance comparison echo -n "Test 8: Performance metrics... " if echo "$OUTPUT" | grep -qiE "(time|ms|second|performance|speed)"; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${YELLOW}⚠ WARNING${NC}" echo "Expected performance/timing information" ((PASSED++)) fi # Test 9: Thread sanitizer on the program itself if command -v gcc &> /dev/null; then echo -n "Test 9: Thread sanitizer... " gcc -Wall -Wextra -Werror -pthread -fsanitize=thread -g race_detector.c -o race_detector_tsan 2>/dev/null if [ $? -eq 0 ]; then TSAN_OUTPUT=$(timeout 60 ./race_detector_tsan 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 in the race detector itself!" ((FAILED++)) else echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) fi rm -f race_detector_tsan else echo -e "${YELLOW}⊘ Could not compile with thread sanitizer${NC}" fi else echo -e "${YELLOW}⊘ Test 9: gcc not available, skipping${NC}" fi # Test 10: Memory leak check if command -v valgrind &> /dev/null; then echo -n "Test 10: Memory leak check... " VALGRIND_OUTPUT=$(timeout 60 valgrind --leak-check=full --error-exitcode=42 ./$EXERCISE 4 410 200 200 2>&1) VALGRIND_EXIT=$? if [ $VALGRIND_EXIT -eq 0 ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) elif [ $VALGRIND_EXIT -eq 124 ]; then echo -e "${YELLOW}⚠ TIMEOUT${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Memory leaks detected!" ((FAILED++)) fi else echo -e "${YELLOW}⊘ Test 10: Valgrind not installed, skipping${NC}" fi # Cleanup rm -f /tmp/race_detector_*.txt /tmp/race_detector_output.txt # Summary echo "========================================" echo -e "Results: ${GREEN}$PASSED passed${NC}, ${RED}$FAILED failed${NC}" echo "========================================" echo "" echo "Note: For thorough testing, run:" echo " valgrind --tool=helgrind ./race_detector 4 410 200 200" echo " valgrind --tool=drd ./race_detector 4 410 200 200" [ $FAILED -eq 0 ] && exit 0 || exit 1