1 | // |
2 | // TextIteratorTest.cpp |
3 | // |
4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "TextIteratorTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/TextIterator.h" |
15 | #include "Poco/Latin1Encoding.h" |
16 | #include "Poco/UTF8Encoding.h" |
17 | #include "Poco/UTF16Encoding.h" |
18 | |
19 | |
20 | using Poco::TextIterator; |
21 | using Poco::Latin1Encoding; |
22 | using Poco::UTF8Encoding; |
23 | using Poco::UTF16Encoding; |
24 | |
25 | |
26 | TextIteratorTest::TextIteratorTest(const std::string& rName): CppUnit::TestCase(rName) |
27 | { |
28 | } |
29 | |
30 | |
31 | TextIteratorTest::~TextIteratorTest() |
32 | { |
33 | } |
34 | |
35 | |
36 | void TextIteratorTest::testEmptyLatin1() |
37 | { |
38 | Latin1Encoding encoding; |
39 | std::string text; |
40 | TextIterator it(text, encoding); |
41 | TextIterator end(text); |
42 | |
43 | assertTrue (it == end); |
44 | } |
45 | |
46 | |
47 | void TextIteratorTest::testOneLatin1() |
48 | { |
49 | Latin1Encoding encoding; |
50 | std::string text("x" ); |
51 | TextIterator it(text, encoding); |
52 | TextIterator end(text); |
53 | |
54 | assertTrue (it != end); |
55 | assertTrue (*it == 'x'); |
56 | ++it; |
57 | assertTrue (it == end); |
58 | } |
59 | |
60 | |
61 | void TextIteratorTest::testLatin1() |
62 | { |
63 | Latin1Encoding encoding; |
64 | std::string text("Latin1" ); |
65 | TextIterator it(text, encoding); |
66 | TextIterator end(text); |
67 | |
68 | assertTrue (it != end); |
69 | assertTrue (*it++ == 'L'); |
70 | assertTrue (it != end); |
71 | assertTrue (*it++ == 'a'); |
72 | assertTrue (it != end); |
73 | assertTrue (*it++ == 't'); |
74 | assertTrue (it != end); |
75 | assertTrue (*it++ == 'i'); |
76 | assertTrue (it != end); |
77 | assertTrue (*it++ == 'n'); |
78 | assertTrue (it != end); |
79 | assertTrue (*it++ == '1'); |
80 | assertTrue (it == end); |
81 | |
82 | std::string empty; |
83 | it = TextIterator(empty, encoding); |
84 | end = TextIterator(empty); |
85 | assertTrue (it == end); |
86 | } |
87 | |
88 | |
89 | void TextIteratorTest::testEmptyUTF8() |
90 | { |
91 | UTF8Encoding encoding; |
92 | std::string text; |
93 | TextIterator it(text, encoding); |
94 | TextIterator end(text); |
95 | |
96 | assertTrue (it == end); |
97 | } |
98 | |
99 | |
100 | void TextIteratorTest::testOneUTF8() |
101 | { |
102 | UTF8Encoding encoding; |
103 | |
104 | // 1 byte sequence |
105 | std::string text("x" ); |
106 | TextIterator it(text, encoding); |
107 | TextIterator end(text); |
108 | |
109 | assertTrue (it != end); |
110 | assertTrue (*it == 'x'); |
111 | ++it; |
112 | assertTrue (it == end); |
113 | |
114 | unsigned char data[Poco::TextEncoding::MAX_SEQUENCE_LENGTH]; |
115 | |
116 | // 2 byte sequence |
117 | int n = encoding.convert(0xab, data, sizeof(data)); |
118 | assertTrue (n == 2); |
119 | text.assign((char*) data, n); |
120 | it = TextIterator(text, encoding); |
121 | end = TextIterator(text); |
122 | |
123 | assertTrue (it != end); |
124 | assertTrue (*it++ == 0xab); |
125 | assertTrue (it == end); |
126 | |
127 | // 3 byte sequence |
128 | n = encoding.convert(0xabcd, data, sizeof(data)); |
129 | assertTrue (n == 3); |
130 | text.assign((char*) data, n); |
131 | it = TextIterator(text, encoding); |
132 | end = TextIterator(text); |
133 | |
134 | assertTrue (it != end); |
135 | assertTrue (*it++ == 0xabcd); |
136 | assertTrue (it == end); |
137 | |
138 | // 4 byte sequence |
139 | n = encoding.convert(0xabcde, data, sizeof(data)); |
140 | assertTrue (n == 4); |
141 | text.assign((char*) data, n); |
142 | it = TextIterator(text, encoding); |
143 | end = TextIterator(text); |
144 | |
145 | assertTrue (it != end); |
146 | assertTrue (*it++ == 0xabcde); |
147 | assertTrue (it == end); |
148 | |
149 | // 5 byte sequence - not supported |
150 | n = encoding.convert(0xabcdef, data, sizeof(data)); |
151 | assertTrue (n == 0); |
152 | |
153 | // 6 byte sequence - not supported |
154 | n = encoding.convert(0xfabcdef, data, sizeof(data)); |
155 | assertTrue (n == 0); |
156 | } |
157 | |
158 | |
159 | void TextIteratorTest::testUTF8() |
160 | { |
161 | UTF8Encoding encoding; |
162 | const unsigned char greek[] = {0x20, 0xce, 0xba, 0xe1, 0xbd, 0xb9, 0xcf, 0x83, 0xce, 0xbc, 0xce, 0xb5, 0x20, 0x00}; |
163 | std::string text((const char*) greek); |
164 | TextIterator it(text, encoding); |
165 | TextIterator end(text); |
166 | |
167 | assertTrue (it != end); |
168 | assertTrue (*it++ == 0x0020); |
169 | assertTrue (it != end); |
170 | assertTrue (*it++ == 0x03ba); |
171 | assertTrue (it != end); |
172 | assertTrue (*it++ == 0x1f79); |
173 | assertTrue (it != end); |
174 | assertTrue (*it++ == 0x03c3); |
175 | assertTrue (it != end); |
176 | assertTrue (*it++ == 0x03bc); |
177 | assertTrue (it != end); |
178 | assertTrue (*it++ == 0x03b5); |
179 | assertTrue (it != end); |
180 | assertTrue (*it++ == 0x0020); |
181 | assertTrue (it == end); |
182 | } |
183 | |
184 | |
185 | void TextIteratorTest::testUTF8Supplementary() |
186 | { |
187 | UTF8Encoding encoding; |
188 | const unsigned char supp[] = {0x41, 0x42, 0xf0, 0x90, 0x82, 0xa4, 0xf0, 0xaf, 0xa6, 0xa0, 0xf0, 0xaf, 0xa8, 0x9d, 0x00}; |
189 | std::string text((const char*) supp); |
190 | TextIterator it(text, encoding); |
191 | TextIterator end(text); |
192 | |
193 | assertTrue (it != end); |
194 | assertTrue (*it++ == 0x0041); |
195 | assertTrue (it != end); |
196 | assertTrue (*it++ == 0x0042); |
197 | assertTrue (it != end); |
198 | assertTrue (*it++ == 0x100a4); |
199 | assertTrue (it != end); |
200 | assertTrue (*it++ == 0x2f9a0); |
201 | assertTrue (it != end); |
202 | assertTrue (*it++ == 0x2fa1d); |
203 | assertTrue (it == end); |
204 | } |
205 | |
206 | |
207 | void TextIteratorTest::testUTF16Supplementary() |
208 | { |
209 | UTF16Encoding encoding; |
210 | const Poco::UInt16 supp [] = { 0x0041, 0x0042, 0xD800, 0xDCA4, 0xD87E, 0xDDA0, 0xD87E, 0xDE1D, 0x00}; |
211 | std::string text((const char*) supp, 16); |
212 | TextIterator it(text, encoding); |
213 | TextIterator end(text); |
214 | |
215 | assertTrue (it != end); |
216 | assertTrue (*it++ == 0x0041); |
217 | assertTrue (it != end); |
218 | assertTrue (*it++ == 0x0042); |
219 | assertTrue (it != end); |
220 | assertTrue (*it++ == 0x100a4); |
221 | assertTrue (it != end); |
222 | assertTrue (*it++ == 0x2f9a0); |
223 | assertTrue (it != end); |
224 | assertTrue (*it++ == 0x2fa1d); |
225 | assertTrue (it == end); |
226 | } |
227 | |
228 | |
229 | void TextIteratorTest::testSwap() |
230 | { |
231 | Latin1Encoding encoding; |
232 | std::string text("x" ); |
233 | TextIterator it1(text, encoding); |
234 | TextIterator it2(text, encoding); |
235 | TextIterator end(text); |
236 | |
237 | assertTrue (it1 == it2); |
238 | it2.swap(end); |
239 | assertTrue (it1 != it2); |
240 | it2.swap(end); |
241 | assertTrue (it1 == it2); |
242 | } |
243 | |
244 | |
245 | void TextIteratorTest::setUp() |
246 | { |
247 | } |
248 | |
249 | |
250 | void TextIteratorTest::tearDown() |
251 | { |
252 | } |
253 | |
254 | |
255 | CppUnit::Test* TextIteratorTest::suite() |
256 | { |
257 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TextIteratorTest" ); |
258 | |
259 | CppUnit_addTest(pSuite, TextIteratorTest, testEmptyLatin1); |
260 | CppUnit_addTest(pSuite, TextIteratorTest, testOneLatin1); |
261 | CppUnit_addTest(pSuite, TextIteratorTest, testLatin1); |
262 | CppUnit_addTest(pSuite, TextIteratorTest, testEmptyUTF8); |
263 | CppUnit_addTest(pSuite, TextIteratorTest, testOneUTF8); |
264 | CppUnit_addTest(pSuite, TextIteratorTest, testUTF8); |
265 | CppUnit_addTest(pSuite, TextIteratorTest, testUTF8Supplementary); |
266 | CppUnit_addTest(pSuite, TextIteratorTest, testUTF16Supplementary); |
267 | CppUnit_addTest(pSuite, TextIteratorTest, testSwap); |
268 | |
269 | return pSuite; |
270 | } |
271 | |