1 //=============================================================================
3 * @file SString_Test.cpp
5 * This is a simple test that illustrates the use of ACE_CString
6 * and ACE_WString. No command line arguments are needed to run
9 * @author Prashant Jain <pjain@cs.wustl.edu>
11 //=============================================================================
13 #include "test_config.h"
14 #include "ace/OS_NS_string.h"
16 #include "ace/SString.h"
18 static int testConcatenation() {
22 if (s1
!= ACE_WString(L
"H")) {
23 ACE_ERROR((LM_ERROR
, "Concat wchar_t\n"));
26 s1
= ACE_WString(L
"Hello");
28 if (s1
!= ACE_WString(L
"Hello World")) {
29 ACE_ERROR((LM_ERROR
, "Concat wchar_t*\n"));
33 s1
+= ACE_WString(L
" World");
34 if (s1
!= ACE_WString(L
"Hello World")) {
35 ACE_ERROR((LM_ERROR
, "Concat wstring\n"));
39 s1
.append(L
" World", 6);
40 if (s1
!= ACE_WString(L
"Hello World")) {
41 ACE_ERROR((LM_ERROR
, "Concat wchar_t* 2\n"));
45 if (s1
!= ACE_WString(L
"Hello World.")) {
46 ACE_ERROR((LM_ERROR
, "Concat wchar_t\n"));
49 ACE_WString
s2(L
"Hello World");
51 if (s2
!= ACE_WString(L
"Hello World.")) {
52 ACE_ERROR((LM_ERROR
, "Concat wchar_t 2\n"));
55 #endif /* ACE_HAS_WCHAR */
61 ACE_CString
s1 ("Hello, World");
63 // Use the advance () method to count number of characters.
65 for (ACE_CString::ITERATOR
iter (s1
); !iter
.done (); iter
.advance ())
68 if (count
!= s1
.length ())
69 ACE_ERROR_RETURN ((LM_ERROR
,
70 ACE_TEXT ("advance () failed")),
73 // Use the prefix operator to count number of characters.
75 for (ACE_CString::ITERATOR
iter (s1
); !iter
.done (); ++ iter
)
78 if (count
!= s1
.length ())
79 ACE_ERROR_RETURN ((LM_ERROR
,
80 ACE_TEXT ("++ operator failed")),
85 for (ACE_CString::iterator iter
= s1
.begin (), iter_end
= s1
.end ();
86 iter
!= iter_end
; iter
++)
91 if (count
!= s1
.length ())
92 ACE_ERROR_RETURN ((LM_ERROR
,
93 ACE_TEXT ("end () failed")),
96 ACE_CString::iterator
iter1 (s1
);
99 ACE_ERROR_RETURN ((LM_ERROR
,
100 ACE_TEXT ("dereference operator failed")),
106 int testConstIterator()
108 const ACE_CString
s1 ("Hello, World");
110 // Use the advance () method to count number of characters.
112 for (ACE_CString::CONST_ITERATOR
iter (s1
); !iter
.done (); iter
.advance ())
115 if (count
!= s1
.length ())
116 ACE_ERROR_RETURN ((LM_ERROR
,
117 ACE_TEXT ("advance () failed")),
120 // Use the prefix operator to count number of characters.
122 for (ACE_CString::CONST_ITERATOR
iter (s1
); !iter
.done (); ++ iter
)
125 if (count
!= s1
.length ())
126 ACE_ERROR_RETURN ((LM_ERROR
,
127 ACE_TEXT ("++ operator failed")),
132 for (ACE_CString::const_iterator iter
= s1
.begin (), iter_end
= s1
.end ();
133 iter
!= iter_end
; iter
++)
138 if (count
!= s1
.length ())
139 ACE_ERROR_RETURN ((LM_ERROR
,
140 ACE_TEXT ("end () failed")),
143 ACE_CString::const_iterator
iter1 (s1
);
146 ACE_ERROR_RETURN ((LM_ERROR
,
147 ACE_TEXT ("dereference operator failed")),
155 run_main (int, ACE_TCHAR
*[])
157 ACE_START_TEST (ACE_TEXT ("SString_Test"));
161 ACE_CString
s0 ("hello");
162 ACE_CString
s1 ("hello");
163 ACE_CString
s2 ("world");
164 ACE_CString
s3 ("ll");
165 ACE_CString
s4 ("ello");
166 ACE_CString s5
= s1
+ " " + s2
;
168 char single_character
= 'z';
169 ACE_CString
single_character_string (single_character
);
171 ACE_CString empty_string
;
172 ACE_CString
zero_size_string (s1
.c_str (), 0, 0, 1);
174 if (ACE_CString::npos
== 0)
175 ACE_ERROR((LM_ERROR
,"Set #1: npos is incorrect.\n"));
177 // Not equal comparisons. Error if they are equal
178 if (s1
== s2
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
179 if (s1
== s5
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
181 // Equal comparisons. Error if they are not equal
182 if (s1
!= s1
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
183 if (s1
!= s0
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
185 // Substring match. Error if they are not equal
186 if (s1
.strstr (s2
) != ACE_CString::npos
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
187 if (s1
.strstr (s3
) != 2){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
188 if (s3
.strstr (s1
) != ACE_CString::npos
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
189 if (s1
.strstr (s4
) != 1){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
191 // Substring creation. Error if they are not equal
192 if (s1
.substring (0) != s1
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
193 if (s1
.substring (1) != s4
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
194 if (s1
.substring (2, 2) != s3
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
195 if (s1
.substring (0, 0) != empty_string
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
196 if (s1
.substring (4, 10).length () != 1){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
198 // Forward search. Error if they are not equal
199 if (s1
.find (s3
) != 2){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
200 if (s3
.find (s1
) != ACE_CString::npos
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
201 if (s1
.find (s3
, 2) != 2){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
202 if (s3
.find (s1
, 1) != ACE_CString::npos
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
203 if (s1
.find (s2
) != ACE_CString::npos
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
204 if (s1
.find ('o') != 4){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
206 // Reverse search. Error if they are not equal
207 if (s1
.rfind ('l') != 3){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
208 if (s1
.rfind ('l', 3) != 2){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
210 // Assignment. Error if they are not equal
213 if (s6
!= s0
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
215 if (s4
!= s6
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
217 if (s6
!= s5
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
222 ACE_CString s0
= "hello";
223 ACE_CString
s1 ("hello", 0, false);
224 ACE_CString
s2 ("world", 0, false);
225 ACE_CString
s3 ("ll", 0, false);
226 ACE_CString
s4 ("ello", 0, false);
227 ACE_CString s5
= s1
+ " " + s2
;
229 char single_character
= 'z';
230 ACE_CString
single_character_string (single_character
);
232 ACE_CString
empty_string (0, 0, false);
233 ACE_CString
zero_size_string (s1
.c_str (), 0, 0, false);
235 // Not equal comparisons. Error if they are equal
236 if (s1
== s2
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
237 if (s1
== s5
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
239 // Equal comparisons. Error if they are not equal
240 if (s1
!= s1
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
241 if (s1
!= s0
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
243 // Substring match. Error if they are not equal
244 if (s1
.strstr (s2
) != ACE_CString::npos
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
245 if (s1
.strstr (s3
) != 2){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
246 if (s3
.strstr (s1
) != ACE_CString::npos
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
247 if (s1
.strstr (s4
) != 1){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
249 // Substring creation. Error if they are not equal
250 if (s1
.substring (0) != s1
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
251 if (s1
.substring (1) != s4
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
252 if (s1
.substring (2, 2) != s3
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
253 if (s1
.substring (0, 0) != empty_string
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
255 // Forward search. Error if they are not equal
256 if (s1
.find (s3
) != 2){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
257 if (s3
.find (s1
) != ACE_CString::npos
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
258 if (s1
.find (s3
, 2) != 2){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
259 if (s3
.find (s1
, 1) != ACE_CString::npos
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
260 if (s1
.find (s2
) != ACE_CString::npos
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
261 if (s1
.find ('o') != 4){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
263 // Reverse search. Error if they are not equal
264 if (s1
.rfind ('l') != 3){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
265 if (s1
.rfind ('l', 3) != 2){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
267 // Assignment. Error if they are not equal
270 if (s6
!= s0
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
272 if (s4
!= s6
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
274 if (s6
!= s5
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
276 // Clear. Error if they are not equal
278 if (s0
.length() != 0){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
280 // Rep. Error if they are not equal
281 std::unique_ptr
<char[]> s (s1
.rep ());
282 if (ACE_OS::strlen (s
.get ()) != s1
.length ())
284 ACE_ERROR((LM_ERROR
,"Auto_ptr s:\n"));
287 ACE_CString
s7 (s
.get ());
288 if (s1
!= s7
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
293 ACE_NS_WString
s0 ("hello");
294 ACE_NS_WString
s1 ("hello");
295 ACE_NS_WString
s2 ("world");
296 ACE_NS_WString
s3 ("ll");
297 ACE_NS_WString
s4 ("ello");
298 ACE_NS_WString s5
= s1
+ " " + s2
;
299 ACE_NS_WString s6
= ("hella"); // Same length as s1, off by one char.
301 ACE_WCHAR_T single_character
= 'z';
302 ACE_NS_WString
single_character_string (single_character
);
304 ACE_NS_WString empty_string
;
305 ACE_NS_WString
zero_size_string (s1
.c_str (), 0, 0);
307 // Not equal comparisons. Error if they are equal
308 if (s1
== s2
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
309 if (s1
== s5
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
310 if (s1
== s6
){ACE_ERROR((LM_ERROR
,"Set #3: off-by-one failed\n"));return 1;}
312 // Equal comparisons. Error if they are not equal
313 if (s1
!= s1
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
314 if (s1
!= s0
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
316 // Substring match. Error if they are not equal
317 if (s1
.strstr (s2
) != ACE_NS_WString::npos
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
318 if (s1
.strstr (s3
) != 2){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
319 if (s3
.strstr (s1
) != ACE_NS_WString::npos
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
320 if (s1
.strstr (s4
) != 1){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
322 // Substring creation. Error if they are not equal
323 if (s1
.substring (0) != s1
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
324 if (s1
.substring (1) != s4
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
325 if (s1
.substring (2, 2) != s3
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
326 if (s1
.substring (0, 0) != empty_string
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
328 // Forward search. Error if they are not equal
329 if (s1
.find (s3
) != 2){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
330 if (s3
.find (s1
) != ACE_NS_WString::npos
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
331 if (s1
.find (s3
, 2) != 2){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
332 if (s3
.find (s1
, 1) != ACE_NS_WString::npos
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
333 if (s1
.find (s2
) != ACE_NS_WString::npos
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
334 if (s1
.find ('o') != 4){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
336 // Reverse search. Error if they are not equal
337 if (s1
.rfind ('l') != 3){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
338 if (s1
.rfind ('l', 3) != 2){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
340 // Assignment. Error if they are not equal
343 if (s7
!= s0
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
345 if (s4
!= s7
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
347 if (s7
!= s5
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
349 // Clear. Error if they are not equal
351 if (s0
.length() != 0){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
356 ACE_CString
s1("dog");
359 if (s1
== s2
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
360 if (!(s1
> s2
)){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
361 if (s1
< s2
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
363 ACE_CString
s3 ("dog");
364 ACE_CString
s4 ("dogbert");
366 if (s3
== s4
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
367 if (!(s3
< s4
)){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
368 if (s3
> s4
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
370 ACE_CString
s5 ("dogbert",3);
371 ACE_CString
s6 ("dogbert",5);
373 if(s5
== s6
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
374 if(!(s5
< s6
)){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
375 if(s5
> s6
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
377 ACE_CString
s7 ("dogbert",4);
378 ACE_CString
s8 ("dogbert",2);
380 if(s7
== s8
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
381 if(!(s7
> s8
)){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
382 if(s7
< s8
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
384 ACE_CString
s9 ("dogbert",3);
385 ACE_CString
s10 ("dogbert");
387 if(s9
== s10
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
388 if(!(s9
< s10
)){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
389 if(s9
> s10
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
391 ACE_CString
s11 ("dogbert",5);
392 ACE_CString
s12 ("dog");
394 if(s11
== s12
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
395 if(!(s11
> s12
)){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
396 if(s11
< s12
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
399 if (s11
.length () != 0)
400 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("fast_clear didn't yield 0 length\n")));
404 // Set 1 for ACE_SString, which is not tested
407 const char *old
= sstr
.rep ();
408 const char *str
= "What_a_day_it_has_been";
410 sstr
.rep (const_cast<char *>(str
));
413 sstr
.substring (2, 300);
415 if (tmp
.length () == 300)
416 ACE_ERROR ((LM_ERROR
, "SString substring\n"));
418 // Constring an ACE_SString without a character pointer or from an
419 // existing ACE_SString causes memory to be allocated that will not
420 // be delete (apparently by design).
421 ACE_Allocator::instance ()->free (const_cast<char *> (old
));
422 ACE_Allocator::instance ()->free (const_cast<char *> (tmp
.rep ()));
425 int err
= testConcatenation ();
426 err
+= testIterator ();
427 err
+= testConstIterator ();