11#! /bin/bash
22
3- # Test script for Q11
4- # This tests the calculateAverage function.
5-
6- gcc src/q11.c -o q11_exec
7- if [ $? -ne 0 ]; then
8- echo " Compilation failed."
9- exit 1
10- fi
11-
12- total_tests=0
13- passed_tests=0
14-
15- # Test Case 1: Simple average
16- (( total_tests++ ))
17- output=$( ./q11_exec 10 20 30)
18- # Use grep to check for the floating point number 20.0 or just 20
19- if echo " $output " | grep -q " 20" ; then
20- echo " Test Case 1 (Simple) PASSED"
21- (( passed_tests++ ))
22- else
23- echo " Test Case 1 (Simple) FAILED"
3+ # Automatically detect question number from script name (testqX.sh)
4+ num=$( basename " $0 " | grep -o -E ' [0-9]+' )
5+ SRC=" ../src/q${num} .c"
6+
7+ # 1. Remove all comments (single-line // and block /* ... */)
8+ code_no_comments=$( sed -E '
9+ s://.*$::g; # remove // comments
10+ :a; /\/*/{N; s:/\*.*\*/::; ba;} # remove /* ... */ comments (multi-line)
11+ ' " $SRC " )
12+
13+ # 2. Check if file (after removing comments) has any code left
14+ if ! echo " $code_no_comments " | grep -q ' [^[:space:]]' ; then
15+ echo " ❌ q${num} .c is empty or only contains comments"
16+ exit 0
2417fi
2518
26- # Test Case 2: Average with decimal result
27- (( total_tests++ ))
28- output=$( ./q11_exec 1 2 4)
29- # Check for 2.33...
30- if echo " $output " | grep -q " 2.33" ; then
31- echo " Test Case 2 (Decimal) PASSED"
32- (( passed_tests++ ))
33- else
34- echo " Test Case 2 (Decimal) FAILED"
35- fi
36-
37- # Test Case 3: Negative numbers
38- (( total_tests++ ))
39- output=$( ./q11_exec -10 0 10 20)
40- if echo " $output " | grep -q " 5" ; then
41- echo " Test Case 3 (Negative) PASSED"
42- (( passed_tests++ ))
19+ # 3. Try to compile
20+ gcc " $SRC " -o " q${num} .out" 2> compile.log
21+ if [ $? -ne 0 ]; then
22+ echo " ❌ Compilation failed for q${num} .c"
23+ cat compile.log
4324else
44- echo " Test Case 3 (Negative) FAILED "
25+ echo " ✅ Compilation successful for q ${num} .c "
4526fi
4627
47- echo " ----------------------------------------"
48- echo " Summary: $passed_tests / $total_tests tests passed."
49-
50- rm q11_exec
51- exit 0
28+ # Cleanup
29+ rm -f " q${num} .out" compile.log
30+ exit 0
0 commit comments