162 lines
4.6 KiB
Bash
Executable File
162 lines
4.6 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Tester for producer_consumer exercise
|
|
# Tests producer-consumer pattern with buffer
|
|
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m'
|
|
|
|
EXERCISE="producer_consumer"
|
|
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 producer_consumer.c -o producer_consumer"
|
|
exit 1
|
|
fi
|
|
|
|
# Test 1: Program runs to completion
|
|
echo -n "Test 1: Program execution... "
|
|
timeout 30 ./$EXERCISE > /tmp/producer_consumer_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/producer_consumer_output.txt 2>/dev/null)
|
|
|
|
# Test 2: Check for producer activity
|
|
echo -n "Test 2: Producer threads working... "
|
|
PRODUCER_COUNT=$(echo "$OUTPUT" | grep -ciE "(produc|add|insert|put)")
|
|
if [ "$PRODUCER_COUNT" -gt 0 ]; then
|
|
echo -e "${GREEN}✓ PASSED${NC} ($PRODUCER_COUNT producer actions)"
|
|
((PASSED++))
|
|
else
|
|
echo -e "${RED}✗ FAILED${NC}"
|
|
echo "No producer activity detected"
|
|
((FAILED++))
|
|
fi
|
|
|
|
# Test 3: Check for consumer activity
|
|
echo -n "Test 3: Consumer threads working... "
|
|
CONSUMER_COUNT=$(echo "$OUTPUT" | grep -ciE "(consum|take|remove|get|process)")
|
|
if [ "$CONSUMER_COUNT" -gt 0 ]; then
|
|
echo -e "${GREEN}✓ PASSED${NC} ($CONSUMER_COUNT consumer actions)"
|
|
((PASSED++))
|
|
else
|
|
echo -e "${RED}✗ FAILED${NC}"
|
|
echo "No consumer activity detected"
|
|
((FAILED++))
|
|
fi
|
|
|
|
# Test 4: Check that items 1-100 are produced
|
|
echo -n "Test 4: Items 1-100 produced... "
|
|
ITEMS_FOUND=0
|
|
for i in {1..100}; do
|
|
if echo "$OUTPUT" | grep -q "\\b$i\\b"; then
|
|
((ITEMS_FOUND++))
|
|
fi
|
|
done
|
|
|
|
if [ "$ITEMS_FOUND" -ge 90 ]; then
|
|
echo -e "${GREEN}✓ PASSED${NC} (found $ITEMS_FOUND/100 items)"
|
|
((PASSED++))
|
|
else
|
|
echo -e "${RED}✗ FAILED${NC}"
|
|
echo "Expected 100 items, found: $ITEMS_FOUND"
|
|
((FAILED++))
|
|
fi
|
|
|
|
# Test 5: Check for buffer management
|
|
echo -n "Test 5: Buffer management... "
|
|
if echo "$OUTPUT" | grep -qiE "(buffer|full|empty)"; then
|
|
echo -e "${GREEN}✓ PASSED${NC}"
|
|
((PASSED++))
|
|
else
|
|
echo -e "${YELLOW}⚠ WARNING${NC}"
|
|
echo "No explicit buffer status messages (this may be OK)"
|
|
((PASSED++))
|
|
fi
|
|
|
|
# Test 6: Verify no duplicate processing
|
|
echo -n "Test 6: No duplicate consumption... "
|
|
# This is hard to test perfectly, but we can check the logic
|
|
DUPLICATE_FOUND=false
|
|
for i in {1..100}; do
|
|
COUNT=$(echo "$OUTPUT" | grep -ciE "(consum|process).*\\b$i\\b")
|
|
if [ "$COUNT" -gt 1 ]; then
|
|
DUPLICATE_FOUND=true
|
|
break
|
|
fi
|
|
done
|
|
|
|
if [ "$DUPLICATE_FOUND" = false ]; then
|
|
echo -e "${GREEN}✓ PASSED${NC}"
|
|
((PASSED++))
|
|
else
|
|
echo -e "${RED}✗ FAILED${NC}"
|
|
echo "Item $i was consumed multiple times - race condition!"
|
|
((FAILED++))
|
|
fi
|
|
|
|
# Test 7: Thread sanitizer check
|
|
if command -v gcc &> /dev/null; then
|
|
echo -n "Test 7: Data race detection... "
|
|
gcc -Wall -Wextra -Werror -pthread -fsanitize=thread -g producer_consumer.c -o producer_consumer_tsan 2>/dev/null
|
|
if [ $? -eq 0 ]; then
|
|
TSAN_OUTPUT=$(timeout 35 ./producer_consumer_tsan 2>&1)
|
|
if echo "$TSAN_OUTPUT" | grep -q "WARNING: ThreadSanitizer: data race"; then
|
|
echo -e "${RED}✗ FAILED${NC}"
|
|
echo "Data race detected!"
|
|
echo "$TSAN_OUTPUT" | grep -A 5 "data race" | head -10
|
|
((FAILED++))
|
|
else
|
|
echo -e "${GREEN}✓ PASSED${NC}"
|
|
((PASSED++))
|
|
fi
|
|
rm -f producer_consumer_tsan
|
|
else
|
|
echo -e "${YELLOW}⊘ Could not compile with thread sanitizer${NC}"
|
|
fi
|
|
else
|
|
echo -e "${YELLOW}⊘ Test 7: gcc not available, skipping${NC}"
|
|
fi
|
|
|
|
# Test 8: Memory leak check
|
|
if command -v valgrind &> /dev/null; then
|
|
echo -n "Test 8: 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 8: Valgrind not installed, skipping${NC}"
|
|
fi
|
|
|
|
# Cleanup
|
|
rm -f /tmp/producer_consumer_output.txt
|
|
|
|
# Summary
|
|
echo "========================================"
|
|
echo -e "Results: ${GREEN}$PASSED passed${NC}, ${RED}$FAILED failed${NC}"
|
|
echo "========================================"
|
|
|
|
[ $FAILED -eq 0 ] && exit 0 || exit 1
|