|
2904
|
1 // This file is part of the uSTL library, an STL implementation. |
|
|
2 // |
|
3152
|
3 // Copyright (c) 2005 by Mike Sharov <msharov@users.sourceforge.net> |
|
2904
|
4 // This file is free software, distributed under the MIT License. |
|
|
5 |
|
|
6 #include "stdtest.h" |
|
|
7 |
|
|
8 void MyFormat (const char* fmt, ...) __attribute__((__format__(__printf__,1,2))); |
|
|
9 |
|
|
10 void TestString (void) |
|
|
11 { |
|
|
12 static const char c_TestString1[] = "123456789012345678901234567890"; |
|
|
13 static const char c_TestString2[] = "abcdefghijklmnopqrstuvwxyz"; |
|
|
14 static const char c_TestString3[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|
|
15 string s1 (c_TestString1); |
|
|
16 string s2 (VectorRange (c_TestString2)); |
|
|
17 string s3 (s1); |
|
|
18 |
|
|
19 cout << s1 << endl; |
|
|
20 cout << s2 << endl; |
|
|
21 cout << s3 << endl; |
|
|
22 s3.reserve (48); |
|
|
23 s3.resize (20); |
|
|
24 |
|
|
25 uoff_t i; |
|
|
26 for (i = 0; i < s3.length(); ++ i) |
|
|
27 s3.at(i) = s3.at(i); |
|
|
28 for (i = 0; i < s3.length(); ++ i) |
|
|
29 s3[i] = s3[i]; |
|
|
30 cout.format ("%s\ns3.size() = %zu, max_size() = ", s3.c_str(), s3.size()); |
|
|
31 if (s3.max_size() == SIZE_MAX - 1) |
|
|
32 cout << "(SIZE_MAX/elsize)-1"; |
|
|
33 else |
|
|
34 cout << s3.max_size(); |
|
|
35 cout.format (", capacity() = %zu\n", s3.capacity()); |
|
|
36 |
|
|
37 s1.unlink(); |
|
|
38 s1 = c_TestString2; |
|
|
39 s1 += c_TestString3; |
|
|
40 s1 += '$'; |
|
|
41 cout << s1 << endl; |
|
|
42 |
|
|
43 s1 = "Hello"; |
|
|
44 s2.unlink(); |
|
|
45 s2 = "World"; |
|
|
46 s3 = s1 + s2; |
|
|
47 cout << s3 << endl; |
|
3152
|
48 s3 = "Concatenated " + s1 + s2 + " string."; |
|
2904
|
49 cout << s3 << endl; |
|
|
50 |
|
|
51 if (s1 < s2) |
|
|
52 cout << "s1 < s2\n"; |
|
|
53 if (s1 == s1) |
|
|
54 cout << "s1 == s1\n"; |
|
|
55 if (s1[0] != s1[0]) |
|
|
56 cout << "s1[0] != s1[0]\n"; |
|
|
57 |
|
|
58 string s4; |
|
|
59 s4.link (s1); |
|
|
60 if (s1 == s4) |
|
|
61 cout << "s1 == s4\n"; |
|
|
62 |
|
|
63 s1 = c_TestString1; |
|
|
64 string s5 (s1.begin() + 4, s1.begin() + 4 + 5); |
|
|
65 string s6 (s1.begin() + 4, s1.begin() + 4 + 5); |
|
|
66 if (s5 == s6) |
|
|
67 cout.format ("%s == %s\n", s5.c_str(), s6.c_str()); |
|
|
68 string tail (s1.begin() + 7, s1.end()); |
|
|
69 cout.format ("&s1[7] = %s\n", tail.c_str()); |
|
|
70 |
|
|
71 cout.format ("initial:\t\t%s\n", s1.c_str()); |
|
|
72 cout << "erase(5,find(9)-5)\t"; |
|
|
73 s1.erase (5, s1.find ('9')-5); |
|
|
74 cout << s1 << endl; |
|
|
75 cout << "erase(5,5)\t\t"; |
|
|
76 s1.erase (s1.begin() + 5, 2U); |
|
|
77 s1.erase (5, 3); |
|
|
78 assert (!*s1.end()); |
|
|
79 cout << s1 << endl; |
|
|
80 cout << "push_back('x')\t\t"; |
|
|
81 s1.push_back ('x'); |
|
|
82 assert (!*s1.end()); |
|
|
83 cout << s1 << endl; |
|
|
84 cout << "pop_back()\n"; |
|
|
85 s1.pop_back(); |
|
|
86 assert (!*s1.end()); |
|
|
87 cout << "insert(10,#)\t\t"; |
|
|
88 s1.insert (s1.begin() + 10, '#'); |
|
|
89 assert (!*s1.end()); |
|
|
90 cout << s1 << endl; |
|
|
91 cout << "replace(0,5,@)\t\t"; |
|
|
92 s1.replace (s1.begin(), s1.begin() + 5, 1, '@'); |
|
|
93 assert (!*s1.end()); |
|
|
94 cout << s1 << endl; |
|
|
95 |
|
|
96 s1 = c_TestString1; |
|
|
97 cout.format ("8 found at %zu\n", s1.find ('8')); |
|
|
98 cout.format ("9 found at %zu\n", s1.find ("9")); |
|
|
99 cout.format ("7 rfound at %zu\n", s1.rfind ('7')); |
|
|
100 cout.format ("7 rfound again at %zu\n", s1.rfind ('7', s1.rfind ('7') - 1)); |
|
|
101 cout.format ("67 rfound at %zu\n", s1.rfind ("67")); |
|
|
102 if (s1.rfind("X") == string::npos) |
|
|
103 cout << "X was not rfound\n"; |
|
|
104 else |
|
|
105 cout.format ("X rfound at %zu\n", s1.rfind ("X")); |
|
|
106 uoff_t poundfound = s1.find ("#"); |
|
|
107 if (poundfound != string::npos) |
|
|
108 cout.format ("# found at %zu\n", poundfound); |
|
|
109 cout.format ("[456] found at %zu\n", s1.find_first_of ("456")); |
|
|
110 cout.format ("[456] last found at %zu\n", s1.find_last_of ("456")); |
|
|
111 |
|
|
112 s2.clear(); |
|
|
113 assert (!*s2.end()); |
|
|
114 if (s2.empty()) |
|
|
115 cout.format ("s2 is empty [%s], capacity %zu bytes\n", s2.c_str(), s2.capacity()); |
|
|
116 |
|
|
117 s2.format ("<const] %d, %s, 0x%08X", 42, "[rfile>", 0xDEADBEEF); |
|
|
118 cout.format ("<%zu bytes of %zu> Format '%s'\n", s2.length(), s2.capacity(), s2.c_str()); |
|
|
119 MyFormat ("'<const] %d, %s, 0x%08X'", 42, "[rfile>", 0xDEADBEEF); |
|
|
120 |
|
|
121 cout.format ("hash_value(s2) = %08X, string::hash(s2) = %08X\n", hash_value (s2.begin()), string::hash (s2.begin(), s2.end())); |
|
|
122 } |
|
|
123 |
|
|
124 void MyFormat (const char* fmt, ...) |
|
|
125 { |
|
|
126 string buf; |
|
|
127 simd::reset_mmx(); |
|
|
128 va_list args; |
|
|
129 va_start (args, fmt); |
|
|
130 buf.vformat (fmt, args); |
|
|
131 cout.format ("Custom vararg MyFormat: %s\n", buf.c_str()); |
|
|
132 va_end (args); |
|
|
133 } |
|
|
134 |
|
|
135 StdBvtMain (TestString) |