a gleam implementation of a CS assignment originally written in cpp
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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

+248 -71
+3 -7
.gitignore
··· 1 - #ignore all files 2 - * 3 - 4 - # Include project specific files 5 - !lab66.cpp 6 - !coversheet.md 7 - !.gitignore 1 + lab66 2 + lab66.output 3 + build/
OT.txt test/OT.txt
lab66.cpp src/lab66.cpp
-3
lab66.input
··· 1 - Genesis 2 - 1 3 - 1
-2
lab66_exp.output
··· 1 - Please enter the reference of the verse you would like 2 - the book: the chapter: the verse: GENESIS 1:1 In the beginning God created the heaven and the earth.
+13 -6
makefile
··· 1 - all: lab66 test clean 1 + all: lab66 test 2 + 3 + lab66: src/lab66.cpp 4 + mkdir -p build && g++ src/lab66.cpp -Wall -o build/lab66 2 5 3 - lab66: lab66.cpp 4 - g++ lab66.cpp -Wall -o lab66 6 + test: lab66 7 + cd test && ./test.sh 8 + 9 + exec: 10 + ./build/lab66 5 11 6 - test: 7 - ./test.sh 12 + run: lab66 exec 8 13 9 14 clean: 10 - rm -f lab66 15 + rm -rf build verses.txt 16 + 17 +
-53
test.sh
··· 1 - #!/bin/bash 2 - temp_file="lab66.output" 3 - inp_file="lab66.input" 4 - 5 - rm -f verses.txt 6 - ./lab66 < "$inp_file" > "$temp_file" 7 - verses=$(< verses.txt) 8 - echo "verses.txt : ${verses} " 9 - 10 - resp=$(./lab66 < "$inp_file") 11 - 12 - if [ $? -eq 0 ] ; then 13 - echo "Pass: Program exited zero" 14 - else 15 - echo "Fail: Program did not exit zero" 16 - exit 1 17 - fi 18 - 19 - exp_resp=$(< lab66_exp.output) 20 - 21 - 22 - if [[ $resp = $exp_resp ]]; then 23 - echo "Pass: Program output matched expected string" 24 - else 25 - # Check if the strings are the same length 26 - if [ "${#resp}" -ne "${#exp_resp}" ]; then 27 - echo "Strings are different lengths : ${#resp} vs ${#exp_resp}" 28 - fi 29 - 30 - # Iterate over the characters in the strings 31 - for (( i=0; i<${#resp}; i++ )); do 32 - # Extract the character at position i from each string 33 - char1="${resp:$i:1}" 34 - char2="${exp_resp:$i:1}" 35 - ch1_ascii=$(printf "%d" "'$char1") 36 - ch2_ascii=$(printf "%d" "'$char2") 37 - 38 - # Compare the characters 39 - if [ "$char1" != "$char2" ]; then 40 - echo "First difference at position $i: $ch1_ascii vs $ch2_ascii" 41 - break 42 - fi 43 - done 44 - echo Error : Expected \"$exp_resp\", your output was \"$resp\" 45 - exit 1 46 - fi 47 - 48 - rm -f "$temp_file" 49 - 50 - echo 51 - echo "All tests passed." 52 - 53 - exit 0
+232
test/test.sh
··· 1 + #!/bin/bash 2 + 3 + # Colors for output 4 + RED='\033[0;31m' 5 + GREEN='\033[0;32m' 6 + YELLOW='\033[1;33m' 7 + BLUE='\033[0;34m' 8 + NC='\033[0m' # No Color 9 + 10 + # Test configuration 11 + temp_file="lab66.output" 12 + program="../build/lab66" 13 + 14 + # Test counters 15 + total_tests=0 16 + passed_tests=0 17 + 18 + # Cleanup function 19 + cleanup() { 20 + rm -f "$temp_file" verses.txt 21 + } 22 + 23 + # Error trap 24 + trap cleanup EXIT ERR 25 + 26 + # Function to print test results 27 + print_result() { 28 + local test_name="$1" 29 + local result="$2" 30 + local message="$3" 31 + 32 + total_tests=$((total_tests + 1)) 33 + 34 + if [ "$result" = "PASS" ]; then 35 + echo -e "${GREEN}[PASS]${NC} $test_name: $message" 36 + passed_tests=$((passed_tests + 1)) 37 + else 38 + echo -e "${RED}[FAIL]${NC} $test_name: $message" 39 + fi 40 + } 41 + 42 + # Function to print test section header 43 + print_section() { 44 + echo -e "\n${BLUE}=== $1 ===${NC}" 45 + } 46 + 47 + # Build program if needed 48 + if [ ! -f "$program" ]; then 49 + print_section "Building Program" 50 + cd .. && make lab66 > /dev/null 2>&1 51 + build_result=$? 52 + cd test 53 + if [ $build_result -ne 0 ]; then 54 + print_result "Program Build" "FAIL" "Could not build $program" 55 + exit 1 56 + fi 57 + print_result "Program Build" "PASS" "Program built successfully" 58 + fi 59 + 60 + # Test 1: Program compilation/existence 61 + if [ ! -f "$program" ]; then 62 + print_section "Test 1: Program Availability" 63 + print_result "Program Existence" "FAIL" "Program $program not found" 64 + exit 1 65 + fi 66 + 67 + # Test 2: Basic Bible Verse Lookup Test (Genesis 1:1) 68 + print_section "Basic Bible Verse Lookup Test" 69 + 70 + # Test with known valid verse 71 + basic_input="Genesis\n1\n1" 72 + resp=$(echo -e "$basic_input" | "$program" 2>&1) 73 + exit_code=$? 74 + 75 + # Print the output 76 + if [ -n "$resp" ]; then 77 + echo "$resp" 78 + else 79 + echo "(No output produced)" 80 + fi 81 + echo 82 + 83 + if [ $exit_code -ne 0 ]; then 84 + print_result "Basic Execution" "FAIL" "Program exited with code $exit_code" 85 + exit 1 86 + else 87 + print_result "Basic Execution" "PASS" "Program executed successfully" 88 + fi 89 + 90 + # Check if output contains required prompts 91 + if [[ "$resp" == *"Please enter the reference of the verse you would like"* ]]; then 92 + print_result "Initial Prompt" "PASS" "Program shows initial prompt" 93 + else 94 + print_result "Initial Prompt" "FAIL" "Program missing initial prompt" 95 + fi 96 + 97 + if [[ "$resp" == *"the book:"* ]]; then 98 + print_result "Book Prompt" "PASS" "Program prompts for book" 99 + else 100 + print_result "Book Prompt" "FAIL" "Program missing book prompt" 101 + fi 102 + 103 + if [[ "$resp" == *"the chapter:"* ]]; then 104 + print_result "Chapter Prompt" "PASS" "Program prompts for chapter" 105 + else 106 + print_result "Chapter Prompt" "FAIL" "Program missing chapter prompt" 107 + fi 108 + 109 + if [[ "$resp" == *"the verse:"* ]]; then 110 + print_result "Verse Prompt" "PASS" "Program prompts for verse" 111 + else 112 + print_result "Verse Prompt" "FAIL" "Program missing verse prompt" 113 + fi 114 + 115 + # Check for verse output format 116 + if [[ "$resp" == *"GENESIS 1:1"* ]]; then 117 + print_result "Verse Format" "PASS" "Program outputs verse in correct format" 118 + else 119 + print_result "Verse Format" "FAIL" "Program missing or incorrect verse format" 120 + fi 121 + 122 + # Check if verses.txt file was created 123 + if [ -f "verses.txt" ]; then 124 + verses_content=$(cat verses.txt) 125 + if [[ "$verses_content" == *"In the beginning God created the heaven and the earth"* ]]; then 126 + print_result "File Output" "PASS" "verses.txt created with correct content" 127 + else 128 + print_result "File Output" "FAIL" "verses.txt has incorrect content" 129 + fi 130 + else 131 + print_result "File Output" "FAIL" "verses.txt file not created" 132 + fi 133 + 134 + # Function to run verse lookup test 135 + run_verse_test() { 136 + local test_name="$1" 137 + local book="$2" 138 + local chapter="$3" 139 + local verse="$4" 140 + local expected_pattern="$5" 141 + local should_create_file="$6" 142 + 143 + # Clean up previous test 144 + rm -f verses.txt 145 + 146 + input="$book\n$chapter\n$verse" 147 + output=$(echo -e "$input" | "$program" 2>&1) 148 + exit_code=$? 149 + 150 + if [ $exit_code -ne 0 ]; then 151 + print_result "$test_name" "FAIL" "Program crashed (exit code $exit_code)" 152 + return 153 + fi 154 + 155 + # Check if expected pattern is found in output 156 + pattern_found=false 157 + if [[ "$output" == *"$expected_pattern"* ]]; then 158 + pattern_found=true 159 + fi 160 + 161 + # For tests expecting specific output, pattern must be found 162 + if [ -n "$expected_pattern" ] && [ "$pattern_found" = "false" ]; then 163 + print_result "$test_name" "FAIL" "Expected '$expected_pattern' not found in output" 164 + return 165 + fi 166 + 167 + # Check file creation expectations 168 + if [ "$should_create_file" = "true" ]; then 169 + if [ -f "verses.txt" ] && [ "$pattern_found" = "true" ]; then 170 + print_result "$test_name" "PASS" "Expected output found and file created" 171 + elif [ ! -f "verses.txt" ]; then 172 + print_result "$test_name" "FAIL" "Expected file verses.txt not created" 173 + else 174 + print_result "$test_name" "FAIL" "File created but expected output not found" 175 + fi 176 + else 177 + if [ ! -f "verses.txt" ] && [ "$pattern_found" = "true" ]; then 178 + print_result "$test_name" "PASS" "Expected error output and no file created" 179 + elif [ -f "verses.txt" ]; then 180 + print_result "$test_name" "FAIL" "verses.txt file should not be created for error cases" 181 + else 182 + print_result "$test_name" "FAIL" "Expected error message not found" 183 + fi 184 + fi 185 + } 186 + 187 + # Valid Verse Tests 188 + print_section "Valid Verse Tests" 189 + 190 + run_verse_test "Exodus 20:3" "Exodus" "20" "3" "EXODUS 20:3" true 191 + run_verse_test "Psalm 23:1" "Psalm" "23" "1" "PSALM 23:1" true 192 + 193 + # Error Case Tests 194 + print_section "Error Case Tests" 195 + 196 + run_verse_test "Invalid Book" "Matthew" "1" "1" "Matthew does not exist in the Old Testament" false 197 + run_verse_test "Invalid Chapter" "Esther" "18" "3" "Chapter 18 does not exist in Esther" false 198 + run_verse_test "Invalid Verse" "Psalm" "117" "5" "Verse 5 does not exist in Psalm 117" false 199 + 200 + # Adversarial Testing Section 201 + print_section "Adversarial Tests" 202 + 203 + # Test edge cases 204 + run_verse_test "Empty Book Name" "" "1" "1" "Please enter the reference" false 205 + run_verse_test "Non-numeric Chapter" "Genesis" "abc" "1" "Please enter the reference" false 206 + run_verse_test "Non-numeric Verse" "Genesis" "1" "xyz" "Please enter the reference" false 207 + run_verse_test "Zero Chapter" "Genesis" "0" "1" "Please enter the reference" false 208 + run_verse_test "Zero Verse" "Genesis" "1" "0" "Please enter the reference" false 209 + run_verse_test "Negative Chapter" "Genesis" "-1" "1" "Please enter the reference" false 210 + run_verse_test "Negative Verse" "Genesis" "1" "-1" "Please enter the reference" false 211 + run_verse_test "Very Large Chapter" "Genesis" "999999" "1" "Please enter the reference" false 212 + run_verse_test "Very Large Verse" "Genesis" "1" "999999" "Please enter the reference" false 213 + 214 + # Test case sensitivity 215 + run_verse_test "Lowercase Book" "genesis" "1" "1" "genesis does not exist in the Old Testament" false 216 + run_verse_test "Mixed Case Book" "GEnesis" "1" "1" "GEnesis does not exist in the Old Testament" false 217 + 218 + # Test two-word books 219 + run_verse_test "First Samuel" "First Samuel" "1" "1" "FIRST SAMUEL 1:1" true 220 + run_verse_test "Second Kings" "Second Kings" "1" "1" "SECOND KINGS 1:1" true 221 + 222 + # Test summary 223 + print_section "Test Summary" 224 + echo -e "${BLUE}Tests passed: $passed_tests/$total_tests${NC}" 225 + 226 + if [ $passed_tests -eq $total_tests ]; then 227 + echo -e "${GREEN}All tests passed!${NC}" 228 + exit 0 229 + else 230 + echo -e "${RED}Some tests failed!${NC}" 231 + exit 1 232 + fi