File indexing completed on 2024-05-05 17:58:04
0001 /* 0002 This file is part of the Okteta Core library, made within the KDE community. 0003 0004 SPDX-FileCopyrightText: 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 "valuecodectest.hpp" 0010 0011 // test object 0012 #include <valuecodec.hpp> 0013 // Qt 0014 #include <QTest> 0015 #include <QBitArray> 0016 0017 namespace Okteta { 0018 0019 //---------------------------------------------------------------------------- Tests ----- 0020 0021 // keep in order with ValueCoding 0022 struct ValueCodecDescription 0023 { 0024 const char* name; 0025 int id; 0026 uint encodingWidth; 0027 const char* validDigits; 0028 }; 0029 0030 static constexpr ValueCodecDescription valueCodecDescriptions[] = 0031 { 0032 {"HexadecimalByteCodec", HexadecimalCoding, 2, "0123456789ABCDEFabcdef"}, 0033 {"DecimalByteCodec", DecimalCoding, 3, "0123456789"}, 0034 {"OctalByteCodec", OctalCoding, 3, "01234567"}, 0035 {"BinaryByteCodec", BinaryCoding, 8, "01"} 0036 }; 0037 void ValueCodecTest::testCreateCodec_data() 0038 { 0039 QTest::addColumn<int>("codecId"); 0040 QTest::addColumn<uint>("encodingWidth"); 0041 0042 for (auto& valueCodecDescription : valueCodecDescriptions) { 0043 QTest::newRow(valueCodecDescription.name) 0044 << valueCodecDescription.id 0045 << valueCodecDescription.encodingWidth; 0046 } 0047 } 0048 0049 void ValueCodecTest::testCreateCodec() 0050 { 0051 QFETCH(int, codecId); 0052 QFETCH(uint, encodingWidth); 0053 0054 ValueCodec* codec = ValueCodec::createCodec((ValueCoding)codecId); 0055 0056 QVERIFY(codec != nullptr); 0057 QCOMPARE(codec->encodingWidth(), encodingWidth); 0058 0059 delete codec; 0060 } 0061 0062 void ValueCodecTest::testEncodeDecode_data() 0063 { 0064 QTest::addColumn<int>("codecId"); 0065 QTest::addColumn<Byte>("byte"); 0066 0067 for (auto& valueCodecDescription : valueCodecDescriptions) { 0068 for (int j = 0; j < 256; ++j) { 0069 const QString rowTitle = 0070 QLatin1String(valueCodecDescription.name) + 0071 QStringLiteral(" - %1").arg(j); 0072 0073 QTest::newRow(rowTitle.toLatin1().constData()) 0074 << valueCodecDescription.id 0075 << Byte(j); 0076 } 0077 } 0078 } 0079 0080 void ValueCodecTest::testEncodeDecode() 0081 { 0082 QFETCH(int, codecId); 0083 QFETCH(Byte, byte); 0084 0085 ValueCodec* codec = ValueCodec::createCodec((ValueCoding)codecId); 0086 0087 QString digits; 0088 codec->encode(&digits, 0, byte); 0089 QCOMPARE(digits.length(), (int)codec->encodingWidth()); 0090 0091 Byte decodedByte; 0092 const int usedDigits = codec->decode(&decodedByte, digits, 0); 0093 QCOMPARE(usedDigits, digits.length()); 0094 QCOMPARE(decodedByte, byte); 0095 0096 delete codec; 0097 } 0098 0099 void ValueCodecTest::testEncodeShortDecode_data() 0100 { 0101 QTest::addColumn<int>("codecId"); 0102 QTest::addColumn<Byte>("byte"); 0103 0104 for (auto& valueCodecDescription : valueCodecDescriptions) { 0105 for (int j = 0; j < 256; ++j) { 0106 const QString rowTitle = 0107 QLatin1String(valueCodecDescription.name) + 0108 QStringLiteral(" - %1").arg(j); 0109 0110 QTest::newRow(rowTitle.toLatin1().constData()) 0111 << valueCodecDescription.id 0112 << Byte(j); 0113 } 0114 } 0115 } 0116 0117 void ValueCodecTest::testEncodeShortDecode() 0118 { 0119 QFETCH(int, codecId); 0120 QFETCH(Byte, byte); 0121 0122 ValueCodec* codec = ValueCodec::createCodec((ValueCoding)codecId); 0123 0124 QString digits; 0125 codec->encodeShort(&digits, 0, byte); 0126 QVERIFY(!digits.isEmpty()); 0127 QVERIFY(digits.length() <= (int)codec->encodingWidth()); 0128 0129 Byte decodedByte; 0130 const int usedDigits = codec->decode(&decodedByte, digits, 0); 0131 QCOMPARE(usedDigits, digits.length()); 0132 QCOMPARE(decodedByte, byte); 0133 0134 delete codec; 0135 } 0136 0137 void ValueCodecTest::testAppendDigit_data() 0138 { 0139 QTest::addColumn<int>("codecId"); 0140 QTest::addColumn<Byte>("byte"); 0141 0142 for (auto& valueCodecDescription : valueCodecDescriptions) { 0143 for (int j = 0; j < 256; ++j) { 0144 const QString rowTitle = 0145 QLatin1String(valueCodecDescription.name) + 0146 QStringLiteral(" - %1").arg(j); 0147 0148 QTest::newRow(rowTitle.toLatin1().constData()) 0149 << valueCodecDescription.id 0150 << Byte(j); 0151 } 0152 } 0153 } 0154 0155 void ValueCodecTest::testAppendDigit() 0156 { 0157 QFETCH(int, codecId); 0158 QFETCH(Byte, byte); 0159 0160 ValueCodec* codec = ValueCodec::createCodec((ValueCoding)codecId); 0161 0162 QString digits; 0163 codec->encode(&digits, 0, byte); 0164 0165 Byte decodedByte = 0; 0166 for (auto d : qAsConst(digits)) { 0167 codec->appendDigit(&decodedByte, d.toLatin1()); 0168 } 0169 0170 QCOMPARE(decodedByte, byte); 0171 0172 delete codec; 0173 } 0174 0175 void ValueCodecTest::testRemoveLastDigit_data() 0176 { 0177 QTest::addColumn<int>("codecId"); 0178 QTest::addColumn<Byte>("byte"); 0179 QTest::addColumn<uint>("removedDigitCount"); 0180 0181 for (auto& valueCodecDescription : valueCodecDescriptions) { 0182 for (int b = 0; b < 256; ++b) { 0183 for (uint r = 1; r <= valueCodecDescription.encodingWidth; ++r) { 0184 const QString rowTitle = 0185 QLatin1String(valueCodecDescription.name) + 0186 QStringLiteral(" - %1 - removed last %2").arg(b).arg(r); 0187 0188 QTest::newRow(rowTitle.toLatin1().constData()) 0189 << valueCodecDescription.id 0190 << Byte(b) 0191 << r; 0192 } 0193 } 0194 } 0195 } 0196 0197 void ValueCodecTest::testRemoveLastDigit() 0198 { 0199 QFETCH(int, codecId); 0200 QFETCH(Byte, byte); 0201 QFETCH(uint, removedDigitCount); 0202 0203 ValueCodec* codec = ValueCodec::createCodec((ValueCoding)codecId); 0204 0205 QString digits; 0206 codec->encode(&digits, 0, byte); 0207 0208 Byte modifiedByte = byte; 0209 for (uint i = 0; i < removedDigitCount; ++i) { 0210 codec->removeLastDigit(&modifiedByte); 0211 } 0212 0213 QString modifiedDigits; 0214 codec->encode(&modifiedDigits, 0, modifiedByte); 0215 0216 QVERIFY(digits.startsWith(modifiedDigits.mid(removedDigitCount))); 0217 QVERIFY(modifiedDigits.startsWith(QString(removedDigitCount, QLatin1Char('0')))); 0218 0219 delete codec; 0220 } 0221 0222 void ValueCodecTest::testIsValidDigit_data() 0223 { 0224 QTest::addColumn<int>("codecId"); 0225 QTest::addColumn<uchar>("digit"); 0226 QTest::addColumn<bool>("isValid"); 0227 0228 constexpr int digitCount = 256; 0229 0230 for (auto& valueCodecDescription : valueCodecDescriptions) { 0231 QBitArray validnessPerDigitField = QBitArray(digitCount, false); 0232 const QByteArray validDigits = 0233 QByteArray(valueCodecDescription.validDigits); 0234 0235 for (int j = 0; j < validDigits.count(); ++j) { 0236 validnessPerDigitField.setBit(validDigits[j], true); 0237 } 0238 0239 for (int j = 0; j < validnessPerDigitField.count(); ++j) { 0240 const uchar digit = uchar(j); 0241 const bool isValid = validnessPerDigitField.testBit(j); 0242 const QString rowTitle = 0243 QLatin1String(valueCodecDescription.name) + 0244 QStringLiteral(" - \"%1\" is ").arg(QLatin1Char(digit)) + 0245 (isValid ? QStringLiteral("valid") : QStringLiteral("invalid")); 0246 0247 QTest::newRow(rowTitle.toLatin1().constData()) 0248 << valueCodecDescription.id 0249 << digit 0250 << isValid; 0251 } 0252 } 0253 } 0254 0255 void ValueCodecTest::testIsValidDigit() 0256 { 0257 QFETCH(int, codecId); 0258 QFETCH(uchar, digit); 0259 QFETCH(bool, isValid); 0260 0261 ValueCodec* codec = ValueCodec::createCodec((ValueCoding)codecId); 0262 0263 QCOMPARE(codec->isValidDigit(digit), isValid); 0264 0265 delete codec; 0266 } 0267 0268 } 0269 0270 QTEST_GUILESS_MAIN(Okteta::ValueCodecTest) 0271 0272 #include "moc_valuecodectest.cpp"