int main() { #if 1 printf("sizeof(tu_string) == %d\n", sizeof(tu_string)); swf_array<tu_string> storage; storage.resize(2); tu_string& a = storage[0]; tu_string& b = storage[1]; a = "test1"; printf("&a = 0x%X, &b = 0x%X\n", int(&a), int(&b)); printf("%s\n", a.c_str()); assert(a == "test1"); assert(a.length() == 5); a += "2"; assert(a == "test12"); a += "this is some more text"; assert(a.length() == 28); assert(a[2] == 's'); assert(a[3] == 't'); assert(a[4] == '1'); assert(a[5] == '2'); assert(a[7] == 'h'); assert(a[28] == 0); assert(b.length() == 0); assert(b[0] == 0); assert(b.c_str()[0] == 0); tu_string c = a + b; assert(c.length() == a.length()); c.resize(2); assert(c == "te"); assert(c == tu_string("te")); assert(tu_string("fourscore and sevent") == "fourscore and sevent"); b = "#sacrificial lamb"; // Test growing & shrinking. a = ""; for (int i = 0; i < 1000; i++) { assert(a.length() == i); if (i == 8) { assert(a == "01234567"); } else if (i == 27) { assert(a == "012345678901234567890123456"); } a.resize(a.length() + 1); a[a.length() - 1] = '0' + (i % 10); } {for (int i = 999; i >= 0; i--) { a.resize(a.length() - 1); assert(a.length() == i); if (i == 8) { assert(a == "01234567"); } else if (i == 27) { assert(a == "012345678901234567890123456"); } }} // Test larger shrinking across heap/local boundary. a = "this is a string longer than 16 characters"; a = "short"; // Test larger expand across heap/local boundary. a = "another longer string..."; assert(b == "#sacrificial lamb"); // Test erase() a = "abcdef"; a.erase(3, 1); assert(a == "abcef"); a = "abcdefghijklmnopqrst"; a.erase(12, 6); assert(a == "abcdefghijklst"); // Test insert() a = "abcdef"; a.insert(3, 'z'); assert(a == "abczdef"); a = "abcdefghijklmn"; a.insert(12, 'y'); a.insert(13, 'z'); assert(a == "abcdefghijklyzmn"); // Test operator+=(tu_string) a = "abcdef"; b = "ghijklmnopqrst"; a += b; assert(a == "abcdefghijklmnopqrst"); // Test operator+(const char*) a = "abcdef"; b = "ghi"; assert(a + b == "abcdefghi"); // Test operator<(const char*) a = "abc"; assert(a < "def"); assert(a < "abd"); assert(!(a < "aab")); assert(!(a < "aa")); assert(!(a < "aabz")); assert(a < "abcd"); // Test operator<(const tu_string&) a = "abc"; assert(a < tu_string("def")); assert(a < tu_string("abd")); assert(!(a < tu_string("aab"))); assert(!(a < tu_string("aa"))); assert(!(a < tu_string("aabz"))); assert(a < tu_string("abcd")); // Test that we can store strings with embedded \0's. a = "abc"; a += '\0'; assert(a.length() == 4); a += "def"; assert(a.length() == 7); assert(a[4] == 'd'); assert(a[5] == 'e'); assert(a[6] == 'f'); assert(a[7] == 0); // Test that we can upsize strings with embedded \0's a = "abc"; a += '\0'; a += "def"; b = a; assert(a == b); assert(b.length() == 7); a = a + b; assert(a.length() == 14); assert(a == (b + b)); assert(a > b); a = a + b; assert(a.size() == 21); assert(a == (b + b + b)); assert(a > b); // Test that we can downsize strings with embedded \0's a = "abc"; a += '\0'; a += "def"; assert(a.length() == 7); b = a + a + a; assert(b.length() == 21); b.resize(14); assert(b.length() == 14); assert(b == (a + a)); b.erase(3, 1); assert(b.size() == 13); assert(b == tu_string("abcdef") + a); // Test strings that are nothing but \0's. a = ""; b = ""; for (int i = 0; i < 100; i++) { a += '\0'; assert(a.length() == i + 1); assert(a > b); b += '\0'; assert(b.length() == i + 1); assert(a == b); assert((a + b).length() == 2 * (i + 1)); } test_hash(); test_stringi(); test_stringi_hash(); test_unicode(); // TODO: unit tests for swf_array<>, string_hash<> #endif test_hash_speed(); printf("OK\n"); return 0; }
int main() { #if 1 printf("sizeof(tu_string) == %d\n", sizeof(tu_string)); array<tu_string> storage; storage.resize(2); tu_string& a = storage[0]; tu_string& b = storage[1]; a = "test1"; printf("&a = 0x%X, &b = 0x%X\n", int(&a), int(&b)); printf("%s\n", a.c_str()); assert(a == "test1"); assert(a.length() == 5); a += "2"; assert(a == "test12"); a += "this is some more text"; assert(a.length() == 28); assert(a[2] == 's'); assert(a[3] == 't'); assert(a[4] == '1'); assert(a[5] == '2'); assert(a[7] == 'h'); assert(a[28] == 0); assert(b.length() == 0); assert(b[0] == 0); assert(b.c_str()[0] == 0); tu_string c = a + b; assert(c.length() == a.length()); c.resize(2); assert(c == "te"); assert(c == tu_string("te")); assert(tu_string("fourscore and sevent") == "fourscore and sevent"); b = "#sacrificial lamb"; // Test growing & shrinking. a = ""; for (int i = 0; i < 1000; i++) { assert(a.length() == i); if (i == 8) { assert(a == "01234567"); } else if (i == 27) { assert(a == "012345678901234567890123456"); } a.resize(a.length() + 1); a[a.length() - 1] = '0' + (i % 10); } {for (int i = 999; i >= 0; i--) { a.resize(a.length() - 1); assert(a.length() == i); if (i == 8) { assert(a == "01234567"); } else if (i == 27) { assert(a == "012345678901234567890123456"); } }} // Test larger shrinking across heap/local boundary. a = "this is a string longer than 16 characters"; a = "short"; // Test larger expand across heap/local boundary. a = "another longer string..."; assert(b == "#sacrificial lamb"); test_hash(); test_stringi(); test_stringi_hash(); test_unicode(); // TODO: unit tests for array<>, string_hash<> #endif test_hash_speed(); return 0; }