| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 | /* librevenge |
| 3 | * Version: MPL 2.0 / LGPLv2.1+ |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | * |
| 9 | * Alternatively, the contents of this file may be used under the terms |
| 10 | * of the GNU Lesser General Public License Version 2.1 or later |
| 11 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 12 | * applicable instead of those above. |
| 13 | */ |
| 14 | |
| 15 | #ifndef RVNGSTRINGTEST_H |
| 16 | #define RVNGSTRINGTEST_H |
| 17 | |
| 18 | #include <algorithm> |
| 19 | #include <cstring> |
| 20 | |
| 21 | #include <cppunit/TestFixture.h> |
| 22 | #include <cppunit/extensions/HelperMacros.h> |
| 23 | |
| 24 | #include <librevenge/librevenge.h> |
| 25 | |
| 26 | #include "librevenge_internal.h" |
| 27 | |
| 28 | namespace test |
| 29 | { |
| 30 | |
| 31 | using librevenge::RVNGString; |
| 32 | |
| 33 | using std::equal; |
| 34 | using std::size_t; |
| 35 | using std::strlen; |
| 36 | |
| 37 | namespace |
| 38 | { |
| 39 | |
| 40 | void implTestEscapeXML(const char *const input, const char *const expected) |
| 41 | { |
| 42 | const size_t len = strlen(expected); |
| 43 | |
| 44 | { |
| 45 | // appending a C string |
| 46 | RVNGString str; |
| 47 | str.appendEscapedXML(input); |
| 48 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned long>(len), str.size()); |
| 49 | CPPUNIT_ASSERT_EQUAL(int(len), str.len()); |
| 50 | CPPUNIT_ASSERT(equal(expected, expected + len, str.cstr())); |
| 51 | } |
| 52 | |
| 53 | { |
| 54 | // appending a RVNGString |
| 55 | RVNGString str; |
| 56 | str.appendEscapedXML(RVNGString(input)); |
| 57 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned long>(len), str.size()); |
| 58 | CPPUNIT_ASSERT_EQUAL(int(len), str.len()); |
| 59 | CPPUNIT_ASSERT(equal(expected, expected + len, str.cstr())); |
| 60 | } |
| 61 | |
| 62 | { |
| 63 | // creating from a C string |
| 64 | RVNGString str(RVNGString::escapeXML(input)); |
| 65 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned long>(len), str.size()); |
| 66 | CPPUNIT_ASSERT_EQUAL(int(len), str.len()); |
| 67 | CPPUNIT_ASSERT(equal(expected, expected + len, str.cstr())); |
| 68 | } |
| 69 | |
| 70 | { |
| 71 | // creating from a RVNGString |
| 72 | RVNGString str(RVNGString::escapeXML(RVNGString(input))); |
| 73 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned long>(len), str.size()); |
| 74 | CPPUNIT_ASSERT_EQUAL(int(len), str.len()); |
| 75 | CPPUNIT_ASSERT(equal(expected, expected + len, str.cstr())); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | } |
| 80 | |
| 81 | class RVNGStringTest : public CPPUNIT_NS::TestFixture |
| 82 | { |
| 83 | public: |
| 84 | virtual void setUp(); |
| 85 | virtual void tearDown(); |
| 86 | |
| 87 | private: |
| 88 | CPPUNIT_TEST_SUITE(RVNGStringTest); |
| 89 | CPPUNIT_TEST(testConstruction); |
| 90 | CPPUNIT_TEST(testAppend); |
| 91 | CPPUNIT_TEST(testAppendEscapedXML); |
| 92 | CPPUNIT_TEST(testClear); |
| 93 | CPPUNIT_TEST(testInvalid); |
| 94 | CPPUNIT_TEST_SUITE_END(); |
| 95 | |
| 96 | private: |
| 97 | void testConstruction(); |
| 98 | void testAppend(); |
| 99 | void testAppendEscapedXML(); |
| 100 | void testClear(); |
| 101 | void testInvalid(); |
| 102 | }; |
| 103 | |
| 104 | void RVNGStringTest::setUp() |
| 105 | { |
| 106 | } |
| 107 | |
| 108 | void RVNGStringTest::tearDown() |
| 109 | { |
| 110 | } |
| 111 | |
| 112 | void RVNGStringTest::testConstruction() |
| 113 | { |
| 114 | // default ctor creates empty string |
| 115 | RVNGString empty; |
| 116 | CPPUNIT_ASSERT_EQUAL(0ul, empty.size()); |
| 117 | CPPUNIT_ASSERT_EQUAL(0, empty.len()); |
| 118 | |
| 119 | // construction from a C string |
| 120 | const char input[] = "hello world"; |
| 121 | const size_t len = RVNG_NUM_ELEMENTS(input) - 1; |
| 122 | RVNGString str(input); |
| 123 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned long>(len), str.size()); |
| 124 | CPPUNIT_ASSERT_EQUAL(int(len), str.len()); |
| 125 | CPPUNIT_ASSERT(equal(input, input + len, str.cstr())); |
| 126 | |
| 127 | // copy construction |
| 128 | RVNGString copy(str); |
| 129 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned long>(len), copy.size()); |
| 130 | CPPUNIT_ASSERT_EQUAL(int(len), copy.len()); |
| 131 | CPPUNIT_ASSERT(equal(input, input + len, copy.cstr())); |
| 132 | |
| 133 | // assignment |
| 134 | RVNGString assign("x"); |
| 135 | assign = str; |
| 136 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned long>(len), assign.size()); |
| 137 | CPPUNIT_ASSERT_EQUAL(int(len), assign.len()); |
| 138 | CPPUNIT_ASSERT(equal(input, input + len, assign.cstr())); |
| 139 | |
| 140 | // assignment from C string |
| 141 | RVNGString assign2("x"); |
| 142 | assign2 = input; |
| 143 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned long>(len), assign2.size()); |
| 144 | CPPUNIT_ASSERT_EQUAL(int(len), assign2.len()); |
| 145 | CPPUNIT_ASSERT(equal(input, input + len, assign2.cstr())); |
| 146 | } |
| 147 | |
| 148 | void RVNGStringTest::testAppend() |
| 149 | { |
| 150 | const char input[] = "hello world"; |
| 151 | const size_t len = RVNG_NUM_ELEMENTS(input) - 1; |
| 152 | RVNGString str; |
| 153 | |
| 154 | // appending a character |
| 155 | str.append('h'); |
| 156 | CPPUNIT_ASSERT_EQUAL(1ul, str.size()); |
| 157 | CPPUNIT_ASSERT_EQUAL(1, str.len()); |
| 158 | CPPUNIT_ASSERT_EQUAL('h', str.cstr()[0]); |
| 159 | |
| 160 | RVNGString str2; |
| 161 | // appending a RVNGString |
| 162 | str2.append(str); |
| 163 | CPPUNIT_ASSERT_EQUAL(1ul, str2.size()); |
| 164 | CPPUNIT_ASSERT_EQUAL(1, str2.len()); |
| 165 | CPPUNIT_ASSERT_EQUAL('h', str2.cstr()[0]); |
| 166 | |
| 167 | // appending a C string |
| 168 | str2.append(input + 1); |
| 169 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned long>(len), str2.size()); |
| 170 | CPPUNIT_ASSERT_EQUAL(int(len), str2.len()); |
| 171 | CPPUNIT_ASSERT(equal(input, input + len, str2.cstr())); |
| 172 | } |
| 173 | |
| 174 | void RVNGStringTest::testAppendEscapedXML() |
| 175 | { |
| 176 | implTestEscapeXML("hello world", "hello world"); |
| 177 | implTestEscapeXML("<greetings who=\"world\" kind=\'&hello;\'>", "<greetings who="world" kind='&hello;'>"); |
| 178 | } |
| 179 | |
| 180 | void RVNGStringTest::testClear() |
| 181 | { |
| 182 | RVNGString str; |
| 183 | // clearing empty str does nothing |
| 184 | str.clear(); |
| 185 | CPPUNIT_ASSERT_EQUAL(0ul, str.size()); |
| 186 | CPPUNIT_ASSERT_EQUAL(0, str.len()); |
| 187 | |
| 188 | str.append('a'); |
| 189 | str.clear(); |
| 190 | CPPUNIT_ASSERT_EQUAL(0ul, str.size()); |
| 191 | CPPUNIT_ASSERT_EQUAL(0, str.len()); |
| 192 | } |
| 193 | |
| 194 | void RVNGStringTest::testInvalid() |
| 195 | { |
| 196 | const char input[] = "hello\xfd"; |
| 197 | const size_t len = RVNG_NUM_ELEMENTS(input) - 1; |
| 198 | |
| 199 | // construction from a C string |
| 200 | RVNGString str(input); |
| 201 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned long>(len - 1), str.size()); |
| 202 | CPPUNIT_ASSERT_EQUAL(int(len - 1), str.len()); |
| 203 | CPPUNIT_ASSERT(equal(input, input + len - 1, str.cstr())); |
| 204 | |
| 205 | // assignment from a C string |
| 206 | str.clear(); |
| 207 | str = input; |
| 208 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned long>(len - 1), str.size()); |
| 209 | CPPUNIT_ASSERT_EQUAL(int(len - 1), str.len()); |
| 210 | CPPUNIT_ASSERT(equal(input, input + len - 1, str.cstr())); |
| 211 | |
| 212 | // appending a C string |
| 213 | str.clear(); |
| 214 | str.append(input); |
| 215 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned long>(len - 1), str.size()); |
| 216 | CPPUNIT_ASSERT_EQUAL(int(len - 1), str.len()); |
| 217 | CPPUNIT_ASSERT(equal(input, input + len - 1, str.cstr())); |
| 218 | |
| 219 | // TODO: enable again in librevenge 0.1 |
| 220 | // appending a char |
| 221 | // str.clear(); |
| 222 | // str.append('\xfa'); |
| 223 | // CPPUNIT_ASSERT_EQUAL(0ul, str.size()); |
| 224 | // CPPUNIT_ASSERT_EQUAL(0, str.len()); |
| 225 | } |
| 226 | |
| 227 | CPPUNIT_TEST_SUITE_REGISTRATION(RVNGStringTest); |
| 228 | |
| 229 | } |
| 230 | |
| 231 | #endif // RVNGSTRINGTEST_H |
| 232 | |
| 233 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |