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

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

Harmonize string constructors with assignments, refactor implementations to include string_res, add missing tests

  • Property mode set to 100644
File size: 10.3 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,4) ) | ( s3   == s(1,4) );
54    sout | ( s3   != s(1,4) ) | ( frag != s(1,4) );
55    sout | ( s2(1,4) == s(1,4) ) | ( s3(1,4)   == s(1,4) );
56    sout | ( s3(1,4) != s(1,4) ) | ( s2(1,4)   != s(1,4) );
57    sout | ( s(1,4) == frag ) | ( s(1,4) == s3   );
58    sout | ( s(1,4) != s3   ) | ( s(1,4) != frag );
59    sout | ( s(1,4) == "ell"   ) | ( s(1,4) == "world" );
60    sout | ( s(1,4) != "world" ) | ( s(1,4) != "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    //...
229
230    //
231    // character access
232    //
233
234    char c = s[1];
235    sout | c;   // e
236
237    s[3] = "p!!!";
238    sout | s;   // help!!!o
239
240    s[7] = '!';
241    sout | s;   // help!!!!
242
243    s[7] = "";
244    sout | s;   // help!!!
245
246    sout | s[3]; // p
247
248    //
249    // search
250    //
251
252    s += '?'; // already tested
253    sout | contains( s, 'h' ) | contains( s, '?' ) | contains( s, 'o' ); // true true false
254
255    sout
256        | find( s, 'h' )  // 0
257        | find( s, '!' )  // 4
258        | find( s, '?' )  // 7
259        | find( s, 'o' ); // 8, not found
260
261    string alphabet = "abcdefghijklmnopqrstuvwxyz";
262
263    sout
264        | find( alphabet, "" )    // 0
265        | find( alphabet, "a" )   // 0
266        | find( alphabet, "z" )   // 25
267        | find( alphabet, "abc" ) // 0
268        | find( alphabet, "abq" ) // 26, not found
269        | find( alphabet, "def"); // 3
270   
271    sout
272        | includes( alphabet, "" )    // true
273        | includes( alphabet, "a" )   // true
274        | includes( alphabet, "z" )   // true
275        | includes( alphabet, "abc" ) // true
276        | includes( alphabet, "abq" ) // false
277        | includes( alphabet, "def"); // true
278   
279    {
280        char *empty_c = "";
281        char *a_c = "a";
282        char *z_c = "z";
283        char *dex_c = "dex";
284
285        sout
286            | find( alphabet, empty_c )   // 0
287            | find( alphabet, a_c )       // 0
288            | find( alphabet, dex_c )     // 26, not found
289            | find( alphabet, dex_c, 2 ); // 3
290
291        sout
292            | includes( alphabet, empty_c )   // true
293            | includes( alphabet, a_c )       // true
294            | includes( alphabet, dex_c )     // false
295            | includes( alphabet, dex_c, 2 ); // true
296
297        sout
298            | startsWith( alphabet, a_c)            // true
299            | endsWith  ( alphabet, a_c)            // false
300            | startsWith( alphabet, z_c)            // false
301            | endsWith  ( alphabet, z_c);           // true
302
303        string empty = empty_c;
304        string a = a_c;
305        string z = z_c;
306        string dex = dex_c;
307
308        sout
309            | find( alphabet, empty )     // 0
310            | find( alphabet, a )         // 0
311            | find( alphabet, dex )       // 26, not found
312            | find( alphabet, dex(0,2) ); // 3
313
314        sout
315            | includes( alphabet, empty )     // true
316            | includes( alphabet, a )         // true
317            | includes( alphabet, dex )       // false
318            | includes( alphabet, dex(0,2) ); // true
319
320        sout
321            | startsWith( alphabet, a)            // true
322            | endsWith  ( alphabet, a)            // false
323            | startsWith( alphabet, z)            // false
324            | endsWith  ( alphabet, z);           // true
325    }
326
327    sout
328        | find( alphabet        , "def")  // 3
329        | find( alphabet( 0, 26), "def")  // 3
330        | find( alphabet( 2, 26), "def")  // 1
331        | find( alphabet( 3, 26), "def")  // 0
332        | find( alphabet( 4, 26), "def")  // 22, not found
333        | find( alphabet( 4, 26),  "ef")  // 0
334        | find( alphabet( 0,  6), "def")  // 3
335        | find( alphabet( 0,  5), "def")  // 5, not found
336        | find( alphabet( 0,  5), "de" ); // 3
337
338    sout
339        | includes( alphabet        , "def")  // true
340        | includes( alphabet( 0, 26), "def")  // true
341        | includes( alphabet( 2, 26), "def")  // true
342        | includes( alphabet( 3, 26), "def")  // true
343        | includes( alphabet( 4, 26), "def")  // false
344        | includes( alphabet( 4, 26),  "ef")  // true
345        | includes( alphabet( 0,  6), "def")  // true
346        | includes( alphabet( 0,  5), "def")  // false
347        | includes( alphabet( 0,  5), "de" ); // true
348
349    sout
350        | startsWith( alphabet        , "abc")  // true
351        | startsWith( alphabet( 0, 26), "abc")  // true
352        | startsWith( alphabet( 1, 26), "abc")  // false
353        | startsWith( alphabet( 1, 26),  "bc")  // true
354        | startsWith( alphabet( 0, 26), "abc")  // true
355        | startsWith( alphabet( 0,  4), "abc")  // true
356        | startsWith( alphabet( 0,  3), "abc")  // true
357        | startsWith( alphabet( 0,  3), "ab" )  // true
358        | startsWith( alphabet        , "xyz"); // false
359
360    sout
361        | endsWith( alphabet        , "xyz")  // true
362        | endsWith( alphabet        , "xyzz") // false
363        | endsWith( alphabet( 0, 26), "xyz")  // true
364        | endsWith( alphabet( 0, 25), "xyz")  // false
365        | endsWith( alphabet( 0, 25), "xy" )  // true
366        | endsWith( alphabet( 0, 26), "xyz")  // true
367        | endsWith( alphabet(23, 26), "xyz")  // true
368        | endsWith( alphabet(24, 26), "xyz")  // false
369        | endsWith( alphabet(24, 26),  "yz")  // true
370        | endsWith( alphabet        , "abc"); // false
371
372    charclass cc_cba = {"cba"};
373    charclass cc_onml = {"onml"};
374    charclass cc_alphabet = {alphabet};
375
376    // include (rest of the) numbers:  tell me where the numbers stop
377    // exclude (until)       numbers:  tell me where the numbers start (include rest of the non-numbers)
378
379    sout
380        | include( alphabet, cc_cba )  // 3
381        | exclude( alphabet, cc_cba )  // 0
382        | include( alphabet, cc_onml )  // 0
383        | exclude( alphabet, cc_onml )  // 11
384        | include( alphabet, cc_alphabet )  // 26
385        | exclude( alphabet, cc_alphabet ); // 0
386}
387
Note: See TracBrowser for help on using the repository browser.