File indexing completed on 2024-09-15 09:19:38
0001 /* 0002 SPDX-FileCopyrightText: 2012 Ni Hui <shuizhuyuanluo@126.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "kencodingprobertest.h" 0008 0009 #include <QDir> 0010 #include <QTest> 0011 #include <kencodingprober.h> 0012 0013 static KEncodingProber *ep = nullptr; 0014 0015 void KEncodingProberTest::initTestCase() 0016 { 0017 ep = new KEncodingProber; 0018 } 0019 0020 void KEncodingProberTest::cleanupTestCase() 0021 { 0022 delete ep; 0023 ep = nullptr; 0024 } 0025 0026 void KEncodingProberTest::cleanup() 0027 { 0028 ep->reset(); 0029 } 0030 0031 void KEncodingProberTest::testReset() 0032 { 0033 ep->feed(QByteArray("some random data @*@#&jd")); 0034 ep->reset(); 0035 QCOMPARE(ep->state(), KEncodingProber::Probing); 0036 QCOMPARE(ep->encoding().toLower(), QByteArray("utf-8")); 0037 } 0038 0039 void KEncodingProberTest::testProbe() 0040 { 0041 // utf-8 0042 ep->setProberType(KEncodingProber::Universal); 0043 ep->feed(QByteArray::fromHex("e998bfe5b094e58d91e696afe5b1b1e88489")); 0044 QCOMPARE(ep->encoding().toLower(), QByteArray("utf-8")); 0045 ep->reset(); 0046 0047 // gb18030 0048 ep->setProberType(KEncodingProber::ChineseSimplified); 0049 ep->feed(QByteArray::fromHex("d7d4d3c9b5c4b0d9bfc6c8abcae9")); 0050 QCOMPARE(ep->encoding().toLower(), QByteArray("gb18030")); 0051 ep->reset(); 0052 0053 // shift_jis 0054 ep->setProberType(KEncodingProber::Japanese); 0055 ep->feed(QByteArray::fromHex("8374838a815b955389c88e969354")); 0056 QCOMPARE(ep->encoding().toLower(), QByteArray("shift_jis")); 0057 ep->reset(); 0058 0059 // big5 0060 ep->setProberType(KEncodingProber::ChineseTraditional); 0061 ep->feed(QByteArray::fromHex("aefcafc7a6caa474a141a6b3ae65a444a46a")); 0062 QCOMPARE(ep->encoding().toLower(), QByteArray("big5")); 0063 ep->reset(); 0064 0065 // binary data, just make sure we do not crash (cf. crash in bug #357341) 0066 const QDir dataDir(QFINDTESTDATA("data")); 0067 const QString binaryFile = dataDir.filePath(QStringLiteral("binary_data")); 0068 QFile file(binaryFile); 0069 QVERIFY(file.open(QIODevice::ReadOnly)); 0070 QByteArray binaryData(file.readAll()); 0071 ep->setProberType(KEncodingProber::Universal); 0072 ep->feed(binaryData); 0073 QCOMPARE(ep->encoding().toLower(), QByteArray("utf-8")); 0074 ep->reset(); 0075 } 0076 0077 QTEST_MAIN(KEncodingProberTest) 0078 0079 #include "moc_kencodingprobertest.cpp"