#!/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" 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 or compile the executable --- # Priority: # 1. rendu/executable # 2. root/executable # 3. rendu/source -> compile # 4. root/source -> compile # Define potential paths for executable and source EXE_IN_RENDU="$PROJECT_ROOT/rendu/$EXERCISE" EXE_IN_ROOT="$PROJECT_ROOT/$EXERCISE" SRC_IN_RENDU="$PROJECT_ROOT/rendu/${EXERCISE}.c" SRC_IN_ROOT="$PROJECT_ROOT/${EXERCISE}.c" # Check for existing executable if [ -f "$EXE_IN_RENDU" ]; then EXE_PATH="$EXE_IN_RENDU" echo -e "${YELLOW}Found executable in 'rendu' directory.${NC}" elif [ -f "$EXE_IN_ROOT" ]; then EXE_PATH="$EXE_IN_ROOT" echo -e "${YELLOW}Found executable in root directory.${NC}" else echo -e "${YELLOW}Executable not found. Attempting to compile from source...${NC}" COMPILE_CMD="gcc -Wall -Wextra -Werror -pthread" # Check for source file and compile if [ -f "$SRC_IN_RENDU" ]; then echo "Found source file in 'rendu'. Compiling..." if $COMPILE_CMD "$SRC_IN_RENDU" -o "$EXE_IN_RENDU"; then EXE_PATH="$EXE_IN_RENDU" echo -e "${GREEN}Compilation successful.${NC}" else echo -e "${RED}✗ Compilation failed for '$SRC_IN_RENDU'.${NC}" exit 1 fi elif [ -f "$SRC_IN_ROOT" ]; then echo "Found source file in root. Compiling..." if $COMPILE_CMD "$SRC_IN_ROOT" -o "$EXE_IN_ROOT"; then EXE_PATH="$EXE_IN_ROOT" echo -e "${GREEN}Compilation successful.${NC}" else echo -e "${RED}✗ Compilation failed for '$SRC_IN_ROOT'.${NC}" exit 1 fi else echo -e "${RED}✗ Neither executable nor source file found for '$EXERCISE'.${NC}" echo "Looked for:" echo " - $EXE_IN_RENDU" echo " - $EXE_IN_ROOT" echo " - $SRC_IN_RENDU" echo " - $SRC_IN_ROOT" exit 1 fi fi echo -e "${YELLOW}Using executable at: $EXE_PATH${NC}" # Test 1: Basic execution echo -n "Test 1: Program runs successfully... " OUTPUT=$(timeout 5 "$EXE_PATH" 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 "$EXE_PATH" 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 "$EXE_PATH" 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 "$EXE_PATH" 2>&1) if echo "$OUTPUT" | grep -qE "Time difference: [0-9]+ ms"; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Expected output format: 'Time difference: ms'" echo "Got: $OUTPUT" ((FAILED++)) fi # Test 5: Check for memory leaks with valgrind if command -v valgrind &> /dev/null; then echo -n "Test 5: Memory leak check... " VALGRIND_OUTPUT=$(valgrind --leak-check=full --error-exitcode=42 "$EXE_PATH" 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 memory test${NC}" fi # 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