CSCI 261 Programming Concepts (C++)

Winter/Spring 2012

Homework 23: String Test

Right-click me and select "Save target as..." to save.

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 methods (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 datatypes, 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. Doing so will make anyone near you think that there is something seriously wrong with you.

"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."

Instructions

A skeletal test suite (a collection of functions) has been provided for you in main.cpp. Complete the function implementations using the string API such that all tests pass. Note that for this assignment, you should not modify the contents of main 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" and does not print "FAILED," then you know that your test suite passes and your program is complete.

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 six functions may require some extra effort (especially middleName, capitalize and substitute).

Leverage the string API as much as you can. Explore the string API to see how the functions work. Since the documentation may be somewhat confusing, if there is something you don't understand, be sure to ask!

Requirements and Rubric

A friendly message from The Terminator, our grading program

Hello ag *bzzzt* again. I will check for the following:

Your program must not print "FAILED" in order to receive full credit.

Each function you implement must use the string API appropriately.

This work is worth 100 points.

Requirement Points Notes
Place your name in the comment header in main.cpp 3
Correct submission of src directory as a .zip file. 2
Correctly passes all tests (5 points each). 95

Concepts Exercised: unit testing, functions, objects, APIs, application, repeating tasks