📄 strtst.cpp
字号:
compare(a.rfind("shells",13), String::npos); compare(a.rfind("shore"), 32); compare(a.rfind("es"), String::npos); compare(a.rfind("She",0), 0); compare(a.rfind("She",1), 0); cout << "test find_first_of\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; a = "The quick brown fox jumps over the lazy dog."; compare(a.find_first_of(String("over")), 2); compare(a.find_first_of(String("over"),2), 2); compare(a.find_first_of(String("over"),3), 11); compare(a.find_first_of(String("over"),12), 12); compare(a.find_first_of(String("The")), 0); compare(a.find_first_of(String("The"),1), 1); compare(a.find_first_of(String("1.2")), 43); compare(a.find_first_of(String("1.2"),43), 43); compare(a.find_first_of(String("1.2"),44), String::npos); compare(a.find_first_of(String("")), String::npos); compare(a.find_first_of(String("q")), 4); compare(a.find_first_of("over"), 2); compare(a.find_first_of("over",2), 2); compare(a.find_first_of("over",3), 11); compare(a.find_first_of("over",12), 12); compare(a.find_first_of("The"), 0); compare(a.find_first_of("The",1), 1); compare(a.find_first_of("1.2"), 43); compare(a.find_first_of("1.2",43), 43); compare(a.find_first_of("1.2",44), String::npos); compare(a.find_first_of(""), String::npos); compare(a.find_first_of("q"), 4); cout << "test find_last_of\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; a = "The quick brown fox jumps over the lazy dog."; compare(a.find_last_of(String("over")), 41); compare(a.find_last_of(String("over"),41), 41); compare(a.find_last_of(String("over"),40), 33); compare(a.find_last_of(String("over"),32), 29); compare(a.find_last_of(String("The")), 33); compare(a.find_last_of(String("The"),1), 1); compare(a.find_last_of(String("The"),0), 0); compare(a.find_last_of(String("1.2")), 43); compare(a.find_last_of(String("1.2"),43), 43); compare(a.find_last_of(String("1.2"),42), String::npos); compare(a.find_last_of(String("")), String::npos); compare(a.find_last_of(String("q")), 4); compare(a.find_last_of("over"), 41); compare(a.find_last_of("over",41), 41); compare(a.find_last_of("over",40), 33); compare(a.find_last_of("over",32), 29); compare(a.find_last_of("The"), 33); compare(a.find_last_of("The",1), 1); compare(a.find_last_of("The",0), 0); compare(a.find_last_of("1.2"), 43); compare(a.find_last_of("1.2",43), 43); compare(a.find_last_of("1.2",42), String::npos); compare(a.find_last_of(""), String::npos); compare(a.find_last_of("q"), 4); cout << "test find_first_not_of\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; a = "She sells sea shells by the sea shore"; compare(a.find_first_not_of(String("Shells ")), 12); compare(a.find_first_not_of(String("Shells "),12), 12); compare(a.find_first_not_of(String("Shells "),13), 21); compare(a.find_first_not_of(String("shell ")), 0); compare(a.find_first_not_of(String("shell "),1), 12); compare(a.find_first_not_of(String("short "),31), 36); compare(a.find_first_not_of(String("short "),30), 30); compare(a.find_first_not_of(String("shore hat"),23), String::npos); compare(a.find_first_not_of(String(""),23), 23); compare(a.find_first_not_of(String("")), 0); compare(a.find_first_not_of(String(""),50), String::npos); compare(a.find_first_not_of("Shells "), 12); compare(a.find_first_not_of("Shells ",12), 12); compare(a.find_first_not_of("Shells ",13), 21); compare(a.find_first_not_of("shell "), 0); compare(a.find_first_not_of("shell ",1), 12); compare(a.find_first_not_of("short ",31), 36); compare(a.find_first_not_of("short ",30), 30); compare(a.find_first_not_of("shore hat",23), String::npos); compare(a.find_first_not_of("",23), 23); compare(a.find_first_not_of(""), 0); compare(a.find_first_not_of("",50), String::npos); cout << "test find_last_not_of\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; a = "She sells sea shells by the sea shore"; compare(a.find_last_not_of(String("Shells ")), 35); compare(a.find_last_not_of(String("Shells "),11), String::npos); compare(a.find_last_not_of(String("Shells "),12), 12); compare(a.find_last_not_of(String("shell "),12), 12); compare(a.find_last_not_of(String("shell "),11), 0); compare(a.find_last_not_of(String("short ")), 36); compare(a.find_last_not_of(String("short "),30), 30); compare(a.find_last_not_of(String("shore hat")), 22); compare(a.find_last_not_of(String(""),23), 23); compare(a.find_last_not_of(String("")), 36); compare(a.find_last_not_of(String(""),50), 36); compare(a.find_last_not_of("Shells "), 35); compare(a.find_last_not_of("Shells ",11), String::npos); compare(a.find_last_not_of("Shells ",12), 12); compare(a.find_last_not_of("shell ",12), 12); compare(a.find_last_not_of("shell ",11), 0); compare(a.find_last_not_of("short "), 36); compare(a.find_last_not_of("short ",30), 30); compare(a.find_last_not_of("shore hat"), 22); compare(a.find_last_not_of("",23), 23); compare(a.find_last_not_of(""), 36); compare(a.find_last_not_of("",50), 36); cout << "test finds with zero length string\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; a = ""; compare(a.find(""), 0); compare(a.rfind(""), 0); compare(a.find_first_of(""), String::npos); compare(a.find_last_of(""), String::npos); compare(a.find_first_not_of(""), String::npos); compare(a.find_last_not_of(""), String::npos); compare(a.find('1'), String::npos); compare(a.rfind('1'), String::npos); compare(a.find_first_of('1'), String::npos); compare(a.find_last_of('1'), String::npos); compare(a.find_first_not_of('1'), String::npos); compare(a.find_last_not_of('1'), String::npos); compare(a.find("1"), String::npos); compare(a.rfind("1"), String::npos); compare(a.find_first_of("1"), String::npos); compare(a.find_last_of("1"), String::npos); compare(a.find_first_not_of("1"), String::npos); compare(a.find_last_not_of("1"), String::npos); compare(a.find("",1), String::npos); compare(a.rfind("",1), 0); compare(a.find_first_of("",1), String::npos); compare(a.find_last_of("",1), String::npos); compare(a.find_first_not_of("",1), String::npos); compare(a.find_last_not_of("",1), String::npos); compare(a.find('1',1), String::npos); compare(a.rfind('1',1), String::npos); compare(a.find_first_of('1',1), String::npos); compare(a.find_last_of('1',1), String::npos); compare(a.find_first_not_of('1',1), String::npos); compare(a.find_last_not_of('1',1), String::npos); compare(a.find("1",1), String::npos); compare(a.rfind("1",1), String::npos); compare(a.find_first_of("1",1), String::npos); compare(a.find_last_of("1",1), String::npos); compare(a.find_first_not_of("1",1), String::npos); compare(a.find_last_not_of("1",1), String::npos); cout << "test multiple reference to sum\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; prstr(thrice("123"), "123/123/123. [0]"); a = "1"; b = "2"; c = "3"; prstr(thrice(a + b + c), "123/123/123. [0]"); prstr(twice(a + b + c), "123/123. [0]"); a = "abc"; b = "def"; c = "ghi"; prstr(twice(a + b), "abcdef/abcdef. [0]"); none(a + b + c); cout << "test substitution in function\n"; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; String TheString = "This is an old string which will be replaced"; MemRet(TheString); prstr(TheString, "The new string [0]"); cout << "test input" << endl; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; { ifstream fin("strtst.dat"); fin >> a >> b >> c >> d; prstr(a, "Twinkle, [0]"); prstr(b, "twinkle, [0]"); prstr(c, "little [0]"); prstr(d, "star, [0]"); fin >> a >> b >> c >> d >> e >> f; prstr(a, "How [0]"); prstr(b, "I [0]"); prstr(c, "wonder [0]"); prstr(d, "what [0]"); prstr(e, "you [0]"); prstr(f, "are. [0]"); getline(fin, a); // to start newline getline(fin, a); prstr(a, "The quick brown fox [0]"); getline(fin, a); prstr(a, "jumps over the lazy dog. [0]"); getline(fin, a); prstr(a, "---------- [0]"); } cout << "test string functions" << endl; cout << " ... doing test " << (Totals.Value() + 1 ) << "\n"; { a = "1234567890AaBbCcZz ,./<>?;'{}[]\\~`!@#$%^&*()_-+="; b = "1234567890AABBCCZZ ,./<>?;'{}[]\\~`!@#$%^&*()_-+="; c = "1234567890aabbcczz ,./<>?;'{}[]\\~`!@#$%^&*()_-+="; d = a; UpperCase(d); prstr(d, b + " [0]"); d = a; LowerCase(d); prstr(d, c + " [0]"); a = ToString(1234); prstr(a, "1234 [0]"); a = ToString(-1234); prstr(a, "-1234 [0]"); a = ToString(0); prstr(a, "0 [0]"); a = ToString(999999); prstr(a, "999999 [0]"); a = ToString(-999999); prstr(a, "-999999 [0]"); a = ToString(1234.98); prstr(a, "1234.9800 [0]"); a = ToString(-1234.05); prstr(a, "-1234.0500 [0]"); a = ToString(0.0); prstr(a, "0.0000 [0]"); a = ToString(999999.9999); prstr(a, "999999.9999 [0]"); a = ToString(-999999.9999); prstr(a, "-999999.9999 [0]"); a = ToString(999999.999951); prstr(a, "1000000.0000 [0]"); a = ToString(-999999.999951); prstr(a, "-1000000.0000 [0]"); } cout << "test printing - the following 2 lines should be identical\n"; a = "The "; b = "quick "; c = "brown "; d = "fox\n"; cout << a << b << c << d; cout << "The quick brown fox\n"; cout << "printing npos" << endl; cout << String::npos << endl; } { String str; str.reserve(30000); s2 = str.data(); } cout << "(The following memory checks are probably not valid with all\n"; cout << "compilers - see newmat documentation)\n"; cout << "Checking for lost memory: " << (unsigned long)s1 << " " << (unsigned long)s2 << " "; if (s1 != s2) cout << " - error\n"; else cout << " - ok\n"; { String str = "a"; s4 = str.data(); } cout << "Checking for lost memory: " << (unsigned long)s3 << " " << (unsigned long)s4 << " "; if (s3 != s4) cout << " - error\n"; else cout << " - ok\n";#ifdef DO_FREE_CHECK FreeCheck::Status();#endif return 0;}// the compare functionsvoid prstr(const String& s, const String& t){ String u; ++Totals; int sr = s.refcount(); switch (sr) { case 0: u = "[0]"; break; case 1: u = "[1]"; break; case 2: u = "[2]"; break; case 3: u = "[3]"; break; default: u = "[x]"; break; } if (t != s + ' ' + u) { ++Errors; String s1 = s; // printing will disengage s1 from s cout << "Error at test " << Totals.Value() << ":\n"; cout << "prstr: " << s1 << " [" << sr << "]: should be " << t << "\n"; }}void compare(long i, long j){ ++Totals; if (i!=j) { ++Errors; cout << "Error at test " << Totals.Value() << ":\n"; cout << "found " << i << " should be " << j << "\n"; }}void compare_bool(bool i, bool j){ ++Totals; if (i!=j) { ++Errors; cout << "Error at test " << Totals.Value() << ":\n"; cout << "found " << i << " should be " << j << "\n"; }}void compare(const void* i, const void* j, bool agree){ ++Totals; if ((i==j) != agree) { cout << "Error at test " << Totals.Value() << ":\n"; ++Errors; if (agree) cout << "found " << (unsigned long)i << " should be " << (unsigned long)j << "\n"; else cout << "found " << (unsigned long)i << " shouldn't be " << (unsigned long)j << "\n"; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -