Skip to content

Commit 296ec45

Browse files
authored
upload test.cpp
1 parent 6f9fa50 commit 296ec45

File tree

1 file changed

+303
-0
lines changed

1 file changed

+303
-0
lines changed

test.cpp

+303
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
#include "basic_string.hpp"
2+
#include "cstdio"
3+
#include <vector>
4+
#include <list>
5+
6+
template<typename T> void tsize(T const& t, std::size_t size)
7+
{
8+
static int count = 0;
9+
++count;
10+
auto tsize = t.size();
11+
auto len = std::strlen(t.data());
12+
assert(tsize == size);
13+
std::printf("test %d, size: %zu\n%s\n", count, size, t.data());
14+
}
15+
// clang-format off
16+
// macro to simplify test
17+
#define TRY try {
18+
// must throw, if not, terminate it
19+
#define CATCH std::terminate(); } catch(std::out_of_range &){}
20+
21+
// clang-format on
22+
int main()
23+
{
24+
using bstring = bizwen::basic_string<char>;
25+
26+
// begin test constructors
27+
{
28+
bstring a(30, '0');
29+
tsize(a, 30);
30+
assert(a.capacity() == 30);
31+
bstring b(31, '1');
32+
assert(b.capacity() > 30);
33+
bstring c(b, 1);
34+
tsize(c, 30);
35+
TRY bstring d(a, 31);
36+
CATCH
37+
bstring e(a, 2, 4);
38+
tsize(e, 4);
39+
bstring f(a, 1, 30);
40+
tsize(f, 29);
41+
TRY bstring g(a, 31, 1);
42+
CATCH
43+
bstring h(std::move(a), 1, 30);
44+
tsize(h, 29);
45+
bstring i(std::move(b), 1, 30);
46+
tsize(i, 30);
47+
assert(i.capacity() > 30);
48+
bstring j("12345678", 8);
49+
tsize(j, 8);
50+
bstring k(j.data());
51+
tsize(k, 8);
52+
std::vector<char> ll{ 'a', 'b', 'c', 'd', 'e' };
53+
bstring l{ ll.begin(), ll.end() };
54+
tsize(l, 5);
55+
std::list<char> mm{ ll.begin(), ll.end() };
56+
bstring m{ mm.begin(), mm.end() };
57+
tsize(m, 5);
58+
m.resize(31);
59+
tsize(m, 31);
60+
bstring n(m);
61+
tsize(n, 31);
62+
bstring o(std::move(n));
63+
tsize(o, 31);
64+
bstring p{ 'a', 'b', 'c', 'd', 'e' };
65+
tsize(p, 5);
66+
bstring q{ std::string_view{ "1234567890123456789012345678901234567890" } };
67+
tsize(q, 40);
68+
bstring r{ std::string_view{ "1234567890123456789012345678901234567890" }, 1, 20 };
69+
TRY bstring s{ std::string_view{ "1234567890123456789012345678901234567890" }, 41, 1 };
70+
CATCH
71+
}
72+
// end test constructors
73+
// begin test operator=
74+
{
75+
bstring a(40, 'a');
76+
bstring b{};
77+
b = a;
78+
tsize(b, 40);
79+
bstring c(50, 'c');
80+
tsize(c, 50);
81+
c = a;
82+
tsize(c, 40);
83+
b = std::move(c);
84+
tsize(b, 40);
85+
tsize(c, 40);
86+
c = b.data();
87+
tsize(c, 40);
88+
c = 'c';
89+
tsize(c, 1);
90+
b = { 'a', 'b', 'c' };
91+
tsize(b, 3);
92+
c = std::string_view{ "1234567890123456789012345678901" };
93+
tsize(c, 31);
94+
}
95+
// end test operator=
96+
// begin test assign
97+
{
98+
bstring s(20, 's');
99+
bstring l(40, 'l');
100+
s.assign(40, 'l');
101+
tsize(s, 40);
102+
l.assign(20, 's');
103+
tsize(l, 20);
104+
l.assign(s);
105+
tsize(l, 40);
106+
s.assign(l, 1, 40);
107+
tsize(s, 39);
108+
l.assign(s, 1, 20);
109+
tsize(l, 20);
110+
TRY s.assign(l, 21, 1);
111+
CATCH
112+
l.assign(std::move(s));
113+
tsize(s, 20);
114+
tsize(l, 39);
115+
l.assign(100, '1');
116+
s.assign(l.data(), 20);
117+
tsize(s, 20);
118+
l.assign(s.data());
119+
tsize(l, 20);
120+
std::vector<char> ll(40, 'd');
121+
l.assign(ll.begin(), ll.end());
122+
tsize(l, 40);
123+
s.assign({ 'a', 'b', 'c' });
124+
tsize(s, 3);
125+
l.assign(std::string_view{ "1234567890123456789012345678901" });
126+
tsize(l, 31);
127+
s.assign(std::string_view{ "1234567890123456789012345678901" }, 30);
128+
tsize(s, 1);
129+
TRY s.assign(std::string_view{ "1234567890123456789012345678901" }, 32);
130+
CATCH
131+
}
132+
// end test assign
133+
// begin test iterator
134+
{
135+
bstring l = "1234567890123456789012345678901234567890";
136+
bstring ll{ l.begin(), l.end() };
137+
tsize(ll, 40);
138+
std::vector<char> lll{ ll.begin(), ll.end() };
139+
assert(lll.size() == 40);
140+
}
141+
// end test iterator
142+
// begin test insert
143+
{
144+
bstring l = "abcdefg";
145+
l.insert(7, 30, '0');
146+
tsize(l, 37);
147+
TRY l.insert(38, 5, '0');
148+
CATCH
149+
l.insert(7, "1234567890");
150+
tsize(l, 47);
151+
l.insert(7, "09876543210", 10);
152+
tsize(l, 57);
153+
bstring ll{ "1234567890" };
154+
l.insert(7, ll);
155+
tsize(l, 67);
156+
l.insert(7, ll, 7, 10);
157+
tsize(l, 70);
158+
l.insert(l.begin() + 7, 'a');
159+
tsize(l, 71);
160+
l.insert(l.begin() + 7, 9, 'b');
161+
tsize(l, 80);
162+
bstring lll{ "hijklmn" };
163+
l.insert(l.begin() + 7, lll.begin(), lll.end());
164+
tsize(l, 87);
165+
l.insert(l.begin() + 7, { '1', '2', '3' });
166+
tsize(l, 90);
167+
std::string_view llll{ "1234567890" };
168+
l.insert(7, llll);
169+
tsize(l, 100);
170+
l.insert(7, llll, 4, 8);
171+
tsize(l, 106);
172+
}
173+
// end test insert
174+
// begin test erase
175+
{
176+
bstring s(20, 's');
177+
bstring l(40, 'l');
178+
TRY s.erase(21, 10);
179+
CATCH
180+
s.erase(10, 20);
181+
tsize(s, 10);
182+
l.erase(0, 10);
183+
tsize(l, 30);
184+
s = bstring(20, 's');
185+
s.erase(s.begin() + 10);
186+
tsize(s, 19);
187+
l = bstring(40, 'l');
188+
l.erase(l.begin() + 10, l.begin() + 20);
189+
tsize(l, 30);
190+
}
191+
{
192+
bstring s(20, 's');
193+
bstring l(40, 'l');
194+
s.erase(10, 10);
195+
tsize(s, 10);
196+
l.erase(0, 10);
197+
tsize(l, 30);
198+
s.erase(s.begin());
199+
tsize(s, 9);
200+
l.erase(l.begin(), l.end());
201+
tsize(l, 0);
202+
}
203+
// end test erase
204+
// begin test push_back, pop_back
205+
{
206+
bstring s(30, 's');
207+
bstring l(40, 'l');
208+
s.push_back('l');
209+
tsize(s, 31);
210+
l.push_back('l');
211+
tsize(l, 41);
212+
s.pop_back();
213+
tsize(s, 30);
214+
l.pop_back();
215+
tsize(l, 40);
216+
}
217+
// end test push_back, pop_back
218+
// begin test append
219+
{
220+
bstring s(20, 's');
221+
bstring l(40, 'l');
222+
s.append(10, 's');
223+
tsize(s, 30);
224+
s.append(10, 'l');
225+
tsize(s, 40);
226+
l.append(10, 'l');
227+
tsize(l, 50);
228+
l.append(s);
229+
tsize(l, 90);
230+
s.append(l, 10, 10);
231+
tsize(s, 50);
232+
s.append("0123456789", 10);
233+
tsize(s, 60);
234+
s.append("9876543210");
235+
tsize(s, 70);
236+
bstring ss{};
237+
ss.append(s.begin() + 50, s.end());
238+
tsize(ss, 20);
239+
ss.append({ 'a', 'b', 'c', 'd' });
240+
tsize(ss, 24);
241+
ss.append(std::string_view{ "fghijk" });
242+
tsize(ss, 30);
243+
ss.append(std::string_view{ "12345678901234567890" }, 10,10);
244+
tsize(ss, 40);
245+
TRY ss.append(std::string_view{ "12345678901234567890" }, 21, 10);
246+
CATCH
247+
}
248+
// end test append
249+
// begin test search
250+
{
251+
bstring l("1234567890abcdefghijklmnopqrstuvwxyz");
252+
auto r = l.starts_with('2');
253+
assert(r == false);
254+
r = l.starts_with('1');
255+
assert(r == true);
256+
r = l.starts_with(std::string_view{ "123" });
257+
assert(r == true);
258+
r = l.starts_with(std::string_view{ "321" });
259+
assert(r == false);
260+
r = l.starts_with("1234567890abcdefghijklmnopqrstuvwxyz");
261+
assert(r == true);
262+
r = l.ends_with('y');
263+
assert(r == false);
264+
r = l.ends_with('z');
265+
assert(r == true);
266+
r = l.ends_with(std::string_view{ "xyz" });
267+
assert(r == true);
268+
r = l.ends_with(std::string_view{ "zyx" });
269+
assert(r == false);
270+
r = l.ends_with("1234567890abcdefghijklmnopqrstuvwxyz");
271+
assert(r == true);
272+
r = l.contains("}");
273+
assert(r == false);
274+
r = l.contains("abc");
275+
assert(r == true);
276+
r = l.contains("1234567890abcdefghijklmnopqrstuvwxyz");
277+
assert(r == true);
278+
}
279+
// end test search
280+
// begin test compare
281+
{
282+
bstring greater("1234567890abcdefghijklmnopqrstuvwxyz");
283+
bstring less1("1234567890abcdefghijklmnopqrstuvwxy");
284+
bstring less2("1234567890abcdefghijklmnopqrstuvwxyy");
285+
auto e = std::strong_ordering::equal;
286+
auto g = std::strong_ordering::greater;
287+
auto l = std::strong_ordering::less;
288+
assert((greater == greater) == true);
289+
assert((greater == less1) == false);
290+
assert((greater == less2) == false);
291+
assert((greater <=> greater) == e);
292+
assert((greater <=> less1) == g);
293+
assert((greater <=> less2) == g);
294+
assert((less2 <=> greater) == l);
295+
assert((greater == "1234567890abcdefghijklmnopqrstuvwxyz") == true);
296+
assert((greater == "1234567890abcdefghijklmnopqrstuvwxy") == false);
297+
assert((greater == "1234567890abcdefghijklmnopqrstuvwxy") == false);
298+
assert((greater <=> "1234567890abcdefghijklmnopqrstuvwxyz") == e);
299+
assert((greater <=> "1234567890abcdefghijklmnopqrstuvwxy") == g);
300+
assert((greater <=> "1234567890abcdefghijklmnopqrstuvwxyy") == g);
301+
assert((less2 <=> "1234567890abcdefghijklmnopqrstuvwxyz") == l);
302+
}
303+
}

0 commit comments

Comments
 (0)