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