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

stuck-waitfor-destruct
Last change on this file since 5bf685f was e891349, checked in by Michael Brooks <mlbrooks@…>, 2 years ago

Tweak string assignment-strcpy-strncpy and concatenate-strcat-strncat declarations.

Implement -n- versions correctly.

Refactor to include string_res layer.

Add missing tests.

  • Property mode set to 100644
File size: 10.1 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", 3 };
69 sout | b1; // 123
70
71 string b2 = s;
72 sout | b2; // hello
73
74 // todo: a plain string &
75 const string & s_ref = s;
76 string b3 = s_ref;
77 sout | b3; // hello
78
79 & s_ref = & s3;
80 b3 = s_ref;
81 sout | b3; // world
82
83 const string & s_constref = s;
84 string b4 = s_constref;
85 sout | b4; // hello
86
87 & s_constref = & s3;
88 b4 = s_constref;
89 sout | b4; // world
90 }
91 assertWellFormedHandleList( 10 );
92 //
93 // Assignments
94 //
95 {
96 string b = "xxx";
97
98 b = "1234567";
99 sout | b; // 1234567
100
101 b = "xxx";
102 b = s;
103 sout | b; // hello
104
105 b = "xxx";
106 b = 'Q';
107 sout | b; // Q
108
109 b = "xxx";
110 assign( b, "1234567", 3 );
111 sout | b; // 123
112
113 b = "xxx";
114 assign( b, s, 4 );
115 sout | b; // hell
116
117 b = "xxx";
118 strcpy(b, "1234567");
119 sout | b; // 1234567
120
121 b = "xxx";
122 strcpy(b, s);
123 sout | b; // hello
124
125 b = "xxx";
126 strncpy( b, "1234567", 3 );
127 sout | b; // 123
128
129 b = "xxx";
130 strncpy( b, s, 4 );
131 sout | b; // hell
132 }
133 assertWellFormedHandleList( 10 );
134
135
136
137 sout | size(s); // 5
138
139 //
140 // concatenation/append
141 //
142
143 string sx = s + s3;
144 assertWellFormedHandleList( 10 );
145 sout | sx; // helloworld
146 assertWellFormedHandleList( 10 );
147 sx = "xx";
148 assertWellFormedHandleList( 10 );
149 sx = s + s3;
150 assertWellFormedHandleList( 10 );
151 sout | sx; // helloworld
152 assertWellFormedHandleList( 10 );
153
154 sx += '!';
155 sout | sx; // helloworld!
156 sx = s + '!';
157 sout | sx; // hello!
158
159 sx = s;
160 sx += s;
161 sout | sx; // hellohello
162 assertWellFormedHandleList( 10 );
163 sx += ", friend";
164 sout | sx; // hellohello, friend
165
166 sx = s + ", friend";
167 sout | sx; // hello, friend
168
169 sx = "bye, " + "friend";
170 sout | sx; // bye, friend
171
172 sx = "o";
173 strcat( sx, s );
174 sout | sx; // ohello
175
176 sx = "o";
177 append( sx, s, 4 );
178 sout | sx; // ohell
179
180 sx = "o";
181 strncat( sx, s, 4 );
182 sout | sx; // ohell
183
184 sx = "o";
185 strcat( sx, "mydarling" );
186 sout | sx; // omydarling
187
188 sx = "o";
189 append( sx, "mydarling", 2 );
190 sout | sx; // omy
191
192 sx = "o";
193 strncat( sx, "mydarling", 2 );
194 sout | sx; // omy
195
196 //
197 // repetition
198 //
199
200 sx = s;
201 sx *= 4;
202 sout | sx; // hellohellohellohello
203
204 sx = s * 3;
205 sout | sx; // hellohellohello
206
207 sx = 'Q' * (size_t)3;
208 sout | sx; // QQQ
209
210 sx = "asdf" * 3;
211 sout | sx; // asdfasdfasdf
212
213 //
214 // slicing
215 //
216
217 //...
218
219 //
220 // character access
221 //
222
223 char c = s[1];
224 sout | c; // e
225
226 s[3] = "p!!!";
227 sout | s; // help!!!o
228
229 s[7] = '!';
230 sout | s; // help!!!!
231
232 s[7] = "";
233 sout | s; // help!!!
234
235 sout | s[3]; // p
236
237 //
238 // search
239 //
240
241 s += '?'; // already tested
242 sout | contains( s, 'h' ) | contains( s, '?' ) | contains( s, 'o' ); // true true false
243
244 sout
245 | find( s, 'h' ) // 0
246 | find( s, '!' ) // 4
247 | find( s, '?' ) // 7
248 | find( s, 'o' ); // 8, not found
249
250 string alphabet = "abcdefghijklmnopqrstuvwxyz";
251
252 sout
253 | find( alphabet, "" ) // 0
254 | find( alphabet, "a" ) // 0
255 | find( alphabet, "z" ) // 25
256 | find( alphabet, "abc" ) // 0
257 | find( alphabet, "abq" ) // 26, not found
258 | find( alphabet, "def"); // 3
259
260 sout
261 | includes( alphabet, "" ) // true
262 | includes( alphabet, "a" ) // true
263 | includes( alphabet, "z" ) // true
264 | includes( alphabet, "abc" ) // true
265 | includes( alphabet, "abq" ) // false
266 | includes( alphabet, "def"); // true
267
268 {
269 char *empty_c = "";
270 char *a_c = "a";
271 char *z_c = "z";
272 char *dex_c = "dex";
273
274 sout
275 | find( alphabet, empty_c ) // 0
276 | find( alphabet, a_c ) // 0
277 | find( alphabet, dex_c ) // 26, not found
278 | find( alphabet, dex_c, 2 ); // 3
279
280 sout
281 | includes( alphabet, empty_c ) // true
282 | includes( alphabet, a_c ) // true
283 | includes( alphabet, dex_c ) // false
284 | includes( alphabet, dex_c, 2 ); // true
285
286 sout
287 | startsWith( alphabet, a_c) // true
288 | endsWith ( alphabet, a_c) // false
289 | startsWith( alphabet, z_c) // false
290 | endsWith ( alphabet, z_c); // true
291
292 string empty = empty_c;
293 string a = a_c;
294 string z = z_c;
295 string dex = dex_c;
296
297 sout
298 | find( alphabet, empty ) // 0
299 | find( alphabet, a ) // 0
300 | find( alphabet, dex ) // 26, not found
301 | find( alphabet, dex(0,2) ); // 3
302
303 sout
304 | includes( alphabet, empty ) // true
305 | includes( alphabet, a ) // true
306 | includes( alphabet, dex ) // false
307 | includes( alphabet, dex(0,2) ); // true
308
309 sout
310 | startsWith( alphabet, a) // true
311 | endsWith ( alphabet, a) // false
312 | startsWith( alphabet, z) // false
313 | endsWith ( alphabet, z); // true
314 }
315
316 sout
317 | find( alphabet , "def") // 3
318 | find( alphabet( 0, 26), "def") // 3
319 | find( alphabet( 2, 26), "def") // 1
320 | find( alphabet( 3, 26), "def") // 0
321 | find( alphabet( 4, 26), "def") // 22, not found
322 | find( alphabet( 4, 26), "ef") // 0
323 | find( alphabet( 0, 6), "def") // 3
324 | find( alphabet( 0, 5), "def") // 5, not found
325 | find( alphabet( 0, 5), "de" ); // 3
326
327 sout
328 | includes( alphabet , "def") // true
329 | includes( alphabet( 0, 26), "def") // true
330 | includes( alphabet( 2, 26), "def") // true
331 | includes( alphabet( 3, 26), "def") // true
332 | includes( alphabet( 4, 26), "def") // false
333 | includes( alphabet( 4, 26), "ef") // true
334 | includes( alphabet( 0, 6), "def") // true
335 | includes( alphabet( 0, 5), "def") // false
336 | includes( alphabet( 0, 5), "de" ); // true
337
338 sout
339 | startsWith( alphabet , "abc") // true
340 | startsWith( alphabet( 0, 26), "abc") // true
341 | startsWith( alphabet( 1, 26), "abc") // false
342 | startsWith( alphabet( 1, 26), "bc") // true
343 | startsWith( alphabet( 0, 26), "abc") // true
344 | startsWith( alphabet( 0, 4), "abc") // true
345 | startsWith( alphabet( 0, 3), "abc") // true
346 | startsWith( alphabet( 0, 3), "ab" ) // true
347 | startsWith( alphabet , "xyz"); // false
348
349 sout
350 | endsWith( alphabet , "xyz") // true
351 | endsWith( alphabet , "xyzz") // false
352 | endsWith( alphabet( 0, 26), "xyz") // true
353 | endsWith( alphabet( 0, 25), "xyz") // false
354 | endsWith( alphabet( 0, 25), "xy" ) // true
355 | endsWith( alphabet( 0, 26), "xyz") // true
356 | endsWith( alphabet(23, 26), "xyz") // true
357 | endsWith( alphabet(24, 26), "xyz") // false
358 | endsWith( alphabet(24, 26), "yz") // true
359 | endsWith( alphabet , "abc"); // false
360
361 charclass cc_cba = {"cba"};
362 charclass cc_onml = {"onml"};
363 charclass cc_alphabet = {alphabet};
364
365 // include (rest of the) numbers: tell me where the numbers stop
366 // exclude (until) numbers: tell me where the numbers start (include rest of the non-numbers)
367
368 sout
369 | include( alphabet, cc_cba ) // 3
370 | exclude( alphabet, cc_cba ) // 0
371 | include( alphabet, cc_onml ) // 0
372 | exclude( alphabet, cc_onml ) // 11
373 | include( alphabet, cc_alphabet ) // 26
374 | exclude( alphabet, cc_alphabet ); // 0
375}
376
Note: See TracBrowser for help on using the repository browser.