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

ADTast-experimentalenumpthread-emulationqualifiedEnum
Last change on this file since 6f7aff3 was 6f7aff3, checked in by Michael Brooks <mlbrooks@…>, 3 years ago

String hybrid assignment to unshared now optimizes to overwrite instead of copy.

  • Property mode set to 100644
File size: 8.8 KB
Line 
1#include <containers/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    sout | "string sharing disabled";
31    string_sharectx c = { NO_SHARING };
32    #endif
33
34    string s = "hello";
35    string s2 = "hello";
36    string s3 = "world";
37    string frag = "ell";
38
39    // IO operator, x2
40    sout | s | s | s;
41
42    // Comparisons
43    // all print "true false"
44    sout | (s == s2) | (s == s3);
45    sout | (s != s3) | (s != s2);
46    sout | (s == "hello") | (s == "world");
47    sout | (s != "world") | (s != "hello");
48    sout | ( frag == s(1,4) ) | ( s3   == s(1,4) );
49    sout | ( s3   != s(1,4) ) | ( frag != s(1,4) );
50    sout | ( s2(1,4) == s(1,4) ) | ( s3(1,4)   == s(1,4) );
51    sout | ( s3(1,4) != s(1,4) ) | ( s2(1,4)   != s(1,4) );
52    sout | ( s(1,4) == frag ) | ( s(1,4) == s3   );
53    sout | ( s(1,4) != s3   ) | ( s(1,4) != frag );
54    sout | ( s(1,4) == "ell"   ) | ( s(1,4) == "world" );
55    sout | ( s(1,4) != "world" ) | ( s(1,4) != "ell"   );
56
57
58                                            assertWellFormedHandleList( 10 );
59    //
60    // breadth Constructors
61    //
62    {
63        string b1 = { "1234567", 3 };
64        sout | b1; // 123
65
66        string b2 = s;
67        sout | b2; // hello
68
69        // todo: a plain string &
70        const string & s_ref = s;
71        string b3 = s_ref;
72        sout | b3;  // hello
73
74        & s_ref = & s3;
75        b3 = s_ref;
76        sout | b3; // world
77
78        const string & s_constref = s;
79        string b4 = s_constref;
80        sout | b4; // hello
81
82        & s_constref = & s3;
83        b4 = s_constref;
84        sout | b4;  // world
85    }
86                                            assertWellFormedHandleList( 10 );
87
88    sout | size(s); // 5
89
90    //
91    // concatenation/append
92    //
93
94    string sx = s + s3;
95                                            assertWellFormedHandleList( 10 );
96    sout | sx; // helloworld
97                                            assertWellFormedHandleList( 10 );
98    sx = "xx";
99                                            assertWellFormedHandleList( 10 );
100    sx = s + s3;
101                                            assertWellFormedHandleList( 10 );
102    sout | sx; // helloworld
103                                            assertWellFormedHandleList( 10 );
104
105    sx += '!';
106    sout | sx; // helloworld!
107    sx = s + '!';
108    sout | sx; // hello!
109
110    sx = s;
111    sx += s;
112    sout | sx; // hellohello
113                                            assertWellFormedHandleList( 10 );
114    sx += ", friend";   
115    sout | sx; // hellohello, friend
116
117    sx = s + ", friend";
118    sout | sx; // hello, friend
119
120    sx = "bye, " + "friend";
121    sout | sx; // bye, friend
122
123    //
124    // repetition
125    //
126    sx = s * 3;
127    sout | sx; // hellohellohello
128
129    sx = 'Q' * (size_t)3;
130    sout | sx; // QQQ
131
132    sx = "asdf" * 3;
133    sout | sx; // asdfasdfasdf
134
135    //
136    // slicing
137    //
138
139    //...
140
141    //
142    // character access
143    //
144
145    char c = s[1];
146    sout | c;   // e
147
148    s[3] = "p!!!";
149    sout | s;   // help!!!o
150
151    s[7] = '!';
152    sout | s;   // help!!!!
153
154    s[7] = "";
155    sout | s;   // help!!!
156
157    sout | s[3]; // p
158
159    //
160    // search
161    //
162
163    s += '?'; // already tested
164    sout | contains( s, 'h' ) | contains( s, '?' ) | contains( s, 'o' ); // true true false
165
166    sout
167        | find( s, 'h' )  // 0
168        | find( s, '!' )  // 4
169        | find( s, '?' )  // 7
170        | find( s, 'o' ); // 8, not found
171
172    string alphabet = "abcdefghijklmnopqrstuvwxyz";
173
174    sout
175        | find( alphabet, "" )    // 0
176        | find( alphabet, "a" )   // 0
177        | find( alphabet, "z" )   // 25
178        | find( alphabet, "abc" ) // 0
179        | find( alphabet, "abq" ) // 26, not found
180        | find( alphabet, "def"); // 3
181   
182    sout
183        | includes( alphabet, "" )    // true
184        | includes( alphabet, "a" )   // true
185        | includes( alphabet, "z" )   // true
186        | includes( alphabet, "abc" ) // true
187        | includes( alphabet, "abq" ) // false
188        | includes( alphabet, "def"); // true
189   
190    {
191        char *empty_c = "";
192        char *a_c = "a";
193        char *z_c = "z";
194        char *dex_c = "dex";
195
196        sout
197            | find( alphabet, empty_c )   // 0
198            | find( alphabet, a_c )       // 0
199            | find( alphabet, dex_c )     // 26, not found
200            | find( alphabet, dex_c, 2 ); // 3
201
202        sout
203            | includes( alphabet, empty_c )   // true
204            | includes( alphabet, a_c )       // true
205            | includes( alphabet, dex_c )     // false
206            | includes( alphabet, dex_c, 2 ); // true
207
208        sout
209            | startsWith( alphabet, a_c)            // true
210            | endsWith  ( alphabet, a_c)            // false
211            | startsWith( alphabet, z_c)            // false
212            | endsWith  ( alphabet, z_c);           // true
213
214        string empty = empty_c;
215        string a = a_c;
216        string z = z_c;
217        string dex = dex_c;
218
219        sout
220            | find( alphabet, empty )     // 0
221            | find( alphabet, a )         // 0
222            | find( alphabet, dex )       // 26, not found
223            | find( alphabet, dex(0,2) ); // 3
224
225        sout
226            | includes( alphabet, empty )     // true
227            | includes( alphabet, a )         // true
228            | includes( alphabet, dex )       // false
229            | includes( alphabet, dex(0,2) ); // true
230
231        sout
232            | startsWith( alphabet, a)            // true
233            | endsWith  ( alphabet, a)            // false
234            | startsWith( alphabet, z)            // false
235            | endsWith  ( alphabet, z);           // true
236    }
237
238    sout
239        | find( alphabet        , "def")  // 3
240        | find( alphabet( 0, 26), "def")  // 3
241        | find( alphabet( 2, 26), "def")  // 1
242        | find( alphabet( 3, 26), "def")  // 0
243        | find( alphabet( 4, 26), "def")  // 22, not found
244        | find( alphabet( 4, 26),  "ef")  // 0
245        | find( alphabet( 0,  6), "def")  // 3
246        | find( alphabet( 0,  5), "def")  // 5, not found
247        | find( alphabet( 0,  5), "de" ); // 3
248
249    sout
250        | includes( alphabet        , "def")  // true
251        | includes( alphabet( 0, 26), "def")  // true
252        | includes( alphabet( 2, 26), "def")  // true
253        | includes( alphabet( 3, 26), "def")  // true
254        | includes( alphabet( 4, 26), "def")  // false
255        | includes( alphabet( 4, 26),  "ef")  // true
256        | includes( alphabet( 0,  6), "def")  // true
257        | includes( alphabet( 0,  5), "def")  // false
258        | includes( alphabet( 0,  5), "de" ); // true
259
260    sout
261        | startsWith( alphabet        , "abc")  // true
262        | startsWith( alphabet( 0, 26), "abc")  // true
263        | startsWith( alphabet( 1, 26), "abc")  // false
264        | startsWith( alphabet( 1, 26),  "bc")  // true
265        | startsWith( alphabet( 0, 26), "abc")  // true
266        | startsWith( alphabet( 0,  4), "abc")  // true
267        | startsWith( alphabet( 0,  3), "abc")  // true
268        | startsWith( alphabet( 0,  3), "ab" )  // true
269        | startsWith( alphabet        , "xyz"); // false
270
271    sout
272        | endsWith( alphabet        , "xyz")  // true
273        | endsWith( alphabet        , "xyzz") // false
274        | endsWith( alphabet( 0, 26), "xyz")  // true
275        | endsWith( alphabet( 0, 25), "xyz")  // false
276        | endsWith( alphabet( 0, 25), "xy" )  // true
277        | endsWith( alphabet( 0, 26), "xyz")  // true
278        | endsWith( alphabet(23, 26), "xyz")  // true
279        | endsWith( alphabet(24, 26), "xyz")  // false
280        | endsWith( alphabet(24, 26),  "yz")  // true
281        | endsWith( alphabet        , "abc"); // false
282
283    charclass cc_cba = {"cba"};
284    charclass cc_onml = {"onml"};
285    charclass cc_alphabet = {alphabet};
286
287    // include (rest of the) numbers:  tell me where the numbers stop
288    // exclude (until)       numbers:  tell me where the numbers start (include rest of the non-numbers)
289
290    sout
291        | include( alphabet, cc_cba )  // 3
292        | exclude( alphabet, cc_cba )  // 0
293        | include( alphabet, cc_onml )  // 0
294        | exclude( alphabet, cc_onml )  // 11
295        | include( alphabet, cc_alphabet )  // 26
296        | exclude( alphabet, cc_alphabet ); // 0
297}
298
Note: See TracBrowser for help on using the repository browser.