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

Last change on this file since c17dc80 was f2898df, checked in by Michael Brooks <mlbrooks@…>, 5 months ago

Implement string initialization and assignment from various numeric types

  • Property mode set to 100644
File size: 11.2 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        string b6 = 42;
101        sout | b6; // 42
102
103        string b7 = -42;
104        sout | b7; // -42
105
106        string b8 = 5.5;
107        sout | b8; // 5.5
108
109        string b9 = 5.5L;
110        sout | b9; // 5.5
111
112        string b10 = 5.5+3.4i;
113        sout | b10; // 5.5+3.4i
114
115        string b11 = 5.5L+3.4Li;
116        sout | b11; // 5.5+3.4i
117    }
118                                            assertWellFormedHandleList( 10 );
119    //
120    // Assignments
121    //
122    {
123        string b = "xxx";
124
125        b = "1234567";
126        sout | b; // 1234567
127
128        b = "xxx";
129        b = s;
130        sout | b; // hello
131       
132        b = "xxx";
133        b = 'Q';
134        sout | b; // Q
135       
136        b = "xxx";
137        assign( b, "1234567", 3 );
138        sout | b; // 123
139
140        b = "xxx";
141        assign( b, s, 4 );
142        sout | b; // hell
143
144        b = "xxx";
145        strcpy(b, "1234567");
146        sout | b; // 1234567
147
148        b = "xxx";
149        strcpy(b, s);
150        sout | b; // hello
151       
152        b = "xxx";
153        strncpy( b, "1234567", 3 );
154        sout | b; // 123
155
156        b = "xxx";
157        strncpy( b, s, 4 );
158        sout | b; // hell
159
160        b = 42;
161        sout | b; // 42
162
163        b = -42;
164        sout | b; // -42
165
166        b = 5.5;
167        sout | b; // 5.5
168
169        b = 5.5L;
170        sout | b; // 5.5
171
172        b = 5.5+3.4i;
173        sout | b; // 5.5+3.4i
174
175        b = 5.5L+3.4Li;
176        sout | b; // 5.5+3.4i
177    }
178                                            assertWellFormedHandleList( 10 );
179
180
181
182    sout | size(s); // 5
183
184    //
185    // concatenation/append
186    //
187
188    string sx = s + s3;
189                                            assertWellFormedHandleList( 10 );
190    sout | sx; // helloworld
191                                            assertWellFormedHandleList( 10 );
192    sx = "xx";
193                                            assertWellFormedHandleList( 10 );
194    sx = s + s3;
195                                            assertWellFormedHandleList( 10 );
196    sout | sx; // helloworld
197                                            assertWellFormedHandleList( 10 );
198
199    sx += '!';
200    sout | sx; // helloworld!
201    sx = s + '!';
202    sout | sx; // hello!
203
204    sx = s;
205    sx += s;
206    sout | sx; // hellohello
207                                            assertWellFormedHandleList( 10 );
208    sx += ", friend";   
209    sout | sx; // hellohello, friend
210
211    sx = s + ", friend";
212    sout | sx; // hello, friend
213
214    sx = "bye, " + "friend";
215    sout | sx; // bye, friend
216
217    sx = "o";
218    strcat( sx, s );
219    sout | sx; // ohello
220
221    sx = "o";
222    append( sx, s, 4 );
223    sout | sx; // ohell
224
225    sx = "o";
226    strncat( sx, s, 4 );
227    sout | sx; // ohell
228
229    sx = "o";
230    strcat( sx, "mydarling" );
231    sout | sx; // omydarling
232
233    sx = "o";
234    append( sx, "mydarling", 2 );
235    sout | sx; // omy
236
237    sx = "o";
238    strncat( sx, "mydarling", 2 );
239    sout | sx; // omy
240
241    //
242    // repetition
243    //
244
245    sx = s;
246    sx *= 4;
247    sout | sx; // hellohellohellohello
248
249    sx = s * 3;
250    sout | sx; // hellohellohello
251
252    sx = 'Q' * (size_t)3;
253    sout | sx; // QQQ
254
255    sx = "asdf" * 3;
256    sout | sx; // asdfasdfasdf
257
258    //
259    // slicing
260    //
261
262    // Range cases treated thoroughly in "string-overwrite" test.
263    // Composability with comparison and search are demoed above and below.
264    // Coverage here adds the single-argument ("rest of string") overload.
265
266    sx = s;
267    sout | sx(3); // lo
268    sx(3) = "iocentric";
269    sout | s | sx; // hello heliocentric
270
271    //
272    // character access
273    //
274
275    char c = s[1];
276    sout | c;   // e
277
278    s[3] = "p!!!";
279    sout | s;   // help!!!o
280
281    s[7] = '!';
282    sout | s;   // help!!!!
283
284    s[7] = "";
285    sout | s;   // help!!!
286
287    sout | s[3]; // p
288
289    //
290    // search
291    //
292
293    s += '?'; // already tested
294    sout | contains( s, 'h' ) | contains( s, '?' ) | contains( s, 'o' ); // true true false
295
296    sout
297        | find( s, 'h' )  // 0
298        | find( s, '!' )  // 4
299        | find( s, '?' )  // 7
300        | find( s, 'o' ); // 8, not found
301
302    string alphabet = "abcdefghijklmnopqrstuvwxyz";
303
304    sout
305        | find( alphabet, "" )    // 0
306        | find( alphabet, "a" )   // 0
307        | find( alphabet, "z" )   // 25
308        | find( alphabet, "abc" ) // 0
309        | find( alphabet, "abq" ) // 26, not found
310        | find( alphabet, "def"); // 3
311   
312    sout
313        | includes( alphabet, "" )    // true
314        | includes( alphabet, "a" )   // true
315        | includes( alphabet, "z" )   // true
316        | includes( alphabet, "abc" ) // true
317        | includes( alphabet, "abq" ) // false
318        | includes( alphabet, "def"); // true
319   
320    {
321        char *empty_c = "";
322        char *a_c = "a";
323        char *z_c = "z";
324        char *dex_c = "dex";
325
326        sout
327            | find( alphabet, empty_c )   // 0
328            | find( alphabet, a_c )       // 0
329            | find( alphabet, dex_c )     // 26, not found
330            | find( alphabet, dex_c, 2 ); // 3
331
332        sout
333            | includes( alphabet, empty_c )   // true
334            | includes( alphabet, a_c )       // true
335            | includes( alphabet, dex_c )     // false
336            | includes( alphabet, dex_c, 2 ); // true
337
338        sout
339            | startsWith( alphabet, a_c)            // true
340            | endsWith  ( alphabet, a_c)            // false
341            | startsWith( alphabet, z_c)            // false
342            | endsWith  ( alphabet, z_c);           // true
343
344        string empty = empty_c;
345        string a = a_c;
346        string z = z_c;
347        string dex = dex_c;
348
349        sout
350            | find( alphabet, empty )     // 0
351            | find( alphabet, a )         // 0
352            | find( alphabet, dex )       // 26, not found
353            | find( alphabet, dex(0,2) ); // 3
354
355        sout
356            | includes( alphabet, empty )     // true
357            | includes( alphabet, a )         // true
358            | includes( alphabet, dex )       // false
359            | includes( alphabet, dex(0,2) ); // true
360
361        sout
362            | startsWith( alphabet, a)            // true
363            | endsWith  ( alphabet, a)            // false
364            | startsWith( alphabet, z)            // false
365            | endsWith  ( alphabet, z);           // true
366    }
367
368    sout
369        | find( alphabet        , "def")  // 3
370        | find( alphabet( 0, 26), "def")  // 3
371        | find( alphabet( 2, 24), "def")  // 1
372        | find( alphabet( 3, 23), "def")  // 0
373        | find( alphabet( 4, 22), "def")  // 22, not found
374        | find( alphabet( 4, 22),  "ef")  // 0
375        | find( alphabet( 0,  6), "def")  // 3
376        | find( alphabet( 0,  5), "def")  // 5, not found
377        | find( alphabet( 0,  5), "de" ); // 3
378
379    sout
380        | includes( alphabet        , "def")  // true
381        | includes( alphabet( 0, 26), "def")  // true
382        | includes( alphabet( 2, 24), "def")  // true
383        | includes( alphabet( 3, 23), "def")  // true
384        | includes( alphabet( 4, 22), "def")  // false
385        | includes( alphabet( 4, 22),  "ef")  // true
386        | includes( alphabet( 0,  6), "def")  // true
387        | includes( alphabet( 0,  5), "def")  // false
388        | includes( alphabet( 0,  5), "de" ); // true
389
390    sout
391        | startsWith( alphabet        , "abc")  // true
392        | startsWith( alphabet( 0, 26), "abc")  // true
393        | startsWith( alphabet( 1, 25), "abc")  // false
394        | startsWith( alphabet( 1, 25),  "bc")  // true
395        | startsWith( alphabet( 0, 26), "abc")  // true
396        | startsWith( alphabet( 0,  4), "abc")  // true
397        | startsWith( alphabet( 0,  3), "abc")  // true
398        | startsWith( alphabet( 0,  3), "ab" )  // true
399        | startsWith( alphabet        , "xyz"); // false
400
401    sout
402        | endsWith( alphabet        , "xyz")  // true
403        | endsWith( alphabet        , "xyzz") // false
404        | endsWith( alphabet( 0, 26), "xyz")  // true
405        | endsWith( alphabet( 0, 25), "xyz")  // false
406        | endsWith( alphabet( 0, 25), "xy" )  // true
407        | endsWith( alphabet( 0, 26), "xyz")  // true
408        | endsWith( alphabet(23,  3), "xyz")  // true
409        | endsWith( alphabet(24,  2), "xyz")  // false
410        | endsWith( alphabet(24,  2),  "yz")  // true
411        | endsWith( alphabet        , "abc"); // false
412
413    charclass cc_cba = {"cba"};
414    charclass cc_onml = {"onml"};
415    charclass cc_alphabet = {alphabet};
416
417    // include (rest of the) numbers:  tell me where the numbers stop
418    // exclude (until)       numbers:  tell me where the numbers start (include rest of the non-numbers)
419
420    sout
421        | include( alphabet, cc_cba )  // 3
422        | exclude( alphabet, cc_cba )  // 0
423        | include( alphabet, cc_onml )  // 0
424        | exclude( alphabet, cc_onml )  // 11
425        | include( alphabet, cc_alphabet )  // 26
426        | exclude( alphabet, cc_alphabet ); // 0
427}
428
Note: See TracBrowser for help on using the repository browser.