#!/bin/bash # Tester for thread_basics exercise # Tests basic thread creation and joining RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color EXERCISE="thread_basics" PASSED=0 FAILED=0 echo "========================================" echo "Testing: $EXERCISE" echo "========================================" # Check if executable exists if [ ! -f "./$EXERCISE" ]; then echo -e "${RED}✗ Executable ./$EXERCISE not found${NC}" echo "Please compile your program first: gcc -Wall -Wextra -Werror -pthread thread_basics.c -o thread_basics" exit 1 fi # Test 1: Basic execution with 1 message echo -n "Test 1: Basic execution (1 message)... " OUTPUT=$(timeout 5 ./$EXERCISE 1 2>&1) if [ $? -eq 0 ]; then if echo "$OUTPUT" | grep -q "Thread 1: Hello from thread 1" && \ echo "$OUTPUT" | grep -q "Thread 2: Hello from thread 2"; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Expected output to contain both thread messages" echo "Got: $OUTPUT" ((FAILED++)) fi else echo -e "${RED}✗ FAILED (timeout or crash)${NC}" ((FAILED++)) fi # Test 2: Multiple messages (5) echo -n "Test 2: Multiple messages (5)... " OUTPUT=$(timeout 5 ./$EXERCISE 5 2>&1) if [ $? -eq 0 ]; then COUNT_T1=$(echo "$OUTPUT" | grep -c "Thread 1: Hello from thread 1") COUNT_T2=$(echo "$OUTPUT" | grep -c "Thread 2: Hello from thread 2") if [ "$COUNT_T1" -eq 5 ] && [ "$COUNT_T2" -eq 5 ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Expected 5 messages from each thread" echo "Got: Thread 1: $COUNT_T1, Thread 2: $COUNT_T2" ((FAILED++)) fi else echo -e "${RED}✗ FAILED (timeout or crash)${NC}" ((FAILED++)) fi # Test 3: Large number (10) echo -n "Test 3: Large number (10)... " OUTPUT=$(timeout 5 ./$EXERCISE 10 2>&1) if [ $? -eq 0 ]; then COUNT_T1=$(echo "$OUTPUT" | grep -c "Thread 1: Hello from thread 1") COUNT_T2=$(echo "$OUTPUT" | grep -c "Thread 2: Hello from thread 2") if [ "$COUNT_T1" -eq 10 ] && [ "$COUNT_T2" -eq 10 ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Expected 10 messages from each thread" echo "Got: Thread 1: $COUNT_T1, Thread 2: $COUNT_T2" ((FAILED++)) fi else echo -e "${RED}✗ FAILED (timeout or crash)${NC}" ((FAILED++)) fi # Test 4: Check for memory leaks with valgrind if command -v valgrind &> /dev/null; then echo -n "Test 4: Memory leak check... " VALGRIND_OUTPUT=$(valgrind --leak-check=full --error-exitcode=42 ./$EXERCISE 3 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 4: Valgrind not installed, skipping memory test${NC}" fi # Summary echo "========================================" echo -e "Results: ${GREEN}$PASSED passed${NC}, ${RED}$FAILED failed${NC}" echo "========================================" if [ $FAILED -eq 0 ]; then exit 0 else exit 1 fi