File size: 7,854 Bytes
5cee033
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * Copyright (C) 2010, 2011, Pino Toscano <[email protected]>
 * Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, <[email protected]>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <QtTest/QTest>

#include <poppler-qt6.h>
#include <poppler-private.h>

#include <GlobalParams.h>
#include "UTF.h"

Q_DECLARE_METATYPE(GooString *)
Q_DECLARE_METATYPE(Unicode *)

class TestStrings : public QObject
{
    Q_OBJECT

public:
    explicit TestStrings(QObject *parent = nullptr) : QObject(parent) { }

private slots:
    void initTestCase();
    void cleanupTestCase();
    void check_unicodeToQString_data();
    void check_unicodeToQString();
    void check_UnicodeParsedString_data();
    void check_UnicodeParsedString();
    void check_QStringToUnicodeGooString_data();
    void check_QStringToUnicodeGooString();
    void check_QStringToGooString_data();
    void check_QStringToGooString();

private:
    GooString *newGooString(const char *s);
    GooString *newGooString(const char *s, int l);

    QVector<GooString *> m_gooStrings;
};

void TestStrings::initTestCase()
{
    qRegisterMetaType<GooString *>("GooString*");
    qRegisterMetaType<Unicode *>("Unicode*");

    globalParams = std::make_unique<GlobalParams>();
}

void TestStrings::cleanupTestCase()
{
    qDeleteAll(m_gooStrings);

    globalParams.reset();
}

void TestStrings::check_unicodeToQString_data()
{
    QTest::addColumn<Unicode *>("data");
    QTest::addColumn<int>("length");
    QTest::addColumn<QString>("result");

    {
        const int l = 1;
        Unicode *u = new Unicode[l];
        u[0] = int('a');
        QTest::newRow("a") << u << l << QStringLiteral("a");
    }
    {
        const int l = 1;
        Unicode *u = new Unicode[l];
        u[0] = 0x0161;
        QTest::newRow("\u0161") << u << l << QStringLiteral("\u0161");
    }
    {
        const int l = 2;
        Unicode *u = new Unicode[l];
        u[0] = int('a');
        u[1] = int('b');
        QTest::newRow("ab") << u << l << QStringLiteral("ab");
    }
    {
        const int l = 2;
        Unicode *u = new Unicode[l];
        u[0] = int('a');
        u[1] = 0x0161;
        QTest::newRow("a\u0161") << u << l << QStringLiteral("a\u0161");
    }
    {
        const int l = 2;
        Unicode *u = new Unicode[l];
        u[0] = 0x5c01;
        u[1] = 0x9762;
        QTest::newRow("\xe5\xb0\x81\xe9\x9d\xa2") << u << l << QStringLiteral("封面");
    }
    {
        const int l = 3;
        Unicode *u = new Unicode[l];
        u[0] = 0x5c01;
        u[1] = 0x9762;
        u[2] = 0x0;
        QTest::newRow("\xe5\xb0\x81\xe9\x9d\xa2 + 0") << u << l << QStringLiteral("封面");
    }
    {
        const int l = 4;
        Unicode *u = new Unicode[l];
        u[0] = 0x5c01;
        u[1] = 0x9762;
        u[2] = 0x0;
        u[3] = 0x0;
        QTest::newRow("\xe5\xb0\x81\xe9\x9d\xa2 + two 0") << u << l << QStringLiteral("封面");
    }
}

void TestStrings::check_unicodeToQString()
{
    QFETCH(Unicode *, data);
    QFETCH(int, length);
    QFETCH(QString, result);

    QCOMPARE(Poppler::unicodeToQString(data, length), result);

    delete[] data;
}

