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