a gleam implementation of a CS assignment originally written in cpp

feat: implement loading file from argv and default location

Changed files
+82 -6
src
test
+30 -6
src/lab66.cpp
···
/*********************************************************
-
* Summary:
+
* Summary: find verses from the old testament
+
* appending them to file as found
*
-
* Author:
-
* Created:
+
* Author: Kieran Klukas
+
* Created: October 2025
*
********************************************************/
···
using namespace std;
-
int main() {
-
-
/* your code here */
+
int main(int argc, char **argv) {
+
+
ifstream OT;
+
string filename = "../test/OT.txt";
+
+
// get path to OT.txt if provided otherwise use default path
+
if (argc == 2) {
+
filename = argv[1];
+
}
+
+
OT.open(filename);
+
+
if (!OT.is_open()) {
+
cout << filename << ": failed to open file";
+
return 1;
+
}
+
+
string verse;
+
getline(OT, verse);
+
getline(OT, verse);
+
getline(OT, verse);
+
getline(OT, verse);
+
+
cout << verse;
+
+
OT.close();
return 0;
}
+52
test/test.sh
···
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
+
# Function to run file path test
+
run_file_path_test() {
+
local test_name="$1"
+
local file_path="$2"
+
local expected_pattern="$3"
+
local should_succeed="$4"
+
+
# Clean up previous test
+
rm -f verses.txt
+
+
# Provide verse lookup input for file path tests
+
input="Genesis\n1\n1"
+
+
if [ -n "$file_path" ]; then
+
output=$(echo -e "$input" | "$program" "$file_path" 2>&1)
+
else
+
output=$(echo -e "$input" | "$program" 2>&1)
+
fi
+
exit_code=$?
+
+
if [ "$should_succeed" = "true" ]; then
+
if [ $exit_code -eq 0 ]; then
+
print_result "$test_name" "PASS" "Program accepted file path successfully"
+
else
+
print_result "$test_name" "FAIL" "Program failed with exit code $exit_code: $output"
+
fi
+
else
+
if [ $exit_code -ne 0 ] && [[ "$output" == *"$expected_pattern"* ]]; then
+
print_result "$test_name" "PASS" "Program correctly rejected invalid file path"
+
elif [ $exit_code -eq 0 ]; then
+
print_result "$test_name" "FAIL" "Program should have failed with invalid file path"
+
else
+
print_result "$test_name" "FAIL" "Expected error message '$expected_pattern' not found: $output"
+
fi
+
fi
+
}
+
+
# Test with existing file (absolute path)
+
run_file_path_test "Existing File (Absolute)" "/Users/kierank/code/school/lab-6/test/OT.txt" "" true
+
+
# Test with existing file (relative path)
+
run_file_path_test "Existing File (Relative)" "OT.txt" "" true
+
+
# Test with non-existing file
+
run_file_path_test "Non-existing File" "nonexistent.txt" "failed to open file" false
+
+
# Test with invalid path
+
run_file_path_test "Invalid Path" "/invalid/path/file.txt" "failed to open file" false
+
+
# Test with empty path
+
run_file_path_test "Empty Path" "" "" true
+
# Test summary
print_section "Test Summary"
echo -e "${BLUE}Tests passed: $passed_tests/$total_tests${NC}"