Debian Codegraph @

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