CSCI 261 - Programming Concepts - Maynooth 2022

Lab 2D - Strings Test Suite

This lab is due by Friday, July 01, 2022, 11:59 PM.
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.


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 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 the following output:

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_char_at():  !!>FAILED<!! Returned: "" != Expected: "p"
Test #  5           Testing string_char_at():  !!>FAILED<!! Returned: "" != Expected: "r"
Test #  6           Testing string_char_at():  !!>FAILED<!! Returned: "" != Expected: "d"
Test #  7            Testing string_append():  !!>FAILED<!! Returned: "There's a " != Expected: "There's a natural mystic."
Test #  8            Testing string_append():  !!>FAILED<!! Returned: "It's the " != Expected: "It's the eye of the tiger."
Test #  9            Testing string_insert():  !!>FAILED<!! Returned: "If you carefully." != Expected: "If you listen carefully."
Test # 10            Testing string_insert():  !!>FAILED<!! Returned: "carefully." != Expected: "Watch carefully."
Test # 11              Testing string_find():  !!>FAILED<!! Returned: "-1" != Expected: "6"
Test # 12              Testing string_find():  !!>FAILED<!! Returned: "-1" != Expected: "3"
Test # 13              Testing string_find():     PASSED
Test # 14         Testing string_substring():  !!>FAILED<!! Returned: "Such a natural mystic" != Expected: "natural"
Test # 15         Testing string_substring():  !!>FAILED<!! Returned: "Such a natural mystic" != Expected: "Such"
Test # 16           Testing string_replace():  !!>FAILED<!! Returned: "Strings are not the way" != Expected: "Things are not the way"
Test # 17           Testing string_replace():  !!>FAILED<!! Returned: "Show me the things" != Expected: "Show me the way"
Test # 18           Testing string_replace():     PASSED
Test # 19        Testing string_first_word():  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "The"
Test # 20        Testing string_first_word():  !!>FAILED<!! Returned: "A man a plan a canal Panama" != Expected: "A"
Test # 21        Testing string_first_word():  !!>FAILED<!! Returned: "I have the hang of this" != Expected: "I"
Test # 22        Testing string_first_word():     PASSED
Test # 23        Testing string_first_word():  !!>FAILED<!! Returned: "Uh oh" != Expected: "Uh"
Test # 24 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"
Test # 25 Testing string_remove_first_word():  !!>FAILED<!! Returned: "Goodbye" != Expected: ""
Test # 26 Testing string_remove_first_word():  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "brown fox jumped over the lazy dog"
Test # 27       Testing string_second_word():  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "quick"
Test # 28       Testing string_second_word():  !!>FAILED<!! Returned: "A man a plan a canal Panama" != Expected: "man"
Test # 29       Testing string_second_word():  !!>FAILED<!! Returned: "I have the hang of this" != Expected: "have"
Test # 30       Testing string_second_word():  !!>FAILED<!! Returned: "Single" != Expected: ""
Test # 31       Testing string_second_word():  !!>FAILED<!! Returned: "Uh oh" != Expected: "oh"
Test # 32        Testing string_third_word():  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "brown"
Test # 33        Testing string_third_word():  !!>FAILED<!! Returned: "A man a plan a canal Panama" != Expected: "a"
Test # 34        Testing string_third_word():  !!>FAILED<!! Returned: "I have the hang of this" != Expected: "the"
Test # 35        Testing string_third_word():  !!>FAILED<!! Returned: "Single" != Expected: ""
Test # 36        Testing string_third_word():  !!>FAILED<!! Returned: "Uh oh" != Expected: ""
Test # 37         Testing string_nth_word(1):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "The"
Test # 38         Testing string_nth_word(2):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "quick"
Test # 39         Testing string_nth_word(3):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "brown"
Test # 40         Testing string_nth_word(4):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "fox"
Test # 41         Testing string_nth_word(5):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "jumped"
Test # 42         Testing string_nth_word(6):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "over"
Test # 43         Testing string_nth_word(7):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "the"
Test # 44         Testing string_nth_word(8):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "lazy"
Test # 45         Testing string_nth_word(9):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: "dog"
Test # 46        Testing string_nth_word(10):  !!>FAILED<!! Returned: "The quick brown fox jumped over the lazy dog" != Expected: ""
Test # 47        Testing string_substitute():  !!>FAILED<!! Returned: "The Gxxgle" != Expected: "The Google"
Test # 48        Testing string_substitute():  !!>FAILED<!! Returned: "$chool of Mine$" != Expected: "school of Mines"
Test # 49        Testing string_substitute():  !!>FAILED<!! Returned: "D--" != Expected: "C++"
Test # 50          Testing string_to_lower():  !!>FAILED<!! Returned: "This SHOULD be LOWER case" != Expected: "this should be lower case"
Test # 51          Testing string_to_lower():  !!>FAILED<!! Returned: "MNASDF874792L[]//.;[     ]," != Expected: "mnasdf874792l[]//.;[  ],"
Test # 52          Testing string_to_lower():  !!>FAILED<!! Returned: "C++" != Expected: "c++"
Test # 53          Testing string_to_upper():  !!>FAILED<!! Returned: "This SHOULD be upper case" != Expected: "THIS SHOULD BE UPPER CASE"
Test # 54          Testing string_to_upper():  !!>FAILED<!! Returned: "mnasdf874792l[]//.;[     ]," != Expected: "MNASDF874792L[]//.;[  ],"
Test # 55          Testing string_to_upper():  !!>FAILED<!! Returned: "c++" != Expected: "C++"
Test # 56           Testing string_compare():  !!>FAILED<!! Returned: "0" != Expected: "-1"
Test # 57           Testing string_compare():     PASSED
Test # 58           Testing string_compare():  !!>FAILED<!! Returned: "0" != Expected: "1"
Test # 59           Testing string_compare():  !!>FAILED<!! Returned: "0" != Expected: "-1"
Test # 60           Testing string_compare():  !!>FAILED<!! Returned: "0" != Expected: "1"

Tests Passed:   7 /  60 (12%)

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



Lab Submission


Submit your string_functions.cpp file(s).

You will submit your solution to this lab with the rest of Set2. Detailed instructions for doing this are posted in Assignment 2.


This lab is due by Friday, July 01, 2022, 11:59 PM.
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.