1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <QTextCodec>
42 #include <QFile>
43 #include <qtest.h>
44 
45 #ifdef Q_OS_SYMBIAN
46 // In Symbian OS test data is located in applications private dir
47 // Application private dir is default serach path for files, so SRCDIR can be set to empty
48 #define SRCDIR ""
49 #endif
50 
51 Q_DECLARE_METATYPE(QList<QByteArray>)
52 Q_DECLARE_METATYPE(QTextCodec *)
53 
54 class tst_QTextCodec: public QObject
55 {
56     Q_OBJECT
57 private slots:
58     void codecForName() const;
59     void codecForName_data() const;
60     void codecForMib() const;
61     void fromUnicode_data() const;
62     void fromUnicode() const;
63     void toUnicode_data() const;
64     void toUnicode() const;
65 };
66 
codecForName() const67 void tst_QTextCodec::codecForName() const
68 {
69     QFETCH(QList<QByteArray>, codecs);
70 
71     QBENCHMARK {
72         foreach(const QByteArray& c, codecs) {
73             QVERIFY(QTextCodec::codecForName(c));
74             QVERIFY(QTextCodec::codecForName(c + "-"));
75         }
76         foreach(const QByteArray& c, codecs) {
77             QVERIFY(QTextCodec::codecForName(c + "+"));
78             QVERIFY(QTextCodec::codecForName(c + "*"));
79         }
80     }
81 }
82 
codecForName_data() const83 void tst_QTextCodec::codecForName_data() const
84 {
85     QTest::addColumn<QList<QByteArray> >("codecs");
86 
87     QTest::newRow("all") << QTextCodec::availableCodecs();
88     QTest::newRow("many utf-8") << (QList<QByteArray>()
89             << "utf-8" << "utf-8" << "utf-8" << "utf-8" << "utf-8"
90             << "utf-8" << "utf-8" << "utf-8" << "utf-8" << "utf-8"
91             << "utf-8" << "utf-8" << "utf-8" << "utf-8" << "utf-8"
92             << "utf-8" << "utf-8" << "utf-8" << "utf-8" << "utf-8"
93             << "utf-8" << "utf-8" << "utf-8" << "utf-8" << "utf-8"
94             << "utf-8" << "utf-8" << "utf-8" << "utf-8" << "utf-8"
95             << "utf-8" << "utf-8" << "utf-8" << "utf-8" << "utf-8"
96             << "utf-8" << "utf-8" << "utf-8" << "utf-8" << "utf-8"
97             << "utf-8" << "utf-8" << "utf-8" << "utf-8" << "utf-8"   );
98 }
99 
codecForMib() const100 void tst_QTextCodec::codecForMib() const
101 {
102     QBENCHMARK {
103         QTextCodec::codecForMib(106);
104         QTextCodec::codecForMib(111);
105         QTextCodec::codecForMib(106);
106         QTextCodec::codecForMib(2254);
107         QTextCodec::codecForMib(2255);
108         QTextCodec::codecForMib(2256);
109         QTextCodec::codecForMib(2257);
110         QTextCodec::codecForMib(2258);
111         QTextCodec::codecForMib(111);
112         QTextCodec::codecForMib(2250);
113         QTextCodec::codecForMib(2251);
114         QTextCodec::codecForMib(2252);
115         QTextCodec::codecForMib(106);
116         QTextCodec::codecForMib(106);
117         QTextCodec::codecForMib(106);
118         QTextCodec::codecForMib(106);
119     }
120 }
121 
fromUnicode_data() const122 void tst_QTextCodec::fromUnicode_data() const
123 {
124     QTest::addColumn<QTextCodec*>("codec");
125 
126     QTest::newRow("utf-8") << QTextCodec::codecForName("utf-8");
127     QTest::newRow("latin 1") << QTextCodec::codecForName("latin 1");
128     QTest::newRow("utf-16") << QTextCodec::codecForName("utf16"); ;
129     QTest::newRow("utf-32") << QTextCodec::codecForName("utf32");
130     QTest::newRow("latin15") << QTextCodec::codecForName("iso-8859-15");
131     QTest::newRow("eucKr") << QTextCodec::codecForName("eucKr");
132 }
133 
134 
fromUnicode() const135 void tst_QTextCodec::fromUnicode() const
136 {
137     QFETCH(QTextCodec*, codec);
138     QFile file(SRCDIR "utf-8.txt");
139     if (!file.open(QFile::ReadOnly)) {
140         qFatal("Cannot open input file");
141         return;
142     }
143     QByteArray data = file.readAll();
144     const char *d = data.constData();
145     int size = data.size();
146     QString s = QString::fromUtf8(d, size);
147     s = s + s + s;
148     s = s + s + s;
149     QBENCHMARK {
150         for (int i = 0; i < 10; i ++)
151             codec->fromUnicode(s);
152     }
153 }
154 
155 
toUnicode_data() const156 void tst_QTextCodec::toUnicode_data() const
157 {
158     fromUnicode_data();
159 }
160 
161 
toUnicode() const162 void tst_QTextCodec::toUnicode() const
163 {
164     QFETCH(QTextCodec*, codec);
165     QFile file(SRCDIR "utf-8.txt");
166     QVERIFY(file.open(QFile::ReadOnly));
167     QByteArray data = file.readAll();
168     const char *d = data.constData();
169     int size = data.size();
170     QString s = QString::fromUtf8(d, size);
171     s = s + s + s;
172     s = s + s + s;
173     QByteArray orig = codec->fromUnicode(s);
174     QBENCHMARK {
175         for (int i = 0; i < 10; i ++)
176             codec->toUnicode(orig);
177     }
178 }
179 
180 
181 
182 
183 QTEST_MAIN(tst_QTextCodec)
184 
185 #include "main.moc"
186