···11-Please enter the reference of the verse you would like
22- the book: the chapter: the verse: GENESIS 1:1 In the beginning God created the heaven and the earth.
···11-#!/bin/bash
22-temp_file="lab66.output"
33-inp_file="lab66.input"
44-55-rm -f verses.txt
66-./lab66 < "$inp_file" > "$temp_file"
77-verses=$(< verses.txt)
88-echo "verses.txt : ${verses} "
99-1010-resp=$(./lab66 < "$inp_file")
1111-1212-if [ $? -eq 0 ] ; then
1313- echo "Pass: Program exited zero"
1414-else
1515- echo "Fail: Program did not exit zero"
1616- exit 1
1717-fi
1818-1919-exp_resp=$(< lab66_exp.output)
2020-2121-2222-if [[ $resp = $exp_resp ]]; then
2323- echo "Pass: Program output matched expected string"
2424-else
2525- # Check if the strings are the same length
2626- if [ "${#resp}" -ne "${#exp_resp}" ]; then
2727- echo "Strings are different lengths : ${#resp} vs ${#exp_resp}"
2828- fi
2929-3030- # Iterate over the characters in the strings
3131- for (( i=0; i<${#resp}; i++ )); do
3232- # Extract the character at position i from each string
3333- char1="${resp:$i:1}"
3434- char2="${exp_resp:$i:1}"
3535- ch1_ascii=$(printf "%d" "'$char1")
3636- ch2_ascii=$(printf "%d" "'$char2")
3737-3838- # Compare the characters
3939- if [ "$char1" != "$char2" ]; then
4040- echo "First difference at position $i: $ch1_ascii vs $ch2_ascii"
4141- break
4242- fi
4343- done
4444- echo Error : Expected \"$exp_resp\", your output was \"$resp\"
4545- exit 1
4646-fi
4747-4848-rm -f "$temp_file"
4949-5050-echo
5151-echo "All tests passed."
5252-5353-exit 0
+232
test/test.sh
···11+#!/bin/bash
22+33+# Colors for output
44+RED='\033[0;31m'
55+GREEN='\033[0;32m'
66+YELLOW='\033[1;33m'
77+BLUE='\033[0;34m'
88+NC='\033[0m' # No Color
99+1010+# Test configuration
1111+temp_file="lab66.output"
1212+program="../build/lab66"
1313+1414+# Test counters
1515+total_tests=0
1616+passed_tests=0
1717+1818+# Cleanup function
1919+cleanup() {
2020+ rm -f "$temp_file" verses.txt
2121+}
2222+2323+# Error trap
2424+trap cleanup EXIT ERR
2525+2626+# Function to print test results
2727+print_result() {
2828+ local test_name="$1"
2929+ local result="$2"
3030+ local message="$3"
3131+3232+ total_tests=$((total_tests + 1))
3333+3434+ if [ "$result" = "PASS" ]; then
3535+ echo -e "${GREEN}[PASS]${NC} $test_name: $message"
3636+ passed_tests=$((passed_tests + 1))
3737+ else
3838+ echo -e "${RED}[FAIL]${NC} $test_name: $message"
3939+ fi
4040+}
4141+4242+# Function to print test section header
4343+print_section() {
4444+ echo -e "\n${BLUE}=== $1 ===${NC}"
4545+}
4646+4747+# Build program if needed
4848+if [ ! -f "$program" ]; then
4949+ print_section "Building Program"
5050+ cd .. && make lab66 > /dev/null 2>&1
5151+ build_result=$?
5252+ cd test
5353+ if [ $build_result -ne 0 ]; then
5454+ print_result "Program Build" "FAIL" "Could not build $program"
5555+ exit 1
5656+ fi
5757+ print_result "Program Build" "PASS" "Program built successfully"
5858+fi
5959+6060+# Test 1: Program compilation/existence
6161+if [ ! -f "$program" ]; then
6262+ print_section "Test 1: Program Availability"
6363+ print_result "Program Existence" "FAIL" "Program $program not found"
6464+ exit 1
6565+fi
6666+6767+# Test 2: Basic Bible Verse Lookup Test (Genesis 1:1)
6868+print_section "Basic Bible Verse Lookup Test"
6969+7070+# Test with known valid verse
7171+basic_input="Genesis\n1\n1"
7272+resp=$(echo -e "$basic_input" | "$program" 2>&1)
7373+exit_code=$?
7474+7575+# Print the output
7676+if [ -n "$resp" ]; then
7777+ echo "$resp"
7878+else
7979+ echo "(No output produced)"
8080+fi
8181+echo
8282+8383+if [ $exit_code -ne 0 ]; then
8484+ print_result "Basic Execution" "FAIL" "Program exited with code $exit_code"
8585+ exit 1
8686+else
8787+ print_result "Basic Execution" "PASS" "Program executed successfully"
8888+fi
8989+9090+# Check if output contains required prompts
9191+if [[ "$resp" == *"Please enter the reference of the verse you would like"* ]]; then
9292+ print_result "Initial Prompt" "PASS" "Program shows initial prompt"
9393+else
9494+ print_result "Initial Prompt" "FAIL" "Program missing initial prompt"
9595+fi
9696+9797+if [[ "$resp" == *"the book:"* ]]; then
9898+ print_result "Book Prompt" "PASS" "Program prompts for book"
9999+else
100100+ print_result "Book Prompt" "FAIL" "Program missing book prompt"
101101+fi
102102+103103+if [[ "$resp" == *"the chapter:"* ]]; then
104104+ print_result "Chapter Prompt" "PASS" "Program prompts for chapter"
105105+else
106106+ print_result "Chapter Prompt" "FAIL" "Program missing chapter prompt"
107107+fi
108108+109109+if [[ "$resp" == *"the verse:"* ]]; then
110110+ print_result "Verse Prompt" "PASS" "Program prompts for verse"
111111+else
112112+ print_result "Verse Prompt" "FAIL" "Program missing verse prompt"
113113+fi
114114+115115+# Check for verse output format
116116+if [[ "$resp" == *"GENESIS 1:1"* ]]; then
117117+ print_result "Verse Format" "PASS" "Program outputs verse in correct format"
118118+else
119119+ print_result "Verse Format" "FAIL" "Program missing or incorrect verse format"
120120+fi
121121+122122+# Check if verses.txt file was created
123123+if [ -f "verses.txt" ]; then
124124+ verses_content=$(cat verses.txt)
125125+ if [[ "$verses_content" == *"In the beginning God created the heaven and the earth"* ]]; then
126126+ print_result "File Output" "PASS" "verses.txt created with correct content"
127127+ else
128128+ print_result "File Output" "FAIL" "verses.txt has incorrect content"
129129+ fi
130130+else
131131+ print_result "File Output" "FAIL" "verses.txt file not created"
132132+fi
133133+134134+# Function to run verse lookup test
135135+run_verse_test() {
136136+ local test_name="$1"
137137+ local book="$2"
138138+ local chapter="$3"
139139+ local verse="$4"
140140+ local expected_pattern="$5"
141141+ local should_create_file="$6"
142142+143143+ # Clean up previous test
144144+ rm -f verses.txt
145145+146146+ input="$book\n$chapter\n$verse"
147147+ output=$(echo -e "$input" | "$program" 2>&1)
148148+ exit_code=$?
149149+150150+ if [ $exit_code -ne 0 ]; then
151151+ print_result "$test_name" "FAIL" "Program crashed (exit code $exit_code)"
152152+ return
153153+ fi
154154+155155+ # Check if expected pattern is found in output
156156+ pattern_found=false
157157+ if [[ "$output" == *"$expected_pattern"* ]]; then
158158+ pattern_found=true
159159+ fi
160160+161161+ # For tests expecting specific output, pattern must be found
162162+ if [ -n "$expected_pattern" ] && [ "$pattern_found" = "false" ]; then
163163+ print_result "$test_name" "FAIL" "Expected '$expected_pattern' not found in output"
164164+ return
165165+ fi
166166+167167+ # Check file creation expectations
168168+ if [ "$should_create_file" = "true" ]; then
169169+ if [ -f "verses.txt" ] && [ "$pattern_found" = "true" ]; then
170170+ print_result "$test_name" "PASS" "Expected output found and file created"
171171+ elif [ ! -f "verses.txt" ]; then
172172+ print_result "$test_name" "FAIL" "Expected file verses.txt not created"
173173+ else
174174+ print_result "$test_name" "FAIL" "File created but expected output not found"
175175+ fi
176176+ else
177177+ if [ ! -f "verses.txt" ] && [ "$pattern_found" = "true" ]; then
178178+ print_result "$test_name" "PASS" "Expected error output and no file created"
179179+ elif [ -f "verses.txt" ]; then
180180+ print_result "$test_name" "FAIL" "verses.txt file should not be created for error cases"
181181+ else
182182+ print_result "$test_name" "FAIL" "Expected error message not found"
183183+ fi
184184+ fi
185185+}
186186+187187+# Valid Verse Tests
188188+print_section "Valid Verse Tests"
189189+190190+run_verse_test "Exodus 20:3" "Exodus" "20" "3" "EXODUS 20:3" true
191191+run_verse_test "Psalm 23:1" "Psalm" "23" "1" "PSALM 23:1" true
192192+193193+# Error Case Tests
194194+print_section "Error Case Tests"
195195+196196+run_verse_test "Invalid Book" "Matthew" "1" "1" "Matthew does not exist in the Old Testament" false
197197+run_verse_test "Invalid Chapter" "Esther" "18" "3" "Chapter 18 does not exist in Esther" false
198198+run_verse_test "Invalid Verse" "Psalm" "117" "5" "Verse 5 does not exist in Psalm 117" false
199199+200200+# Adversarial Testing Section
201201+print_section "Adversarial Tests"
202202+203203+# Test edge cases
204204+run_verse_test "Empty Book Name" "" "1" "1" "Please enter the reference" false
205205+run_verse_test "Non-numeric Chapter" "Genesis" "abc" "1" "Please enter the reference" false
206206+run_verse_test "Non-numeric Verse" "Genesis" "1" "xyz" "Please enter the reference" false
207207+run_verse_test "Zero Chapter" "Genesis" "0" "1" "Please enter the reference" false
208208+run_verse_test "Zero Verse" "Genesis" "1" "0" "Please enter the reference" false
209209+run_verse_test "Negative Chapter" "Genesis" "-1" "1" "Please enter the reference" false
210210+run_verse_test "Negative Verse" "Genesis" "1" "-1" "Please enter the reference" false
211211+run_verse_test "Very Large Chapter" "Genesis" "999999" "1" "Please enter the reference" false
212212+run_verse_test "Very Large Verse" "Genesis" "1" "999999" "Please enter the reference" false
213213+214214+# Test case sensitivity
215215+run_verse_test "Lowercase Book" "genesis" "1" "1" "genesis does not exist in the Old Testament" false
216216+run_verse_test "Mixed Case Book" "GEnesis" "1" "1" "GEnesis does not exist in the Old Testament" false
217217+218218+# Test two-word books
219219+run_verse_test "First Samuel" "First Samuel" "1" "1" "FIRST SAMUEL 1:1" true
220220+run_verse_test "Second Kings" "Second Kings" "1" "1" "SECOND KINGS 1:1" true
221221+222222+# Test summary
223223+print_section "Test Summary"
224224+echo -e "${BLUE}Tests passed: $passed_tests/$total_tests${NC}"
225225+226226+if [ $passed_tests -eq $total_tests ]; then
227227+ echo -e "${GREEN}All tests passed!${NC}"
228228+ exit 0
229229+else
230230+ echo -e "${RED}Some tests failed!${NC}"
231231+ exit 1
232232+fi