Debian Codegraph @

qt5/tests/check_strings.cpp

raw
1 /*
2 * Copyright (C) 2010, 2011, Pino Toscano <[email protected]>
3 * Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, <[email protected]>
4 * Copyright (C) 2024 Oliver Sander <[email protected]>
5 * Copyright (C) 2025 g10 Code GmbH, Author: Sune Stolborg Vuorela <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22 #include <QtTest/QTest>
23
24 #include <poppler-qt5.h>
25 #include <poppler-private.h>
26
27 #include <GlobalParams.h>
28 #include <UTF.h>
29
30 Q_DECLARE_METATYPE(GooString *)
31 Q_DECLARE_METATYPE(Unicode *)
32
33 class TestStrings : public QObject
34 {
35 Q_OBJECT
36
37 public:
38 explicit TestStrings(QObject *parent = nullptr) : QObject(parent) { }
39
40 GooString *newGooString(const char *s);
41 GooString *newGooString(const char *s, int l);
42
43 QVector<GooString *> m_gooStrings;
44
45 private Q_SLOTS:
46 void initTestCase();
47 void cleanupTestCase();
48 void check_unicodeToQString_data();
49 void check_unicodeToQString();
50 void check_UnicodeParsedString_data();
51 void check_UnicodeParsedString();
52 void check_QStringToUnicodeGooString_data();
53 void check_QStringToUnicodeGooString();
54 void check_QStringToGooString_data();
55 void check_QStringToGooString();
56 };
57
58 void TestStrings::initTestCase()
59 {
60 qRegisterMetaType<GooString *>("GooString*");
61 qRegisterMetaType<Unicode *>("Unicode*");
62
63 globalParams = std::make_unique<GlobalParams>();
64 }
65
66 void TestStrings::cleanupTestCase()
67 {
68 qDeleteAll(m_gooStrings);
69
70 globalParams.reset();
71 }
72
73 void TestStrings::check_unicodeToQString_data()
74 {
75 QTest::addColumn<Unicode *>("data");
76 QTest::addColumn<int>("length");
77 QTest::addColumn<QString>("result");
78
79 {
80 const int l = 1;
81 Unicode *u = new Unicode[l];
82 u[0] = int('a');
83 QTest::newRow("a") << u << l << QStringLiteral("a");
84 }
85 {
86 const int l = 1;
87 Unicode *u = new Unicode[l];
88 u[0] = 0x0161;
89 QTest::newRow("\u0161") << u << l << QStringLiteral("\u0161");
90 }
91 {
92 const int l = 2;
93 Unicode *u = new Unicode[l];
94 u[0] = int('a');
95 u[1] = int('b');
96 QTest::newRow("ab") << u << l << QStringLiteral("ab");
97 }
98 {
99 const int l = 2;
100 Unicode *u = new Unicode[l];
101 u[0] = int('a');
102 u[1] = 0x0161;
103 QTest::newRow("a\u0161") << u << l << QStringLiteral("a\u0161");
104 }
105 {
106 const int l = 2;
107 Unicode *u = new Unicode[l];
108 u[0] = 0x5c01;
109 u[1] = 0x9762;
110 QTest::newRow("\xe5\xb0\x81\xe9\x9d\xa2") << u << l << QStringLiteral("封面");
111 }
112 {
113 const int l = 3;
114 Unicode *u = new Unicode[l];
115 u[0] = 0x5c01;
116 u[1] = 0x9762;
117 u[2] = 0x0;
118 QTest::newRow("\xe5\xb0\x81\xe9\x9d\xa2 + 0") << u << l << QStringLiteral("封面");
119 }
120 {
121 const int l = 4;
122 Unicode *u = new Unicode[l];
123 u[0] = 0x5c01;
124 u[1] = 0x9762;
125 u[2] = 0x0;
126 u[3] = 0x0;
127 QTest::newRow("\xe5\xb0\x81\xe9\x9d\xa2 + two 0") << u << l << QStringLiteral("封面");
128 }
129 }
130
131 void TestStrings::check_unicodeToQString()
132 {
133 QFETCH(Unicode *, data);
134 QFETCH(int, length);
135 QFETCH(QString, result);
136
137 QCOMPARE(Poppler::unicodeToQString(data, length), result);
138
139 delete[] data;
140 }
141
142 void TestStrings::check_UnicodeParsedString_data()
143 {
144 QTest::addColumn<GooString *>("string");
145 QTest::addColumn<QString>("result");
146
147 // non-unicode strings
148 QTest::newRow("<empty>") << newGooString("") << QString();
149 QTest::newRow("a") << newGooString("a") << QStringLiteral("a");
150 QTest::newRow("ab") << newGooString("ab") << QStringLiteral("ab");
151 QTest::newRow("~") << newGooString("~") << QStringLiteral("~");
152 QTest::newRow("test string") << newGooString("test string") << QStringLiteral("test string");
153
154 // unicode strings
155 QTest::newRow("<unicode marks>") << newGooString("\xFE\xFF") << QString();
156 QTest::newRow("U a") << newGooString("\xFE\xFF\0a", 4) << QStringLiteral("a");
157 QTest::newRow("U ~") << newGooString("\xFE\xFF\0~", 4) << QStringLiteral("~");
158 QTest::newRow("U aa") << newGooString("\xFE\xFF\0a\0a", 6) << QStringLiteral("aa");
159 QTest::newRow("U \xC3\x9F") << newGooString("\xFE\xFF\0\xDF", 4) << QStringLiteral("ß");
160 QTest::newRow("U \xC3\x9F\x61") << newGooString("\xFE\xFF\0\xDF\0\x61", 6) << QStringLiteral("ßa");
161 QTest::newRow("U \xC5\xA1") << newGooString("\xFE\xFF\x01\x61", 4) << QStringLiteral("š");
162 QTest::newRow("U \xC5\xA1\x61") << newGooString("\xFE\xFF\x01\x61\0\x61", 6) << QStringLiteral("ša");
163 QTest::newRow("test string") << newGooString("\xFE\xFF\0t\0e\0s\0t\0 \0s\0t\0r\0i\0n\0g", 24) << QStringLiteral("test string");
164 QTest::newRow("UTF16-LE") << newGooString("\xFF\xFE\xDA\x00\x6E\x00\xEE\x00\x63\x00\xF6\x00\x64\x00\xE9\x00\x51\x75", 18) << QStringLiteral("Únîcödé畑");
165 }
166
167 void TestStrings::check_UnicodeParsedString()
168 {
169 QFETCH(GooString *, string);
170 QFETCH(QString, result);
171
172 QCOMPARE(Poppler::UnicodeParsedString(string), result);
173 }
174
175 void TestStrings::check_QStringToUnicodeGooString_data()
176 {
177 QTest::addColumn<QString>("string");
178 QTest::addColumn<QByteArray>("result");
179
180 QTest::newRow("<null>") << QString() << QByteArray("");
181 QTest::newRow("<empty>") << QString(QLatin1String("")) << QByteArray("");
182 QTest::newRow("a") << QStringLiteral("a") << QByteArray("\0a", 2);
183 QTest::newRow("ab") << QStringLiteral("ab") << QByteArray("\0a\0b", 4);
184 QTest::newRow("test string") << QStringLiteral("test string") << QByteArray("\0t\0e\0s\0t\0 \0s\0t\0r\0i\0n\0g", 22);
185 QTest::newRow("\xC3\x9F") << QStringLiteral("ß") << QByteArray("\0\xDF", 2);
186 QTest::newRow("\xC3\x9F\x61") << QStringLiteral("ßa") << QByteArray("\0\xDF\0\x61", 4);
187 }
188
189 void TestStrings::check_QStringToUnicodeGooString()
190 {
191 QFETCH(QString, string);
192 QFETCH(QByteArray, result);
193
194 std::unique_ptr<GooString> goo = Poppler::QStringToUnicodeGooString(string);
195 if (string.isEmpty()) {
196 QVERIFY(goo->toStr().empty());
197 QCOMPARE(goo->size(), 0);
198 } else {
199 QVERIFY(hasUnicodeByteOrderMark(goo->toStr()));
200 QCOMPARE(goo->size(), string.length() * 2 + 2);
201 QCOMPARE(result, QByteArray::fromRawData(goo->c_str() + 2, goo->size() - 2));
202 }
203 }
204
205 void TestStrings::check_QStringToGooString_data()
206 {
207 QTest::addColumn<QString>("string");
208 QTest::addColumn<GooString *>("result");
209
210 QTest::newRow("<null>") << QString() << newGooString("");
211 QTest::newRow("<empty>") << QString(QLatin1String("")) << newGooString("");
212 QTest::newRow("a") << QStringLiteral("a") << newGooString("a");
213 QTest::newRow("ab") << QStringLiteral("ab") << newGooString("ab");
214 }
215
216 void TestStrings::check_QStringToGooString()
217 {
218 QFETCH(QString, string);
219 QFETCH(GooString *, result);
220
221 const std::unique_ptr<GooString> goo = Poppler::QStringToGooString(string);
222 QCOMPARE(goo->c_str(), result->c_str());
223 }
224
225 GooString *TestStrings::newGooString(const char *s)
226 {
227 GooString *goo = new GooString(s);
228 m_gooStrings.append(goo);
229 return goo;
230 }
231
232 GooString *TestStrings::newGooString(const char *s, int l)
233 {
234 GooString *goo = new GooString(s, l);
235 m_gooStrings.append(goo);
236 return goo;
237 }
238
239 QTEST_GUILESS_MAIN(TestStrings)
240
241 #include "check_strings.moc"