VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/tests/StringFactoringTests/test_main.cpp@ 102017

Last change on this file since 102017 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.1 KB
Line 
1#include <iostream.h>
2#include "nsStringIO.h"
3
4//#define TEST_STD_STRING
5
6
7#include "nsString.h"
8#include "nsFragmentedString.h"
9#include "nsReadableUtils.h"
10#include "nsSlidingString.h"
11
12#ifdef TEST_STD_STRING
13#include "nsStdStringWrapper.h"
14#else
15 typedef nsString nsStdString;
16 typedef nsCString nsStdCString;
17#endif
18
19template <class CharT>
20basic_nsLiteralString<CharT>
21literal_hello( CharT* )
22 {
23 }
24
25NS_SPECIALIZE_TEMPLATE
26basic_nsLiteralString<char>
27literal_hello( char* )
28 {
29 return basic_nsLiteralString<char>("Hello");
30 }
31
32NS_SPECIALIZE_TEMPLATE
33basic_nsLiteralString<PRUnichar>
34literal_hello( PRUnichar* )
35 {
36#ifdef HAVE_CPP_2BYTE_WCHAR_T
37 return basic_nsLiteralString<PRUnichar>(L"Hello");
38#else
39 static PRUnichar constant_unicode[] = { 'H', 'e', 'l', 'l', 'o', PRUnichar() };
40 return basic_nsLiteralString<PRUnichar>(constant_unicode);
41#endif
42 }
43
44template <class T>
45struct string_class_traits
46 {
47 };
48
49NS_SPECIALIZE_TEMPLATE
50struct string_class_traits<PRUnichar>
51 {
52 typedef PRUnichar* pointer;
53 typedef nsString implementation_t;
54
55 static basic_nsLiteralString<PRUnichar> literal_hello() { return ::literal_hello(pointer()); }
56 };
57
58NS_SPECIALIZE_TEMPLATE
59struct string_class_traits<char>
60 {
61 typedef char* pointer;
62 typedef nsCString implementation_t;
63
64 static basic_nsLiteralString<char> literal_hello() { return ::literal_hello(pointer()); }
65 };
66
67
68static
69void
70CallCMid( nsACString& aResult, const nsACString& aSource, PRUint32 aStartPos, PRUint32 aLengthToCopy )
71 {
72 aSource.Mid(aResult, aStartPos, aLengthToCopy);
73 }
74
75
76
77template <class CharT>
78int
79test_multifragment_iterators( const basic_nsAString<CharT>& aString )
80 /*
81 ...this tests a problem that was present in |nsPromiseConcatenation| where,
82 because it originally stored some iteration state in the object itself, rather than
83 in the fragment, the iterators could get confused if used out of sequence.
84
85 This test should be run on any multi-fragment implementation to verify that it
86 does not have the same bug. Make sure the first fragment is only one character long.
87 */
88 {
89 typedef typename basic_nsAString<CharT>::const_iterator ConstIterator;
90
91 int tests_failed = 0;
92
93 ConstIterator iter1 = aString.BeginReading();
94 ConstIterator iter2 = aString.BeginReading();
95 ++iter2; ++iter2;
96
97 ConstIterator iter3 = aString.EndReading();
98 --iter3;
99 ++iter1; ++iter1;
100 if ( iter1 != iter2 )
101 {
102 cout << "FAILED in |test_multifragment_iterators|" << endl;
103 ++tests_failed;
104 }
105
106 return tests_failed;
107 }
108
109template <class CharT>
110int
111test_Vidur_functions( const basic_nsAString<CharT>& aString )
112 {
113 char* char_copy = ToNewCString(aString);
114 PRUnichar* PRUnichar_copy = ToNewUnicode(aString);
115
116 nsMemory::Free(PRUnichar_copy);
117 nsMemory::Free(char_copy);
118
119 return 0;
120 }
121
122template <class CharT>
123int
124test_readable_hello( const basic_nsAString<CharT>& aReadable )
125 {
126 int tests_failed = 0;
127
128 if ( aReadable.Length() != 5 )
129 {
130 cout << "FAILED |test_readable_hello|: |Length()| --> " << aReadable.Length() << endl;
131 ++tests_failed;
132 }
133
134 if ( aReadable.First() != CharT('H') )
135 {
136 cout << "FAILED |test_readable_hello|: |First()| --> '" << aReadable.First() << "'" << endl;
137 ++tests_failed;
138 }
139
140 if ( aReadable.Last() != CharT('o') )
141 {
142 cout << "FAILED |test_readable_hello|: |Last()| --> '" << aReadable.Last() << "'" << endl;
143 ++tests_failed;
144 }
145
146 if ( aReadable[3] != CharT('l') )
147 {
148 cout << "FAILED |test_readable_hello|: |operator[]| --> '" << aReadable[3] << "'" << endl;
149 ++tests_failed;
150 }
151
152 if ( aReadable.CountChar( CharT('l') ) != 2 )
153 {
154 cout << "FAILED |test_readable_hello|: |CountChar('l')| --> " << aReadable.CountChar(CharT('l')) << endl;
155 ++tests_failed;
156 }
157
158 basic_nsAString<CharT>::const_iterator iter = aReadable.BeginReading();
159 if ( *iter != CharT('H') )
160 {
161 cout << "FAILED |test_readable_hello|: didn't start out pointing to the right thing, or else couldn't be dereferenced. --> '" << *iter << "'" << endl;
162 ++tests_failed;
163 }
164
165 ++iter;
166
167 if ( *iter != CharT('e') )
168 {
169 cout << "FAILED |test_readable_hello|: iterator couldn't be incremented, or else couldn't be dereferenced. --> '" << *iter << "'" << endl;
170 ++tests_failed;
171 }
172
173 iter = aReadable.EndReading();
174 --iter;
175 if ( *iter != CharT('o') )
176 {
177 cout << "FAILED |test_readable_hello|: iterator couldn't be set to |EndReading()|, or else couldn't be decremented, or else couldn't be dereferenced. --> '" << *iter << "'" << endl;
178 ++tests_failed;
179 }
180
181 basic_nsAString<CharT>::const_iterator iter1 = aReadable.BeginReading().advance(3);
182 if ( *iter1 != CharT('l') )
183 {
184 cout << "FAILED |test_readable_hello|: iterator couldn't be set to |BeginReading()+=n|, or else couldn't be dereferenced. --> '" << *iter1 << "'" << endl;
185 ++tests_failed;
186 }
187
188 basic_nsAString<CharT>::const_iterator iter2 = aReadable.EndReading().advance(-2);
189 if ( *iter2 != CharT('l') )
190 {
191 cout << "FAILED |test_readable_hello|: iterator couldn't be set to |EndReading()-=n|, or else couldn't be dereferenced. --> '" << *iter2 << "'" << endl;
192 ++tests_failed;
193 }
194
195 if ( iter1 != iter2 )
196 {
197 cout << "FAILED |test_readable_hello|: iterator comparison with !=." << endl;
198 ++tests_failed;
199 }
200
201 if ( !(iter1 == iter2) )
202 {
203 cout << "FAILED |test_readable_hello|: iterator comparison with ==." << endl;
204 ++tests_failed;
205 }
206
207 typedef CharT* CharT_ptr;
208 if ( aReadable != literal_hello(CharT_ptr()) )
209 {
210 cout << "FAILED |test_readable_hello|: comparison with \"Hello\"" << endl;
211 ++tests_failed;
212 }
213
214 tests_failed += test_multifragment_iterators(aReadable);
215 // tests_failed += test_deprecated_GetBufferGetUnicode(aReadable);
216
217 test_Vidur_functions(aReadable);
218
219 return tests_failed;
220 }
221
222
223template <class CharT>
224int
225test_SetLength( basic_nsAString<CharT>& aWritable )
226 {
227 int tests_failed = 0;
228
229 string_class_traits<CharT>::implementation_t oldValue(aWritable);
230
231
232 size_t oldLength = aWritable.Length();
233
234 if ( oldValue != Substring(aWritable, 0, oldLength) )
235 {
236 cout << "FAILED growing a string in |test_SetLength|, saving the value didn't work." << endl;
237 ++tests_failed;
238 }
239
240 size_t newLength = 2*(oldLength+1);
241
242 aWritable.SetLength(newLength);
243 if ( aWritable.Length() != newLength )
244 {
245 cout << "FAILED growing a string in |test_SetLength|, length is wrong." << endl;
246 ++tests_failed;
247 }
248
249 if ( oldValue != Substring(aWritable, 0, oldLength) )
250 {
251 cout << "FAILED growing a string in |test_SetLength|, contents damaged after growing." << endl;
252 ++tests_failed;
253 }
254
255 aWritable.SetLength(oldLength);
256 if ( aWritable.Length() != oldLength )
257 {
258 cout << "FAILED shrinking a string in |test_SetLength|." << endl;
259 ++tests_failed;
260 }
261
262 if ( oldValue != Substring(aWritable, 0, oldLength) )
263 {
264 cout << "FAILED growing a string in |test_SetLength|, contents damaged after shrinking." << endl;
265 ++tests_failed;
266 }
267
268 return tests_failed;
269 }
270
271
272template <class CharT>
273int
274test_insert( basic_nsAString<CharT>& aWritable )
275 {
276 int tests_failed = 0;
277
278 string_class_traits<CharT>::implementation_t oldValue(aWritable);
279
280 if ( oldValue != aWritable )
281 {
282 cout << "FAILED saving the old string value in |test_insert|." << endl;
283 ++tests_failed;
284 }
285
286 string_class_traits<CharT>::implementation_t insertable( string_class_traits<CharT>::literal_hello() );
287
288 insertable.SetLength(1);
289 aWritable.Insert(insertable, 0);
290
291 if ( aWritable != (insertable + oldValue) )
292 {
293 cout << "FAILED in |test_insert|." << endl;
294 ++tests_failed;
295 }
296
297 aWritable = oldValue;
298
299 return tests_failed;
300 }
301
302
303template <class CharT>
304int
305test_cut( basic_nsAString<CharT>& aWritable )
306 {
307 int tests_failed = 0;
308
309 aWritable.Cut(0, aWritable.Length()+5);
310
311 return tests_failed;
312 }
313
314template <class CharT>
315int
316test_self_assign( basic_nsAString<CharT>& aWritable )
317 {
318 int tests_failed = 0;
319 string_class_traits<CharT>::implementation_t oldValue(aWritable);
320
321 aWritable = aWritable;
322 if ( aWritable != oldValue )
323 {
324 cout << "FAILED self assignment." << endl;
325 ++tests_failed;
326 }
327
328 aWritable = oldValue;
329 return tests_failed;
330 }
331
332template <class CharT>
333int
334test_self_append( basic_nsAString<CharT>& aWritable )
335 {
336 int tests_failed = 0;
337 string_class_traits<CharT>::implementation_t oldValue(aWritable);
338
339 aWritable += aWritable;
340 if ( aWritable != oldValue + oldValue )
341 {
342 cout << "FAILED self append." << endl;
343 ++tests_failed;
344 }
345
346 aWritable = oldValue;
347 return tests_failed;
348 }
349
350template <class CharT>
351int
352test_self_insert( basic_nsAString<CharT>& aWritable )
353 {
354 int tests_failed = 0;
355 string_class_traits<CharT>::implementation_t oldValue(aWritable);
356
357 aWritable.Insert(aWritable, 0);
358 if ( aWritable != oldValue + oldValue )
359 {
360 cout << "FAILED self insert." << endl;
361 ++tests_failed;
362 }
363
364 aWritable = oldValue;
365 return tests_failed;
366 }
367
368template <class CharT>
369int
370test_self_replace( basic_nsAString<CharT>& aWritable )
371 {
372 int tests_failed = 0;
373 string_class_traits<CharT>::implementation_t oldValue(aWritable);
374
375 aWritable.Replace(0, 0, aWritable);
376 if ( aWritable != oldValue + oldValue )
377 {
378 cout << "FAILED self insert." << endl;
379 ++tests_failed;
380 }
381
382 aWritable = oldValue;
383 return tests_failed;
384 }
385
386
387
388template <class CharT>
389int
390test_writable( basic_nsAString<CharT>& aWritable )
391 {
392 int tests_failed = 0;
393 // ...
394
395
396 {
397 typedef CharT* CharT_ptr;
398 aWritable = literal_hello(CharT_ptr());
399
400 if ( aWritable != literal_hello(CharT_ptr()) )
401 {
402 cout << "FAILED assignment and/or comparison in |test_writable|." << endl;
403 ++tests_failed;
404 }
405
406 tests_failed += test_readable_hello(aWritable);
407 }
408
409 tests_failed += test_SetLength(aWritable);
410 tests_failed += test_insert(aWritable);
411 tests_failed += test_cut(aWritable);
412 tests_failed += test_self_assign(aWritable);
413 tests_failed += test_self_append(aWritable);
414 tests_failed += test_self_insert(aWritable);
415 tests_failed += test_self_replace(aWritable);
416
417 return tests_failed;
418 }
419
420
421
422typedef void* void_ptr;
423
424int
425main()
426 {
427 int tests_failed = 0;
428 cout << "String unit tests. Compiled " __DATE__ " " __TIME__ << endl;
429
430#if 0
431 {
432 nsFragmentedCString fs0;
433 fs0.Append("Hello");
434 tests_failed += test_readable_hello(fs0);
435 tests_failed += test_writable(fs0);
436 }
437#endif
438
439 {
440 NS_NAMED_LITERAL_STRING(literal, "Hello");
441 PRUnichar* buffer = ToNewUnicode(literal);
442
443 nsSlidingString ss0(buffer, buffer+5, buffer+6);
444// ss0.AppendBuffer(buffer, buffer+5, buffer+6);
445 nsReadingIterator<PRUnichar> ri0;
446 ss0.BeginReading(ri0);
447
448 tests_failed += test_readable_hello(ss0);
449
450 nsSlidingSubstring ss1(ss0);
451 tests_failed += test_readable_hello(ss1);
452
453 buffer = ToNewUnicode(literal);
454 ss0.AppendBuffer(buffer, buffer+5, buffer+6);
455
456 ri0.advance(5);
457 ss0.DiscardPrefix(ri0);
458
459 tests_failed += test_readable_hello(ss0);
460 tests_failed += test_readable_hello(ss1);
461
462 nsReadingIterator<PRUnichar> ri1;
463 ss0.EndReading(ri1);
464
465 nsSlidingSubstring ss2(ss0, ri0, ri1);
466 tests_failed += test_readable_hello(ss2);
467 }
468
469
470 {
471 nsLiteralCString s0("Patrick Beard made me write this: \"This is just a test\"\n");
472 print_string(s0);
473
474 const char* raw_string = "He also made me write this.\n";
475 nsFileCharSink<char> output(stdout);
476 copy_string(raw_string, raw_string+nsCharTraits<char>::length(raw_string), output);
477
478 nsLiteralCString s1("This ", 5), s2("is ", 3), s3("a ", 2), s4("test\n", 5);
479 print_string(s1+s2+s3+s4);
480
481 nsLiteralCString s5( "This is " "a " "test" );
482 print_string(s5+NS_LITERAL_CSTRING("\n"));
483
484 print_string(nsLiteralCString("The value of the string |x| is \"") + Substring(s0, 0, s0.Length()-1) + NS_LITERAL_CSTRING("\". Hope you liked it."));
485 }
486
487
488 {
489 tests_failed += test_readable_hello(NS_LITERAL_STRING("Hello"));
490
491 nsLiteralCString s1("Hello");
492 tests_failed += test_readable_hello(s1);
493 }
494
495 {
496
497 nsString s3( NS_LITERAL_STRING("Hello") );
498
499 tests_failed += test_readable_hello(s3);
500 tests_failed += test_writable(s3);
501
502 nsCString s4("Hello");
503 tests_failed += test_readable_hello(s4);
504 tests_failed += test_writable(s4);
505 }
506
507 {
508 nsStdString s5( NS_LITERAL_STRING("Hello") );
509
510 tests_failed += test_readable_hello(s5);
511 tests_failed += test_writable(s5);
512
513 nsStdCString s6("Hello");
514 tests_failed += test_readable_hello(s6);
515 tests_failed += test_writable(s6);
516 }
517
518 {
519 nsLiteralString s7(NS_LITERAL_STRING("He"));
520 nsString s8(NS_LITERAL_STRING("l"));
521 nsStdString s9(NS_LITERAL_STRING("lo"));
522
523 tests_failed += test_readable_hello(s7+s8+s9);
524
525 nsString s13( s7 + s8 + s9 );
526 tests_failed += test_readable_hello(s13);
527
528 nsStdString s14( s7 + s8 + s9 );
529 tests_failed += test_readable_hello(s14);
530
531 nsCString s10("He");
532 nsLiteralCString s11("l");
533 nsStdCString s12("lo");
534
535 tests_failed += test_readable_hello(s10+s11+s12);
536
537
538 }
539
540 {
541 const char *foo = "this is a really long string";
542 nsCString origString;
543 nsCString string2;
544 nsCString string3;
545
546 origString = foo;
547
548 string2 = Substring(origString, 0, 5);
549 string3 = Substring(origString, 6, origString.Length() - 6);
550 }
551
552 {
553 nsLiteralCString s13("He");
554 nsCAutoString s14("l");
555 nsLiteralCString s15("lo");
556
557 s14.Assign(s13 + s14 + s15);
558
559 if ( int failures = test_readable_hello(s14) )
560 {
561 tests_failed += failures;
562 cout << "FAILED to keep a promise." << endl;
563 }
564 }
565
566
567 {
568 nsLiteralCString str1("Hello");
569 nsStdCString str2("Hello");
570
571 nsLiteralCString str3("Hello there");
572
573 if ( str1 != str2 )
574 {
575 cout << "string comparison using != failed" << endl;
576 ++tests_failed;
577 }
578
579 if ( !(str3 > str2) )
580 {
581 cout << "string comparison using > failed" << endl;
582 ++tests_failed;
583 }
584
585 if ( !(str2 < str3) )
586 {
587 cout << "string comparison using < failed" << endl;
588 ++tests_failed;
589 }
590
591 if ( str1 != "Hello" )
592 {
593 cout << "string comparison using == failed" << endl;
594 ++tests_failed;
595 }
596 }
597
598#if 0
599 nsStdCString extracted_middle("XXXXXXXXXX");
600 CallCMid(extracted_middle, part1+part2a+part2b, 1, 3);
601
602 cout << "The result of mid was \"" << extracted_middle << "\"" << endl;
603
604 nsLiteralCString middle_answer("ell");
605 if ( middle_answer != extracted_middle )
606 {
607 cout << "mid FAILED on nsConcatString" << endl;
608 ++tests_failed;
609 }
610
611
612
613 //
614 // |nsStdStringWrapper|, i.e., |nsStdCString|
615 //
616
617 {
618 nsStdCString extracted_middle;
619 CallCMid(extracted_middle, part1+part2a+part2b, 1, 3);
620
621 cout << "The result of mid was \"" << extracted_middle << "\"" << endl;
622
623 nsLiteralCString middle_answer("ell");
624 if ( middle_answer != extracted_middle )
625 {
626 cout << "mid FAILED on nsStdCString" << endl;
627 ++tests_failed;
628 }
629 }
630
631
632
633 nsStdCString leftString;
634 source.Left(leftString, 9);
635 // cout << static_cast<const nsACString>(leftString) << endl;
636
637
638
639 tests_failed += test_multifragment_iterators(part1+part2a+part2b);
640#endif
641
642
643
644 cout << "End of string unit tests." << endl;
645 if ( !tests_failed )
646 cout << "OK, all tests passed." << endl;
647 else
648 cout << "FAILED one or more tests." << endl;
649
650 return tests_failed;
651 }
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette