//: C03:Find.h // From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. // (c) 1995-2004 MindView, Inc. All Rights Reserved. // See source code use permissions stated in the file 'License.txt', // distributed with the code package available at www.MindView.net. #ifndef FIND_H #define FIND_H #include #include #include #include "../TestSuite/Test.h" using std::size_t; using std::string; using std::tolower; using std::toupper; // Make an uppercase copy of s inline string upperCase(const string& s) { string upper(s); for(size_t i = 0; i < s.length(); ++i) upper[i] = toupper(upper[i]); return upper; } // Make a lowercase copy of s inline string lowerCase(const string& s) { string lower(s); for(size_t i = 0; i < s.length(); ++i) lower[i] = tolower(lower[i]); return lower; } class FindTest : public TestSuite::Test { string chooseOne; public: FindTest() : chooseOne("Eenie, Meenie, Miney, Mo") {} void testUpper() { string upper = upperCase(chooseOne); const string LOWER = "abcdefghijklmnopqrstuvwxyz"; test_(upper.find_first_of(LOWER) == string::npos); } void testLower() { string lower = lowerCase(chooseOne); const string UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; test_(lower.find_first_of(UPPER) == string::npos); } void testSearch() { // Case sensitive search size_t i = chooseOne.find("een"); test_(i == 8); // Search lowercase: string test = lowerCase(chooseOne); i = test.find("een"); test_(i == 0); i = test.find("een", ++i); test_(i == 8); i = test.find("een", ++i); test_(i == string::npos); // Search uppercase: test = upperCase(chooseOne); i = test.find("EEN"); test_(i == 0); i = test.find("EEN", ++i); test_(i == 8); i = test.find("EEN", ++i); test_(i == string::npos); } void run() { testUpper(); testLower(); testSearch(); } }; #endif // FIND_H ///:~