Question
IN C++ ONLY This assignment introduces you to a simple form of unit testing as a mechanism for exploring the string API. Focus on learning
IN C++ ONLY 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 assignment 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 assignment, 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).
The string Mantra
How you read and interpret object-oriented code is important when it comes to understanding the difference between data types, variables, and the "values" that a variable represents. Take a look at the following code.
string name = "Jimi Hendrix";
You are familiar with that syntax, but as we delve into objects we want to emphasize a particular way of translating that from "computerese" to English. When reading the above code, you should say to yourself "name is a string whose contents are the words Jimi Hendrix." Say that sentence aloud while reading the line of code above. Seriously, say it aloud about three or four times:
"name is a string whose contents are the words Jimi Hendrix. name is a string whose contents are the words Jimi Hendrix. name is a string whose contents are the words Jimi Hendrix. name is a string whose contents are the words Jimi Hendrix."
Doing this will help hammer home the way you should translate this line of code (and make anyone near you think there is something seriously wrong with you).
Instructions
A skeletal test suite (a collection of functions) has been provided for you in StringTests_main.cpp. Notice that the functions in this code are defined below main.cpp. In this case, each function is performming a single test on a string. The function will need to return the result of the test.
Your job: complete the function implementations using the string API such that all tests pass. You should not modify the contents of main in this lab, and instead only implement the test functions.
You should start by reading the body of the function called runAllTests() in order to see what your functions must accomplish. For example, the function stringLength() must return the length of the string "Now" using the string API. Take a look at the function stringLength() 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. Once you get all functions to print "PASSED", do your victory lap!
Hints
We recommend that you complete the functions in the order in which they are called in runAllTests() .
If you have trouble getting a function to pass its test, use cout statements to help you troubleshoot what your code is doing.
The last four functions may require some extra effort (especially middleName() and substitute() ).
Leverage the string API as much as you can. Explore the string API to see how the functions work. The documentation may be somewhat confusing; thus, if there is something you don't understand, be sure to ask!
Functional Requirements
You must not modify the contents of main() or runAllTests().
#include#include #include using namespace std; void runAllTests(); int main() { cout << "Testing your functions... "; runAllTests(); // Exit program. return 0; } // A generic test function, that simply prints "PASSED" if b is true // and otherwise prints false. Do not modify this function. void test(string message, bool b) { cout << setw(30); cout << message << ": "; cout << setw(15); if (b) cout << " PASSED "; else cout << " !!>FAILEDint stringLength(string s) { return s.length(); } // Returns the character of a string at a given index char charAt(string s, int index) { return '\0'; // stub } // Returns a concatenation of strings left and right string stringAppend(string left, string right) { return ""; // stub } // Returns the result of inserting a string into another string stringInsert(string s, string toInsert, int index) { return ""; // stub } // Returns the first index of character c in string s int stringFind(string s, char c) { return 0; // stub } // Returns part of a string string stringSubstring(string s, int index, int length) { return ""; // stub } // Replaces part of a string string stringReplace(string s, string textToReplace, string replaceWith) { return ""; // stub } // Returns the first name, given a full name string firstName(string s) { return ""; // stub } // Returns the middle name, given a full name string middleName(string s) { return ""; // stub } // Returns the last name, given a full name string lastName(string s) { return ""; // stub } // Returns a string substituting character target with character replacement string substitute(string s, char target, char replacement) { return ""; // stub } // Test suite. You should read, but not modify, this function. void runAllTests() { test( "Testing length()", stringLength("Now") == 3 ); test( "Testing length()", stringLength("Tablet") == 6 ); test( "Testing at()", charAt("Elephant", 3) == 'p' ); test( "Testing at()", charAt("Giraffe", 2) == 'r' ); test( "Testing append()", stringAppend("There's a ", "natural mystic.") == "There's a natural mystic." ); test( "Testing append()", stringAppend("It's the ", "eye of the tiger.") == "It's the eye of the tiger." ); test( "Testing insert()", stringInsert("If you carefully.", "listen ", 7) == "If you listen carefully." ); test( "Testing insert()", stringInsert("carefully.", "Watch ", 0) == "Watch carefully." ); test( "Testing find()", stringFind("Have to face reality now.", 'o') == 6 ); test( "Testing find()", stringFind("Have to face reality now.", 'e') == 3 ); test( "Testing substr()", stringSubstring("Such a natural mystic", 7, 7) == "natural" ); test( "Testing substr()", stringSubstring("Such a natural mystic", 0, 4) == "Such" ); test( "Testing replace()", stringReplace("Strings are not the way", "Strings", "Things") == "Things are not the way" ); test( "Testing replace()", stringReplace("Show me the things", "things", "way") == "Show me the way" ); test( "Testing firstName()", firstName("Robert Nesta Marley") == "Robert" ); test( "Testing firstName()", firstName("William Henry Harrison") == "William" ); test( "Testing firstName()", firstName("Chester A Arthur") == "Chester" ); test( "Testing middleName()", middleName("Robert Nesta Marley") == "Nesta" ); test( "Testing middleName()", middleName("William Henry Harrison") == "Henry" ); test( "Testing middleName()", middleName("Chester A Arthur") == "A" ); test( "Testing lastName()", lastName("Robert Nesta Marley") == "Marley" ); test( "Testing lastName()", lastName("William Henry Harrison") == "Harrison" ); test( "Testing lastName()", lastName("Chester A Arthur") == "Arthur" ); test( "Testing substitute()", substitute("The Gxxgle", 'x', 'o') == "The Google" ); test( "Testing substitute()", substitute("$chool of Mine$", '$', 's') == "school of Mines" ); test( "Testing substitute()", substitute(substitute("D--", '-', '+'), 'D', 'C') == "C++" ); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started