CSCI 200 - Fall 2024
Foundational Programming Concepts & Design

Lab 3B - Strings Test Suite

This lab is due by Monday, October 07, 2024, Before Class.
As with all labs you may, and are encouraged, to pair program a solution to this lab. If you choose to pair program a solution, be sure that you individually understand how to generate the correct solution.

Jump To: Rubric Submission


Concepts


This assignment introduces you to a simple form of "unit testing" as a mechanism for exploring the string API. Focus on learning what unit tests are, what an "API" is, and what you can do with string objects.


APIs (Application Programming Interface)


As you can see written above, the acronym API stands for Application Programming Interface. But what does that mean? In a nutshell, an API describes what you can do with a particular library or object that you are provided (or that you create). It describes how your code can "interface with" or "use" a particular library or object.

For example, the string API consists of member functions that tell you how long the string is, allow you to capitalize the string, tell you whether it contains a specific character, or allow you to extract a part of the string.

While the API really is the programmatic components that you can actually use, we often rely on API documentation to discover what you can do with a particular library or object. For example, in this lab you will use the string API, and you will need to look up some API documentation about how you can use string objects.


Unit Testing


As entire books have been written on unit testing, we will merely introduce the topic here. A "unit test" is a small piece of code designed to test a specific part of a program's functionality. In other words, they are bits of code that test the functionality of other code!

For this lab, that's all the preliminary information you really need to know about unit testing. You will actually write the unit tests, eventually getting the entire test suite to pass (at which point you should go outside and run a victory lap).


Instructions


A skeletal test suite (a collection of functions) has been provided for you in string_tests.zip. Notice that the functions in this code are defined across multiple files. The functions are grouped into files based on their application. In this case, each string function is performing a single test on a string. The function will need to return the result of the test.

When you first run the program, you will see output similar to below:

Testing your functions...

Test #  1                      Testing string_length():     PASSED   
Test #  2                      Testing string_length():     PASSED   
Test #  3                      Testing string_length():     PASSED   
Test #  4                      Testing string_length():     PASSED   
Test #  5                      Testing string_length():     PASSED

TODO: implement string_char_at("Elephant", 3)
Test #  6                     Testing string_char_at():  !!>FAILED<!! Returned: "" != Expected: "p"
TODO: implement string_char_at("Giraffe", 2)
Test #  7                     Testing string_char_at():  !!>FAILED<!! Returned: "" != Expected: "r"
TODO: implement string_char_at("Armadillo", 4)
Test #  8                     Testing string_char_at():  !!>FAILED<!! Returned: "" != Expected: "d"

TODO: implement string_append("There's a ", "natural mystic.")
Test #  9                      Testing string_append():  !!>FAILED<!! Returned: "There's a " != Expected: "There's a natural mystic."
TODO: implement string_append("It's the ", "eye of the tiger.")
Test # 10                      Testing string_append():  !!>FAILED<!! Returned: "It's the " != Expected: "It's the eye of the tiger."
TODO: implement string_append("Some", "thing")
Test # 11                      Testing string_append():  !!>FAILED<!! Returned: "Some" != Expected: "Something"
TODO: implement string_append("", "Nothing to something.")
Test # 12                      Testing string_append():  !!>FAILED<!! Returned: "" != Expected: "Nothing to something."
TODO: implement string_append("Not adding anything.", "")
Test # 13                      Testing string_append():     PASSED
TODO: implement string_append("", "")
Test # 14                      Testing string_append():     PASSED

TODO: implement string_insert("If you carefully.", "listen ", 7)
Test # 15                      Testing string_insert():  !!>FAILED<!! Returned: "If you carefully." != Expected: "If you listen carefully."
TODO: implement string_insert("carefully.", "Watch ", 0)
Test # 16                      Testing string_insert():  !!>FAILED<!! Returned: "carefully." != Expected: "Watch carefully."
TODO: implement string_insert("Iner", "t", 2)
Test # 17                      Testing string_insert():  !!>FAILED<!! Returned: "Iner" != Expected: "Inter"
TODO: implement string_insert("", "TA DA!", 0)
Test # 18                      Testing string_insert():  !!>FAILED<!! Returned: "" != Expected: "TA DA!"
TODO: implement string_insert("TA DA!", "", 0)
Test # 19                      Testing string_insert():     PASSED
TODO: implement string_insert("", "", 0)
Test # 20                      Testing string_insert():     PASSED

