source: tests/collections/string-api-coverage.cfa@ 9aa8dcc

Last change on this file since 9aa8dcc was 9aa8dcc, checked in by Peter A. Buhr <pabuhr@…>, 6 months ago

update test programs with new length name "len"

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