#include <stdio.h>
#include "SubString.h"
Include dependency graph for SubStrTest.C:
Go to the source code of this file.
Functions | |
bool | SubStringPassByValue (SubString subStr, size_t previousRefCnt) |
bool | SubStringPassByReference (SubString &subStr, size_t previousRefCnt) |
bool | subStrRefCnt () |
Test to verify that reference counting is handled properly for the SubString class. | |
bool | subStrSection () |
Test SubString section operations. | |
bool | subStrAssign () |
Test assignment to and from a SubString object. | |
bool | subStrRelations () |
Test SubString relational operations. | |
bool | globalSubStrRelations () |
Test for other types on the LHS of a relation. | |
int | main () |
Note that the existence of these regression tests does not change that statement this code is provided without waranty. You use this code at your own risk.
Compiling this code:
This code is written to compile on Microsoft Visual C++ 6.0 and higher. It will also compile on GNU C++ and Sun's 6.0 and later C++ compilers. To compile under Microsoft VC++:
The -GX flag is needed to support exceptions, which are used rather than the assert() functions in the earlier versions.
Copyright and Use
You may use this source code without limitation and without fee as long as you include:
This software was written and is copyrighted by Ian Kaplan, Bear Products International, www.bearcave.com, 2001.This software is provided "as is", without any warrenty or claim as to its usefulness. Anyone who uses this source code uses it at their own risk. Nor is any support provided by Ian Kaplan and Bear Products International.
Please send any bug fixes or suggested source changes to:
Definition in file SubStrTest.C.
Function Documentation
|
Test for other types on the LHS of a relation. This must be supported by global operators. Definition at line 705 of file SubStrTest.C. Referenced by main().
00706 { 00707 printf("Test global SubString relational operators\n"); 00708 bool rslt = true; 00709 00710 // 012345678901234 00711 String a = "abcdexyzzyabcde"; 00712 00713 // Cstr == SubString 00714 bool t1 = false; 00715 if ("abcde" == a(0, 5)) { 00716 t1 = true; 00717 } 00718 if (!t1) { 00719 printf("1. Cstr == SubString\n"); 00720 rslt = false; 00721 } 00722 00723 // Cstr != SubString 00724 t1 = false; 00725 if ("abcde" != a(5, 5)) { 00726 t1 = true; 00727 } 00728 if (!t1) { 00729 printf("2. Cstr != SubString\n"); 00730 rslt = false; 00731 } 00732 00733 // Cstr < SubString 00734 t1 = false; 00735 if ("abcde" < a(5, 5)) { 00736 t1 = true; 00737 } 00738 if (!t1) { 00739 printf("3. Cstr < SubString\n"); 00740 rslt = false; 00741 } 00742 00743 // Cstr > SubString 00744 t1 = false; 00745 if ("xyzzy" > a(0, 5)) { 00746 t1 = true; 00747 } 00748 if (!t1) { 00749 printf("4. Cstr > SubString\n"); 00750 rslt = false; 00751 } 00752 00753 // Cstr >= SubString 00754 t1 = false; 00755 if ("abcde" >= a(0, 5)) { 00756 t1 = true; 00757 } 00758 if (!t1) { 00759 printf("5. Cstr >= SubString\n"); 00760 rslt = false; 00761 } 00762 00763 // Cstr >= SubString 00764 t1 = false; 00765 if ("xyzzy" >= a(0, 5)) { 00766 t1 = true; 00767 } 00768 if (!t1) { 00769 printf("6. Cstr >= SubString\n"); 00770 rslt = false; 00771 } 00772 00773 // Cstr <= SubString 00774 t1 = false; 00775 if ("abcde" <= a(0, 5)) { 00776 t1 = true; 00777 } 00778 if (!t1) { 00779 printf("7. Cstr <= SubString\n"); 00780 rslt = false; 00781 } 00782 00783 // Cstr <= SubString 00784 t1 = false; 00785 if ("abcde" <= a(5, 5)) { 00786 t1 = true; 00787 } 00788 if (!t1) { 00789 printf("8. Cstr <= SubString\n"); 00790 rslt = false; 00791 } 00792 00793 return rslt; 00794 } // globalSubStrRelations |
|
Definition at line 798 of file SubStrTest.C. References globalSubStrRelations(), subStrAssign(), subStrRefCnt(), subStrRelations(), and subStrSection().
00799 { 00800 bool passed = true; 00801 printf("SubString tests \n"); 00802 00803 if (!subStrRefCnt()) { 00804 passed = false; 00805 } 00806 00807 if (!subStrSection()) { 00808 passed = false; 00809 } 00810 00811 if (!subStrAssign()) { 00812 passed = false; 00813 } 00814 00815 if (!subStrRelations()) { 00816 passed = false; 00817 } 00818 00819 if (! globalSubStrRelations()) { 00820 passed = false; 00821 } 00822 00823 printf("Tests "); 00824 if (passed) 00825 printf("passed\n"); 00826 else 00827 printf("failed\n"); 00828 return 0; 00829 } |
|
Test assignment to and from a SubString object.
Definition at line 281 of file SubStrTest.C. References String::getRefCnt(), and String::strlen(). Referenced by main().
00282 { 00283 bool rslt = true; 00284 00285 printf("Test assignment to and from a SubString object\n"); 00286 const char *CStr1 = "abcde1234jklm"; 00287 const char *CStr2 = "abcdefghijklm"; 00288 String a = CStr1; 00289 00290 if (a != CStr1) { 00291 printf("1. Contents of String is incorrect\n"); 00292 rslt = false; 00293 } 00294 00295 String b = a; 00296 if (b.getRefCnt() != 2) { 00297 printf("2. Reference count is incorrect\n"); 00298 rslt = false; 00299 } 00300 00301 a(5, 4) = "fghi"; 00302 if (a != CStr2) { 00303 printf("3. Contents of String is incorrect\n"); 00304 rslt = false; 00305 } 00306 00307 if (b.getRefCnt() != 1) { 00308 printf("4. Reference count is incorrect\n"); 00309 rslt = false; 00310 } 00311 00312 String c = a(0, 5); 00313 if (c != "abcde") { 00314 printf("5. Contents of String is incorrect\n"); 00315 rslt = false; 00316 } 00317 00318 // assign a string to a substring section 00319 a(5, 5) = c; 00320 if (a(0,10) != "abcdeabcde") { 00321 printf("6. Contents of String is incorrect\n"); 00322 rslt = false; 00323 } 00324 00325 // Now check assignment to regions that are not the same 00326 // length as the string being assigned. 00327 00328 String target = "the lazy golden"; 00329 String t2 = target; 00330 00331 // assign a longer C-string to a shorter assignment region 00332 t2(0, 3) = "morgie is a"; 00333 if (t2 != "morgie is a lazy golden") { 00334 printf("7. C-string assignment to SubString is wrong\n"); 00335 rslt = false; 00336 } 00337 00338 if (target.getRefCnt() != 1) { 00339 printf("8. Reference count is wrong\n"); 00340 rslt = false; 00341 } 00342 00343 if (target != "the lazy golden") { 00344 printf("9. The contents of target is wrong\n"); 00345 rslt = false; 00346 } 00347 00348 t2(17, 6) = "spaniel"; 00349 // assign a shorter C-string to a longer assignment region 00350 t2(0, 6) = "max"; 00351 if (t2 != "max is a lazy spaniel") { 00352 const char *Cstr = t2; 00353 printf("10. The contents of t2 is incorrect. t2 = %s\n", Cstr ); 00354 rslt = false; 00355 } 00356 00357 // Test assignments of String to SubString 00358 00359 // Longer assigned to shorter 00360 target = "max is a dog"; 00361 String replaceAnimal = "people"; 00362 String replaceName = "Maggie"; 00363 target(9,3) = replaceAnimal; 00364 target(0,3) = replaceName; 00365 if (target != "Maggie is a people") { 00366 printf("11. String to SubString assignment failed\n"); 00367 rslt = false; 00368 } 00369 00370 // shorter assignments to longer SubString region 00371 replaceName = "Ian"; 00372 target(0, 6) = replaceName; 00373 00374 if (target != "Ian is a people") { 00375 printf("11. String to SubString assignment failed\n"); 00376 rslt = false; 00377 } 00378 00379 if (target.strlen() != strlen("Ian is a people")) { 00380 printf("12. length of the string is wrong\n"); 00381 rslt = false; 00382 } 00383 00384 // Test the explicit SubString constructor with assignment. 00385 // Also make sure that chained assignment works in this case. 00386 00387 const char *expected_result = "the lazy golden"; 00388 String u; 00389 String s = "the lazy dog"; 00390 SubString t(s, 9, 3); 00391 00392 u = t = "golden"; 00393 00394 if (s != expected_result) { 00395 printf("13. the contents of s is \"%s\", should be \"%s\"\n", 00396 (const char *)s, expected_result); 00397 rslt = false; 00398 } 00399 00400 if (u != expected_result) { 00401 printf("14. the contents of u is \"%s\", should be \"%s\"\n", 00402 (const char *)u, expected_result); 00403 rslt = false; 00404 } 00405 00406 // The result of the assignment to the SubString should be the 00407 // String, "s" passed to the constructor. This is assigned to 00408 // the String u, so the reference count should be 2. 00409 if (u.getRefCnt() != 2) { 00410 printf("15. reference count is %d, should be 2\n", u.getRefCnt() ); 00411 rslt = false; 00412 } 00413 00414 return rslt; 00415 } // subStrAssign |
|
Definition at line 63 of file SubStrTest.C. References SubString::getRefCnt(). Referenced by subStrRefCnt().
00064 { 00065 bool result = (subStr.getRefCnt() == previousRefCnt); 00066 return result; 00067 } |
|
Definition at line 51 of file SubStrTest.C. References SubString::getRefCnt(). Referenced by subStrRefCnt().
00052 { 00053 bool result = (subStr.getRefCnt() == previousRefCnt + 1); 00054 if (result == false) { 00055 printf("SubStringPassByValue: argument subStr.getRefCnt() = %d\n", 00056 subStr.getRefCnt() ); 00057 printf("SubStringPassByValue: previousRefCnt = %d\n", previousRefCnt ); 00058 } 00059 return result; 00060 } |
|
Test to verify that reference counting is handled properly for the SubString class. This also tests assignment as a side-effect. Definition at line 74 of file SubStrTest.C. References String::getRefCnt(), SubString::getRefCnt(), SubStringPassByReference(), and SubStringPassByValue(). Referenced by main().
00075 { 00076 bool result = true; 00077 printf("Test SubString reference count\n"); 00078 00079 String a("abcdefghijk"); 00080 00081 // 00082 // Test reference count 00083 // 00084 SubString subStrA(a, 5, 6); 00085 if (subStrA.getRefCnt() != 1) { 00086 printf("1. reference count is %d, should be 1\n", 00087 subStrA.getRefCnt()); 00088 result = false; 00089 } 00090 00091 size_t previousRefCnt = subStrA.getRefCnt(); 00092 if (! SubStringPassByReference( subStrA, previousRefCnt) ) { 00093 printf("2. Pass by reference failed\n"); 00094 result = false; 00095 } 00096 00097 previousRefCnt = subStrA.getRefCnt(); 00098 if (! SubStringPassByValue( subStrA, previousRefCnt) ) { 00099 printf("3. Pass by reference value\n"); 00100 result = false; 00101 } 00102 00103 if (a.getRefCnt() != 1) { 00104 printf("4. refCnt of the String referenced by the SubString = %d, should be 1\n", a.getRefCnt() ); 00105 result = false; 00106 } 00107 00108 return result; 00109 } // subStrRefCnt |
|
Test SubString relational operations.
Definition at line 421 of file SubStrTest.C. Referenced by main().
00422 { 00423 bool rslt = true; 00424 String a = "abcdeabcde"; 00425 printf("Test SubString relational operators\n"); 00426 00427 // 00428 // == operator 00429 // 00430 00431 // SubString != SubString 00432 bool t1 = false; 00433 if (a(0,5) == a(5, 5)) { 00434 t1 = true; 00435 } 00436 if (!t1) { 00437 printf("1. SubString == SubString failed\n"); 00438 rslt = false; 00439 } 00440 00441 // SubString == Cstr 00442 bool t2 = false; 00443 if (a(0,5) == "abcde") { 00444 t2 = true; 00445 } 00446 if (!t2) { 00447 printf("2. SubString == Cstr failed\n"); 00448 rslt = false; 00449 } 00450 00451 // SubString == String 00452 bool t3 = false; 00453 String b = "abcde"; 00454 if (a(0,5) == b) { 00455 t3 = true; 00456 } 00457 if (!t3) { 00458 printf("3. SubString == String failed\n"); 00459 rslt = false; 00460 } 00461 00462 t1 = true; 00463 if (a(1,4) == "bcd") { 00464 t1 = false; 00465 } 00466 if (!t1) { 00467 printf("4. SubString == CStr of unequal length should have been false\n"); 00468 rslt = false; 00469 } 00470 00471 00472 // 00473 // != operator 00474 // 00475 t1 = false; 00476 t2 = false; 00477 t3 = false; 00478 // 012345678901234 00479 String c = "abcdexyzzyabcde"; 00480 // SubString != SubString 00481 if (a(0,5) != c(5,5)) { 00482 t1 = true; 00483 } 00484 if (!t1) { 00485 printf("5. SubString != SubString failed\n"); 00486 rslt = false; 00487 } 00488 00489 // SubString != Cstr 00490 if (a(0,5) != "xyzzy") { 00491 t2 = true; 00492 } 00493 if (!t2) { 00494 printf("6. SubString != SubString failed\n"); 00495 rslt = false; 00496 } 00497 // SubString != String 00498 b = "zyzzy"; 00499 if (a(0,5) != b) { 00500 t3 = true; 00501 } 00502 if (!t3) { 00503 printf("7. SubString != String failed\n"); 00504 rslt = false; 00505 } 00506 00507 bool t4 = false; 00508 if (a(0, 5) != "abcd") { 00509 t4 = true; 00510 } 00511 if (!t4) { 00512 printf("8. SubString != CStr of unequal length failed\n"); 00513 rslt = false; 00514 } 00515 00516 t4 = true; 00517 if (a(1, 4) != "bcde") { 00518 t4 = false; 00519 } 00520 if (!t4) { 00521 printf("9. SubString != CStr are equal - test should be false\n"); 00522 rslt = false; 00523 } 00524 00525 // 00526 // < operator 00527 // 00528 t1 = false; 00529 t2 = false; 00530 t3 = false; 00531 t4 = false; 00532 00533 // SubString < SubString 00534 if (a(1,5) < c(5,5)) { 00535 t1 = true; 00536 } 00537 if (!t1) { 00538 printf("10. SubString < SubString failed\n"); 00539 rslt = false; 00540 } 00541 00542 // SubString < SubString: overlapping SubString sections on the same String 00543 t1 = false; 00544 if (c(1,5) < c(5,5)) { 00545 t1 = true; 00546 } 00547 if (!t1) { 00548 printf("11. SubString < SubString failed for overlapping sections\n"); 00549 rslt = false; 00550 } 00551 00552 // SubString < Cstr 00553 t1 = false; 00554 if (c(1,5) < "pdsrsmzf") { 00555 t1 = true; 00556 } 00557 if (!t1) { 00558 printf("12. SubString < Cstr \n"); 00559 rslt = false; 00560 } 00561 00562 // SubString < String 00563 // note that b is still "xyzzy" 00564 t1 = false; 00565 if (c(0,8) < b) { 00566 t1 = true; 00567 } 00568 if (!t1) { 00569 printf("13. SubString < String \n"); 00570 rslt = false; 00571 } 00572 00573 // 00574 // > operator 00575 // 00576 00577 // SubString > SubString 00578 // 0123456789 00579 String d = " abc "; 00580 t1 = false; 00581 if (c(10, 5) > d(2, 5)) { 00582 t1 = true; 00583 } 00584 if (!t1) { 00585 printf("14. SubString > SubString\n"); 00586 rslt = false; 00587 } 00588 00589 // SubString > Cstr (note, "xyz" will be "extended" with null chars) 00590 t1 = false; 00591 if (c(5, 5) > "xyz") { 00592 t1 = true; 00593 } 00594 if (!t1) { 00595 printf("15. SubString > Cstr\n"); 00596 rslt = false; 00597 } 00598 00599 // SubString > String 00600 b = "lesser of two evils"; 00601 t1 = false; 00602 if (c(5, 5) > b) { 00603 t1 = true; 00604 } 00605 if (!t1) { 00606 printf("16. SubString > String\n"); 00607 String ts = c(0, 5); 00608 const char *t1 = ts; 00609 const char *t2 = b; 00610 printf("c(0, 5) = %s, b = %s\n", t1, t2); 00611 rslt = false; 00612 } 00613 00614 // 00615 // >= operator 00616 // 00617 t1 = false; 00618 if (c(0,5) >= c(10, 5)) { 00619 t1 = true; 00620 } 00621 if (!t1) { 00622 printf("17. SubString >= SubString\n"); 00623 rslt = false; 00624 } 00625 00626 t1 = false; 00627 if (c(0,5) >= "abcde") { 00628 t1 = true; 00629 } 00630 if (!t1) { 00631 printf("18. SubString >= CStr\n"); 00632 rslt = false; 00633 } 00634 00635 t1 = false; 00636 if (c(5,5) >= "abcde") { 00637 t1 = true; 00638 } 00639 if (!t1) { 00640 printf("19. SubString >= CStr\n"); 00641 rslt = false; 00642 } 00643 00644 00645 t1 = false; 00646 b = "abcde"; 00647 if (c(0,5) >= b) { 00648 t1 = true; 00649 } 00650 if (!t1) { 00651 printf("20. SubString >= String\n"); 00652 rslt = false; 00653 } 00654 00655 // 00656 // <= operator 00657 // 00658 t1 = false; 00659 if (c(0,5) <= c(5, 5)) { 00660 t1 = true; 00661 } 00662 if (!t1) { 00663 printf("21. SubString <= SubString\n"); 00664 rslt = false; 00665 } 00666 00667 t1 = false; 00668 if (c(0,5) <= "xyzzy") { 00669 t1 = true; 00670 } 00671 if (!t1) { 00672 printf("22. SubString <= CStr\n"); 00673 rslt = false; 00674 } 00675 00676 t1 = false; 00677 b = "xyzzy"; 00678 if (c(0,5) <= b) { 00679 t1 = true; 00680 } 00681 if (!t1) { 00682 printf("23. SubString >= String\n"); 00683 rslt = false; 00684 } 00685 00686 t1 = false; 00687 b = "abcde"; 00688 if (c(0,5) <= b) { 00689 t1 = true; 00690 } 00691 if (!t1) { 00692 printf("24. SubString >= String\n"); 00693 rslt = false; 00694 } 00695 00696 return rslt; 00697 } // subStrRelations |
|
Test SubString section operations. The SubString constructor creates a section:
|