TODO: implement string_find("Have to face reality now.", 'o')
Test # 21                        Testing string_find():  !!>FAILED<!! Returned: "0" != Expected: "6"
TODO: implement string_find("Have to face reality now.", 'e')
Test # 22                        Testing string_find():  !!>FAILED<!! Returned: "0" != Expected: "3"
TODO: implement string_find("Have to face reality now.", 'r')
Test # 23                        Testing string_find():  !!>FAILED<!! Returned: "0" != Expected: "13"
TODO: implement string_find("Have to face reality now.", 'a')
Test # 24                        Testing string_find():  !!>FAILED<!! Returned: "0" != Expected: "1"
TODO: implement string_find("Have to face reality now.", 'q')
Test # 25                        Testing string_find():  !!>FAILED<!! Returned: "0" != Expected: "18446744073709551615"
TODO: implement string_find("Have to face reality now.", 'h')
Test # 26                        Testing string_find():  !!>FAILED<!! Returned: "0" != Expected: "18446744073709551615"
TODO: implement string_find("", '?')
Test # 27                        Testing string_find():  !!>FAILED<!! Returned: "0" != Expected: "18446744073709551615"

TODO: implement string_substring("Such a natural mystic", 7, 7)
Test # 28                   Testing string_substring():  !!>FAILED<!! Returned: "Such a natural mystic" != Expected: "natural"
TODO: implement string_substring("Such a natural mystic", 8, 2)
Test # 29                   Testing string_substring():  !!>FAILED<!! Returned: "Such a natural mystic" != Expected: "at"
TODO: implement string_substring("Such a natural mystic", 0, 4)
Test # 30                   Testing string_substring():  !!>FAILED<!! Returned: "Such a natural mystic" != Expected: "Such"
TODO: implement string_substring("Something to nothing.", 0, 0)
Test # 31                   Testing string_substring():  !!>FAILED<!! Returned: "Something to nothing." != Expected: ""
TODO: implement string_substring("Something to nothing.", 10, 0)
Test # 32                   Testing string_substring():  !!>FAILED<!! Returned: "Something to nothing." != Expected: ""
TODO: implement string_substring("", 0, 0)
Test # 33                   Testing string_substring():     PASSED

