philosophers_prep/testers/test_3_precise_timing.sh

138 lines
3.8 KiB
Bash
Executable File

#!/bin/bash
# Tester for precise_timing exercise
# Tests timing functions accuracy
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
EXERCISE="precise_timing"
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 precise_timing.c -o precise_timing"
exit 1
fi
# Test 1: Basic execution
echo -n "Test 1: Program runs successfully... "
OUTPUT=$(timeout 5 ./$EXERCISE 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
# Test 2: Check if time difference is approximately 100ms
echo -n "Test 2: Sleep precision (~100ms)... "
OUTPUT=$(timeout 5 ./$EXERCISE 2>&1)
# Extract number that should be around 100
TIME_DIFF=$(echo "$OUTPUT" | grep -oP "\d+" | tail -1)
if [ -n "$TIME_DIFF" ]; then
# Allow 5ms margin of error (95-105ms is acceptable)
if [ "$TIME_DIFF" -ge 95 ] && [ "$TIME_DIFF" -le 105 ]; then
echo -e "${GREEN}✓ PASSED${NC} (measured: ${TIME_DIFF}ms)"
((PASSED++))
elif [ "$TIME_DIFF" -ge 90 ] && [ "$TIME_DIFF" -le 110 ]; then
echo -e "${YELLOW}⚠ PARTIAL${NC} (measured: ${TIME_DIFF}ms, acceptable but not ideal)"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Expected ~100ms, got: ${TIME_DIFF}ms"
echo "Output: $OUTPUT"
((FAILED++))
fi
else
echo -e "${RED}✗ FAILED${NC}"
echo "Could not extract time difference from output"
echo "Output: $OUTPUT"
((FAILED++))
fi
# Test 3: Consistency across multiple runs
echo -n "Test 3: Timing consistency (5 runs)... "
CONSISTENT=true
TOTAL=0
COUNT=0
for i in {1..5}; do
OUTPUT=$(timeout 5 ./$EXERCISE 2>&1)
TIME_DIFF=$(echo "$OUTPUT" | grep -oP "\d+" | tail -1)
if [ -n "$TIME_DIFF" ]; then
TOTAL=$((TOTAL + TIME_DIFF))
COUNT=$((COUNT + 1))
if [ "$TIME_DIFF" -lt 90 ] || [ "$TIME_DIFF" -gt 110 ]; then
CONSISTENT=false
fi
fi
done
if [ "$CONSISTENT" = true ] && [ "$COUNT" -eq 5 ]; then
AVG=$((TOTAL / COUNT))
echo -e "${GREEN}✓ PASSED${NC} (avg: ${AVG}ms)"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
if [ "$COUNT" -eq 5 ]; then
AVG=$((TOTAL / COUNT))
echo "Timing inconsistent across runs (avg: ${AVG}ms)"
else
echo "Some runs failed to complete"
fi
((FAILED++))
fi
# Test 4: Check output format
echo -n "Test 4: Output format check... "
OUTPUT=$(timeout 5 ./$EXERCISE 2>&1)
if echo "$OUTPUT" | grep -qiE "(diff|difference|elapsed|time|ms)"; then
echo -e "${GREEN}✓ PASSED${NC}"
((PASSED++))
else
echo -e "${RED}✗ FAILED${NC}"
echo "Program should clearly indicate the time difference"
echo "Output: $OUTPUT"
((FAILED++))
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!"
((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 "========================================"
echo ""
echo "Note: Timing precision is crucial for Philosophers project"
echo "Your sleep function should be accurate within ±5ms"
[ $FAILED -eq 0 ] && exit 0 || exit 1