File indexing completed on 2024-04-28 05:52:32

0001 /*
0002     This file is part of the Okteta Core library, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2006, 2011 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "charcodectest.hpp"
0010 
0011 // test object
0012 #include <charcodec.hpp>
0013 // lib
0014 #include <character.hpp>
0015 // KF
0016 #include <QTest>
0017 
0018 namespace Okteta {
0019 
0020 //---------------------------------------------------------------------------- Tests -----
0021 
0022 void CharCodecTest::testCreateCodec_data()
0023 {
0024     QTest::addColumn<QString>("codecName");
0025 
0026     for (const QString& codecName : CharCodec::codecNames()) {
0027         QTest::newRow(codecName.toLatin1().constData()) << codecName;
0028     }
0029 }
0030 
0031 void CharCodecTest::testCreateCodec()
0032 {
0033     QFETCH(QString, codecName);
0034 
0035     CharCodec* codec = CharCodec::createCodec(codecName);
0036 
0037     QVERIFY(codec != nullptr);
0038     QCOMPARE(codec->name(), codecName);
0039 
0040     delete codec;
0041 }
0042 
0043 void CharCodecTest::testEncodeDecode_data()
0044 {
0045     QTest::addColumn<QString>("codecName");
0046     QTest::addColumn<int>("byteValue");
0047 
0048     for (const QString& codecName : CharCodec::codecNames()) {
0049         for (int i = 0; i < 256; ++i) {
0050             const QString rowTitle = codecName + QStringLiteral(" - %1").arg(i);
0051             QTest::newRow(rowTitle.toLatin1().constData()) << codecName << i;
0052         }
0053     }
0054 }
0055 
0056 void CharCodecTest::testEncodeDecode()
0057 {
0058     QFETCH(QString, codecName);
0059     QFETCH(int, byteValue);
0060 
0061     CharCodec* codec = CharCodec::createCodec(codecName);
0062 
0063     // current assumption: the mapping of chars to byte values is biunique for all used charsets
0064     const Byte byte = Byte(byteValue);
0065     Character character = codec->decode(byte);
0066     if (!character.isUndefined()) {
0067         QVERIFY(codec->canEncode(character));
0068 
0069         Byte encodedByte;
0070         const bool encodeSuccess = codec->encode(&encodedByte, character);
0071         QVERIFY(encodeSuccess);
0072         QCOMPARE(encodedByte, byte);
0073     }
0074 
0075     delete codec;
0076 }
0077 
0078 }
0079 
0080 QTEST_GUILESS_MAIN(Okteta::CharCodecTest)
0081 
0082 #include "moc_charcodectest.cpp"