135 lines
4.0 KiB
Bash
Executable File
135 lines
4.0 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Tester for mutex_basics exercise
|
|
# Tests mutex protection of shared variables
|
|
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m'
|
|
|
|
EXERCISE="mutex_basics"
|
|
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 mutex_basics.c -o mutex_basics"
|
|
exit 1
|
|
fi
|
|
|
|
# Test 1: Counter should be exactly 4000
|
|
echo -n "Test 1: Counter correctness (should be 4000)... "
|
|
OUTPUT=$(timeout 10 ./$EXERCISE 2>&1)
|
|
EXIT_CODE=$?
|
|
|
|
if [ $EXIT_CODE -eq 0 ]; then
|
|
COUNTER=$(echo "$OUTPUT" | grep -oP "(?i)counter.*?[:=]\s*\K\d+" | head -1)
|
|
if [ -z "$COUNTER" ]; then
|
|
COUNTER=$(echo "$OUTPUT" | grep -oP "\d{4}" | head -1)
|
|
fi
|
|
|
|
if [ "$COUNTER" = "4000" ]; then
|
|
echo -e "${GREEN}✓ PASSED${NC}"
|
|
((PASSED++))
|
|
else
|
|
echo -e "${RED}✗ FAILED${NC}"
|
|
echo "Expected counter = 4000, got: $COUNTER"
|
|
echo "Output: $OUTPUT"
|
|
((FAILED++))
|
|
fi
|
|
else
|
|
echo -e "${RED}✗ FAILED (timeout or crash)${NC}"
|
|
((FAILED++))
|
|
fi
|
|
|
|
# Test 2: Run multiple times to ensure consistency
|
|
echo -n "Test 2: Consistency check (5 runs)... "
|
|
CONSISTENT=true
|
|
for i in {1..5}; do
|
|
OUTPUT=$(timeout 10 ./$EXERCISE 2>&1)
|
|
COUNTER=$(echo "$OUTPUT" | grep -oP "(?i)counter.*?[:=]\s*\K\d+" | head -1)
|
|
if [ -z "$COUNTER" ]; then
|
|
COUNTER=$(echo "$OUTPUT" | grep -oP "\d{4}" | head -1)
|
|
fi
|
|
|
|
if [ "$COUNTER" != "4000" ]; then
|
|
CONSISTENT=false
|
|
break
|
|
fi
|
|
done
|
|
|
|
if [ "$CONSISTENT" = true ]; then
|
|
echo -e "${GREEN}✓ PASSED${NC}"
|
|
((PASSED++))
|
|
else
|
|
echo -e "${RED}✗ FAILED${NC}"
|
|
echo "Counter value inconsistent across multiple runs"
|
|
echo "This suggests race conditions - mutex not working properly"
|
|
((FAILED++))
|
|
fi
|
|
|
|
# Test 3: Check execution time is reported
|
|
echo -n "Test 3: Execution time reported... "
|
|
OUTPUT=$(timeout 10 ./$EXERCISE 2>&1)
|
|
if echo "$OUTPUT" | grep -qiE "(time|ms|milliseconds|seconds|duration)"; then
|
|
echo -e "${GREEN}✓ PASSED${NC}"
|
|
((PASSED++))
|
|
else
|
|
echo -e "${RED}✗ FAILED${NC}"
|
|
echo "Program should print the execution time"
|
|
((FAILED++))
|
|
fi
|
|
|
|
# Test 4: Thread sanitizer check
|
|
if command -v gcc &> /dev/null; then
|
|
echo -n "Test 4: Data race detection... "
|
|
# Try to compile with thread sanitizer
|
|
gcc -Wall -Wextra -Werror -pthread -fsanitize=thread -g mutex_basics.c -o mutex_basics_tsan 2>/dev/null
|
|
if [ $? -eq 0 ]; then
|
|
TSAN_OUTPUT=$(timeout 15 ./mutex_basics_tsan 2>&1)
|
|
if echo "$TSAN_OUTPUT" | grep -q "WARNING: ThreadSanitizer: data race"; then
|
|
echo -e "${RED}✗ FAILED${NC}"
|
|
echo "Data race detected! Mutex not protecting all accesses"
|
|
echo "$TSAN_OUTPUT" | head -20
|
|
((FAILED++))
|
|
else
|
|
echo -e "${GREEN}✓ PASSED${NC}"
|
|
((PASSED++))
|
|
fi
|
|
rm -f mutex_basics_tsan
|
|
else
|
|
echo -e "${YELLOW}⊘ Could not compile with thread sanitizer${NC}"
|
|
fi
|
|
else
|
|
echo -e "${YELLOW}⊘ Test 4: gcc not available, skipping sanitizer test${NC}"
|
|
fi
|
|
|
|
# Test 5: Memory leak check
|
|
if command -v valgrind &> /dev/null; then
|
|
echo -n "Test 5: Memory leak check... "
|
|
VALGRIND_OUTPUT=$(valgrind --leak-check=full --error-exitcode=42 ./$EXERCISE 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 5: Valgrind not installed, skipping${NC}"
|
|
fi
|
|
|
|
# Summary
|
|
echo "========================================"
|
|
echo -e "Results: ${GREEN}$PASSED passed${NC}, ${RED}$FAILED failed${NC}"
|
|
echo "========================================"
|
|
|
|
[ $FAILED -eq 0 ] && exit 0 || exit 1
|