Skip to content

Instantly share code, notes, and snippets.

@itsjohncs
Created November 5, 2012 07:33
Show Gist options
  • Save itsjohncs/4015811 to your computer and use it in GitHub Desktop.
Save itsjohncs/4015811 to your computer and use it in GitHub Desktop.
Classroom Session 6 - Test Harness Example: extract_name()
#include <iostream>
using namespace std;
/**
* @brief Extracts the name out of a greeting.
*
* For example, given the input "Hello, my name is John.", this function should
* return "John". Exclamatory statements should work as well ("... is John!").
* Also, any greeting may be used, ex: "Grettings fellow, my name is John!" will
* still return "John".
*
* @param sentence The greeting to extract the name from.
* @return The name and nothing else.
*/
string extract_name(string sentence) {
// TODO: Finish this function.
return "John";
}
int main() {
string result;
result = extract_name("Hello, my name is John.");
if (result != "John") {
cout << "FAIL: Failed on input 'Hello, my name is John'. Returned '"
<< result << "'.";
}
// TODO: Finish this Test Harness.
return 0;
}
#include <iostream>
using namespace std;
/**
* @brief Extracts the name out of a greeting.
*
* For example, given the input "Hello, my name is John.", this function should
* return "John". Exclamatory statements should work as well ("... is John!").
* Also, any greeting may be used, ex: "Grettings fellow, my name is John!" will
* still return "John".
*
* @param sentence The greeting to extract the name from.
* @return The name and nothing else.
*/
string extract_name(string sentence) {
return "John";
}
int main() {
string result;
result = extract_name("Hello, my name is John.");
if (result != "John") {
cout << "FAIL: Failed on input 'Hello, my name is John.'. Returned '"
<< result << "'." << endl;
} else {
cout << "SUCCESS: Succeeded on input 'Hello, my name is John.'. "
<< "Returned '" << result << "'." << endl;
}
result = extract_name("Hello, my name is Billy.");
if (result != "Billy") {
cout << "FAIL: Failed on input 'Hello, my name is Billy.'. Returned '"
<< result << "'." << endl;
} else {
cout << "SUCCESS: Succeeded on input 'Hello, my name is Billy.'. "
<< "Returned '" << result << "'." << endl;
}
result = extract_name("Hello, my name is John!");
if (result != "John") {
cout << "FAIL: Failed on input 'Hello, my name is John!'. Returned '"
<< result << "'." << endl;
} else {
cout << "SUCCESS: Succeeded on input 'Hello, my name is John!'. "
<< "Returned '" << result << "'." << endl;
}
result = extract_name("Greetings human, my name is Joseph!");
if (result != "Joseph") {
cout << "FAIL: Failed on input 'Greetings human, my name is Joseph!'. "
<< "Returned '" << result << "'." << endl;
} else {
cout << "SUCCESS: Succeeded on input 'Greetings human, my name is "
<< "Joseph'. Returned '" << result << "'." << endl;
}
return 0;
}
#include <iostream>
using namespace std;
/**
* @brief Extracts the name out of a greeting.
*
* For example, given the input "Hello, my name is John.", this function should
* return "John". Exclamatory statements should work as well ("... is John!").
* Also, any greeting may be used, ex: "Grettings fellow, my name is John!" will
* still return "John".
*
* @param sentence The greeting to extract the name from.
* @return The name and nothing else.
*/
string extract_name(string sentence) {
return "John";
}
void test_extract_name(string sentence, string expected) {
string result = extract_name(sentence);
if (result != expected) {
cout << "FAILURE: Failed on input '" << sentence << "'. Expected '"
<< expected << "' but got '" << result << "'." << endl;
} else {
cout << "SUCCESS: Succeeded on input '" << sentence << "'. Got '"
<< result << "'." << endl;
}
}
int main() {
test_extract_name("Hello, my name is John.", "John");
test_extract_name("Hello, my name is Billy.", "Billy");
test_extract_name("Hello, my name is John!", "John");
test_extract_name("Greetings human, my name is Joseph!", "Joseph");
return 0;
}
#include <iostream>
using namespace std;
/**
* @brief Extracts the name out of a greeting.
*
* For example, given the input "Hello, my name is John.", this function should
* return "John". Exclamatory statements should work as well ("... is John!").
* Also, any greeting may be used, ex: "Grettings fellow, my name is John!" will
* still return "John".
*
* @param sentence The greeting to extract the name from.
* @return The name and nothing else.
*/
string extract_name(string sentence) {
// I create a constant here because I use this string twice in the following
// code and I did not want to repeat myself.
const string MY_NAME_IS = " my name is ";
// Find the " my name is " portion of the sentence.
int first_marker = sentence.find(MY_NAME_IS);
// Grab from the end of " my name is " to the end of the string.
string name = sentence.substr(first_marker + MY_NAME_IS.size());
// Cut off the ending punctuation mark.
name = name.substr(0, name.size() - 1);
return name;
}
void test_extract_name(string sentence, string expected) {
string result = extract_name(sentence);
if (result != expected) {
cout << "FAILURE: Failed on input '" << sentence << "'. Expected '"
<< expected << "' but got '" << result << "'." << endl;
} else {
cout << "SUCCESS: Succeeded on input '" << sentence << "'. Got '"
<< result << "'." << endl;
}
}
int main() {
test_extract_name("Hello, my name is John.", "John");
test_extract_name("Hello, my name is Billy.", "Billy");
test_extract_name("Hello, my name is John!", "John");
test_extract_name("Greetings human, my name is Joseph!", "Joseph");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment