| 1 | /* librevenge |
| 2 | * Version: MPL 2.0 / LGPLv2.1+ |
| 3 | * |
| 4 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 | * |
| 8 | * Alternatively, the contents of this file may be used under the terms |
| 9 | * of the GNU Lesser General Public License Version 2.1 or later |
| 10 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 11 | * applicable instead of those above. |
| 12 | */ |
| 13 | |
| 14 | #ifndef RVNGSTRINGITERTEST_H |
| 15 | #define RVNGSTRINGITERTEST_H |
| 16 | |
| 17 | #include <cassert> |
| 18 | |
| 19 | #include <cppunit/TestFixture.h> |
| 20 | #include <cppunit/extensions/HelperMacros.h> |
| 21 | |
| 22 | #include <librevenge/librevenge.h> |
| 23 | |
| 24 | #include "librevenge_internal.h" |
| 25 | |
| 26 | namespace test |
| 27 | { |
| 28 | |
| 29 | using librevenge::RVNGString; |
| 30 | |
| 31 | using std::string; |
| 32 | |
| 33 | namespace |
| 34 | { |
| 35 | |
| 36 | void implTestIter(const string &msg, const char *chars[], const std::size_t count) |
| 37 | { |
| 38 | assert(count > 1); |
| 39 | |
| 40 | RVNGString str; |
| 41 | for (std::size_t i = 0; i != count; ++i) |
| 42 | str.append(chars[i]); |
| 43 | |
| 44 | RVNGString::Iter iter(str); |
| 45 | |
| 46 | CPPUNIT_ASSERT_MESSAGE(msg + ": " + "not at end after creation", !iter.last()); |
| 47 | CPPUNIT_ASSERT_EQUAL_MESSAGE(msg + ": " + "deref. at first char", string(chars[0]), string(iter())); |
| 48 | CPPUNIT_ASSERT_EQUAL_MESSAGE(msg + ": " + "second deref. doesn't change result", string(chars[0]), string(iter())); |
| 49 | CPPUNIT_ASSERT_MESSAGE(msg + ": " + "can advance to second char", iter.next()); |
| 50 | |
| 51 | for (std::size_t i = 1; i < count - 1; ++i) |
| 52 | { |
| 53 | const string pos(std::to_string(i)); |
| 54 | CPPUNIT_ASSERT_MESSAGE(msg + ": " + "not at end at char pos " + pos, !iter.last()); |
| 55 | CPPUNIT_ASSERT_EQUAL_MESSAGE(msg + ": " + "deref. at char pos " + pos, string(chars[i]), string(iter())); |
| 56 | CPPUNIT_ASSERT_MESSAGE(msg + ": " + "can advance at char pos " + pos, iter.next()); |
| 57 | } |
| 58 | |
| 59 | CPPUNIT_ASSERT_MESSAGE(msg + ": " + "not at end at last char", !iter.last()); |
| 60 | CPPUNIT_ASSERT_EQUAL_MESSAGE(msg + ": " + "deref. at last char", string(chars[count - 1]), string(iter())); |
| 61 | CPPUNIT_ASSERT_MESSAGE(msg + ": " + "can't advance anymore", !iter.next()); |
| 62 | CPPUNIT_ASSERT_MESSAGE(msg + ": " + "is at end", iter.last()); |
| 63 | } |
| 64 | |
| 65 | } |
| 66 | |
| 67 | class RVNGStringIterTest : public CPPUNIT_NS::TestFixture |
| 68 | { |
| 69 | public: |
| 70 | virtual void setUp(); |
| 71 | virtual void tearDown(); |
| 72 | |
| 73 | private: |
| 74 | CPPUNIT_TEST_SUITE(RVNGStringIterTest); |
| 75 | CPPUNIT_TEST(testEmpty); |
| 76 | CPPUNIT_TEST(testNonempty); |
| 77 | CPPUNIT_TEST(testRewind); |
| 78 | CPPUNIT_TEST_SUITE_END(); |
| 79 | |
| 80 | private: |
| 81 | void testEmpty(); |
| 82 | void testNonempty(); |
| 83 | void testRewind(); |
| 84 | }; |
| 85 | |
| 86 | void RVNGStringIterTest::setUp() |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | void RVNGStringIterTest::tearDown() |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | void RVNGStringIterTest::testEmpty() |
| 95 | { |
| 96 | const RVNGString str; |
| 97 | RVNGString::Iter iter(str); |
| 98 | |
| 99 | CPPUNIT_ASSERT_MESSAGE("can't advance on empty string", !iter.next()); |
| 100 | CPPUNIT_ASSERT_MESSAGE("deref. returns a string", iter()); |
| 101 | CPPUNIT_ASSERT_EQUAL_MESSAGE("deref. returns an empty string", string(), string(iter())); |
| 102 | CPPUNIT_ASSERT_MESSAGE("check end", iter.last()); |
| 103 | } |
| 104 | |
| 105 | void RVNGStringIterTest::testNonempty() |
| 106 | { |
| 107 | const char *ascii[] = {"a", "b", "c"}; |
| 108 | implTestIter("ASCII text", ascii, RVNG_NUM_ELEMENTS(ascii)); |
| 109 | const char *cyrilic[] = {"\xd0\xb0", "\xd0\xb1", "\xd0\xb2"}; |
| 110 | implTestIter("Cyrilic text", cyrilic, RVNG_NUM_ELEMENTS(cyrilic)); |
| 111 | const char *mixed[] = {"a", "\xd0\xb1", "c", "\xd0\xb3"}; |
| 112 | implTestIter("Mixed text", mixed, RVNG_NUM_ELEMENTS(mixed)); |
| 113 | } |
| 114 | |
| 115 | void RVNGStringIterTest::testRewind() |
| 116 | { |
| 117 | const RVNGString str; |
| 118 | RVNGString::Iter iter(str); |
| 119 | |
| 120 | CPPUNIT_ASSERT_MESSAGE("at end", iter.last()); |
| 121 | iter.rewind(); |
| 122 | CPPUNIT_ASSERT_MESSAGE("not at end", !iter.last()); |
| 123 | CPPUNIT_ASSERT_EQUAL_MESSAGE("deref. is ignored", reinterpret_cast<const char *>(0), iter()); |
| 124 | CPPUNIT_ASSERT_MESSAGE("nothing to step forward at", !iter.next()); |
| 125 | CPPUNIT_ASSERT_MESSAGE("at end again", iter.last()); |
| 126 | CPPUNIT_ASSERT_MESSAGE("deref. returns a string", iter()); |
| 127 | } |
| 128 | |
| 129 | CPPUNIT_TEST_SUITE_REGISTRATION(RVNGStringIterTest); |
| 130 | |
| 131 | } |
| 132 | |
| 133 | #endif // RVNGSTRINGITERTEST_H |
| 134 | |
| 135 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |