a gleam implementation of a CS assignment originally written in cpp

feat: shuffle files to cleanup the repo and added test suite

+3 -7
.gitignore
···
-
#ignore all files
-
*
-
-
# Include project specific files
-
!lab66.cpp
-
!coversheet.md
-
!.gitignore
+
lab66
+
lab66.output
+
build/
OT.txt test/OT.txt
lab66.cpp src/lab66.cpp
-3
lab66.input
···
-
Genesis
-
1
-
1
-2
lab66_exp.output
···
-
Please enter the reference of the verse you would like
-
the book: the chapter: the verse: GENESIS 1:1 In the beginning God created the heaven and the earth.
+13 -6
makefile
···
-
all: lab66 test clean
+
all: lab66 test
+
+
lab66: src/lab66.cpp
+
mkdir -p build && g++ src/lab66.cpp -Wall -o build/lab66
-
lab66: lab66.cpp
-
g++ lab66.cpp -Wall -o lab66
+
test: lab66
+
cd test && ./test.sh
+
+
exec:
+
./build/lab66
-
test:
-
./test.sh
+
run: lab66 exec
clean:
-
rm -f lab66
+
rm -rf build verses.txt
+
+
-53
test.sh
···
-
#!/bin/bash
-
temp_file="lab66.output"
-
inp_file="lab66.input"
-
-
rm -f verses.txt
-
./lab66 < "$inp_file" > "$temp_file"
-
verses=$(< verses.txt)
-
echo "verses.txt : ${verses} "
-
-
resp=$(./lab66 < "$inp_file")
-
-
if [ $? -eq 0 ] ; then
-
echo "Pass: Program exited zero"
-
else
-
echo "Fail: Program did not exit zero"
-
exit 1
-
fi
-
-
exp_resp=$(< lab66_exp.output)
-
-
-
if [[ $resp = $exp_resp ]]; then
-
echo "Pass: Program output matched expected string"
-
else
-
# Check if the strings are the same length
-
if [ "${#resp}" -ne "${#exp_resp}" ]; then
-
echo "Strings are different lengths : ${#resp} vs ${#exp_resp}"
-
fi
-
-
# Iterate over the characters in the strings
-
for (( i=0; i<${#resp}; i++ )); do
-
# Extract the character at position i from each string
-
char1="${resp:$i:1}"
-
char2="${exp_resp:$i:1}"
-
ch1_ascii=$(printf "%d" "'$char1")
-
ch2_ascii=$(printf "%d" "'$char2")
-
-
# Compare the characters
-
if [ "$char1" != "$char2" ]; then
-
echo "First difference at position $i: $ch1_ascii vs $ch2_ascii"
-
break
-
fi
-
done
-
echo Error : Expected \"$exp_resp\", your output was \"$resp\"
-
exit 1
-
fi
-
-
rm -f "$temp_file"
-
-
echo
-
echo "All tests passed."
-
-
exit 0
+232
test/test.sh
···
+
#!/bin/bash
+
+
# Colors for output
+
RED='\033[0;31m'
+
GREEN='\033[0;32m'
+
YELLOW='\033[1;33m'
+
BLUE='\033[0;34m'
+
NC='\033[0m' # No Color
+
+
# Test configuration
+
temp_file="lab66.output"
+
program="../build/lab66"
+
+
# Test counters
+
total_tests=0
+
passed_tests=0
+
+
# Cleanup function
+
cleanup() {
+
rm -f "$temp_file" verses.txt
+
}
+
+
# Error trap
+
trap cleanup EXIT ERR
+
+
# Function to print test results
+
print_result() {
+
local test_name="$1"
+
local result="$2"
+
local message="$3"
+
+
total_tests=$((total_tests + 1))
+
+
if [ "$result" = "PASS" ]; then
+
echo -e "${GREEN}[PASS]${NC} $test_name: $message"
+
passed_tests=$((passed_tests + 1))
+
else
+
echo -e "${RED}[FAIL]${NC} $test_name: $message"
+
fi
+
}
+
+
# Function to print test section header
+
print_section() {
+
echo -e "\n${BLUE}=== $1 ===${NC}"
+
}
+
+
# Build program if needed
+
if [ ! -f "$program" ]; then
+
print_section "Building Program"
+
cd .. && make lab66 > /dev/null 2>&1
+
build_result=$?
+
cd test
+
if [ $build_result -ne 0 ]; then
+
print_result "Program Build" "FAIL" "Could not build $program"
+
exit 1
+
fi
+
print_result "Program Build" "PASS" "Program built successfully"
+
fi
+
+
# Test 1: Program compilation/existence
+
if [ ! -f "$program" ]; then
+
print_section "Test 1: Program Availability"
+
print_result "Program Existence" "FAIL" "Program $program not found"
+
exit 1
+
fi
+
+
# Test 2: Basic Bible Verse Lookup Test (Genesis 1:1)
+
print_section "Basic Bible Verse Lookup Test"
+
+
# Test with known valid verse
+
basic_input="Genesis\n1\n1"
+
resp=$(echo -e "$basic_input" | "$program" 2>&1)
+
exit_code=$?
+
+
# Print the output
+
if [ -n "$resp" ]; then
+
echo "$resp"
+
else
+
echo "(No output produced)"
+
fi
+
echo
+
+
if [ $exit_code -ne 0 ]; then
+
print_result "Basic Execution" "FAIL" "Program exited with code $exit_code"
+
exit 1
+
else
+
print_result "Basic Execution" "PASS" "Program executed successfully"
+
fi
+
+
# Check if output contains required prompts
+
if [[ "$resp" == *"Please enter the reference of the verse you would like"* ]]; then
+
print_result "Initial Prompt" "PASS" "Program shows initial prompt"
+
else
+
print_result "Initial Prompt" "FAIL" "Program missing initial prompt"
+
fi
+
+
if [[ "$resp" == *"the book:"* ]]; then
+
print_result "Book Prompt" "PASS" "Program prompts for book"
+
else
+
print_result "Book Prompt" "FAIL" "Program missing book prompt"
+
fi
+
+
if [[ "$resp" == *"the chapter:"* ]]; then
+
print_result "Chapter Prompt" "PASS" "Program prompts for chapter"
+
else
+
print_result "Chapter Prompt" "FAIL" "Program missing chapter prompt"
+
fi
+
+
if [[ "$resp" == *"the verse:"* ]]; then
+
print_result "Verse Prompt" "PASS" "Program prompts for verse"
+
else
+
print_result "Verse Prompt" "FAIL" "Program missing verse prompt"
+
fi
+
+
# Check for verse output format
+
if [[ "$resp" == *"GENESIS 1:1"* ]]; then
+
print_result "Verse Format" "PASS" "Program outputs verse in correct format"
+
else
+
print_result "Verse Format" "FAIL" "Program missing or incorrect verse format"
+
fi
+
+
# Check if verses.txt file was created
+
if [ -f "verses.txt" ]; then
+
verses_content=$(cat verses.txt)
+
if [[ "$verses_content" == *"In the beginning God created the heaven and the earth"* ]]; then
+
print_result "File Output" "PASS" "verses.txt created with correct content"
+
else
+
print_result "File Output" "FAIL" "verses.txt has incorrect content"
+
fi
+
else
+
print_result "File Output" "FAIL" "verses.txt file not created"
+
fi
+
+
# Function to run verse lookup test
+
run_verse_test() {
+
local test_name="$1"
+
local book="$2"
+
local chapter="$3"
+
local verse="$4"
+
local expected_pattern="$5"
+
local should_create_file="$6"
+
+
# Clean up previous test
+
rm -f verses.txt
+
+
input="$book\n$chapter\n$verse"
+
output=$(echo -e "$input" | "$program" 2>&1)
+
exit_code=$?
+
+
if [ $exit_code -ne 0 ]; then
+
print_result "$test_name" "FAIL" "Program crashed (exit code $exit_code)"
+
return
+
fi
+
+
# Check if expected pattern is found in output
+
pattern_found=false
+
if [[ "$output" == *"$expected_pattern"* ]]; then
+
pattern_found=true
+
fi
+
+
# For tests expecting specific output, pattern must be found
+
if [ -n "$expected_pattern" ] && [ "$pattern_found" = "false" ]; then
+
print_result "$test_name" "FAIL" "Expected '$expected_pattern' not found in output"
+
return
+
fi
+
+
# Check file creation expectations
+
if [ "$should_create_file" = "true" ]; then
+
if [ -f "verses.txt" ] && [ "$pattern_found" = "true" ]; then
+
print_result "$test_name" "PASS" "Expected output found and file created"
+
elif [ ! -f "verses.txt" ]; then
+
print_result "$test_name" "FAIL" "Expected file verses.txt not created"
+
else
+
print_result "$test_name" "FAIL" "File created but expected output not found"
+
fi
+
else
+
if [ ! -f "verses.txt" ] && [ "$pattern_found" = "true" ]; then
+
print_result "$test_name" "PASS" "Expected error output and no file created"
+
elif [ -f "verses.txt" ]; then
+
print_result "$test_name" "FAIL" "verses.txt file should not be created for error cases"
+
else
+
print_result "$test_name" "FAIL" "Expected error message not found"
+
fi
+
fi
+
}
+
+
# Valid Verse Tests
+
print_section "Valid Verse Tests"
+
+
run_verse_test "Exodus 20:3" "Exodus" "20" "3" "EXODUS 20:3" true
+
run_verse_test "Psalm 23:1" "Psalm" "23" "1" "PSALM 23:1" true
+
+
# Error Case Tests
+
print_section "Error Case Tests"
+
+
run_verse_test "Invalid Book" "Matthew" "1" "1" "Matthew does not exist in the Old Testament" false
+
run_verse_test "Invalid Chapter" "Esther" "18" "3" "Chapter 18 does not exist in Esther" false
+
run_verse_test "Invalid Verse" "Psalm" "117" "5" "Verse 5 does not exist in Psalm 117" false
+
+
# Adversarial Testing Section
+
print_section "Adversarial Tests"
+
+
# Test edge cases
+
run_verse_test "Empty Book Name" "" "1" "1" "Please enter the reference" false
+
run_verse_test "Non-numeric Chapter" "Genesis" "abc" "1" "Please enter the reference" false
+
run_verse_test "Non-numeric Verse" "Genesis" "1" "xyz" "Please enter the reference" false
+
run_verse_test "Zero Chapter" "Genesis" "0" "1" "Please enter the reference" false
+
run_verse_test "Zero Verse" "Genesis" "1" "0" "Please enter the reference" false
+
run_verse_test "Negative Chapter" "Genesis" "-1" "1" "Please enter the reference" false
+
run_verse_test "Negative Verse" "Genesis" "1" "-1" "Please enter the reference" false
+
run_verse_test "Very Large Chapter" "Genesis" "999999" "1" "Please enter the reference" false
+
run_verse_test "Very Large Verse" "Genesis" "1" "999999" "Please enter the reference" false
+
+
# Test case sensitivity
+
run_verse_test "Lowercase Book" "genesis" "1" "1" "genesis does not exist in the Old Testament" false
+
run_verse_test "Mixed Case Book" "GEnesis" "1" "1" "GEnesis does not exist in the Old Testament" false
+
+
# Test two-word books
+
run_verse_test "First Samuel" "First Samuel" "1" "1" "FIRST SAMUEL 1:1" true
+
run_verse_test "Second Kings" "Second Kings" "1" "1" "SECOND KINGS 1:1" true
+
+
# Test summary
+
print_section "Test Summary"
+
echo -e "${BLUE}Tests passed: $passed_tests/$total_tests${NC}"
+
+
if [ $passed_tests -eq $total_tests ]; then
+
echo -e "${GREEN}All tests passed!${NC}"
+
exit 0
+
else
+
echo -e "${RED}Some tests failed!${NC}"
+
exit 1
+
fi