source: tests/collections/string-api-coverage.cfa@ 544deb9

stuck-waitfor-destruct
Last change on this file since 544deb9 was e8b3717, checked in by Michael Brooks <mlbrooks@…>, 2 years ago

Modify substring interface from start-end to start-len, and add a missing test.

  • Property mode set to 100644
File size: 10.6 KB
Line 
1#include <collections/string.hfa>
2#include <string_sharectx.hfa>
3
4void assertWellFormedHandleList( int maxLen ) { // with(HeapArea)
5 // HandleNode *n;
6 // int limit1 = maxLen;
7 // for ( n = Header.flink; (limit1-- > 0) && n != &Header; n = n->flink ) {}
8 // assert (n == &Header);
9 // int limit2 = maxLen;
10 // for ( n = Header.blink; (limit2-- > 0) && n != &Header; n = n->blink ) {}
11 // assert (n == &Header);
12 // assert (limit1 == limit2);
13}
14
15// The given string is reachable.
16void assertOnHandleList( string & q ) { // with(HeapArea)
17 // HandleNode *n;
18 // for ( n = Header.flink; n != &Header; n = n->flink ) {
19 // if ( n == & q.inner->Handle ) return;
20 // }
21 // assert( false );
22}
23
24
25// Purpose: call each function in string.hfa, top to bottom
26
27int main () {
28
29 #ifdef STRING_SHARING_OFF
30 string_sharectx c = { NO_SHARING };
31 #endif
32
33 string s = "hello";
34 string s2 = "hello";
35 string s3 = "world";
36 string frag = "ell";
37
38 // IO operator, x2
39 sout | s | s | s; // hello hello hello
40
41 // empty ctor then assign
42 string sxx;
43 sout | sxx; // (blank line)
44 sxx = s;
45 sout | sxx; // hello
46
47 // Comparisons
48 // all print "true false"
49 sout | (s == s2) | (s == s3);
50 sout | (s != s3) | (s != s2);
51 sout | (s == "hello") | (s == "world");
52 sout | (s != "world") | (s != "hello");
53 sout | ( frag == s(1,3) ) | ( s3 == s(1,3) );
54 sout | ( s3 != s(1,3) ) | ( frag != s(1,3) );
55 sout | ( s2(1,3) == s(1,3) ) | ( s3(1,3) == s(1,3) );
56 sout | ( s3(1,3) != s(1,3) ) | ( s2(1,3) != s(1,3) );
57 sout | ( s(1,3) == frag ) | ( s(1,3) == s3 );
58 sout | ( s(1,3) != s3 ) | ( s(1,3) != frag );
59 sout | ( s(1,3) == "ell" ) | ( s(1,3) == "world" );
60 sout | ( s(1,3) != "world" ) | ( s(1,3) != "ell" );
61
62
63 assertWellFormedHandleList( 10 );
64 //
65 // breadth Constructors
66 //
67 {
68 string b1 = "1234567";
69 sout | b1; // 1234567
70
71 string b1x = { "1234567", 3 };
72 sout | b1x; // 123
73
74 string b2 = s;
75 sout | b2; // hello
76
77 string b2x = { s, 4 };
78 sout | b2x; // hell
79
80 // todo: a plain string &
81 const string & s_ref = s;
82 string b3 = s_ref;
83 sout | b3; // hello
84
85 & s_ref = & s3;
86 b3 = s_ref;
87 sout | b3; // world
88
89 const string & s_constref = s;
90 string b4 = s_constref;
91 sout | b4; // hello
92
93 & s_constref = & s3;
94 b4 = s_constref;
95 sout | b4; // world
96
97 string b5 = 'Q';
98 sout | b5; // Q
99
100
101 }
102 assertWellFormedHandleList( 10 );
103 //
104 // Assignments
105 //
106 {
107 string b = "xxx";
108
109 b = "1234567";
110 sout | b; // 1234567
111
112 b = "xxx";
113 b = s;
114 sout | b; // hello
115
116 b = "xxx";
117 b = 'Q';
118 sout | b; // Q
119
120 b = "xxx";
121 assign( b, "1234567", 3 );
122 sout | b; // 123
123
124 b = "xxx";
125 assign( b, s, 4 );
126 sout | b; // hell
127
128 b = "xxx";
129 strcpy(b, "1234567");
130 sout | b; // 1234567
131
132 b = "xxx";
133 strcpy(b, s);
134 sout | b; // hello
135
136 b = "xxx";
137 strncpy( b, "1234567", 3 );
138 sout | b; // 123
139
140 b = "xxx";
141 strncpy( b, s, 4 );
142 sout | b; // hell
143 }
144 assertWellFormedHandleList( 10 );
145
146
147
148 sout | size(s); // 5
149
150 //
151 // concatenation/append
152 //
153
154 string sx = s + s3;
155 assertWellFormedHandleList( 10 );
156 sout | sx; // helloworld
157 assertWellFormedHandleList( 10 );
158 sx = "xx";
159 assertWellFormedHandleList( 10 );
160 sx = s + s3;
161 assertWellFormedHandleList( 10 );
162 sout | sx; // helloworld
163 assertWellFormedHandleList( 10 );
164
165 sx += '!';
166 sout | sx; // helloworld!
167 sx = s + '!';
168 sout | sx; // hello!
169
170 sx = s;
171 sx += s;
172 sout | sx; // hellohello
173 assertWellFormedHandleList( 10 );
174 sx += ", friend";
175 sout | sx; // hellohello, friend
176
177 sx = s + ", friend";
178 sout | sx; // hello, friend
179
180 sx = "bye, " + "friend";
181 sout | sx; // bye, friend
182
183 sx = "o";
184 strcat( sx, s );
185 sout | sx; // ohello
186
187 sx = "o";
188 append( sx, s, 4 );
189 sout | sx; // ohell
190
191 sx = "o";
192 strncat( sx, s, 4 );
193 sout | sx; // ohell
194
195 sx = "o";
196 strcat( sx, "mydarling" );
197 sout | sx; // omydarling
198
199 sx = "o";
200 append( sx, "mydarling", 2 );
201 sout | sx; // omy
202
203 sx = "o";
204 strncat( sx, "mydarling", 2 );
205 sout | sx; // omy
206
207 //
208 // repetition
209 //
210
211 sx = s;
212 sx *= 4;
213 sout | sx; // hellohellohellohello
214
215 sx = s * 3;
216 sout | sx; // hellohellohello
217
218 sx = 'Q' * (size_t)3;
219 sout | sx; // QQQ
220
221 sx = "asdf" * 3;
222 sout | sx; // asdfasdfasdf
223
224 //
225 // slicing
226 //
227
228 // Range cases treated thoroughly in "string-overwrite" test.
229 // Composability with comparison and search are demoed above and below.
230 // Coverage here adds the single-argument ("rest of string") overload.
231
232 sx = s;
233 sout | sx(3); // lo
234 sx(3) = "iocentric";
235 sout | s | sx; // hello heliocentric
236
237 //
238 // character access
239 //
240
241 char c = s[1];
242 sout | c; // e
243
244 s[3] = "p!!!";
245 sout | s; // help!!!o
246
247 s[7] = '!';
248 sout | s; // help!!!!
249
250 s[7] = "";
251 sout | s; // help!!!
252
253 sout | s[3]; // p
254
255 //
256 // search
257 //
258
259 s += '?'; // already tested
260 sout | contains( s, 'h' ) | contains( s, '?' ) | contains( s, 'o' ); // true true false
261
262 sout
263 | find( s, 'h' ) // 0
264 | find( s, '!' ) // 4
265 | find( s, '?' ) // 7
266 | find( s, 'o' ); // 8, not found
267
268 string alphabet = "abcdefghijklmnopqrstuvwxyz";
269
270 sout
271 | find( alphabet, "" ) // 0
272 | find( alphabet, "a" ) // 0
273 | find( alphabet, "z" ) // 25
274 | find( alphabet, "abc" ) // 0
275 | find( alphabet, "abq" ) // 26, not found
276 | find( alphabet, "def"); // 3
277
278 sout
279 | includes( alphabet, "" ) // true
280 | includes( alphabet, "a" ) // true
281 | includes( alphabet, "z" ) // true
282 | includes( alphabet, "abc" ) // true
283 | includes( alphabet, "abq" ) // false
284 | includes( alphabet, "def"); // true
285
286 {
287 char *empty_c = "";
288 char *a_c = "a";
289 char *z_c = "z";
290 char *dex_c = "dex";
291
292 sout
293 | find( alphabet, empty_c ) // 0
294 | find( alphabet, a_c ) // 0
295 | find( alphabet, dex_c ) // 26, not found
296 | find( alphabet, dex_c, 2 ); // 3
297
298 sout
299 | includes( alphabet, empty_c ) // true
300 | includes( alphabet, a_c ) // true
301 | includes( alphabet, dex_c ) // false
302 | includes( alphabet, dex_c, 2 ); // true
303
304 sout
305 | startsWith( alphabet, a_c) // true
306 | endsWith ( alphabet, a_c) // false
307 | startsWith( alphabet, z_c) // false
308 | endsWith ( alphabet, z_c); // true
309
310 string empty = empty_c;
311 string a = a_c;
312 string z = z_c;
313 string dex = dex_c;
314
315 sout
316 | find( alphabet, empty ) // 0
317 | find( alphabet, a ) // 0
318 | find( alphabet, dex ) // 26, not found
319 | find( alphabet, dex(0,2) ); // 3
320
321 sout
322 | includes( alphabet, empty ) // true
323 | includes( alphabet, a ) // true
324 | includes( alphabet, dex ) // false
325 | includes( alphabet, dex(0,2) ); // true
326
327 sout
328 | startsWith( alphabet, a) // true
329 | endsWith ( alphabet, a) // false
330 | startsWith( alphabet, z) // false
331 | endsWith ( alphabet, z); // true
332 }
333
334 sout
335 | find( alphabet , "def") // 3
336 | find( alphabet( 0, 26), "def") // 3
337 | find( alphabet( 2, 24), "def") // 1
338 | find( alphabet( 3, 23), "def") // 0
339 | find( alphabet( 4, 22), "def") // 22, not found
340 | find( alphabet( 4, 22), "ef") // 0
341 | find( alphabet( 0, 6), "def") // 3
342 | find( alphabet( 0, 5), "def") // 5, not found
343 | find( alphabet( 0, 5), "de" ); // 3
344
345 sout
346 | includes( alphabet , "def") // true
347 | includes( alphabet( 0, 26), "def") // true
348 | includes( alphabet( 2, 24), "def") // true
349 | includes( alphabet( 3, 23), "def") // true
350 | includes( alphabet( 4, 22), "def") // false
351 | includes( alphabet( 4, 22), "ef") // true
352 | includes( alphabet( 0, 6), "def") // true
353 | includes( alphabet( 0, 5), "def") // false
354 | includes( alphabet( 0, 5), "de" ); // true
355
356 sout
357 | startsWith( alphabet , "abc") // true
358 | startsWith( alphabet( 0, 26), "abc") // true
359 | startsWith( alphabet( 1, 25), "abc") // false
360 | startsWith( alphabet( 1, 25), "bc") // true
361 | startsWith( alphabet( 0, 26), "abc") // true
362 | startsWith( alphabet( 0, 4), "abc") // true
363 | startsWith( alphabet( 0, 3), "abc") // true
364 | startsWith( alphabet( 0, 3), "ab" ) // true
365 | startsWith( alphabet , "xyz"); // false
366
367 sout
368 | endsWith( alphabet , "xyz") // true
369 | endsWith( alphabet , "xyzz") // false
370 | endsWith( alphabet( 0, 26), "xyz") // true
371 | endsWith( alphabet( 0, 25), "xyz") // false
372 | endsWith( alphabet( 0, 25), "xy" ) // true
373 | endsWith( alphabet( 0, 26), "xyz") // true
374 | endsWith( alphabet(23, 3), "xyz") // true
375 | endsWith( alphabet(24, 2), "xyz") // false
376 | endsWith( alphabet(24, 2), "yz") // true
377 | endsWith( alphabet , "abc"); // false
378
379 charclass cc_cba = {"cba"};
380 charclass cc_onml = {"onml"};
381 charclass cc_alphabet = {alphabet};
382
383 // include (rest of the) numbers: tell me where the numbers stop
384 // exclude (until) numbers: tell me where the numbers start (include rest of the non-numbers)
385
386 sout
387 | include( alphabet, cc_cba ) // 3
388 | exclude( alphabet, cc_cba ) // 0
389 | include( alphabet, cc_onml ) // 0
390 | exclude( alphabet, cc_onml ) // 11
391 | include( alphabet, cc_alphabet ) // 26
392 | exclude( alphabet, cc_alphabet ); // 0
393}
394
Note: See TracBrowser for help on using the repository browser.