1 | #include <containers/string.hfa>
|
---|
2 | #include <string_sharectx.hfa>
|
---|
3 |
|
---|
4 | void 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.
|
---|
16 | void 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 |
|
---|
27 | int 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 | sout | size(s); // 5
|
---|
94 |
|
---|
95 | //
|
---|
96 | // concatenation/append
|
---|
97 | //
|
---|
98 |
|
---|
99 | string sx = s + s3;
|
---|
100 | assertWellFormedHandleList( 10 );
|
---|
101 | sout | sx; // helloworld
|
---|
102 | assertWellFormedHandleList( 10 );
|
---|
103 | sx = "xx";
|
---|
104 | assertWellFormedHandleList( 10 );
|
---|
105 | sx = s + s3;
|
---|
106 | assertWellFormedHandleList( 10 );
|
---|
107 | sout | sx; // helloworld
|
---|
108 | assertWellFormedHandleList( 10 );
|
---|
109 |
|
---|
110 | sx += '!';
|
---|
111 | sout | sx; // helloworld!
|
---|
112 | sx = s + '!';
|
---|
113 | sout | sx; // hello!
|
---|
114 |
|
---|
115 | sx = s;
|
---|
116 | sx += s;
|
---|
117 | sout | sx; // hellohello
|
---|
118 | assertWellFormedHandleList( 10 );
|
---|
119 | sx += ", friend";
|
---|
120 | sout | sx; // hellohello, friend
|
---|
121 |
|
---|
122 | sx = s + ", friend";
|
---|
123 | sout | sx; // hello, friend
|
---|
124 |
|
---|
125 | sx = "bye, " + "friend";
|
---|
126 | sout | sx; // bye, friend
|
---|
127 |
|
---|
128 | //
|
---|
129 | // repetition
|
---|
130 | //
|
---|
131 | sx = s * 3;
|
---|
132 | sout | sx; // hellohellohello
|
---|
133 |
|
---|
134 | sx = 'Q' * (size_t)3;
|
---|
135 | sout | sx; // QQQ
|
---|
136 |
|
---|
137 | sx = "asdf" * 3;
|
---|
138 | sout | sx; // asdfasdfasdf
|
---|
139 |
|
---|
140 | //
|
---|
141 | // slicing
|
---|
142 | //
|
---|
143 |
|
---|
144 | //...
|
---|
145 |
|
---|
146 | //
|
---|
147 | // character access
|
---|
148 | //
|
---|
149 |
|
---|
150 | char c = s[1];
|
---|
151 | sout | c; // e
|
---|
152 |
|
---|
153 | s[3] = "p!!!";
|
---|
154 | sout | s; // help!!!o
|
---|
155 |
|
---|
156 | s[7] = '!';
|
---|
157 | sout | s; // help!!!!
|
---|
158 |
|
---|
159 | s[7] = "";
|
---|
160 | sout | s; // help!!!
|
---|
161 |
|
---|
162 | sout | s[3]; // p
|
---|
163 |
|
---|
164 | //
|
---|
165 | // search
|
---|
166 | //
|
---|
167 |
|
---|
168 | s += '?'; // already tested
|
---|
169 | sout | contains( s, 'h' ) | contains( s, '?' ) | contains( s, 'o' ); // true true false
|
---|
170 |
|
---|
171 | sout
|
---|
172 | | find( s, 'h' ) // 0
|
---|
173 | | find( s, '!' ) // 4
|
---|
174 | | find( s, '?' ) // 7
|
---|
175 | | find( s, 'o' ); // 8, not found
|
---|
176 |
|
---|
177 | string alphabet = "abcdefghijklmnopqrstuvwxyz";
|
---|
178 |
|
---|
179 | sout
|
---|
180 | | find( alphabet, "" ) // 0
|
---|
181 | | find( alphabet, "a" ) // 0
|
---|
182 | | find( alphabet, "z" ) // 25
|
---|
183 | | find( alphabet, "abc" ) // 0
|
---|
184 | | find( alphabet, "abq" ) // 26, not found
|
---|
185 | | find( alphabet, "def"); // 3
|
---|
186 |
|
---|
187 | sout
|
---|
188 | | includes( alphabet, "" ) // true
|
---|
189 | | includes( alphabet, "a" ) // true
|
---|
190 | | includes( alphabet, "z" ) // true
|
---|
191 | | includes( alphabet, "abc" ) // true
|
---|
192 | | includes( alphabet, "abq" ) // false
|
---|
193 | | includes( alphabet, "def"); // true
|
---|
194 |
|
---|
195 | {
|
---|
196 | char *empty_c = "";
|
---|
197 | char *a_c = "a";
|
---|
198 | char *z_c = "z";
|
---|
199 | char *dex_c = "dex";
|
---|
200 |
|
---|
201 | sout
|
---|
202 | | find( alphabet, empty_c ) // 0
|
---|
203 | | find( alphabet, a_c ) // 0
|
---|
204 | | find( alphabet, dex_c ) // 26, not found
|
---|
205 | | find( alphabet, dex_c, 2 ); // 3
|
---|
206 |
|
---|
207 | sout
|
---|
208 | | includes( alphabet, empty_c ) // true
|
---|
209 | | includes( alphabet, a_c ) // true
|
---|
210 | | includes( alphabet, dex_c ) // false
|
---|
211 | | includes( alphabet, dex_c, 2 ); // true
|
---|
212 |
|
---|
213 | sout
|
---|
214 | | startsWith( alphabet, a_c) // true
|
---|
215 | | endsWith ( alphabet, a_c) // false
|
---|
216 | | startsWith( alphabet, z_c) // false
|
---|
217 | | endsWith ( alphabet, z_c); // true
|
---|
218 |
|
---|
219 | string empty = empty_c;
|
---|
220 | string a = a_c;
|
---|
221 | string z = z_c;
|
---|
222 | string dex = dex_c;
|
---|
223 |
|
---|
224 | sout
|
---|
225 | | find( alphabet, empty ) // 0
|
---|
226 | | find( alphabet, a ) // 0
|
---|
227 | | find( alphabet, dex ) // 26, not found
|
---|
228 | | find( alphabet, dex(0,2) ); // 3
|
---|
229 |
|
---|
230 | sout
|
---|
231 | | includes( alphabet, empty ) // true
|
---|
232 | | includes( alphabet, a ) // true
|
---|
233 | | includes( alphabet, dex ) // false
|
---|
234 | | includes( alphabet, dex(0,2) ); // true
|
---|
235 |
|
---|
236 | sout
|
---|
237 | | startsWith( alphabet, a) // true
|
---|
238 | | endsWith ( alphabet, a) // false
|
---|
239 | | startsWith( alphabet, z) // false
|
---|
240 | | endsWith ( alphabet, z); // true
|
---|
241 | }
|
---|
242 |
|
---|
243 | sout
|
---|
244 | | find( alphabet , "def") // 3
|
---|
245 | | find( alphabet( 0, 26), "def") // 3
|
---|
246 | | find( alphabet( 2, 26), "def") // 1
|
---|
247 | | find( alphabet( 3, 26), "def") // 0
|
---|
248 | | find( alphabet( 4, 26), "def") // 22, not found
|
---|
249 | | find( alphabet( 4, 26), "ef") // 0
|
---|
250 | | find( alphabet( 0, 6), "def") // 3
|
---|
251 | | find( alphabet( 0, 5), "def") // 5, not found
|
---|
252 | | find( alphabet( 0, 5), "de" ); // 3
|
---|
253 |
|
---|
254 | sout
|
---|
255 | | includes( alphabet , "def") // true
|
---|
256 | | includes( alphabet( 0, 26), "def") // true
|
---|
257 | | includes( alphabet( 2, 26), "def") // true
|
---|
258 | | includes( alphabet( 3, 26), "def") // true
|
---|
259 | | includes( alphabet( 4, 26), "def") // false
|
---|
260 | | includes( alphabet( 4, 26), "ef") // true
|
---|
261 | | includes( alphabet( 0, 6), "def") // true
|
---|
262 | | includes( alphabet( 0, 5), "def") // false
|
---|
263 | | includes( alphabet( 0, 5), "de" ); // true
|
---|
264 |
|
---|
265 | sout
|
---|
266 | | startsWith( alphabet , "abc") // true
|
---|
267 | | startsWith( alphabet( 0, 26), "abc") // true
|
---|
268 | | startsWith( alphabet( 1, 26), "abc") // false
|
---|
269 | | startsWith( alphabet( 1, 26), "bc") // true
|
---|
270 | | startsWith( alphabet( 0, 26), "abc") // true
|
---|
271 | | startsWith( alphabet( 0, 4), "abc") // true
|
---|
272 | | startsWith( alphabet( 0, 3), "abc") // true
|
---|
273 | | startsWith( alphabet( 0, 3), "ab" ) // true
|
---|
274 | | startsWith( alphabet , "xyz"); // false
|
---|
275 |
|
---|
276 | sout
|
---|
277 | | endsWith( alphabet , "xyz") // true
|
---|
278 | | endsWith( alphabet , "xyzz") // false
|
---|
279 | | endsWith( alphabet( 0, 26), "xyz") // true
|
---|
280 | | endsWith( alphabet( 0, 25), "xyz") // false
|
---|
281 | | endsWith( alphabet( 0, 25), "xy" ) // true
|
---|
282 | | endsWith( alphabet( 0, 26), "xyz") // true
|
---|
283 | | endsWith( alphabet(23, 26), "xyz") // true
|
---|
284 | | endsWith( alphabet(24, 26), "xyz") // false
|
---|
285 | | endsWith( alphabet(24, 26), "yz") // true
|
---|
286 | | endsWith( alphabet , "abc"); // false
|
---|
287 |
|
---|
288 | charclass cc_cba = {"cba"};
|
---|
289 | charclass cc_onml = {"onml"};
|
---|
290 | charclass cc_alphabet = {alphabet};
|
---|
291 |
|
---|
292 | // include (rest of the) numbers: tell me where the numbers stop
|
---|
293 | // exclude (until) numbers: tell me where the numbers start (include rest of the non-numbers)
|
---|
294 |
|
---|
295 | sout
|
---|
296 | | include( alphabet, cc_cba ) // 3
|
---|
297 | | exclude( alphabet, cc_cba ) // 0
|
---|
298 | | include( alphabet, cc_onml ) // 0
|
---|
299 | | exclude( alphabet, cc_onml ) // 11
|
---|
300 | | include( alphabet, cc_alphabet ) // 26
|
---|
301 | | exclude( alphabet, cc_alphabet ); // 0
|
---|
302 | }
|
---|
303 |
|
---|