2 //=============================================================================
4 * @file SString_Test.cpp
6 * This is a simple test that illustrates the use of ACE_CString
7 * and ACE_WString. No command line arguments are needed to run
10 * @author Prashant Jain <pjain@cs.wustl.edu>
12 //=============================================================================
15 #include "test_config.h"
16 #include "ace/OS_NS_string.h"
17 #include "ace/Auto_Ptr.h"
18 #include "ace/SString.h"
22 static int testConcatenation() {
26 if (s1
!= ACE_WString(L
"H")) {
27 ACE_ERROR((LM_ERROR
, "Concat wchar_t\n"));
30 s1
= ACE_WString(L
"Hello");
32 if (s1
!= ACE_WString(L
"Hello World")) {
33 ACE_ERROR((LM_ERROR
, "Concat wchar_t*\n"));
37 s1
+= ACE_WString(L
" World");
38 if (s1
!= ACE_WString(L
"Hello World")) {
39 ACE_ERROR((LM_ERROR
, "Concat wstring\n"));
43 s1
.append(L
" World", 6);
44 if (s1
!= ACE_WString(L
"Hello World")) {
45 ACE_ERROR((LM_ERROR
, "Concat wchar_t* 2\n"));
49 if (s1
!= ACE_WString(L
"Hello World.")) {
50 ACE_ERROR((LM_ERROR
, "Concat wchar_t\n"));
53 ACE_WString
s2(L
"Hello World");
55 if (s2
!= ACE_WString(L
"Hello World.")) {
56 ACE_ERROR((LM_ERROR
, "Concat wchar_t 2\n"));
59 #endif /* ACE_HAS_WCHAR */
65 ACE_CString
s1 ("Hello, World");
67 // Use the advance () method to count number of characters.
69 for (ACE_CString::ITERATOR
iter (s1
); !iter
.done (); iter
.advance ())
72 if (count
!= s1
.length ())
73 ACE_ERROR_RETURN ((LM_ERROR
,
74 ACE_TEXT ("advance () failed")),
77 // Use the prefix operator to count number of characters.
79 for (ACE_CString::ITERATOR
iter (s1
); !iter
.done (); ++ iter
)
82 if (count
!= s1
.length ())
83 ACE_ERROR_RETURN ((LM_ERROR
,
84 ACE_TEXT ("++ operator failed")),
89 for (ACE_CString::iterator iter
= s1
.begin (), iter_end
= s1
.end ();
90 iter
!= iter_end
; iter
++)
95 if (count
!= s1
.length ())
96 ACE_ERROR_RETURN ((LM_ERROR
,
97 ACE_TEXT ("end () failed")),
100 ACE_CString::iterator
iter1 (s1
);
103 ACE_ERROR_RETURN ((LM_ERROR
,
104 ACE_TEXT ("dereference operator failed")),
110 int testConstIterator()
112 const ACE_CString
s1 ("Hello, World");
114 // Use the advance () method to count number of characters.
116 for (ACE_CString::CONST_ITERATOR
iter (s1
); !iter
.done (); iter
.advance ())
119 if (count
!= s1
.length ())
120 ACE_ERROR_RETURN ((LM_ERROR
,
121 ACE_TEXT ("advance () failed")),
124 // Use the prefix operator to count number of characters.
126 for (ACE_CString::CONST_ITERATOR
iter (s1
); !iter
.done (); ++ iter
)
129 if (count
!= s1
.length ())
130 ACE_ERROR_RETURN ((LM_ERROR
,
131 ACE_TEXT ("++ operator failed")),
136 for (ACE_CString::const_iterator iter
= s1
.begin (), iter_end
= s1
.end ();
137 iter
!= iter_end
; iter
++)
142 if (count
!= s1
.length ())
143 ACE_ERROR_RETURN ((LM_ERROR
,
144 ACE_TEXT ("end () failed")),
147 ACE_CString::const_iterator
iter1 (s1
);
150 ACE_ERROR_RETURN ((LM_ERROR
,
151 ACE_TEXT ("dereference operator failed")),
159 run_main (int, ACE_TCHAR
*[])
161 ACE_START_TEST (ACE_TEXT ("SString_Test"));
166 ACE_CString
s0 ("hello");
167 ACE_CString
s1 ("hello");
168 ACE_CString
s2 ("world");
169 ACE_CString
s3 ("ll");
170 ACE_CString
s4 ("ello");
171 ACE_CString s5
= s1
+ " " + s2
;
173 char single_character
= 'z';
174 ACE_CString
single_character_string (single_character
);
176 ACE_CString empty_string
;
177 ACE_CString
zero_size_string (s1
.c_str (), 0, 0, 1);
179 if (ACE_CString::npos
== 0)
180 ACE_ERROR((LM_ERROR
,"Set #1: npos is incorrect.\n"));
182 // Not equal comparisons. Error if they are equal
183 if (s1
== s2
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
184 if (s1
== s5
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
186 // Equal comparisons. Error if they are not equal
187 if (s1
!= s1
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
188 if (s1
!= s0
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
190 // Substring match. Error if they are not equal
191 if (s1
.strstr (s2
) != ACE_CString::npos
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
192 if (s1
.strstr (s3
) != 2){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
193 if (s3
.strstr (s1
) != ACE_CString::npos
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
194 if (s1
.strstr (s4
) != 1){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
196 // Substring creation. Error if they are not equal
197 if (s1
.substring (0) != s1
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
198 if (s1
.substring (1) != s4
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
199 if (s1
.substring (2, 2) != s3
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
200 if (s1
.substring (0, 0) != empty_string
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
201 if (s1
.substring (4, 10).length () != 1){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
203 // Forward search. Error if they are not equal
204 if (s1
.find (s3
) != 2){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
205 if (s3
.find (s1
) != ACE_CString::npos
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
206 if (s1
.find (s3
, 2) != 2){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
207 if (s3
.find (s1
, 1) != ACE_CString::npos
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
208 if (s1
.find (s2
) != ACE_CString::npos
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
209 if (s1
.find ('o') != 4){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
211 // Reverse search. Error if they are not equal
212 if (s1
.rfind ('l') != 3){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
213 if (s1
.rfind ('l', 3) != 2){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
215 // Assignment. Error if they are not equal
218 if (s6
!= s0
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
220 if (s4
!= s6
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
222 if (s6
!= s5
){ACE_ERROR((LM_ERROR
,"Set #1:\n"));return 1;}
227 ACE_CString s0
= "hello";
228 ACE_CString
s1 ("hello", 0, false);
229 ACE_CString
s2 ("world", 0, false);
230 ACE_CString
s3 ("ll", 0, false);
231 ACE_CString
s4 ("ello", 0, false);
232 ACE_CString s5
= s1
+ " " + s2
;
234 char single_character
= 'z';
235 ACE_CString
single_character_string (single_character
);
237 ACE_CString
empty_string (0, 0, false);
238 ACE_CString
zero_size_string (s1
.c_str (), 0, 0, false);
240 // Not equal comparisons. Error if they are equal
241 if (s1
== s2
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
242 if (s1
== s5
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
244 // Equal comparisons. Error if they are not equal
245 if (s1
!= s1
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
246 if (s1
!= s0
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
248 // Substring match. Error if they are not equal
249 if (s1
.strstr (s2
) != ACE_CString::npos
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
250 if (s1
.strstr (s3
) != 2){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
251 if (s3
.strstr (s1
) != ACE_CString::npos
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
252 if (s1
.strstr (s4
) != 1){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
254 // Substring creation. Error if they are not equal
255 if (s1
.substring (0) != s1
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
256 if (s1
.substring (1) != s4
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
257 if (s1
.substring (2, 2) != s3
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
258 if (s1
.substring (0, 0) != empty_string
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
260 // Forward search. Error if they are not equal
261 if (s1
.find (s3
) != 2){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
262 if (s3
.find (s1
) != ACE_CString::npos
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
263 if (s1
.find (s3
, 2) != 2){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
264 if (s3
.find (s1
, 1) != ACE_CString::npos
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
265 if (s1
.find (s2
) != ACE_CString::npos
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
266 if (s1
.find ('o') != 4){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
268 // Reverse search. Error if they are not equal
269 if (s1
.rfind ('l') != 3){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
270 if (s1
.rfind ('l', 3) != 2){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
272 // Assignment. Error if they are not equal
275 if (s6
!= s0
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
277 if (s4
!= s6
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
279 if (s6
!= s5
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
281 // Clear. Error if they are not equal
283 if (s0
.length() != 0){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
285 // Rep. Error if they are not equal
286 ACE_Auto_Basic_Array_Ptr
<char> s (s1
.rep ());
287 if (ACE_OS::strlen (s
.get ()) != s1
.length ())
289 ACE_ERROR((LM_ERROR
,"Auto_ptr s:\n"));
292 ACE_CString
s7 (s
.get ());
293 if (s1
!= s7
){ACE_ERROR((LM_ERROR
,"Set #2:\n"));return 1;}
298 ACE_NS_WString
s0 ("hello");
299 ACE_NS_WString
s1 ("hello");
300 ACE_NS_WString
s2 ("world");
301 ACE_NS_WString
s3 ("ll");
302 ACE_NS_WString
s4 ("ello");
303 ACE_NS_WString s5
= s1
+ " " + s2
;
304 ACE_NS_WString s6
= ("hella"); // Same length as s1, off by one char.
306 ACE_WCHAR_T single_character
= 'z';
307 ACE_NS_WString
single_character_string (single_character
);
309 ACE_NS_WString empty_string
;
310 ACE_NS_WString
zero_size_string (s1
.c_str (), 0, 0);
312 // Not equal comparisons. Error if they are equal
313 if (s1
== s2
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
314 if (s1
== s5
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
315 if (s1
== s6
){ACE_ERROR((LM_ERROR
,"Set #3: off-by-one failed\n"));return 1;}
317 // Equal comparisons. Error if they are not equal
318 if (s1
!= s1
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
319 if (s1
!= s0
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
321 // Substring match. Error if they are not equal
322 if (s1
.strstr (s2
) != ACE_NS_WString::npos
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
323 if (s1
.strstr (s3
) != 2){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
324 if (s3
.strstr (s1
) != ACE_NS_WString::npos
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
325 if (s1
.strstr (s4
) != 1){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
327 // Substring creation. Error if they are not equal
328 if (s1
.substring (0) != s1
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
329 if (s1
.substring (1) != s4
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
330 if (s1
.substring (2, 2) != s3
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
331 if (s1
.substring (0, 0) != empty_string
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
333 // Forward search. Error if they are not equal
334 if (s1
.find (s3
) != 2){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
335 if (s3
.find (s1
) != ACE_NS_WString::npos
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
336 if (s1
.find (s3
, 2) != 2){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
337 if (s3
.find (s1
, 1) != ACE_NS_WString::npos
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
338 if (s1
.find (s2
) != ACE_NS_WString::npos
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
339 if (s1
.find ('o') != 4){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
341 // Reverse search. Error if they are not equal
342 if (s1
.rfind ('l') != 3){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
343 if (s1
.rfind ('l', 3) != 2){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
345 // Assignment. Error if they are not equal
348 if (s7
!= s0
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
350 if (s4
!= s7
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
352 if (s7
!= s5
){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
354 // Clear. Error if they are not equal
356 if (s0
.length() != 0){ACE_ERROR((LM_ERROR
,"Set #3:\n"));return 1;}
361 ACE_CString
s1("dog");
364 if (s1
== s2
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
365 if (!(s1
> s2
)){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
366 if (s1
< s2
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
368 ACE_CString
s3 ("dog");
369 ACE_CString
s4 ("dogbert");
371 if (s3
== s4
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
372 if (!(s3
< s4
)){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
373 if (s3
> s4
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
375 ACE_CString
s5 ("dogbert",3);
376 ACE_CString
s6 ("dogbert",5);
378 if(s5
== s6
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
379 if(!(s5
< s6
)){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
380 if(s5
> s6
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
382 ACE_CString
s7 ("dogbert",4);
383 ACE_CString
s8 ("dogbert",2);
385 if(s7
== s8
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
386 if(!(s7
> s8
)){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
387 if(s7
< s8
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
389 ACE_CString
s9 ("dogbert",3);
390 ACE_CString
s10 ("dogbert");
392 if(s9
== s10
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
393 if(!(s9
< s10
)){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
394 if(s9
> s10
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
396 ACE_CString
s11 ("dogbert",5);
397 ACE_CString
s12 ("dog");
399 if(s11
== s12
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
400 if(!(s11
> s12
)){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
401 if(s11
< s12
){ACE_ERROR((LM_ERROR
,"Set #4:\n"));return 1;}
404 if (s11
.length () != 0)
405 ACE_ERROR ((LM_ERROR
, ACE_TEXT ("fast_clear didn't yield 0 length\n")));
409 // Set 1 for ACE_SString, which is not tested
412 const char *old
= sstr
.rep ();
413 const char *str
= "What_a_day_it_has_been";
415 sstr
.rep (const_cast<char *>(str
));
418 sstr
.substring (2, 300);
420 if (tmp
.length () == 300)
421 ACE_ERROR ((LM_ERROR
, "SString substring\n"));
423 // Constring an ACE_SString without a character pointer or from an
424 // existing ACE_SString causes memory to be allocated that will not
425 // be delete (apparently by design).
426 ACE_Allocator::instance ()->free (const_cast<char *> (old
));
427 ACE_Allocator::instance ()->free (const_cast<char *> (tmp
.rep ()));
430 int err
= testConcatenation ();
431 err
+= testIterator ();
432 err
+= testConstIterator ();