Warning, file /utilities/okteta/kasten/controllers/test/commonparsertest.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 This file is part of the Okteta Kasten Framework, made within the KDE community. 0003 0004 SPDX-FileCopyrightText: 2012 Alex Richardson <alex.richardson@gmx.de> 0005 SPDX-FileCopyrightText: 2016 Aaron Bishop <erroneous@gmail.com> 0006 0007 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0008 */ 0009 0010 #include <QTest> 0011 #include <QScriptValue> 0012 #include <QScriptEngine> 0013 #include <limits> 0014 0015 #include "view/structures/parsers/parserutils.hpp" 0016 0017 class CommonParserTest : public QObject 0018 { 0019 Q_OBJECT 0020 0021 private Q_SLOTS: 0022 void testIntFromString(); 0023 void testIntFromString_data(); 0024 void testUIntFromString(); 0025 void testUIntFromString_data(); 0026 0027 void testIntFromScriptValue(); 0028 void testIntFromScriptValue_data(); 0029 void testUIntFromScriptValue(); 0030 void testUIntFromScriptValue_data(); 0031 0032 void testToStringEncoding(); 0033 void testToStringEncoding_data(); 0034 }; 0035 0036 void CommonParserTest::testIntFromString_data() 0037 { 0038 QTest::addColumn<QString>("str"); 0039 QTest::addColumn<bool>("okay"); 0040 QTest::addColumn<int>("value"); 0041 0042 QTest::newRow("-1") << "-1" << true << -1; 0043 QTest::newRow("hex with minus") << "-0xff" << true << -0xff; 0044 QTest::newRow("0xff") << "0xff" << true << 0xff; 0045 0046 int maxInt = 0x7fffffff; 0047 QTEST_ASSERT(maxInt == std::numeric_limits<int>::max()); 0048 QTest::newRow("max decimal") << "2147483647" << true << maxInt; 0049 QTest::newRow("max hex") << "0x7fffffff" << true << maxInt; 0050 QTest::newRow("max hex no leading 0x") << "7fffffff" << false; 0051 0052 QTest::newRow("max+1 decimal") << "2147483648" << false; 0053 QTest::newRow("max+1 hex") << "0x80000000" << false; 0054 QTest::newRow("max-1 decimal") << "2147483646" << true << 0x7ffffffe; 0055 QTest::newRow("max-1 hex") << "7ffffffe" << false << 2147483646; 0056 0057 int minInt = -0x80000000; 0058 QTEST_ASSERT(minInt == std::numeric_limits<int>::min()); 0059 QTest::newRow("min decimal") << "-2147483648" << true << minInt; 0060 QTest::newRow("min hex") << "-0x80000000" << true << minInt; 0061 0062 QTest::newRow("min+1 decimal") << "-2147483647" << true << -0x7fffffff; 0063 QTest::newRow("min+1 hex") << "-0x7fffffff" << true << -2147483647; 0064 QTest::newRow("min-1 decimal") << "-2147483649" << false; 0065 QTest::newRow("min-1 hex") << "-0x80000001" << false; 0066 0067 } 0068 0069 void CommonParserTest::testIntFromString() 0070 { 0071 QFETCH(QString, str); 0072 QFETCH(bool, okay); 0073 0074 ParsedNumber<int> result = ParserUtils::intFromString(str); 0075 QCOMPARE(result.string, str); 0076 QCOMPARE(result.isValid, okay); 0077 if (result.isValid) { 0078 QFETCH(int, value); 0079 QCOMPARE(result.value, value); 0080 } 0081 } 0082 0083 void CommonParserTest::testUIntFromString_data() 0084 { 0085 QTest::addColumn<QString>("str"); 0086 QTest::addColumn<bool>("okay"); 0087 QTest::addColumn<uint>("value"); 0088 0089 QTest::newRow("-1") << "-1" << false; 0090 QTest::newRow("0xff") << "0xff" << true << 0xffu; 0091 QTest::newRow("1234") << "1234" << true << 1234u; 0092 0093 uint maxUint = 0xffffffff; 0094 QTEST_ASSERT(maxUint == std::numeric_limits<uint>::max()); 0095 QTest::newRow("max decimal") << "4294967295" << true << maxUint; 0096 QTest::newRow("max hex") << "0xffffffff" << true << maxUint; 0097 QTest::newRow("max hex no leading 0x") << "ffffffff" << false; 0098 0099 QTest::newRow("max+1 decimal") << "4294967296" << false; 0100 QTest::newRow("max+1 hex") << "0x100000000" << false; 0101 0102 } 0103 0104 void CommonParserTest::testUIntFromString() 0105 { 0106 QFETCH(QString, str); 0107 QFETCH(bool, okay); 0108 0109 ParsedNumber<uint> result = ParserUtils::uintFromString(str); 0110 QCOMPARE(result.string, str); 0111 QCOMPARE(result.isValid, okay); 0112 if (result.isValid) { 0113 QFETCH(uint, value); 0114 QCOMPARE(result.value, value); 0115 } 0116 } 0117 0118 void CommonParserTest::testIntFromScriptValue_data() 0119 { 0120 QTest::addColumn<QScriptValue>("scriptValue"); 0121 QTest::addColumn<bool>("okay"); 0122 QTest::addColumn<int>("value"); 0123 0124 auto* engine = new QScriptEngine(this); 0125 QTest::newRow("invalid script value") << QScriptValue() << false; 0126 QTest::newRow("boolean true") << QScriptValue(true) << false; 0127 QTest::newRow("boolean false") << QScriptValue(false) << false; 0128 QTest::newRow("null script value") << engine->nullValue() << false; 0129 QTest::newRow("undefined script value") << engine->undefinedValue() << false; 0130 QTest::newRow("array") << engine->newArray(5) << false; 0131 QTest::newRow("object") << engine->newObject() << false; 0132 QTest::newRow("float") << QScriptValue(1234.5) << false; 0133 0134 QTest::newRow("-1234 number") << QScriptValue(-1234) << true << -1234; 0135 QTest::newRow("-1234 string") << QScriptValue(QStringLiteral("-1234")) << true << -1234; 0136 QTest::newRow("1234 number") << QScriptValue(1234) << true << 1234; 0137 QTest::newRow("1234 string") << QScriptValue(QStringLiteral("1234")) << true << 1234; 0138 QTest::newRow("0xff number") << QScriptValue(0xff) << true << 0xff; 0139 QTest::newRow("0xff string") << QScriptValue(QStringLiteral("0xff")) << true << 0xff; 0140 QTest::newRow("-0xff number") << QScriptValue(-0xff) << true << -0xff; 0141 QTest::newRow("-0xff string") << QScriptValue(QStringLiteral("-0xff")) << true << -0xff; 0142 0143 int maxInt = 0x7fffffff; 0144 QTEST_ASSERT(maxInt == std::numeric_limits<int>::max()); 0145 QTest::newRow("max int") << QScriptValue(maxInt) << true << maxInt; 0146 QTest::newRow("max double") << QScriptValue(double(maxInt)) << true << maxInt; 0147 QTest::newRow("max string") << QScriptValue(QStringLiteral("2147483647")) << true << maxInt; 0148 QTest::newRow("max hex string") << QScriptValue(QStringLiteral("0x7fffffff")) << true << maxInt; 0149 // to int32 wraps around 0150 QTest::newRow("max+1") << QScriptValue(double(maxInt) + 1) << false; 0151 QTest::newRow("max+1 string") << QScriptValue(QStringLiteral("2147483648")) << false; 0152 QTest::newRow("max+1 hex string") << QScriptValue(QStringLiteral("0x80000000")) << false; 0153 QTest::newRow("max+2") << QScriptValue(double(maxInt) + 2) << false; 0154 0155 int minInt = -0x80000000; 0156 QTEST_ASSERT(minInt == std::numeric_limits<int>::min()); 0157 QTest::newRow("min int") << QScriptValue(minInt) << true << minInt; 0158 QTest::newRow("min double") << QScriptValue(double(minInt)) << true << minInt; 0159 QTest::newRow("min string") << QScriptValue(QStringLiteral("-2147483648")) << true << minInt; 0160 QTest::newRow("min hex string") << QScriptValue(QStringLiteral("-0x80000000")) << true << minInt; 0161 QTest::newRow("min-1") << QScriptValue(double(minInt) - 1) << false; 0162 QTest::newRow("large number") << QScriptValue(double(0x123456789a)) << false; 0163 0164 } 0165 0166 void CommonParserTest::testIntFromScriptValue() 0167 { 0168 QFETCH(QScriptValue, scriptValue); 0169 QFETCH(bool, okay); 0170 0171 ParsedNumber<int> result = ParserUtils::intFromScriptValue(scriptValue); 0172 QCOMPARE(result.string, scriptValue.toString()); 0173 if (result.isValid != okay) { 0174 qDebug() << scriptValue.toString() << ", int =" << scriptValue.toInt32() << ", double = " 0175 << scriptValue.toNumber(); 0176 } 0177 QCOMPARE(result.isValid, okay); 0178 if (result.isValid) { 0179 QFETCH(int, value); 0180 QCOMPARE(result.value, value); 0181 } 0182 } 0183 0184 void CommonParserTest::testUIntFromScriptValue_data() 0185 { 0186 QTest::addColumn<QScriptValue>("scriptValue"); 0187 QTest::addColumn<bool>("okay"); 0188 QTest::addColumn<uint>("value"); 0189 0190 auto* engine = new QScriptEngine(this); 0191 QTest::newRow("invalid script value") << QScriptValue() << false; 0192 QTest::newRow("boolean true") << QScriptValue(true) << false; 0193 QTest::newRow("boolean false") << QScriptValue(false) << false; 0194 QTest::newRow("null script value") << engine->nullValue() << false; 0195 QTest::newRow("undefined script value") << engine->undefinedValue() << false; 0196 QTest::newRow("array") << engine->newArray(5) << false; 0197 QTest::newRow("object") << engine->newObject() << false; 0198 QTest::newRow("float") << QScriptValue(1234.5) << false; 0199 0200 QTest::newRow("1234 number") << QScriptValue(1234) << true << 1234u; 0201 QTest::newRow("1234 string") << QScriptValue(QStringLiteral("1234")) << true << 1234u; 0202 QTest::newRow("0xff number") << QScriptValue(0xff) << true << 0xffu; 0203 QTest::newRow("0xff string") << QScriptValue(QStringLiteral("0xff")) << true << 0xffu; 0204 0205 QTest::newRow("negative string") << QScriptValue(QStringLiteral("-1")) << false; 0206 QTest::newRow("negative number") << QScriptValue(-1) << false; 0207 0208 uint maxInt = 0xffffffff; 0209 QTEST_ASSERT(maxInt == std::numeric_limits<uint>::max()); 0210 QTest::newRow("max") << QScriptValue(double(maxInt)) << true << maxInt; 0211 QTest::newRow("max int") << QScriptValue(maxInt) << true << maxInt; 0212 QTest::newRow("max string") << QScriptValue(QStringLiteral("4294967295")) << true << maxInt; 0213 QTest::newRow("max hex string") << QScriptValue(QStringLiteral("0xffffffff")) << true << maxInt; 0214 QTest::newRow("max+1") << QScriptValue(double(maxInt) + 1) << false; 0215 QTest::newRow("max+1 string") << QScriptValue(QStringLiteral("4294967296")) << false; 0216 QTest::newRow("max+1 hex string") << QScriptValue(QStringLiteral("0x100000000")) << false; 0217 QTest::newRow("large number") << QScriptValue(double(0x123456789a)) << false; 0218 } 0219 0220 void CommonParserTest::testUIntFromScriptValue() 0221 { 0222 QFETCH(QScriptValue, scriptValue); 0223 QFETCH(bool, okay); 0224 0225 ParsedNumber<uint> result = ParserUtils::uintFromScriptValue(scriptValue); 0226 QCOMPARE(result.string, scriptValue.toString()); 0227 if (result.isValid != okay) { 0228 qDebug() << scriptValue.toString() << ", uint =" << scriptValue.toUInt32() << ", double = " 0229 << scriptValue.toNumber(); 0230 } 0231 QCOMPARE(result.isValid, okay); 0232 if (result.isValid) { 0233 QFETCH(uint, value); 0234 QCOMPARE(result.value, value); 0235 } 0236 } 0237 0238 inline void CommonParserTest::testToStringEncoding() 0239 { 0240 QFETCH(QString, str); 0241 QFETCH(int, expected); 0242 QScopedPointer<ScriptLogger> logger(new ScriptLogger()); 0243 StringDataInformation::StringType type = 0244 ParserUtils::toStringEncoding(str, LoggerWithContext(logger.data(), QString())); 0245 QCOMPARE((int)type, expected); 0246 } 0247 0248 inline void CommonParserTest::testToStringEncoding_data() 0249 { 0250 QTest::addColumn<QString>("str"); 0251 QTest::addColumn<int>("expected"); 0252 0253 QTest::newRow("ascii") << "ascii" << (int)StringDataInformation::StringType::ASCII; 0254 QTest::newRow("ebcdic") << "ebcdic" << (int)StringDataInformation::StringType::EBCDIC; 0255 QTest::newRow("ascii upper") << "ASCII" << (int)StringDataInformation::StringType::ASCII; 0256 QTest::newRow("excess char") << "asciii" << (int)StringDataInformation::StringType::InvalidEncoding; 0257 0258 QTest::newRow("latin1") << "latin1" << (int)StringDataInformation::StringType::Latin1; 0259 QTest::newRow("latin-1") << "latin-1" << (int)StringDataInformation::StringType::Latin1; 0260 QTest::newRow("latin") << "latin" << (int)StringDataInformation::StringType::InvalidEncoding; 0261 0262 QTest::newRow("utf8") << "utf8" << (int)StringDataInformation::StringType::UTF8; 0263 QTest::newRow("utf-8") << "utf-8" << (int)StringDataInformation::StringType::UTF8; 0264 QTest::newRow("utf--8") << "utf--8" << (int)StringDataInformation::StringType::InvalidEncoding; 0265 0266 QTest::newRow("utf16") << "utf16" << (int)StringDataInformation::StringType::UTF16_LE; 0267 QTest::newRow("utf-16") << "utf-16" << (int)StringDataInformation::StringType::UTF16_LE; 0268 QTest::newRow("utf-16le") << "utf-16le" << (int)StringDataInformation::StringType::UTF16_LE; 0269 QTest::newRow("utf-16-le") << "utf-16-le" << (int)StringDataInformation::StringType::UTF16_LE; 0270 QTest::newRow("utf-16--le") << "utf-16--le" << (int)StringDataInformation::StringType::InvalidEncoding; 0271 0272 QTest::newRow("utf-16be") << "utf-16be" << (int)StringDataInformation::StringType::UTF16_BE; 0273 QTest::newRow("utf-16-be") << "utf-16-be" << (int)StringDataInformation::StringType::UTF16_BE; 0274 QTest::newRow("utf-16-abe") << "utf-16-abe" << (int)StringDataInformation::StringType::InvalidEncoding; 0275 0276 QTest::newRow("utf32") << "utf32" << (int)StringDataInformation::StringType::UTF32_LE; 0277 QTest::newRow("utf-32") << "utf-32" << (int)StringDataInformation::StringType::UTF32_LE; 0278 QTest::newRow("utf-32le") << "utf-32le" << (int)StringDataInformation::StringType::UTF32_LE; 0279 QTest::newRow("utf-32-le") << "utf-32-le" << (int)StringDataInformation::StringType::UTF32_LE; 0280 QTest::newRow("utf-32--le") << "utf-32--le" << (int)StringDataInformation::StringType::InvalidEncoding; 0281 0282 QTest::newRow("utf-32be") << "utf-32be" << (int)StringDataInformation::StringType::UTF32_BE; 0283 QTest::newRow("utf-32-be") << "utf-32-be" << (int)StringDataInformation::StringType::UTF32_BE; 0284 QTest::newRow("utf-32-abe") << "utf-32-abe" << (int)StringDataInformation::StringType::InvalidEncoding; 0285 } 0286 0287 QTEST_GUILESS_MAIN(CommonParserTest) 0288 0289 #include "commonparsertest.moc"