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

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since eb9c2dc was f450f2f, checked in by Michael Brooks <mlbrooks@…>, 3 years ago

Baseline of the string implementation.

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