Skip to content

Commit 4215747

Browse files
committed
fix test cases and added docs folder for answer.pdf
1 parent 17d1f68 commit 4215747

34 files changed

+701
-1216
lines changed

.github/workflows/classroom.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ jobs:
5858
tests/testq27.sh \
5959
tests/testq28.sh \
6060
tests/testq29.sh \
61-
tests/testq30.sh; do
61+
tests/testq30.sh \
62+
tests/test_pdf.sh; do
6263
echo "Running $t ..."
6364
if ! $t; then
6465
failed=1 # record failure but continue

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
## Instructions
44

55
1. Write code in the corresponding `src/` file for each programming task.
6-
2. Do not change file names — tests depend on them.
7-
3. Commit and push your changes to GitHub.
6+
2. Upload a `PDF` containing all answers with output screenshot in the docs/ folder
7+
- You can create a doc and convert it to PDF online.
8+
- Name pdf as `answer.pdf`
9+
- Your first line of pdf should be `Name: Firstname-Lastname`. Eg: Name: Nirajan-Thakuri
10+
1. Do not change file names — tests depend on them.
11+
2. Commit and push your changes to GitHub.
812

913
---
1014

docs/.gitkeep

Whitespace-only changes.

tests/test_pdf.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
set -e
3+
4+
PDF_FILE="docs/answer.pdf"
5+
6+
if [ ! -f "$PDF_FILE" ]; then
7+
echo "❌ PDF file $PDF_FILE missing"
8+
exit 1
9+
fi
10+
11+
if [ ! -s "$PDF_FILE" ]; then
12+
echo "❌ PDF file $PDF_FILE is empty"
13+
exit 1
14+
fi
15+
16+
echo "✅ PDF file found and not empty"

tests/testq1.sh

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,30 @@
11
#!/bin/bash
22

3-
# Test script for Q1
4-
# The C program is expected to take numbers as command-line arguments
5-
# and print the max and min values.
6-
7-
# Compile the student's code
8-
gcc src/q1.c -o q1_exec
9-
if [ $? -ne 0 ]; then
10-
echo "Compilation failed."
11-
exit 1
12-
fi
13-
14-
# Test Cases
15-
total_tests=0
16-
passed_tests=0
17-
18-
# Test Case 1: Positive numbers
19-
((total_tests++))
20-
output=$(./q1_exec 10 5 42 8 15)
21-
if echo "$output" | grep -q "Maximum.*42" && echo "$output" | grep -q "Minimum.*5"; then
22-
echo "Test Case 1 (Positive) PASSED"
23-
((passed_tests++))
24-
else
25-
echo "Test Case 1 (Positive) 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
2617
fi
2718

28-
# Test Case 2: Negative and positive numbers
29-
((total_tests++))
30-
output=$(./q1_exec -10 5 -42 8 0)
31-
if echo "$output" | grep -q "Maximum.*8" && echo "$output" | grep -q "Minimum.*-42"; then
32-
echo "Test Case 2 (Mixed) PASSED"
33-
((passed_tests++))
34-
else
35-
echo "Test Case 2 (Mixed) FAILED"
36-
fi
37-
38-
# Test Case 3: Single element
39-
((total_tests++))
40-
output=$(./q1_exec 100)
41-
if echo "$output" | grep -q "Maximum.*100" && echo "$output" | grep -q "Minimum.*100"; then
42-
echo "Test Case 3 (Single Element) PASSED"
43-
((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
4424
else
45-
echo "Test Case 3 (Single Element) FAILED"
25+
echo "✅ Compilation successful for q${num}.c"
4626
fi
4727

48-
echo "----------------------------------------"
49-
echo "Summary: $passed_tests / $total_tests tests passed."
50-
5128
# Cleanup
52-
rm q1_exec
53-
exit 0
29+
rm -f "q${num}.out" compile.log
30+
exit 0

tests/testq10.sh

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,30 @@
11
#!/bin/bash
22

3-
# Test script for Q10
4-
# The program should take two strings and concatenate them without using library functions.
5-
6-
gcc src/q10.c -o q10_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: Two simple words
16-
((total_tests++))
17-
output=$(./q10_exec "hello" "world")
18-
if echo "$output" | grep -q "helloworld"; then
19-
echo "Test Case 1 (Simple Words) PASSED"
20-
((passed_tests++))
21-
else
22-
echo "Test Case 1 (Simple Words) 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
2317
fi
2418

25-
# Test Case 2: One empty string
26-
((total_tests++))
27-
output=$(./q10_exec "test" "")
28-
if echo "$output" | grep -q "test"; then
29-
echo "Test Case 2 (One Empty) PASSED"
30-
((passed_tests++))
31-
else
32-
echo "Test Case 2 (One Empty) FAILED"
33-
fi
34-
35-
# Test Case 3: Strings with spaces (must be quoted)
36-
((total_tests++))
37-
output=$(./q10_exec "first part" " second part")
38-
if echo "$output" | grep -q "first part second part"; then
39-
echo "Test Case 3 (With Spaces) PASSED"
40-
((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
4124
else
42-
echo "Test Case 3 (With Spaces) FAILED"
25+
echo "✅ Compilation successful for q${num}.c"
4326
fi
4427

45-
echo "----------------------------------------"
46-
echo "Summary: $passed_tests / $total_tests tests passed."
47-
48-
rm q10_exec
49-
exit 0
28+
# Cleanup
29+
rm -f "q${num}.out" compile.log
30+
exit 0

tests/testq11.sh

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,30 @@
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
2417
fi
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
4324
else
44-
echo "Test Case 3 (Negative) FAILED"
25+
echo "✅ Compilation successful for q${num}.c"
4526
fi
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

tests/testq12.sh

Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,30 @@
11
#!/bin/bash
22

3-
# Test script for Q12
4-
# This tests the isPalindrome function.
5-
6-
gcc src/q12.c -o q12_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: Is a palindrome
16-
((total_tests++))
17-
output=$(./q12_exec "racecar")
18-
if echo "$output" | grep -iq "is a palindrome"; then
19-
echo "Test Case 1 (Is Palindrome) PASSED"
20-
((passed_tests++))
21-
else
22-
echo "Test Case 1 (Is Palindrome) 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
2317
fi
2418

25-
# Test Case 2: Is not a palindrome
26-
((total_tests++))
27-
output=$(./q12_exec "computer")
28-
if echo "$output" | grep -iq "is not a palindrome"; then
29-
echo "Test Case 2 (Not Palindrome) PASSED"
30-
((passed_tests++))
31-
else
32-
echo "Test Case 2 (Not Palindrome) FAILED"
33-
fi
34-
35-
# Test Case 3: Single character (is a palindrome)
36-
((total_tests++))
37-
output=$(./q12_exec "a")
38-
if echo "$output" | grep -iq "is a palindrome"; then
39-
echo "Test Case 3 (Single Char) PASSED"
40-
((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
4124
else
42-
echo "Test Case 3 (Single Char) FAILED"
25+
echo "✅ Compilation successful for q${num}.c"
4326
fi
4427

45-
echo "----------------------------------------"
46-
echo "Summary: $passed_tests / $total_tests tests passed."
47-
48-
rm q12_exec
49-
exit 0
28+
# Cleanup
29+
rm -f "q${num}.out" compile.log
30+
exit 0

0 commit comments

Comments
 (0)