diff --git a/L3B b/L3B deleted file mode 100755 index 3e1d990..0000000 Binary files a/L3B and /dev/null differ diff --git a/LICENSE.md b/LICENSE.md index 6259067..9b98f73 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,3 +1,5 @@ +All personal code of mine is licensed under the following CC0 license, however most of the code (all of main.cpp, all of test_suite.cpp, and all but the actual string logic in string_functions.cpp) was written by Jeffrey R. Paone and is licensed under the MIT license + # Creative Commons CC0 1.0 Universal CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED HEREUNDER. diff --git a/main.cpp b/main.cpp index d434ccd..6c5e9f4 100644 --- a/main.cpp +++ b/main.cpp @@ -1,23 +1,24 @@ /** * @file main.cpp * @author Tyler Beckman (tyler_beckman@mines.edu) - * @brief CSCI200 L3B - A program to test different string modification APIs in C++ + * @brief CSCI200 L3B - A program to test different string modification APIs in + * C++ * @version 1 * @date 2024-09-21 */ -#include - #include "test_suite.h" +#include + int main() { - std::cout << "Testing your functions..." << std::endl << std::endl; - if( run_all_tests() ) { - std::cout << "ALL TESTS PASSED!" << std::endl; - } else { - std::cout << "Not all tests are passing, errors remain..." << std::endl; - } + std::cout << "Testing your functions..." << std::endl << std::endl; + if (run_all_tests()) { + std::cout << "ALL TESTS PASSED!" << std::endl; + } else { + std::cout << "Not all tests are passing, errors remain..." << std::endl; + } - return 0; + return 0; } \ No newline at end of file diff --git a/main.o b/main.o deleted file mode 100644 index 5526e69..0000000 Binary files a/main.o and /dev/null differ diff --git a/string_functions.cpp b/string_functions.cpp index 7716abb..a68e829 100644 --- a/string_functions.cpp +++ b/string_functions.cpp @@ -4,141 +4,153 @@ using namespace std; -unsigned long string_length(const string STR) { - unsigned long result = -1; - result = STR.length(); // set result to the length of the string - return result; +unsigned long string_length(const string STR) { + unsigned long result = -1; + result = STR.length(); // set result to the length of the string + return result; } char string_char_at(const string STR, const int IDX) { - char result = '\0'; - result = STR.at(IDX); - std::cout << "TODO: implement string_char_at(\"" << STR << "\", " << IDX << ")" << std::endl; - return result; + char result = '\0'; + result = STR.at(IDX); + return result; } -string string_append(const string LEFT, const string RIGHT) { - string result = LEFT; - result = LEFT + RIGHT; - std::cout << "TODO: implement string_append(\"" << LEFT << "\", \"" << RIGHT << "\")" << std::endl; - return result; +string string_append(const string LEFT, const string RIGHT) { + string result = LEFT; + result = LEFT + RIGHT; + return result; } string string_insert(const string STR, const string TO_INSERT, const int IDX) { - string result = STR; - result.insert(IDX, TO_INSERT); - std::cout << "TODO: implement string_insert(\"" << STR << "\", \"" << TO_INSERT << "\", " << IDX << ")" << std::endl; - return result; + string result = STR; + result.insert(IDX, TO_INSERT); + return result; } -size_t string_find(const string STR, const char C) { - size_t result = 0; - result = STR.find_first_of(C); - std::cout << "TODO: implement string_find(\"" << STR << "\", '" << C << "')" << std::endl; - return result; +size_t string_find(const string STR, const char C) { + size_t result = 0; + result = STR.find_first_of(C); + return result; } string string_substring(const string STR, const int IDX, const int LEN) { - string result = STR; - result = STR.substr(IDX, LEN); - std::cout << "TODO: implement string_substring(\"" << STR << "\", " << IDX << ", " << LEN << ")" << std::endl; - return result; + string result = STR; + result = STR.substr(IDX, LEN); + return result; } -string string_replace(const string STR, const string TEXT_TO_REPLACE, const string REPLACE_WITH) { - string result = STR; - auto index = STR.find(TEXT_TO_REPLACE); - if (index != -1ul) { - result.replace(STR.find(TEXT_TO_REPLACE), TEXT_TO_REPLACE.length(), REPLACE_WITH); - } - std::cout << "TODO: implement string_replace(\"" << STR << "\", \"" << TEXT_TO_REPLACE << "\", \"" << REPLACE_WITH << ")\"" << std::endl; - return result; +string string_replace(const string STR, const string TEXT_TO_REPLACE, + const string REPLACE_WITH) { + string result = STR; + auto index = STR.find(TEXT_TO_REPLACE); + if (index != -1ul) { + result.replace(STR.find(TEXT_TO_REPLACE), TEXT_TO_REPLACE.length(), + REPLACE_WITH); + } + return result; } -string string_first_word(const string STR) { - string result = STR; - result = STR.substr(0, STR.find_first_of(' ')); - std::cout << "TODO: implement string_first_word(\"" << STR << "\")" << std::endl; - return result; +string string_first_word(const string STR) { + string result = STR; + result = STR.substr(0, STR.find_first_of(' ')); + return result; } -string string_remove_first_word(const string STR) { - string result = STR; - // TODO 08: set result to be the string with the first word removed - std::cout << "TODO: implement string_remove_first_word(\"" << STR << ")\"" << std::endl; - return result; +string string_remove_first_word(const string STR) { + string result = STR; + if (STR.find_first_of(' ') != -1ul) { + result = STR.substr(STR.find_first_of(' ') + 1, STR.length() - 1); + } else { + result = ""; + } + return result; } -string string_second_word(const string STR) { - string result = STR; - // TODO 09: set result to be the second word from the string - std::cout << "TODO: implement string_second_word(\"" << STR << "\")" << std::endl; - return result; +string string_second_word(const string STR) { + string result = STR; + result = string_remove_first_word(STR); + result = string_first_word(result); + return result; } -string string_third_word(const string STR) { - string result = STR; - // TODO 10: set result to be the third word from the string - std::cout << "TODO: implement string_third_word(\"" << STR << "\")" << std::endl; - return result; +string string_third_word(const string STR) { + string result = STR; + result = string_remove_first_word(STR); + result = string_remove_first_word(result); + result = string_first_word(result); + return result; } -string string_nth_word(const string STR, const int N) { - string result = STR; - // TODO 11: set result to be the nth word from the string - std::cout << "TODO: implement string_nth_word(\"" << STR << "\", " << N << ")" << std::endl; - return result; +string string_nth_word(const string STR, const int N) { + string result = STR; + for (int i = 0; i < N - 1; i++) { + result = string_remove_first_word(result); + } + result = string_first_word(result); + return result; } vector string_tokenize(const string STR, const char DELIMINATOR) { - vector result; - // TODO 12: split the string by the given deliminator - std::cout << "TODO: implement string_tokenize(\"" << STR << "\", '" << DELIMINATOR << "')" << std::endl; - return result; + vector result; + result = {""}; + for (unsigned long i = 0; i < STR.length(); i++) { + if (STR[i] == DELIMINATOR) { + result.push_back(""); + continue; + } + result[result.size() - 1] += STR[i]; + } + return result; } -string string_substitute(const string STR, const char TARGET, const char REPLACEMENT) { - string result = STR; - // TODO 13: set result to be the string with all instances of TARGET replaced - std::cout << "TODO: implement string_substitute(\"" << STR << "\", '" << TARGET << "', '" << REPLACEMENT << "')" << std::endl; - return result; +string string_substitute(const string STR, const char TARGET, + const char REPLACEMENT) { + string result = STR; + while (result.find(TARGET) != -1ul) { + result[result.find(TARGET)] = REPLACEMENT; + } + return result; } string string_to_lower(const string STR) { - string result = STR; - for (unsigned int i = 0; i < STR.length(); i++) { - char newChar = STR[i]; - if (newChar >= 'A' && newChar <= 'Z') { - newChar = (char) (newChar + 32); - } - result[i] = newChar; + string result = STR; + for (unsigned int i = 0; i < STR.length(); i++) { + char newChar = STR[i]; + if (newChar >= 'A' && newChar <= 'Z') { + newChar = (char)(newChar + 32); } - std::cout << "TODO: implement string_to_lower(\"" << STR << "\")" << std::endl; - return result; + result[i] = newChar; + } + std::cout << "TODO: implement string_to_lower(\"" << STR << "\")" + << std::endl; + return result; } string string_to_upper(const string STR) { - string result = STR; - for (unsigned int i = 0; i < STR.length(); i++) { - char newChar = STR[i]; - if (newChar >= 'a' && newChar <= 'z') { - newChar = (char) (newChar - 32); - } - result[i] = newChar; + string result = STR; + for (unsigned int i = 0; i < STR.length(); i++) { + char newChar = STR[i]; + if (newChar >= 'a' && newChar <= 'z') { + newChar = (char)(newChar - 32); } - std::cout << "TODO: implement string_to_upper(\"" << STR << "\")" << std::endl; - return result; + result[i] = newChar; + } + std::cout << "TODO: implement string_to_upper(\"" << STR << "\")" + << std::endl; + return result; } int string_compare(const string LHS, const string RHS) { - int result = 0; - if (LHS > RHS) { - result = 1; - } else { - if (LHS < RHS) { - result = -1; - } + int result = 0; + if (LHS > RHS) { + result = 1; + } else { + if (LHS < RHS) { + result = -1; } - std::cout << "TODO: implement string_compare(\"" << LHS << "\", \"" << RHS << "\")" << std::endl; - return result; + } + std::cout << "TODO: implement string_compare(\"" << LHS << "\", \"" << RHS + << "\")" << std::endl; + return result; } \ No newline at end of file diff --git a/string_functions.h b/string_functions.h index fb0e298..9a06000 100644 --- a/string_functions.h +++ b/string_functions.h @@ -28,19 +28,22 @@ char string_char_at(const std::string STR, const int IDX); std::string string_append(const std::string LEFT, const std::string RIGHT); /** - * @brief Returns the result of inserting a string into another at a given position + * @brief Returns the result of inserting a string into another at a given + * position * @param STR original string * @param TO_INSERT string to be inserted into STR * @param IDX location within STR to insert TO_INSERT * @return a new string with TO_INSERT inserted at index IDX of STR */ -std::string string_insert(const std::string STR, const std::string TO_INSERT, const int IDX); +std::string string_insert(const std::string STR, const std::string TO_INSERT, + const int IDX); /** * @brief Returns the first index of a character in a string * @param STR string to search within (the haystack) * @param C character to search for (the needle) - * @return if found, first position within STR that C is located. otherwise returns string::npos + * @return if found, first position within STR that C is located. otherwise + * returns string::npos */ size_t string_find(const std::string STR, const char C); @@ -51,7 +54,8 @@ size_t string_find(const std::string STR, const char C); * @param LEN length of substring to return * @return a substring of length LEN from STR */ -std::string string_substring(const std::string STR, const int IDX, const int LEN); +std::string string_substring(const std::string STR, const int IDX, + const int LEN); /** * @brief Replaces part of a string @@ -60,7 +64,9 @@ std::string string_substring(const std::string STR, const int IDX, const int LEN * @param REPLACE_WITH the new text to insert * @return modified string (if text found), otherwise the original string */ -std::string string_replace(const std::string STR, const std::string TEXT_TO_REPLACE, const std::string REPLACE_WITH); +std::string string_replace(const std::string STR, + const std::string TEXT_TO_REPLACE, + const std::string REPLACE_WITH); /** * @brief Returns the first word, given a sentence @@ -100,42 +106,48 @@ std::string string_nth_word(const std::string STR, const int N); /** * @brief Splits a string into a list of tokens deliminated by a given character - * + * * @param STR string to tokenize * @param DELIMINATOR character to split on - * @return vector list of all tokens in the order present in the original string + * @return vector list of all tokens in the order present in the + * original string */ -std::vector string_tokenize(const std::string STR, const char DELIMINATOR); +std::vector string_tokenize(const std::string STR, + const char DELIMINATOR); /** - * @brief Returns a string substituting target character with replacement character + * @brief Returns a string substituting target character with replacement + * character * @param STR original full string (the haystack) * @param TARGET character to replace (the needle) * @param REPLACEMENT character to substitute with * @return string with all instance of TARGET replaced with REPLACEMENT */ -std::string string_substitute(const std::string STR, const char TARGET, const char REPLACEMENT); +std::string string_substitute(const std::string STR, const char TARGET, + const char REPLACEMENT); /** - * @brief Returns a string with all uppercase characters converted to lowercase characters - * + * @brief Returns a string with all uppercase characters converted to lowercase + * characters + * * @param STR original string * @return string with lowercase characters */ std::string string_to_lower(const std::string STR); /** - * @brief Returns a string with all lowercase characters converted to uppercase characters - * + * @brief Returns a string with all lowercase characters converted to uppercase + * characters + * * @param STR original string * @return string with uppercase characters */ std::string string_to_upper(const std::string STR); /** - * @brief Compares two strings to determine their ordering or equality. Returns -1 if - * LHS < RHS. Returns 0 if LHS == RHS. Returns 1 if LHS > RHS - * + * @brief Compares two strings to determine their ordering or equality. Returns + * -1 if LHS < RHS. Returns 0 if LHS == RHS. Returns 1 if LHS > RHS + * * @param LHS left hand string * @param RHS right hand string * @return int representing relationship of LHS to RHS diff --git a/string_functions.o b/string_functions.o deleted file mode 100644 index 84faf86..0000000 Binary files a/string_functions.o and /dev/null differ diff --git a/test_suite.cpp b/test_suite.cpp index 98196d2..ad3d427 100644 --- a/test_suite.cpp +++ b/test_suite.cpp @@ -1,4 +1,5 @@ #include "test_suite.h" + #include "string_functions.h" #include @@ -8,249 +9,464 @@ 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 << " !!>FAILEDFAILEDFAILEDFAILEDFAILEDFAILEDFAILEDFAILED& LHS, const vector& RHS) { - cout << "Test #" << setw(3) << right << ++testNum; - cout << setw(MESSAGE_WIDTH) << right; - cout << MESSAGE << ": "; - cout << left; +int test_vector_string(int& testNum, const string MESSAGE, + const vector& 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; - } - } + 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 << " !!>FAILEDFAILED 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 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; + 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); - 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; + vector tokens3 = {""}; + totalPassed += test_vector_string(totalNumTests, "Testing string_tokenize()", + string_tokenize("", '*'), tokens3); + 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_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_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_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_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; + 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; - cout << "Tests Passed: " << setw(3) << right << totalPassed << " / " << setw(3) << totalNumTests << " (" << setprecision(0) << fixed << totalPassed * 100.0f / totalNumTests << "%)" << endl << endl; - - return (totalPassed == totalNumTests); + 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); } \ No newline at end of file diff --git a/test_suite.h b/test_suite.h index 9d0695a..30bf43d 100644 --- a/test_suite.h +++ b/test_suite.h @@ -4,70 +4,76 @@ #include #include -/** +/** * @brief Test suite. Runs all tests. * @return true if all tests pass, false otherwise */ bool run_all_tests(); /** - * @brief A generic test function, that simply prints "PASSED" if LHS equals RHS + * @brief A generic test function, that simply prints "PASSED" if LHS equals RHS * and otherwise prints FAILED. Do not modify this function. - * + * * @param testNum Number of the test. Value will be incremented upon completion * @param MESSAGE description of test * @param LHS computed result * @param RHS expected result * @return 1 if test passed, 0 otherwise */ -int test_int(int& testNum, const std::string MESSAGE, const int LHS, const int RHS); +int test_int(int& testNum, const std::string MESSAGE, const int LHS, + const int RHS); /** - * @brief A generic test function, that simply prints "PASSED" if LHS equals RHS + * @brief A generic test function, that simply prints "PASSED" if LHS equals RHS * and otherwise prints FAILED. Do not modify this function. - * + * * @param testNum Number of the test. Value will be incremented upon completion * @param MESSAGE description of test * @param LHS computed result * @param RHS expected result * @return 1 if test passed, 0 otherwise */ -int test_unsigned_long(int& testNum, const std::string MESSAGE, const unsigned long LHS, const unsigned long RHS); +int test_unsigned_long(int& testNum, const std::string MESSAGE, + const unsigned long LHS, const unsigned long RHS); /** - * @brief A generic test function, that simply prints "PASSED" if LHS equals RHS + * @brief A generic test function, that simply prints "PASSED" if LHS equals RHS * and otherwise prints FAILED. Do not modify this function. - * + * * @param testNum Number of the test. Value will be incremented upon completion * @param MESSAGE description of test * @param LHS computed result * @param RHS expected result * @return 1 if test passed, 0 otherwise */ -int test_char(int& testNum, const std::string MESSAGE, const char LHS, const char RHS); +int test_char(int& testNum, const std::string MESSAGE, const char LHS, + const char RHS); /** - * @brief A generic test function, that simply prints "PASSED" if LHS equals RHS + * @brief A generic test function, that simply prints "PASSED" if LHS equals RHS * and otherwise prints FAILED. Do not modify this function. - * + * * @param testNum Number of the test. Value will be incremented upon completion * @param MESSAGE description of test * @param LHS computed result * @param RHS expected result * @return 1 if test passed, 0 otherwise */ -int test_string(int& testNum, const std::string MESSAGE, const std::string LHS, const std::string RHS); +int test_string(int& testNum, const std::string MESSAGE, const std::string LHS, + const std::string RHS); /** - * @brief A generic test function, that simply prints "PASSED" if LHS equals RHS + * @brief A generic test function, that simply prints "PASSED" if LHS equals RHS * and otherwise prints FAILED. Do not modify this function. - * + * * @param testNum Number of the test. Value will be incremented upon completion * @param MESSAGE description of test * @param LHS computed result * @param RHS expected result * @return 1 if test passed, 0 otherwise */ -int test_vector_string(int& testNum, const std::string MESSAGE, const std::vector& LHS, const std::vector& RHS); +int test_vector_string(int& testNum, const std::string MESSAGE, + const std::vector& LHS, + const std::vector& RHS); #endif \ No newline at end of file diff --git a/test_suite.o b/test_suite.o deleted file mode 100644 index ed0c86f..0000000 Binary files a/test_suite.o and /dev/null differ