#include #include #include "testframework.h" bool checkStringCompareEquals(){ std::string b = "This is test string b"; std::string c = "This is test string c"; return (b == c); } bool checkStringCompareCompare(){ std::string l = "Tommi Maekitalo"; std::string s = "Mae"; int c = l.compare(6, 3, s); return (0 == c); } bool checkStringCompareNotEquals(){ std::string b = "This is test string b"; std::string c = "This is test string c"; return b != c; } bool checkStringCompareForwardsEqual(){ std::string a = "This is test string a"; std::string b = "This is test string b"; a = b + "Test cstring"; a = b; return (a == b); } bool checkStringCompareBackwardsEqual(){ std::string a = "This is test string a"; std::string b = "This is test string b"; a = b + "Test cstring"; a = b; return (b == a); } bool checkStringCompareForwardsNotEqual(){ std::string a = "This is test string a"; std::string b = "This is test string b"; a = b + "Test cstring"; a = b; return (a != b); } bool checkStringCompareBackwardsNotEqual(){ std::string a = "This is test string a"; std::string b = "This is test string b"; a = b + "Test cstring"; a = b; return (b != a); } bool checkStringTextForwardsComparison(){ std::string a = "Test text comparison"; return (a == "Test text comparison"); } bool checkStringTextBackwardsComparison(){ std::string a = "Test text comparison"; return (a == "Test text comparison"); } bool checkStringOperatorLess(){ std::string b = "This is test string b"; std::string c = "This is test string c"; return (b < c); } bool checkStringOperatorGreater(){ std::string b = "This is test string b"; std::string c = "This is test string c"; return (b > c); } bool checkStringSubstr(){ std::string a = "This is the base string"; return ("is th" == a.substr(5, 5) ); } bool checkStringCharConstructor(){ std::string a = std::string(1, 'w'); std::string b = "w"; return (a == b); } bool checkStringOpPlusChar(){ std::string a = std::string("test"); std::string b = '1' + a; return ("1test" == b ); } bool checkStringOpCharPlus(){ std::string a = std::string("test"); std::string b = a + '1'; return ("test1" == b); } bool checkStringOpPlusString(){ std::string a = std::string("test"); std::string b = "1" + a; return ("1test" == b ); } bool checkStringOpStringPlus(){ std::string a = std::string("test"); std::string b = a + "1"; return ("test1" == b); } bool checkStringOpSubscript(){ std::string a = "abcdefg"; return('a' == a[0] && 'b' == a[1] && 'c' == a[2] && 'd' == a[3] && 'e' == a[4] && 'f' == a[5] && 'g' == a[6]); } bool checkStringErase(){ std::string a = "this should be empty"; a.erase(); return ("" == a); } bool checkFindThe(){ std::string a = "This is the string we are searching through"; std::string b = "the"; std::string::size_type retval = a.find(b); return 8 == retval; } bool checkFindCharG(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find('g'); return 17 == retval; } bool checkFindSearch(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find("search"); return 26 == retval; } bool checkFindThrough(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find("through"); return 36 == retval; } bool checkFindIsChar3(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find("is", 3); return 5 == retval; } bool checkFindQ(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find("q"); return a.npos == retval; } bool checkFindQCast(){ std::string a = "This is the string we are searching through"; if( (long)a.find("q") >= 0){ return false; }else{ if((long)a.find("q") < 0){ return true; }else{ return false; } } return false; } bool checkRfindThe(){ std::string a = "This is the string we are searching through"; std::string b = "the"; std::string::size_type retval = a.rfind(b); return 8 == retval; } bool checkRfindCharG(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.rfind('g'); return 41 == retval; } bool checkRfindSearch(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.rfind("search"); return 26 == retval; } bool checkRfindIsChar3(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.rfind("is", 3); return 2 == retval; } bool checkRfindQ(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.rfind("q"); return a.npos == retval; } bool checkRfindThis(){ std::string a = "This is the string we are searching through"; std::string b = "This"; std::string::size_type retval = a.rfind(b); return 0 == retval; } bool checkFindFirstOfThe(){ std::string a = "This is the string we are searching through"; std::string b = "the"; std::string::size_type retval = a.find_first_of(b); return 1 == retval; } bool checkFindFirstOfCharG(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find_first_of('g'); return 17 == retval; } bool checkFindFirstOfSearch(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find_first_of("search"); return 1 == retval; } bool checkFindFirstOfIsChar4(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find_first_of("is", 4); return 5 == retval; } bool checkFindFirstOfQ(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find_first_of("q"); return a.npos == retval; } bool checkFindFirstOfThis(){ std::string a = "This is the string we are searching through"; std::string b = "This"; std::string::size_type retval = a.find_first_of(b); return 0 == retval; } bool checkFindLastOfThe(){ std::string a = "This is the string we are searching through"; std::string b = "the"; std::string::size_type retval = a.find_last_of(b); return 42 == retval; } bool checkFindLastOfCharG(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find_last_of('g'); return 41 == retval; } bool checkFindLastOfSearch(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find_last_of("search"); return 42 == retval; } bool checkFindLastOfIsChar4(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find_last_of("is", 4); return 3 == retval; } bool checkFindLastOfQ(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find_last_of("q"); return a.npos == retval; } bool checkFindLastOfThis(){ std::string a = "This is the string we are searching through"; std::string b = "This"; std::string::size_type retval = a.find_last_of(b); return 42 == retval; } bool checkFindFirstNotOfThe(){ std::string a = "This is the string we are searching through"; std::string b = "the"; std::string::size_type retval = a.find_first_not_of(b); return 0 == retval; } bool checkFindFirstNotOfCharG(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find_first_not_of('g'); return 0 == retval; } bool checkFindFirstNotOfSearch(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find_first_not_of("search"); return 0 == retval; } bool checkFindFirstNotOfIsChar5(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find_first_not_of("is", 5); return 7 == retval; } bool checkFindFirstNotOfQ(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find_first_not_of("q"); return 0 == retval; } bool checkFindFirstNotOfThis(){ std::string a = "This is the string we are searching through"; std::string b = "This"; std::string::size_type retval = a.find_first_not_of(b); return 4 == retval; } bool checkFindLastNotOfThe(){ std::string a = "This is the string we are searching through"; std::string b = "the"; std::string::size_type retval = a.find_last_not_of(b); return 41 == retval; } bool checkFindLastNotOfCharG(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find_last_not_of('g'); return 42 == retval; } bool checkFindLastNotOfSearch(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find_last_not_of("search"); return 41 == retval; } bool checkFindLastNotOfIsChar7(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find_last_not_of("is", 7); return 7 == retval; } bool checkFindLastNotOfQ(){ std::string a = "This is the string we are searching through"; std::string::size_type retval = a.find_last_not_of("q"); return 42 == retval; } bool checkFindLastNotOfThis(){ std::string a = "This is the string we are searching through"; std::string b = "This"; std::string::size_type retval = a.find_last_not_of(b); return 41 == retval; } bool checkInsertAtInteractor() { std::string a = "abcd"; a.insert(a.end(), 'q'); if (a != "abcdq") { return false; } return true; } bool checkAssignFillType() { return true; #if 0 std::string a; a.assign(10, 0x2B); return a == "++++++++++"; #endif } bool checkAssignFillChar() { std::string a; a.assign(10, '+'); return a == "++++++++++"; } bool checkAssignString() { std::string a = "This is a string"; std::string b; b.assign(a); return b == a; } bool checkAssignSubstring() { std::string a = "This is a string"; std::string b; b.assign(a, 2, 5); return b == "is is"; } bool checkAssignCstring() { std::string a; a.assign("This is a c string"); return a == "This is a c string"; } bool checkAssignBuffer() { std::string a; a.assign("This is a c string", 8); return a == "This is "; } bool checkAssignIterator() { std::string a = "This is a string"; std::string b; b.assign(a.begin() + 2, a.end() - 6); return b == "is is a "; } int main(){ TestFramework::init(); std::string a("Testing string constructor"); std::string b, c; std::cout << "Value of a: " << a << std::endl; a = "This is test string a"; b = "This is test string b"; c = "This is test string c"; std::cout << "Test string initial values:\n"; std::cout << a << std::endl << b << std::endl << c << std::endl; a = b; std::cout << "a = b: " << a << std::endl; a = b + c; std::cout << "a = b + c: " << a << std::endl; a = "Test cstring" + b; std::cout << "a = \"Test cstring\" + b: " << a << std::endl; std::cout << "Please enter a test string:" << std::flush; std::cin >> a; std::cout << std::endl << "You entered: " << a << std::endl; TestFramework::AssertReturns(checkStringCompareEquals, false); TestFramework::AssertReturns(checkStringCompareCompare, true); TestFramework::AssertReturns(checkStringCompareNotEquals, true); TestFramework::AssertReturns(checkStringCompareForwardsEqual, true); TestFramework::AssertReturns(checkStringCompareBackwardsEqual, true); TestFramework::AssertReturns(checkStringCompareForwardsNotEqual, false); TestFramework::AssertReturns(checkStringCompareBackwardsNotEqual, false); TestFramework::AssertReturns(checkStringTextForwardsComparison, true); TestFramework::AssertReturns(checkStringTextBackwardsComparison, true); TestFramework::AssertReturns(checkStringOperatorLess, true); TestFramework::AssertReturns(checkStringOperatorGreater, false); TestFramework::AssertReturns(checkStringSubstr, true); TestFramework::AssertReturns(checkStringCharConstructor, true); TestFramework::AssertReturns(checkStringOpPlusChar, true); TestFramework::AssertReturns(checkStringOpCharPlus, true); TestFramework::AssertReturns(checkStringOpPlusString, true); TestFramework::AssertReturns(checkStringOpStringPlus, true); TestFramework::AssertReturns(checkStringOpSubscript, true); TestFramework::AssertReturns(checkStringErase, true); TestFramework::AssertReturns(checkFindThe, true); TestFramework::AssertReturns(checkFindCharG, true); TestFramework::AssertReturns(checkFindSearch, true); TestFramework::AssertReturns(checkFindThrough, true); TestFramework::AssertReturns(checkFindIsChar3, true); TestFramework::AssertReturns(checkFindQ, true); TestFramework::AssertReturns(checkFindQCast, true); TestFramework::AssertReturns(checkRfindThe, true); TestFramework::AssertReturns(checkRfindCharG, true); TestFramework::AssertReturns(checkRfindSearch, true); TestFramework::AssertReturns(checkRfindIsChar3, true); TestFramework::AssertReturns(checkRfindQ, true); TestFramework::AssertReturns(checkRfindThis, true); TestFramework::AssertReturns(checkFindFirstOfThe, true); TestFramework::AssertReturns(checkFindFirstOfCharG, true); TestFramework::AssertReturns(checkFindFirstOfSearch, true); TestFramework::AssertReturns(checkFindFirstOfIsChar4, true); TestFramework::AssertReturns(checkFindFirstOfQ, true); TestFramework::AssertReturns(checkFindFirstOfThis, true); TestFramework::AssertReturns(checkFindLastOfThe, true); TestFramework::AssertReturns(checkFindLastOfCharG, true); TestFramework::AssertReturns(checkFindLastOfSearch, true); TestFramework::AssertReturns(checkFindLastOfIsChar4, true); TestFramework::AssertReturns(checkFindLastOfQ, true); TestFramework::AssertReturns(checkFindLastOfThis, true); TestFramework::AssertReturns(checkFindFirstNotOfThe, true); TestFramework::AssertReturns(checkFindFirstNotOfCharG, true); TestFramework::AssertReturns(checkFindFirstNotOfSearch, true); TestFramework::AssertReturns(checkFindFirstNotOfIsChar5, true); TestFramework::AssertReturns(checkFindFirstNotOfQ, true); TestFramework::AssertReturns(checkFindFirstNotOfThis, true); TestFramework::AssertReturns(checkFindLastNotOfThe, true); TestFramework::AssertReturns(checkFindLastNotOfCharG, true); TestFramework::AssertReturns(checkFindLastNotOfSearch, true); TestFramework::AssertReturns(checkFindLastNotOfIsChar7, true); TestFramework::AssertReturns(checkFindLastNotOfQ, true); TestFramework::AssertReturns(checkFindLastNotOfThis, true); TestFramework::AssertReturns(checkInsertAtInteractor, true); TestFramework::AssertReturns(checkAssignFillType, true); TestFramework::AssertReturns(checkAssignFillChar, true); TestFramework::AssertReturns(checkAssignString, true); TestFramework::AssertReturns(checkAssignSubstring, true); TestFramework::AssertReturns(checkAssignCstring, true); TestFramework::AssertReturns(checkAssignBuffer, true); TestFramework::AssertReturns(checkAssignIterator, true); TestFramework::results(); return 0; }