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