TODO: implement string_replace("Strings are not the way", "Strings", "Things)"
Test # 34                     Testing string_replace():  !!>FAILED<!! Returned: "Strings are not the way" != Expected: "Things are not the way"
TODO: implement string_replace("Strings are not the way", "not the", "the)"
Test # 35                     Testing string_replace():  !!>FAILED<!! Returned: "Strings are not the way" != Expected: "Strings are the way"
TODO: implement string_replace("Show me the things", "things", "way)"
Test # 36                     Testing string_replace():  !!>FAILED<!! Returned: "Show me the things" != Expected: "Show me the way"
TODO: implement string_replace("Show me the things", "car", "way)"
Test # 37                     Testing string_replace():     PASSED
TODO: implement string_replace("Show me the things", "the ", ")"
Test # 38                     Testing string_replace():  !!>FAILED<!! Returned: "Show me the things" != Expected: "Show me things"
TODO: implement string_replace("", "now what?", "hmm)"
Test # 39                     Testing string_replace():     PASSED
TODO: implement string_replace("", "now", ")"
Test # 40                     Testing string_replace():     PASSED

TODO: implement string_first_word("The quick brown fox jumped over the lazy dog")
Test # 41                  Testing string_first_word():  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "The"
TODO: implement string_first_word("A man a plan a canal Panama")
Test # 42                  Testing string_first_word():  !!>FAILED<!! Returned: "A man a plan a canal Panama" != Expected: "A"
TODO: implement string_first_word("I have the hang of this")
Test # 43                  Testing string_first_word():  !!>FAILED<!! Returned: "I have the hang of this" != Expected: "I"
TODO: implement string_first_word("Testing string_first_word()")
Test # 44                  Testing string_first_word():  !!>FAILED<!! Returned: "Testing string_first_word()" != Expected: "Testing"
TODO: implement string_first_word("Single")
Test # 45                  Testing string_first_word():     PASSED
TODO: implement string_first_word("Uh oh")
Test # 46                  Testing string_first_word():  !!>FAILED<!! Returned: "Uh oh" != Expected: "Uh"
TODO: implement string_first_word("")
Test # 47                  Testing string_first_word():     PASSED

TODO: implement string_remove_first_word("The quick brown fox jumped over the lazy dog)"
Test # 48           Testing string_remove_first_word():  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "quick brown fox jumped over the lazy dog"
TODO: implement string_remove_first_word("Testing string_remove_first_word())"
Test # 49           Testing string_remove_first_word():  !!>FAILED<!! Returned: "Testing string_remove_first_word()" != Expected: "string_remove_first_word()"
TODO: implement string_remove_first_word("Goodbye)"
Test # 50           Testing string_remove_first_word():  !!>FAILED<!! Returned: "Goodbye" != Expected: ""
TODO: implement string_remove_first_word(")"
Test # 51           Testing string_remove_first_word():     PASSED
TODO: implement string_remove_first_word("The quick brown fox jumped over the lazy dog)"
TODO: implement string_remove_first_word("The quick brown fox jumped over the lazy dog)"
Test # 52     Testing string_remove_first_word() twice:  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "brown fox jumped over the lazy dog"
TODO: implement string_remove_first_word("Testing string_remove_first_word())"
Test # 53     Testing string_remove_first_word() twice:  !!>FAILED<!! Returned: "Testing string_remove_first_word()" != Expected: ""
TODO: implement string_remove_first_word("Goodbye)"
TODO: implement string_remove_first_word("Goodbye)"
Test # 54     Testing string_remove_first_word() twice:  !!>FAILED<!! Returned: "Goodbye" != Expected: ""

TODO: implement string_second_word("The quick brown fox jumped over the lazy dog")
Test # 55                 Testing string_second_word():  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "quick"
TODO: implement string_second_word("A man a plan a canal Panama")
Test # 56                 Testing string_second_word():  !!>FAILED<!! Returned: "A man a plan a canal Panama" != Expected: "man"
TODO: implement string_second_word("Testing string_second_word()")
Test # 57                 Testing string_second_word():  !!>FAILED<!! Returned: "Testing string_second_word()" != Expected: "string_second_word()"
TODO: implement string_second_word("I have the hang of this")
Test # 58                 Testing string_second_word():  !!>FAILED<!! Returned: "I have the hang of this" != Expected: "have"
TODO: implement string_second_word("Uh oh")
Test # 59                 Testing string_second_word():  !!>FAILED<!! Returned: "Uh oh" != Expected: "oh"
TODO: implement string_second_word("Single")
Test # 60                 Testing string_second_word():  !!>FAILED<!! Returned: "Single" != Expected: ""
TODO: implement string_second_word("")
Test # 61                 Testing string_second_word():     PASSED

TODO: implement string_third_word("The quick brown fox jumped over the lazy dog")
Test # 62                  Testing string_third_word():  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "brown"
TODO: implement string_third_word("A man a plan a canal Panama")
Test # 63                  Testing string_third_word():  !!>FAILED<!! Returned: "A man a plan a canal Panama" != Expected: "a"
TODO: implement string_third_word("I have the hang of this")
Test # 64                  Testing string_third_word():  !!>FAILED<!! Returned: "I have the hang of this" != Expected: "the"
TODO: implement string_third_word("Uh oh no")
Test # 65                  Testing string_third_word():  !!>FAILED<!! Returned: "Uh oh no" != Expected: "no"
TODO: implement string_third_word("Uh oh")
Test # 66                  Testing string_third_word():  !!>FAILED<!! Returned: "Uh oh" != Expected: ""
TODO: implement string_third_word("Single")
Test # 67                  Testing string_third_word():  !!>FAILED<!! Returned: "Single" != Expected: ""
TODO: implement string_third_word("")
Test # 68                  Testing string_third_word():     PASSED

TODO: implement string_nth_word("The quick brown fox jumped over the lazy dog", 1)
Test # 69                   Testing string_nth_word(1):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "The"
TODO: implement string_nth_word("The quick brown fox jumped over the lazy dog", 2)
Test # 70                   Testing string_nth_word(2):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "quick"
TODO: implement string_nth_word("The quick brown fox jumped over the lazy dog", 3)
Test # 71                   Testing string_nth_word(3):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "brown"
TODO: implement string_nth_word("The quick brown fox jumped over the lazy dog", 4)
Test # 72                   Testing string_nth_word(4):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "fox"
TODO: implement string_nth_word("The quick brown fox jumped over the lazy dog", 5)
Test # 73                   Testing string_nth_word(5):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "jumped"
TODO: implement string_nth_word("The quick brown fox jumped over the lazy dog", 6)
Test # 74                   Testing string_nth_word(6):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "over"
TODO: implement string_nth_word("The quick brown fox jumped over the lazy dog", 7)
Test # 75                   Testing string_nth_word(7):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "the"
TODO: implement string_nth_word("The quick brown fox jumped over the lazy dog", 8)
Test # 76                   Testing string_nth_word(8):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "lazy"
TODO: implement string_nth_word("The quick brown fox jumped over the lazy dog", 9)
Test # 77                   Testing string_nth_word(9):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "dog"
TODO: implement string_nth_word("The quick brown fox jumped over the lazy dog", 10)
Test # 78                  Testing string_nth_word(10):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: ""
TODO: implement string_nth_word("The quick brown fox jumped over the lazy dog", 11)
Test # 79                  Testing string_nth_word(11):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: ""
TODO: implement string_nth_word("", 1)
Test # 80                   Testing string_nth_word(1):     PASSED
TODO: implement string_nth_word("", 2)
Test # 81                   Testing string_nth_word(2):     PASSED
TODO: implement string_nth_word("", 3)
Test # 82                   Testing string_nth_word(3):     PASSED
TODO: implement string_nth_word("", 10)
Test # 83                  Testing string_nth_word(10):     PASSED

TODO: implement string_tokenize("The quick brown fox jumped over the lazy dog", ' ')
Test # 84                    Testing string_tokenize():  !!>FAILED<!! Returned: {} != Expected: {"The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"}
TODO: implement string_tokenize("The@quick@brown@fox@jumped@over@the@lazy@dog", '@')
Test # 85                    Testing string_tokenize():  !!>FAILED<!! Returned: {} != Expected: {"The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"}
TODO: implement string_tokenize("The quick brown fox jumped over the lazy dog", '*')
Test # 86                    Testing string_tokenize():  !!>FAILED<!! Returned: {} != Expected: {"The quick brown fox jumped over the lazy dog"}
TODO: implement string_tokenize("", '*')
Test # 87                    Testing string_tokenize():  !!>FAILED<!! Returned: {} != Expected: {""}

TODO: implement string_substitute("The Gxxgle", 'x', 'o')
Test # 88                  Testing string_substitute():  !!>FAILED<!! Returned: "The Gxxgle" != Expected: "The Google"
TODO: implement string_substitute("$chool of Mine$", '$', 's')
Test # 89                  Testing string_substitute():  !!>FAILED<!! Returned: "$chool of Mine$" != Expected: "school of Mines"
TODO: implement string_substitute("$chool of Mine$", '+', '*')
Test # 90                  Testing string_substitute():     PASSED
TODO: implement string_substitute("D--", '-', '+')
TODO: implement string_substitute("D--", 'D', 'C')
Test # 91            Testing string_substitute() twice:  !!>FAILED<!! Returned: "D--" != Expected: "C++"

TODO: implement string_to_lower("This SHOULD be LOWER case")
Test # 92                    Testing string_to_lower():  !!>FAILED<!! Returned: "This SHOULD be LOWER case" != Expected: "this should be lower case"
TODO: implement string_to_lower("MNASDF874792L[]//.;[	],")
Test # 93                    Testing string_to_lower():  !!>FAILED<!! Returned: "MNASDF874792L[]//.;[	]," != Expected: "mnasdf874792l[]//.;[	],"
TODO: implement string_to_lower("C++")
Test # 94                    Testing string_to_lower():  !!>FAILED<!! Returned: "C++" != Expected: "c++"
TODO: implement string_to_lower("this is already lower case")
Test # 95                    Testing string_to_lower():     PASSED
TODO: implement string_to_lower("1234567890,./;'[]")
Test # 96                    Testing string_to_lower():     PASSED

TODO: implement string_to_upper("This SHOULD be upper case")
Test # 97                    Testing string_to_upper():  !!>FAILED<!! Returned: "This SHOULD be upper case" != Expected: "THIS SHOULD BE UPPER CASE"
TODO: implement string_to_upper("mnasdf874792l[]//.;[	],")
Test # 98                    Testing string_to_upper():  !!>FAILED<!! Returned: "mnasdf874792l[]//.;[	]," != Expected: "MNASDF874792L[]//.;[	],"
TODO: implement string_to_upper("c++")
Test # 99                    Testing string_to_upper():  !!>FAILED<!! Returned: "c++" != Expected: "C++"
TODO: implement string_to_upper("THIS IS ALREADY UPPER CASE")
Test #100                    Testing string_to_upper():     PASSED
TODO: implement string_to_upper("1234567890,./;'[]")
Test #101                    Testing string_to_upper():     PASSED

TODO: implement string_compare("C++", "c++")
Test #102                     Testing string_compare():  !!>FAILED<!! Returned: "0" != Expected: "-1"
TODO: implement string_compare("C++", "C++")
Test #103                     Testing string_compare():     PASSED
TODO: implement string_compare("c++", "c++")
Test #104                     Testing string_compare():     PASSED
TODO: implement string_compare("c++", "C++")
Test #105                     Testing string_compare():  !!>FAILED<!! Returned: "0" != Expected: "1"
TODO: implement string_compare("short", "shorter")
Test #106                     Testing string_compare():  !!>FAILED<!! Returned: "0" != Expected: "-1"
TODO: implement string_compare("longer", "long")
Test #107                     Testing string_compare():  !!>FAILED<!! Returned: "0" != Expected: "1"
TODO: implement string_compare("after", "later")
Test #108                     Testing string_compare():  !!>FAILED<!! Returned: "0" != Expected: "-1"
TODO: implement string_compare("later", "after")
Test #109                     Testing string_compare():  !!>FAILED<!! Returned: "0" != Expected: "1"
TODO: implement string_compare("G", "g")
Test #110                     Testing string_compare():  !!>FAILED<!! Returned: "0" != Expected: "-1"
TODO: implement string_compare("g", "G")
Test #111                     Testing string_compare():  !!>FAILED<!! Returned: "0" != Expected: "1"
TODO: implement string_compare("GoOse", "Bye")
Test #112                     Testing string_compare():  !!>FAILED<!! Returned: "0" != Expected: "1"
TODO: implement string_compare("Equal", "Equal")
Test #113                     Testing string_compare():     PASSED
TODO: implement string_compare("Not Empty", "")
Test #114                     Testing string_compare():  !!>FAILED<!! Returned: "0" != Expected: "1"
TODO: implement string_compare("", "Not Empty")
Test #115                     Testing string_compare():  !!>FAILED<!! Returned: "0" != Expected: "-1"
TODO: implement string_compare("", "")
Test #116                     Testing string_compare():     PASSED

Tests Passed:  31 / 116 (27%)

Not all tests are passing, errors remain...

Your job: complete each TODO using the string API such that all tests pass. You should not modify the contents of run_all_tests() in this lab, and instead only insert code at each TODO statement. When you have finished the task, remove the TODO comment.

You should start by reading the body of the function called run_all_tests() in order to see what your functions must accomplish. For example, the function string_length() must return the length of the string "Now" using the string API. Take a look at the function string_length() to see an example of a successful implementation.

When your program prints PASSED for a given unit test instead of FAILED, then you know that your function implementation for that test is complete.


Hints



Functional Requirements



Grading Rubric


Your submission will be graded according to the following rubric:

PointsRequirement Description
0.70Fully meets specifications
0.15Submitted correctly by Monday, October 07, 2024, Before Class
0.15Best Practices and Style Guide followed
1.00Total Points


Lab Submission


Always, always, ALWAYS update the header comments at the top of your main.cpp file. And if you ever get stuck, remember that there is LOTS of help available.

Zip together your string_functions.cpp file(s) and name the zip file L3B.zip. Upload this zip file to Canvas under L3B.

This lab is due by Monday, October 07, 2024, Before Class.
As with all labs you may, and are encouraged, to pair program a solution to this lab. If you choose to pair program a solution, be sure that you individually understand how to generate the correct solution.