void TestStrings::check_UnicodeParsedString_data()
{
    QTest::addColumn<GooString *>("string");
    QTest::addColumn<QString>("result");

    // non-unicode strings
    QTest::newRow("<empty>") << newGooString("") << QString();
    QTest::newRow("a") << newGooString("a") << QStringLiteral("a");
    QTest::newRow("ab") << newGooString("ab") << QStringLiteral("ab");
    QTest::newRow("~") << newGooString("~") << QStringLiteral("~");
    QTest::newRow("test string") << newGooString("test string") << QStringLiteral("test string");

    // unicode strings
    QTest::newRow("<unicode marks>") << newGooString("\xFE\xFF") << QString();
    QTest::newRow("U a") << newGooString("\xFE\xFF\0a", 4) << QStringLiteral("a");
    QTest::newRow("U ~") << newGooString("\xFE\xFF\0~", 4) << QStringLiteral("~");
    QTest::newRow("U aa") << newGooString("\xFE\xFF\0a\0a", 6) << QStringLiteral("aa");
    QTest::newRow("U \xC3\x9F") << newGooString("\xFE\xFF\0\xDF", 4) << QStringLiteral("ß");
    QTest::newRow("U \xC3\x9F\x61") << newGooString("\xFE\xFF\0\xDF\0\x61", 6) << QStringLiteral("ßa");
    QTest::newRow("U \xC5\xA1") << newGooString("\xFE\xFF\x01\x61", 4) << QStringLiteral("š");
    QTest::newRow("U \xC5\xA1\x61") << newGooString("\xFE\xFF\x01\x61\0\x61", 6) << QStringLiteral("ša");
    QTest::newRow("test string") << newGooString("\xFE\xFF\0t\0e\0s\0t\0 \0s\0t\0r\0i\0n\0g", 24) << QStringLiteral("test string");
    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é畑");
}

void TestStrings::check_UnicodeParsedString()
{
    QFETCH(GooString *, string);
    QFETCH(QString, result);

    QCOMPARE(Poppler::UnicodeParsedString(string), result);
}

void TestStrings::check_QStringToUnicodeGooString_data()
{
    QTest::addColumn<QString>("string");
    QTest::addColumn<QByteArray>("result");

    QTest::newRow("<null>") << QString() << QByteArray("");
    QTest::newRow("<empty>") << QString(QLatin1String("")) << QByteArray("");
    QTest::newRow("a") << QStringLiteral("a") << QByteArray("\0a", 2);
    QTest::newRow("ab") << QStringLiteral("ab") << QByteArray("\0a\0b", 4);
    QTest::newRow("test string") << QStringLiteral("test string") << QByteArray("\0t\0e\0s\0t\0 \0s\0t\0r\0i\0n\0g", 22);
    QTest::newRow("\xC3\x9F") << QStringLiteral("ß") << QByteArray("\0\xDF", 2);
    QTest::newRow("\xC3\x9F\x61") << QStringLiteral("ßa") << QByteArray("\0\xDF\0\x61", 4);
}

void TestStrings::check_QStringToUnicodeGooString()
{
    QFETCH(QString, string);
    QFETCH(QByteArray, result);

    GooString *goo = Poppler::QStringToUnicodeGooString(string);
    if (string.isEmpty()) {
        QVERIFY(goo->toStr().empty());
        QCOMPARE(goo->getLength(), 0);
    } else {
        QVERIFY(hasUnicodeByteOrderMark(goo->toStr()));
        QCOMPARE(goo->getLength(), string.length() * 2 + 2);
        QCOMPARE(result, QByteArray::fromRawData(goo->c_str() + 2, goo->getLength() - 2));
    }

    delete goo;
}

void TestStrings::check_QStringToGooString_data()
{
    QTest::addColumn<QString>("string");
    QTest::addColumn<GooString *>("result");

    QTest::newRow("<null>") << QString() << newGooString("");
    QTest::newRow("<empty>") << QString(QLatin1String("")) << newGooString("");
    QTest::newRow("a") << QStringLiteral("a") << newGooString("a");
    QTest::newRow("ab") << QStringLiteral("ab") << newGooString("ab");
}

void TestStrings::check_QStringToGooString()
{
    QFETCH(QString, string);
    QFETCH(GooString *, result);

    GooString *goo = Poppler::QStringToGooString(string);
    QCOMPARE(goo->c_str(), result->c_str());

    delete goo;
}

GooString *TestStrings::newGooString(const char *s)
{
    GooString *goo = new GooString(s);
    m_gooStrings.append(goo);
    return goo;
}

GooString *TestStrings::newGooString(const char *s, int l)
{
    GooString *goo = new GooString(s, l);
    m_gooStrings.append(goo);
    return goo;
}

QTEST_GUILESS_MAIN(TestStrings)

#include "check_strings.moc"