#include #include #include #include using std::string; #include "PndString.h" using std::cerr; using std::cout; using std::endl; using std::fstream; using std::ifstream; using namespace panda::PndString; void failed(int i, const string& stmp) { cerr << "Failed at " << i << " : " ; cerr << stmp << endl; } void StringTest(){ string S0("S00S"); string S1("S01S"); string S2("S02S"); string S3("S03S"); string S4("S04S"); string S5("S05S"); string S6("S06S"); string S7("S07S"); string S8("S08S"); string S9("S09S"); string SX("S0XS"); char* c0 = new char[5]; char* c1 = new char[5]; char* c2 = new char[5]; char* c3 = new char[5]; char* c4 = new char[5]; char* c5 = new char[5]; char* c6 = new char[5]; char* c7 = new char[5]; char* c8 = new char[5]; char* c9 = new char[5]; char* cx = new char[5]; strcpy(c0,"c00c"); strcpy(c1,"c01c"); strcpy(c2,"c02c"); strcpy(c3,"c03c"); strcpy(c4,"c04c"); strcpy(c5,"c05c"); strcpy(c6,"c06c"); strcpy(c7,"c07c"); strcpy(c8,"c08c"); strcpy(c9,"c09c"); strcpy(cx,"c0xc"); // Starting tests cout << "Starting tests ... " << endl; // test constructors string str1; if (str1.length() != 0) failed(1,str1); string str2(c0); if (str2 != "c00c") failed(2,str2); string str3(cx,3); if (str3 != "c0x") failed(3,str3); string str4(S1); if (str4 != "S01S") failed(4,str4); char t = 't'; string str5(1,t); if (str5 != "t") failed(5,str5); string str6(6,'t'); if (str6 != "tttttt") failed(6,str6); // test assignment operators string str7 = c1; if (str7 != "c01c") failed(101,str7); string str8 = S1; if (str8 != "S01S") failed(102,str8); str7 += c2; if (str7 != "c01cc02c") failed(103,str7); str8 += S2; if (str8 != "S01SS02S") failed(104,str8); // test indexing operators const char testc1 = S9[2]; if (testc1 != '9') failed(201,S9); char testc2 = S8[2]; if (testc2 != '8') failed(202,S8); const char testc3 = S7[2]; char testc4 = S6[2]; if (testc3 != '7') failed(203,S7); if (testc4 != '6') failed(204,S6); string str9 = S5.substr(1,2); const string str10 = S5.substr(1,2); if (str9 != "05") failed(205,str9); if (str10 != "05") failed(206,str10); // test append string str11(S2); str11.append(S1); if (str11 != "S02SS01S") failed(301,str11); str11.append(S3,0,3); if (str11 != "S02SS01SS03") failed(302,str11); str11.append(c6); if (str11 != "S02SS01SS03c06c") failed(303,str11); str11.append(c4,3); if (str11 != "S02SS01SS03c06cc04") failed(304,str11); str11.append(4,'r'); if (str11 != "S02SS01SS03c06cc04rrrr") failed(305,str11); // test compare string str12(S6); string str13("s06s"); if (str12.compare("S06S") != 0) failed(401,str12); if (compare_nocase(str12,"s06s") != 0) failed(402,str12); if (str12.compare(S6) != 0) failed(403,str12); if (compare_nocase(str12, str13) != 0) failed(404,str12); if (str12.compare("s06s") == 0) failed(405,str12); if (str12.compare(str13) == 0) failed(406,str12); string str15(S8); if (0 != strcmp(str15.data(),"S08S")) failed(501,str15); // test find string str14("abcdS06Sefg"); if (str14.find("S06S") == string::npos ) failed(407,str14); if (str14.find("s06s") != string::npos ) failed(408,str14); if (str14.find(S6) == string::npos ) failed(409,str14); if (str14.find(str13) != string::npos ) failed(410,str14); if (find_nocase(str14,"s06s") == string::npos ) failed(411,str14); if (find_nocase(str14,str13) == string::npos ) failed(412,str14); string str16("abcdefghi"); if (str16.find_first_of('c') != 2) failed(502,str16); if (str16.find_first_of("jklmd") != 3) failed(504,str16); if (str16.find_first_of("jklmd",0,2) != string::npos) failed(505,str16); string str17("abcdS06Sefgh"); if (str17.find("S06S") != 4 ) failed(505,str17); if (str17.find(S6) != 4 ) failed(506,str17); if (str17.find(c6,0,1) != 2) failed(507,str17); if (str17.find(S8.substr(0,1),0) != 4) failed(508,str17); // test insert string str18(S8); str18.insert(2,c8); if (str18 != "S0c08c8S") failed(601,str18); str18.insert(2,c1,2); if (str18 != "S0c0c08c8S") failed(602,str18); str18.insert(1,S4); if (str18 != "SS04S0c0c08c8S") failed(603,str18); str18.insert(7,S5,0,2); if (str18 != "SS04S0cS00c08c8S") failed(604,str18); // test empty string str19; if ( !str19.empty() ) failed(605,str19); str19 = S9; if (str19.empty() ) failed(606,str19); // test find last string str20(S4); str20 += c3; if (str20.find_last_of('4') != 2) failed(607,str20); if (str20.find_last_of('4',2) != 2) failed(608,str20); // test length if (S2.length() != 4) failed(609,S2); // test insert string str21(S3); str21.insert(0,c1); if (str21 != "c01cS03S") failed(609,str21); str21.insert(0,c2,2); if (str21 != "c0c01cS03S") failed(610,str21); int pos = 0; // otherwise compiler complains of ambiouity str21.insert(pos,3,'d'); if (str21 != "dddc0c01cS03S") failed(611,str21); str21.insert(0,S6); if (str21 != "S06Sdddc0c01cS03S") failed(612,str21); str21.insert(0,S9,0,3); if (str21 != "S09S06Sdddc0c01cS03S") failed(613,str21); // test erase str21.erase(7); if (str21 != "S09S06S") failed(614,str21); str21.erase(2,1); if (str21 != "S0S06S") failed(614,str21); // test replace string str22(S6); str22.append(S7); str22.append(c4); str22.replace(4,4,c2); if (str22 != "S06Sc02cc04c") failed(615,str22); str22.replace(4,4,c5,3); if (str22 != "S06Sc05c04c") failed(616,str22); str22.replace(4,4,SX); if (str22 != "S06SS0XS04c") failed(617,str22); str22.replace(4,4,S1,0,3); if (str22 != "S06SS0104c") failed(618,str22); // test resize size_t oldSize=str22.length(); str22.resize(oldSize+2); if (str22.length() != (oldSize+2)) failed(619,str22); // test substr string str23("This is c04caC05C test"); string str24, str25; str24 = str23.substr(str23.find(c4,8),sizeof(c4)); if (str24 != "c04c") failed(620,str24); // test operator ==, =! etc string str26("c05c"); string str27(S2); if (S2 == c2) failed(701,S2); if (c2 == S2) failed(702,S2); if (S2 == S5) failed(703,S2); if (str26 != c5) failed(704,str26); if (c5 != str26) failed(705,str26); if (str27 != S2) failed(706,str27); string str28(c6); if (str28 > c9) failed(707,str28); if (c4 > str28) failed(708, str28); if (S4 > S6) failed(709,S4); if (c8 < str28) failed(710,str28); if (str28 < c4) failed(711,str28); if (S8 < S4) failed(712,S8); if (str28 >= c9) failed(713,str28); if (c4 >= str28) failed(714, str28); if (S4 >= S6) failed(715,S4); if (c8 <= str28) failed(716,str28); if (str28 <= c4) failed(717,str28); if (S8 <= S4) failed(719,S8); string str29 = S6 + c8; if (str29 != "S06Sc08c") failed(720,str29); str29 = c4 + S8; if (str29 != "c04cS08S") failed(721,str29); str29 = S6 + S9; if (str29 != "S06SS09S") failed(722,str29); // test toUpper, toLower string str31("This IS a test"); transformToUpper(str31); if (str31 != "THIS IS A TEST") failed(801,str31); const string str31a("This IS a test"); string str31b = toUpper(str31a); if (str31b != "THIS IS A TEST") failed(1801,str31b); if (str31a != "This IS a test") failed(2801,str31a); string str32("THIS IS also A TEST"); transformToLower(str32); if (str32 != "this is also a test") failed(802,str32); const string str32a("THIS IS also A TEST"); string str32b = toLower(str32a); if (str32b != "this is also a test") failed(1802,str32b); if (str32a != "THIS IS also A TEST") failed(2802,str32a); // test erase string str33(" When it hits "); string str33a = str33; string str33b = str33; string str33c = str33; str33a.erase(str33a.find_last_not_of(" ")+1); str33b.erase(0,str33b.find_first_not_of(" ")); str33c.erase(0,str33c.find_first_not_of(" ")); str33c.erase(str33c.find_last_not_of(" ")+1); if (str33a != " When it hits") failed(803, str33a); if (str33b != "When it hits ") failed(804, str33b); if (str33c != "When it hits") failed(805, str33c); // tests with files string filenameS("./testString.tex"); const char* filename = filenameS.c_str(); // check if file exists, otherwise, no point to go on ! ifstream dfile(filename); if (dfile.bad()) { cerr << "Could not open file BbrStdUtils/testString.tex"; exit(8); } string str34a; string str34; getline(dfile,str34,'\000'); str34a += "Jack and jill went\n"; str34a += "up the hill to fetch\n"; str34a += "\n"; str34a += "a pail of water\n"; str34a += "\n"; str34a += "Jack was clumsy \n"; str34a += "\n"; str34a += "and tumbled down\n"; str34a += "\n"; str34a += "while Jill looked on with glee.\n"; if (str34 != str34a) failed(900,str34); dfile.close(); dfile.open(filename); string str35; getline(dfile,str35,'\n'); if (str35 != "Jack and jill went") failed(901,str35); getline(dfile,str35,'\n'); getline(dfile,str35,'\n'); if (str35 != "") failed(902,str35); do { getline(dfile,str35); } while (str35 == ""); if (str35 != "a pail of water") failed(903,str35); do { getline(dfile,str35); } while (str35 == ""); if (str35 != "Jack was clumsy ") failed(904,str35); dfile.close(); dfile.open(filename); string str36; getline(dfile,str36,'\000'); if (str36 != str34a) failed(905,str36); dfile.close(); dfile.open(filename); string str37; dfile >> str37; if (str37 != "Jack") failed(906,str37); dfile.close(); dfile.open(filename); string str38; getline(dfile, str38, '\n'); if (str38 != "Jack and jill went") failed(907,str38); dfile.close(); dfile.open(filename); string str39; dfile >> str39; if (str39 != "Jack") failed(908,str39); dfile >> str39; if (str39 != "and") failed(909,str39); dfile.close(); // Finished tests cout << "Tests completed." << endl; delete [] c0; delete [] c1; delete [] c2; delete [] c3; delete [] c4; delete [] c5; delete [] c6; delete [] c7; delete [] c8; delete [] c9; delete [] cx; exit(0); }