#include "test_suite.h" #include "string_functions.h" #include #include using namespace std; const int MESSAGE_WIDTH = 45; int test_int(int& testNum, const string MESSAGE, const int LHS, const int RHS) { cout << "Test #" << setw(3) << right << ++testNum; cout << setw(MESSAGE_WIDTH) << right; cout << MESSAGE << ": "; cout << left; if (LHS == RHS) { cout << " PASSED \n"; } else { cout << " !!>FAILEDFAILEDFAILEDFAILED& LHS, const vector& RHS) { cout << "Test #" << setw(3) << right << ++testNum; cout << setw(MESSAGE_WIDTH) << right; cout << MESSAGE << ": "; cout << left; bool vectorsMatch = (LHS.size() == RHS.size()); if (vectorsMatch) { for (unsigned int i = 0; i < LHS.size(); i++) { if (LHS.at(i) != RHS.at(i)) { vectorsMatch = false; break; } } } if (vectorsMatch) { cout << " PASSED \n"; } else { cout << " !!>FAILED tokens1 = {"The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"}; totalPassed += test_vector_string( totalNumTests, "Testing string_tokenize()", string_tokenize("The quick brown fox jumped over the lazy dog", ' '), tokens1); totalPassed += test_vector_string( totalNumTests, "Testing string_tokenize()", string_tokenize("The@quick@brown@fox@jumped@over@the@lazy@dog", '@'), tokens1); vector tokens2 = {"The quick brown fox jumped over the lazy dog"}; totalPassed += test_vector_string( totalNumTests, "Testing string_tokenize()", string_tokenize("The quick brown fox jumped over the lazy dog", '*'), tokens2); vector tokens3 = {""}; totalPassed += test_vector_string(totalNumTests, "Testing string_tokenize()", string_tokenize("", '*'), tokens3); cout << endl; totalPassed += test_string(totalNumTests, "Testing string_substitute()", string_substitute("The Gxxgle", 'x', 'o'), "The Google"); totalPassed += test_string(totalNumTests, "Testing string_substitute()", string_substitute("$chool of Mine$", '$', 's'), "school of Mines"); totalPassed += test_string(totalNumTests, "Testing string_substitute()", string_substitute("$chool of Mine$", '+', '*'), "$chool of Mine$"); totalPassed += test_string( totalNumTests, "Testing string_substitute() twice", string_substitute(string_substitute("D--", '-', '+'), 'D', 'C'), "C++"); cout << endl; totalPassed += test_string(totalNumTests, "Testing string_to_lower()", string_to_lower("This SHOULD be LOWER case"), "this should be lower case"); totalPassed += test_string(totalNumTests, "Testing string_to_lower()", string_to_lower("MNASDF874792L[]//.;[\t],"), "mnasdf874792l[]//.;[\t],"); totalPassed += test_string(totalNumTests, "Testing string_to_lower()", string_to_lower("C++"), "c++"); totalPassed += test_string(totalNumTests, "Testing string_to_lower()", string_to_lower("this is already lower case"), "this is already lower case"); totalPassed += test_string(totalNumTests, "Testing string_to_lower()", string_to_lower("1234567890,./;'[]"), "1234567890,./;'[]"); cout << endl; totalPassed += test_string(totalNumTests, "Testing string_to_upper()", string_to_upper("This SHOULD be upper case"), "THIS SHOULD BE UPPER CASE"); totalPassed += test_string(totalNumTests, "Testing string_to_upper()", string_to_upper("mnasdf874792l[]//.;[\t],"), "MNASDF874792L[]//.;[\t],"); totalPassed += test_string(totalNumTests, "Testing string_to_upper()", string_to_upper("c++"), "C++"); totalPassed += test_string(totalNumTests, "Testing string_to_upper()", string_to_upper("THIS IS ALREADY UPPER CASE"), "THIS IS ALREADY UPPER CASE"); totalPassed += test_string(totalNumTests, "Testing string_to_upper()", string_to_upper("1234567890,./;'[]"), "1234567890,./;'[]"); cout << endl; totalPassed += test_int(totalNumTests, "Testing string_compare()", string_compare("C++", "c++"), -1); totalPassed += test_int(totalNumTests, "Testing string_compare()", string_compare("C++", "C++"), 0); totalPassed += test_int(totalNumTests, "Testing string_compare()", string_compare("c++", "c++"), 0); totalPassed += test_int(totalNumTests, "Testing string_compare()", string_compare("c++", "C++"), 1); totalPassed += test_int(totalNumTests, "Testing string_compare()", string_compare("short", "shorter"), -1); totalPassed += test_int(totalNumTests, "Testing string_compare()", string_compare("longer", "long"), 1); totalPassed += test_int(totalNumTests, "Testing string_compare()", string_compare("after", "later"), -1); totalPassed += test_int(totalNumTests, "Testing string_compare()", string_compare("later", "after"), 1); totalPassed += test_int(totalNumTests, "Testing string_compare()", string_compare("G", "g"), -1); totalPassed += test_int(totalNumTests, "Testing string_compare()", string_compare("g", "G"), 1); totalPassed += test_int(totalNumTests, "Testing string_compare()", string_compare("GoOse", "Bye"), 1); totalPassed += test_int(totalNumTests, "Testing string_compare()", string_compare("Equal", "Equal"), 0); totalPassed += test_int(totalNumTests, "Testing string_compare()", string_compare("Not Empty", ""), 1); totalPassed += test_int(totalNumTests, "Testing string_compare()", string_compare("", "Not Empty"), -1); totalPassed += test_int(totalNumTests, "Testing string_compare()", string_compare("", ""), 0); cout << endl; cout << "Tests Passed: " << setw(3) << right << totalPassed << " / " << setw(3) << totalNumTests << " (" << setprecision(0) << fixed << totalPassed * 100.0f / totalNumTests << "%)" << endl << endl; return (totalPassed == totalNumTests); }