|
23 | 23 | * http://www.mozilla.org/MPL/ * |
24 | 24 | ***************************************************************************/ |
25 | 25 |
|
| 26 | +#include <cstring> |
26 | 27 | #define _USE_MATH_DEFINES |
27 | 28 | #include <cmath> |
28 | 29 |
|
@@ -53,6 +54,7 @@ class TestByteVector : public CppUnit::TestFixture |
53 | 54 | CPPUNIT_TEST(testAppend1); |
54 | 55 | CPPUNIT_TEST(testAppend2); |
55 | 56 | CPPUNIT_TEST(testBase64); |
| 57 | + CPPUNIT_TEST(testEmpty); |
56 | 58 | CPPUNIT_TEST_SUITE_END(); |
57 | 59 |
|
58 | 60 | public: |
@@ -612,6 +614,94 @@ class TestByteVector : public CppUnit::TestFixture |
612 | 614 |
|
613 | 615 | } |
614 | 616 |
|
| 617 | + void testEmpty() |
| 618 | + { |
| 619 | + const ByteVector empty; |
| 620 | + const ByteVector notEmpty("A"); |
| 621 | + ByteVector mutEmpty; |
| 622 | + |
| 623 | + CPPUNIT_ASSERT_EQUAL(empty, ByteVector("")); |
| 624 | + CPPUNIT_ASSERT_EQUAL(empty, ByteVector("", 0)); |
| 625 | + CPPUNIT_ASSERT_EQUAL(empty, ByteVector(0U)); |
| 626 | + CPPUNIT_ASSERT_EQUAL(empty, ByteVector(empty, 0, 0)); |
| 627 | + CPPUNIT_ASSERT_EQUAL(empty, ByteVector(notEmpty, 1, 0)); |
| 628 | + CPPUNIT_ASSERT_EQUAL(empty, ByteVector(static_cast<const char *>(nullptr))); |
| 629 | + CPPUNIT_ASSERT_EQUAL(empty, ByteVector(static_cast<const char *>(nullptr), 0)); |
| 630 | + CPPUNIT_ASSERT_EQUAL(mutEmpty.setData("", 0), empty); |
| 631 | + CPPUNIT_ASSERT_EQUAL(mutEmpty.setData(""), empty); |
| 632 | + CPPUNIT_ASSERT_EQUAL(mutEmpty.setData(nullptr, 0), empty); |
| 633 | + CPPUNIT_ASSERT_EQUAL(mutEmpty.setData(nullptr), empty); |
| 634 | + CPPUNIT_ASSERT(!empty.data()); |
| 635 | + CPPUNIT_ASSERT(!mutEmpty.data()); |
| 636 | + CPPUNIT_ASSERT_EQUAL(empty.mid(0), empty); |
| 637 | + CPPUNIT_ASSERT_EQUAL(empty.at(0), '\0'); |
| 638 | + // Note that the behavior of ByteVector::find() with an empty pattern is |
| 639 | + // not consistent with String::find() and std::string::find(). |
| 640 | + CPPUNIT_ASSERT_EQUAL(empty.find(mutEmpty), -1); |
| 641 | + CPPUNIT_ASSERT_EQUAL(empty.find(notEmpty), -1); |
| 642 | + CPPUNIT_ASSERT_EQUAL(notEmpty.find(empty), -1); |
| 643 | + CPPUNIT_ASSERT_EQUAL(empty.find('\0'), -1); |
| 644 | + CPPUNIT_ASSERT_EQUAL(empty.rfind(mutEmpty), -1); |
| 645 | + CPPUNIT_ASSERT_EQUAL(empty.rfind(notEmpty), -1); |
| 646 | + CPPUNIT_ASSERT_EQUAL(notEmpty.rfind(empty), -1); |
| 647 | + CPPUNIT_ASSERT_EQUAL(empty.containsAt(mutEmpty, 0), false); |
| 648 | + CPPUNIT_ASSERT_EQUAL(empty.startsWith(mutEmpty), false); |
| 649 | + CPPUNIT_ASSERT_EQUAL(empty.startsWith(notEmpty), false); |
| 650 | + CPPUNIT_ASSERT_EQUAL(notEmpty.startsWith(empty), false); |
| 651 | + CPPUNIT_ASSERT_EQUAL(empty.endsWith(mutEmpty), false); |
| 652 | + CPPUNIT_ASSERT_EQUAL(empty.endsWithPartialMatch(mutEmpty), -1); |
| 653 | + CPPUNIT_ASSERT_EQUAL(mutEmpty.replace('a', 'b'), empty); |
| 654 | + CPPUNIT_ASSERT_EQUAL(mutEmpty.replace("abc", ""), empty); |
| 655 | + CPPUNIT_ASSERT_EQUAL(mutEmpty.append(empty), empty); |
| 656 | + CPPUNIT_ASSERT_EQUAL(mutEmpty.append(notEmpty), notEmpty); |
| 657 | + mutEmpty.clear(); |
| 658 | + CPPUNIT_ASSERT_EQUAL(mutEmpty, empty); |
| 659 | + CPPUNIT_ASSERT_EQUAL(ByteVector(notEmpty).append(empty), notEmpty); |
| 660 | + CPPUNIT_ASSERT_EQUAL(mutEmpty.append('A'), notEmpty); |
| 661 | + CPPUNIT_ASSERT_EQUAL(mutEmpty.resize(0), empty); |
| 662 | + CPPUNIT_ASSERT_EQUAL(empty.size(), 0U); |
| 663 | + CPPUNIT_ASSERT(empty.begin() == empty.end()); |
| 664 | + CPPUNIT_ASSERT(empty.cbegin() == empty.cend()); |
| 665 | + CPPUNIT_ASSERT(empty.rbegin() == empty.rend()); |
| 666 | + CPPUNIT_ASSERT(mutEmpty.begin() == mutEmpty.end()); |
| 667 | + CPPUNIT_ASSERT(mutEmpty.rbegin() == mutEmpty.rend()); |
| 668 | + CPPUNIT_ASSERT(empty.isEmpty()); |
| 669 | + CPPUNIT_ASSERT_EQUAL(empty.toUInt(), 0U); |
| 670 | + CPPUNIT_ASSERT_EQUAL(empty.toUInt(0, true), 0U); |
| 671 | + CPPUNIT_ASSERT_EQUAL(empty.toUInt(0, 0, true), 0U); |
| 672 | + CPPUNIT_ASSERT_EQUAL(empty.toShort(), static_cast<short>(0)); |
| 673 | + CPPUNIT_ASSERT_EQUAL(empty.toShort(0, true), static_cast<short>(0)); |
| 674 | + CPPUNIT_ASSERT_EQUAL(empty.toUShort(), static_cast<unsigned short>(0)); |
| 675 | + CPPUNIT_ASSERT_EQUAL(empty.toUShort(0, true), static_cast<unsigned short>(0)); |
| 676 | + CPPUNIT_ASSERT_EQUAL(empty.toLongLong(), 0LL); |
| 677 | + CPPUNIT_ASSERT_EQUAL(empty.toLongLong(0, true), 0LL); |
| 678 | + CPPUNIT_ASSERT_EQUAL(empty.toULongLong(), 0ULL); |
| 679 | + CPPUNIT_ASSERT_EQUAL(empty.toULongLong(0, true), 0ULL); |
| 680 | + CPPUNIT_ASSERT_EQUAL(empty.toFloat32LE(0), 0.f); |
| 681 | + CPPUNIT_ASSERT_EQUAL(empty.toFloat32BE(0), 0.f); |
| 682 | + CPPUNIT_ASSERT_EQUAL(empty.toFloat64LE(0), 0.); |
| 683 | + CPPUNIT_ASSERT_EQUAL(empty.toFloat64BE(0), 0.); |
| 684 | + CPPUNIT_ASSERT_EQUAL(empty.toFloat80LE(0), 0.l); |
| 685 | + CPPUNIT_ASSERT_EQUAL(empty.toFloat80BE(0), 0.l); |
| 686 | + CPPUNIT_ASSERT(empty == mutEmpty); |
| 687 | + CPPUNIT_ASSERT(empty != notEmpty); |
| 688 | + CPPUNIT_ASSERT(empty == ""); |
| 689 | + CPPUNIT_ASSERT(empty != " "); |
| 690 | + CPPUNIT_ASSERT(empty == static_cast<const char *>(nullptr)); |
| 691 | + CPPUNIT_ASSERT(!(empty != static_cast<const char *>(nullptr))); |
| 692 | + CPPUNIT_ASSERT(empty < notEmpty); |
| 693 | + CPPUNIT_ASSERT(!(empty > notEmpty)); |
| 694 | + CPPUNIT_ASSERT_EQUAL(empty + mutEmpty, empty); |
| 695 | + CPPUNIT_ASSERT_EQUAL(empty + notEmpty, notEmpty); |
| 696 | + CPPUNIT_ASSERT_EQUAL(mutEmpty = static_cast<const char *>(nullptr), empty); |
| 697 | + CPPUNIT_ASSERT_EQUAL(mutEmpty = notEmpty, notEmpty); |
| 698 | + ByteVector tmp; |
| 699 | + mutEmpty.swap(tmp); |
| 700 | + CPPUNIT_ASSERT_EQUAL(mutEmpty, empty); |
| 701 | + CPPUNIT_ASSERT_EQUAL(empty.toHex(), empty); |
| 702 | + CPPUNIT_ASSERT_EQUAL(empty.toBase64(), empty); |
| 703 | + } |
| 704 | + |
615 | 705 | }; |
616 | 706 |
|
617 | 707 | CPPUNIT_TEST_SUITE_REGISTRATION(TestByteVector); |
0 commit comments