#!/bin/bash # Tester for limited_resources exercise # Tests semaphore-like behavior with mutexes RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' EXERCISE="limited_resources" 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 limited_resources.c -o limited_resources" exit 1 fi # Test 1: Program runs to completion echo -n "Test 1: Program execution... " timeout 30 ./$EXERCISE > /tmp/limited_resources_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/limited_resources_output.txt 2>/dev/null) # Test 2: 10 students detected echo -n "Test 2: 10 students present... " STUDENT_COUNT=$(echo "$OUTPUT" | grep -oiE "student [0-9]+" | sort -u | wc -l) if [ "$STUDENT_COUNT" -ge 10 ]; then echo -e "${GREEN}✓ PASSED${NC} (found $STUDENT_COUNT students)" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Expected 10 students, found: $STUDENT_COUNT" ((FAILED++)) fi # Test 3: Check for arrival messages echo -n "Test 3: Students arrive... " ARRIVAL_COUNT=$(echo "$OUTPUT" | grep -ciE "(arrive|enter|library)") if [ "$ARRIVAL_COUNT" -ge 10 ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Expected arrival messages for 10 students" ((FAILED++)) fi # Test 4: Check for computer acquisition echo -n "Test 4: Getting computers... " GET_COUNT=$(echo "$OUTPUT" | grep -ciE "(get|got|using|acquired|computer)") if [ "$GET_COUNT" -ge 10 ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "All students should get a computer eventually" ((FAILED++)) fi # Test 5: Check for leaving/finishing messages echo -n "Test 5: Students leave... " LEAVE_COUNT=$(echo "$OUTPUT" | grep -ciE "(leave|finish|done|release)") if [ "$LEAVE_COUNT" -ge 10 ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Expected leaving messages for 10 students" ((FAILED++)) fi # Test 6: Maximum 3 concurrent users echo -n "Test 6: Resource limit (max 3)... " # This is tricky to test - we need to check timestamps # Extract lines with "get/using" and "finish/leave" and verify max 3 concurrent # For simplicity, we'll check if the logic seems sound if echo "$OUTPUT" | grep -qiE "(wait|waiting|full|limit)"; then echo -e "${GREEN}✓ PASSED${NC} (waiting detected - limit enforced)" ((PASSED++)) else echo -e "${YELLOW}⚠ WARNING${NC}" echo "Could not verify resource limit enforcement" echo "This is OK if program structure enforces it differently" ((PASSED++)) fi # Test 7: No student is starved (all get to use computer) echo -n "Test 7: Fairness check... " ALL_USED=true for i in {0..9}; do if ! echo "$OUTPUT" | grep -qiE "student $i.*((get|using|acquired|computer)|(finish|done|leave))"; then ALL_USED=false break fi done if [ "$ALL_USED" = true ]; then echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) else echo -e "${RED}✗ FAILED${NC}" echo "Student $i never got a computer - possible starvation" ((FAILED++)) fi # Test 8: Thread sanitizer if command -v gcc &> /dev/null; then echo -n "Test 8: Data race detection... " gcc -Wall -Wextra -Werror -pthread -fsanitize=thread -g limited_resources.c -o limited_resources_tsan 2>/dev/null if [ $? -eq 0 ]; then TSAN_OUTPUT=$(timeout 35 ./limited_resources_tsan 2>&1) if echo "$TSAN_OUTPUT" | grep -q "WARNING: ThreadSanitizer: data race"; then echo -e "${RED}✗ FAILED${NC}" echo "Data race detected!" ((FAILED++)) else echo -e "${GREEN}✓ PASSED${NC}" ((PASSED++)) fi rm -f limited_resources_tsan else echo -e "${YELLOW}⊘ Could not compile with thread sanitizer${NC}" fi else echo -e "${YELLOW}⊘ Test 8: gcc not available, skipping${NC}" fi # Test 9: Memory leak check if command -v valgrind &> /dev/null; then echo -n "Test 9: Memory leak check... " VALGRIND_OUTPUT=$(timeout 35 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 9: Valgrind not installed, skipping${NC}" fi # Cleanup rm -f /tmp/limited_resources_output.txt # Summary echo "========================================" echo -e "Results: ${GREEN}$PASSED passed${NC}, ${RED}$FAILED failed${NC}" echo "========================================" [ $FAILED -eq 0 ] && exit 0 || exit 1