0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
|
|
43 |
#include <QtTest/QtTest>
|
|
44 |
|
|
45 |
#include <qtextboundaryfinder.h>
|
|
46 |
#include <qfile.h>
|
|
47 |
#include <qdebug.h>
|
|
48 |
|
|
49 |
//TESTED_CLASS=
|
|
50 |
//TESTED_FILES=gui/text/qtextlayout.h corelib/tools/qtextboundaryfinder.cpp
|
|
51 |
#ifdef Q_OS_SYMBIAN
|
|
52 |
#define SRCDIR "$$PWD"
|
|
53 |
#endif
|
|
54 |
|
|
55 |
class tst_QTextBoundaryFinder : public QObject
|
|
56 |
{
|
|
57 |
Q_OBJECT
|
|
58 |
|
|
59 |
public:
|
|
60 |
tst_QTextBoundaryFinder();
|
|
61 |
virtual ~tst_QTextBoundaryFinder();
|
|
62 |
|
|
63 |
|
|
64 |
public slots:
|
|
65 |
void init();
|
|
66 |
void cleanup();
|
|
67 |
private slots:
|
|
68 |
void graphemeBoundaries();
|
|
69 |
void wordBoundaries();
|
|
70 |
void sentenceBoundaries();
|
|
71 |
void isAtWordStart();
|
|
72 |
void fastConstructor();
|
|
73 |
void isAtBoundaryLine();
|
|
74 |
};
|
|
75 |
|
|
76 |
tst_QTextBoundaryFinder::tst_QTextBoundaryFinder()
|
|
77 |
{
|
|
78 |
}
|
|
79 |
|
|
80 |
tst_QTextBoundaryFinder::~tst_QTextBoundaryFinder()
|
|
81 |
{
|
|
82 |
}
|
|
83 |
|
|
84 |
void tst_QTextBoundaryFinder::init()
|
|
85 |
{
|
|
86 |
#ifndef Q_OS_IRIX
|
|
87 |
QDir::setCurrent(SRCDIR);
|
|
88 |
#endif
|
|
89 |
}
|
|
90 |
|
|
91 |
void tst_QTextBoundaryFinder::cleanup()
|
|
92 |
{
|
|
93 |
}
|
|
94 |
|
|
95 |
void tst_QTextBoundaryFinder::graphemeBoundaries()
|
|
96 |
{
|
|
97 |
QFile file("data/GraphemeBreakTest.txt");
|
|
98 |
file.open(QFile::ReadOnly);
|
|
99 |
|
|
100 |
int lines = 0;
|
|
101 |
while (!file.atEnd()) {
|
|
102 |
QByteArray line = file.readLine();
|
|
103 |
if (line.startsWith('#'))
|
|
104 |
continue;
|
|
105 |
|
|
106 |
lines++;
|
|
107 |
QString test = QString::fromUtf8(line);
|
|
108 |
int hash = test.indexOf('#');
|
|
109 |
if (hash > 0)
|
|
110 |
test = test.left(hash);
|
|
111 |
test = test.simplified();
|
|
112 |
test = test.replace(QLatin1String(" "), QString());
|
|
113 |
|
|
114 |
QList<int> breakPositions;
|
|
115 |
QString testString;
|
|
116 |
int pos = 0;
|
|
117 |
int strPos = 0;
|
|
118 |
while (pos < test.length()) {
|
|
119 |
if (test.at(pos).unicode() == 0xf7)
|
|
120 |
breakPositions.append(strPos);
|
|
121 |
else
|
|
122 |
Q_ASSERT(test.at(pos).unicode() == 0xd7);
|
|
123 |
++pos;
|
|
124 |
if (pos < test.length()) {
|
|
125 |
Q_ASSERT(pos < test.length() - 4);
|
|
126 |
QString hex = test.mid(pos, 4);
|
|
127 |
bool ok = true;
|
|
128 |
testString.append(QChar(hex.toInt(&ok, 16)));
|
|
129 |
Q_ASSERT(ok);
|
|
130 |
pos += 4;
|
|
131 |
}
|
|
132 |
++strPos;
|
|
133 |
}
|
|
134 |
|
|
135 |
QTextBoundaryFinder finder(QTextBoundaryFinder::Grapheme, testString);
|
|
136 |
for (int i = 0; i < breakPositions.size(); ++i) {
|
|
137 |
QCOMPARE(finder.position(), breakPositions.at(i));
|
|
138 |
finder.toNextBoundary();
|
|
139 |
}
|
|
140 |
QCOMPARE(finder.toNextBoundary(), -1);
|
|
141 |
|
|
142 |
for (int i = 0; i < testString.length(); ++i) {
|
|
143 |
finder.setPosition(i);
|
|
144 |
QCOMPARE(finder.isAtBoundary(), breakPositions.contains(i) == true);
|
|
145 |
}
|
|
146 |
}
|
|
147 |
QCOMPARE(lines, 100); // to see it actually found the file and all.
|
|
148 |
}
|
|
149 |
|
|
150 |
void tst_QTextBoundaryFinder::wordBoundaries()
|
|
151 |
{
|
|
152 |
QFile file("data/WordBreakTest.txt");
|
|
153 |
file.open(QFile::ReadOnly);
|
|
154 |
|
|
155 |
while (!file.atEnd()) {
|
|
156 |
QByteArray line = file.readLine();
|
|
157 |
if (line.startsWith('#'))
|
|
158 |
continue;
|
|
159 |
|
|
160 |
QString test = QString::fromUtf8(line);
|
|
161 |
int hash = test.indexOf('#');
|
|
162 |
if (hash > 0)
|
|
163 |
test = test.left(hash);
|
|
164 |
test = test.simplified();
|
|
165 |
test = test.replace(QLatin1String(" "), QString());
|
|
166 |
|
|
167 |
QList<int> breakPositions;
|
|
168 |
QString testString;
|
|
169 |
int pos = 0;
|
|
170 |
int strPos = 0;
|
|
171 |
while (pos < test.length()) {
|
|
172 |
if (test.at(pos).unicode() == 0xf7)
|
|
173 |
breakPositions.append(strPos);
|
|
174 |
else
|
|
175 |
Q_ASSERT(test.at(pos).unicode() == 0xd7);
|
|
176 |
++pos;
|
|
177 |
if (pos < test.length()) {
|
|
178 |
Q_ASSERT(pos < test.length() - 4);
|
|
179 |
QString hex = test.mid(pos, 4);
|
|
180 |
bool ok = true;
|
|
181 |
testString.append(QChar(hex.toInt(&ok, 16)));
|
|
182 |
Q_ASSERT(ok);
|
|
183 |
pos += 4;
|
|
184 |
}
|
|
185 |
++strPos;
|
|
186 |
}
|
|
187 |
|
|
188 |
QTextBoundaryFinder finder(QTextBoundaryFinder::Word, testString);
|
|
189 |
for (int i = 0; i < breakPositions.size(); ++i) {
|
|
190 |
QCOMPARE(finder.position(), breakPositions.at(i));
|
|
191 |
finder.toNextBoundary();
|
|
192 |
}
|
|
193 |
QCOMPARE(finder.toNextBoundary(), -1);
|
|
194 |
|
|
195 |
for (int i = 0; i < testString.length(); ++i) {
|
|
196 |
finder.setPosition(i);
|
|
197 |
QCOMPARE(finder.isAtBoundary(), breakPositions.contains(i) == true);
|
|
198 |
}
|
|
199 |
}
|
|
200 |
}
|
|
201 |
|
|
202 |
void tst_QTextBoundaryFinder::sentenceBoundaries()
|
|
203 |
{
|
|
204 |
QFile file("data/SentenceBreakTest.txt");
|
|
205 |
file.open(QFile::ReadOnly);
|
|
206 |
|
|
207 |
while (!file.atEnd()) {
|
|
208 |
QByteArray line = file.readLine();
|
|
209 |
if (line.startsWith('#'))
|
|
210 |
continue;
|
|
211 |
|
|
212 |
QString test = QString::fromUtf8(line);
|
|
213 |
int hash = test.indexOf('#');
|
|
214 |
if (hash > 0)
|
|
215 |
test = test.left(hash);
|
|
216 |
test = test.simplified();
|
|
217 |
test = test.replace(QLatin1String(" "), QString());
|
|
218 |
|
|
219 |
QList<int> breakPositions;
|
|
220 |
QString testString;
|
|
221 |
int pos = 0;
|
|
222 |
int strPos = 0;
|
|
223 |
while (pos < test.length()) {
|
|
224 |
if (test.at(pos).unicode() == 0xf7)
|
|
225 |
breakPositions.append(strPos);
|
|
226 |
else
|
|
227 |
Q_ASSERT(test.at(pos).unicode() == 0xd7);
|
|
228 |
++pos;
|
|
229 |
if (pos < test.length()) {
|
|
230 |
Q_ASSERT(pos < test.length() - 4);
|
|
231 |
QString hex = test.mid(pos, 4);
|
|
232 |
bool ok = true;
|
|
233 |
testString.append(QChar(hex.toInt(&ok, 16)));
|
|
234 |
Q_ASSERT(ok);
|
|
235 |
pos += 4;
|
|
236 |
}
|
|
237 |
++strPos;
|
|
238 |
}
|
|
239 |
|
|
240 |
QTextBoundaryFinder finder(QTextBoundaryFinder::Sentence, testString);
|
|
241 |
for (int i = 0; i < breakPositions.size(); ++i) {
|
|
242 |
QCOMPARE(finder.position(), breakPositions.at(i));
|
|
243 |
finder.toNextBoundary();
|
|
244 |
}
|
|
245 |
QCOMPARE(finder.toNextBoundary(), -1);
|
|
246 |
|
|
247 |
for (int i = 0; i < testString.length(); ++i) {
|
|
248 |
finder.setPosition(i);
|
|
249 |
QCOMPARE(finder.isAtBoundary(), breakPositions.contains(i) == true);
|
|
250 |
}
|
|
251 |
}
|
|
252 |
}
|
|
253 |
|
|
254 |
void tst_QTextBoundaryFinder::isAtWordStart()
|
|
255 |
{
|
|
256 |
QString txt("The quick brown fox jumped over $the lazy. dog ");
|
|
257 |
QList<int> start, end;
|
|
258 |
start << 0 << 4 << 10 << 16 << 20 << 27 << 32 << 33 << 37 << 41 << 43;
|
|
259 |
end << 3 << 9 << 15 << 19 << 26 << 31 << 33 << 36 << 41 << 42 << 46;
|
|
260 |
QTextBoundaryFinder finder(QTextBoundaryFinder::Word, txt);
|
|
261 |
for(int i=0; i < txt.length(); ++i) {
|
|
262 |
finder.setPosition(i);
|
|
263 |
QTextBoundaryFinder::BoundaryReasons r = finder.boundaryReasons();
|
|
264 |
// qDebug() << i << r;
|
|
265 |
QCOMPARE((r & QTextBoundaryFinder::StartWord) != 0, start.contains(i) == true);
|
|
266 |
QCOMPARE((r & QTextBoundaryFinder::EndWord) != 0, end.contains(i) == true);
|
|
267 |
}
|
|
268 |
}
|
|
269 |
|
|
270 |
void tst_QTextBoundaryFinder::fastConstructor()
|
|
271 |
{
|
|
272 |
QString text("Hello World");
|
|
273 |
QTextBoundaryFinder finder(QTextBoundaryFinder::Word, text.constData(), text.length(), /*buffer*/0, /*buffer size*/0);
|
|
274 |
QVERIFY(finder.boundaryReasons() == QTextBoundaryFinder::StartWord);
|
|
275 |
|
|
276 |
finder.toNextBoundary();
|
|
277 |
QCOMPARE(finder.position(), 5);
|
|
278 |
QVERIFY(finder.boundaryReasons() == QTextBoundaryFinder::EndWord);
|
|
279 |
|
|
280 |
finder.toNextBoundary();
|
|
281 |
QCOMPARE(finder.position(), 6);
|
|
282 |
QVERIFY(finder.boundaryReasons() == QTextBoundaryFinder::StartWord);
|
|
283 |
|
|
284 |
finder.toNextBoundary();
|
|
285 |
QCOMPARE(finder.position(), text.length());
|
|
286 |
QVERIFY(finder.boundaryReasons() == QTextBoundaryFinder::EndWord);
|
|
287 |
|
|
288 |
finder.toNextBoundary();
|
|
289 |
QVERIFY(finder.boundaryReasons() == QTextBoundaryFinder::NotAtBoundary);
|
|
290 |
QCOMPARE(finder.position(), -1);
|
|
291 |
}
|
|
292 |
|
|
293 |
void tst_QTextBoundaryFinder::isAtBoundaryLine()
|
|
294 |
{
|
|
295 |
// idx 0 1 2 3 4 5
|
|
296 |
// break? - - - + - +
|
|
297 |
QChar s[] = { 0x0061, 0x00AD, 0x0062, 0x0009, 0x0063, 0x0064 };
|
|
298 |
QString text(s, sizeof(s)/sizeof(s[0]));
|
|
299 |
qDebug() << "text = " << text << ", length = " << text.length();
|
|
300 |
QTextBoundaryFinder finder(QTextBoundaryFinder::Line, text.constData(), text.length(), /*buffer*/0, /*buffer size*/0);
|
|
301 |
finder.setPosition(0);
|
|
302 |
QVERIFY(!finder.isAtBoundary());
|
|
303 |
finder.setPosition(1);
|
|
304 |
QVERIFY(!finder.isAtBoundary());
|
|
305 |
finder.setPosition(2);
|
|
306 |
QVERIFY(!finder.isAtBoundary());
|
|
307 |
finder.setPosition(3);
|
|
308 |
QVERIFY(finder.isAtBoundary());
|
|
309 |
finder.setPosition(4);
|
|
310 |
QVERIFY(!finder.isAtBoundary());
|
|
311 |
finder.setPosition(5);
|
|
312 |
QVERIFY(finder.isAtBoundary());
|
|
313 |
}
|
|
314 |
|
|
315 |
QTEST_MAIN(tst_QTextBoundaryFinder)
|
|
316 |
#include "tst_qtextboundaryfinder.moc"
|