Warning, file /utilities/okteta/kasten/controllers/test/basicdatainformationtest.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 module, made within the KDE community.
0003  *
0004  *    SPDX-FileCopyrightText: 2012 Alex Richardson <alex.richardson@gmx.de>
0005  *
0006  *    SPDX-License-Identifier: LGPL-2.1-or-later
0007  */
0008 
0009 #include <QTest>
0010 
0011 #include "view/structures/datatypes/array/arraydatainformation.hpp"
0012 #include "view/structures/datatypes/array/primitivearraydata.hpp"
0013 #include "view/structures/datatypes/strings/stringdatainformation.hpp"
0014 #include "view/structures/datatypes/topleveldatainformation.hpp"
0015 #include "view/structures/datatypes/primitive/primitivetemplateinfo.hpp"
0016 #include "view/structures/datatypes/primitive/enumdatainformation.hpp"
0017 #include "view/structures/datatypes/primitive/flagdatainformation.hpp"
0018 #include "view/structures/datatypes/primitive/bitfield/boolbitfielddatainformation.hpp"
0019 #include "view/structures/datatypes/primitive/bitfield/signedbitfielddatainformation.hpp"
0020 #include "view/structures/datatypes/primitive/bitfield/unsignedbitfielddatainformation.hpp"
0021 #include "view/structures/datatypes/primitivefactory.hpp"
0022 #include "view/structures/datatypes/uniondatainformation.hpp"
0023 #include "view/structures/datatypes/structuredatainformation.hpp"
0024 
0025 struct ExpectedResults
0026 {
0027     ExpectedResults()
0028         : isTopLevel(false)
0029         , isEnum(false)
0030         , isStruct(false)
0031         , isUnion(false)
0032         , isArray(false)
0033         , isBitfield(false)
0034         , isPrimitive(false)
0035         , isString(false)
0036         , isDummy(false)
0037         , hasChildren(false)
0038     {
0039         columnFlags[DataInformation::ColumnName] = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
0040         columnFlags[DataInformation::ColumnType] = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
0041         columnFlags[DataInformation::ColumnValue] = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
0042         noFileColumnFlags[DataInformation::ColumnName] = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
0043         noFileColumnFlags[DataInformation::ColumnType] = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
0044         noFileColumnFlags[DataInformation::ColumnValue] = Qt::ItemIsSelectable | Qt::ItemIsEnabled;
0045     }
0046     DataInformationBase* parent = nullptr;
0047     BitCount32 size = 0;
0048     bool isTopLevel : 1;
0049     bool isEnum : 1;
0050     bool isStruct : 1;
0051     bool isUnion : 1;
0052     bool isArray : 1;
0053     bool isBitfield : 1;
0054     bool isPrimitive : 1;
0055     bool isString : 1;
0056     bool isDummy : 1;
0057     bool hasChildren : 1;
0058     Qt::ItemFlags columnFlags[DataInformation::COLUMN_COUNT];
0059     Qt::ItemFlags noFileColumnFlags[DataInformation::COLUMN_COUNT];
0060 };
0061 
0062 class BasicDataInformationTest : public QObject
0063 {
0064     Q_OBJECT
0065 
0066 private:
0067     void basicTest(DataInformationBase* data, const ExpectedResults& expected) const;
0068 
0069 private Q_SLOTS:
0070     void initTestCase();
0071     void testPrimitives();
0072     void testBitfields();
0073     void testStructs();
0074     void testUnions();
0075     void testArrays();
0076     void testEnums();
0077     void testString();
0078     void testDummy();
0079     void testTopLevel();
0080     void cleanupTestCase();
0081 
0082 private:
0083     QVector<PrimitiveDataInformation*> primitives;
0084     QVector<AbstractBitfieldDataInformation*> bitfields;
0085     TopLevelDataInformation* topLevel;
0086     StructureDataInformation* emptyStruct;
0087     StructureDataInformation* structWithChildren;
0088     UnionDataInformation* emptyUnion;
0089     UnionDataInformation* unionWithChildren;
0090     ArrayDataInformation* emptyPrimitiveArray;
0091     ArrayDataInformation* emptyComplexArray;
0092     ArrayDataInformation* primitiveArrayWithChildren;
0093     ArrayDataInformation* complexArrayWithChildren;
0094     StringDataInformation* emptyString;
0095     DummyDataInformation* dummy;
0096     FlagDataInformation* flagData;
0097     EnumDataInformation* enumData;
0098 };
0099 
0100 #if 0
0101 
0102 #ifdef NDEBUG
0103 #pragma message("NDEBUG defined")
0104 #else
0105 #pragma message("NDEBUG not defined")
0106 #endif
0107 
0108 #ifdef DEBUG
0109 #pragma message("DEBUG defined")
0110 #else
0111 #pragma message("DEBUG not defined")
0112 #endif
0113 
0114 #ifdef QT_NO_DEBUG
0115 #pragma message("QT_NO_DEBUG defined")
0116 #else
0117 #pragma message("QT_NO_DEBUG not defined")
0118 #endif
0119 
0120 #ifdef QT_DEBUG
0121 #pragma message("QT_DEBUG defined")
0122 #else
0123 #pragma message("QT_DEBUG not defined")
0124 #endif
0125 
0126 #endif
0127 
0128 namespace {
0129 template <typename T> void castChecker(bool isValid, DataInformationBase* data, const T* constValue, T* nonConstValue)
0130 {
0131     if (isValid) {
0132         QCOMPARE(constValue, static_cast<const T*>(data));
0133         QCOMPARE(nonConstValue, static_cast<T*>(data));
0134     } else {
0135         QCOMPARE(constValue, static_cast<const T*>(nullptr));
0136         QCOMPARE(nonConstValue, static_cast<T*>(nullptr));
0137     }
0138 }
0139 #define CAST_CHECKER(isValid, value, func, type) do { \
0140         const DataInformationBase* constData = data; \
0141         const type* constVal = constData->func(); \
0142         type* nonConstVal = data->func(); \
0143         castChecker(isValid, value, constVal, nonConstVal); \
0144     } while (0)
0145 
0146 }
0147 void BasicDataInformationTest::basicTest(DataInformationBase* data, const ExpectedResults& expected) const
0148 {
0149     QVERIFY(data);
0150     QCOMPARE(data->isTopLevel(), expected.isTopLevel);
0151     if (expected.isTopLevel) {
0152         CAST_CHECKER(true, data, asTopLevel, TopLevelDataInformation);
0153         CAST_CHECKER(false, data, asArray, ArrayDataInformation);
0154         CAST_CHECKER(false, data, asBitfield, AbstractBitfieldDataInformation);
0155         CAST_CHECKER(false, data, asDataInformation, DataInformation);
0156         CAST_CHECKER(false, data, asDummy, DummyDataInformation);
0157         CAST_CHECKER(false, data, asEnum, EnumDataInformation);
0158         CAST_CHECKER(false, data, asPrimitive, PrimitiveDataInformation);
0159         CAST_CHECKER(false, data, asString, StringDataInformation);
0160         CAST_CHECKER(false, data, asStruct, StructureDataInformation);
0161         CAST_CHECKER(false, data, asUnion, UnionDataInformation);
0162         return; // no more can be done with a TopLeveDataInformation
0163     }
0164 
0165     CAST_CHECKER(false, data, asTopLevel, TopLevelDataInformation);
0166     CAST_CHECKER(true, data, asDataInformation, DataInformation);
0167 
0168     QCOMPARE(data->isArray(), expected.isArray);
0169     if (expected.isArray) {
0170         CAST_CHECKER(true, data, asArray, ArrayDataInformation);
0171     } else {
0172         CAST_CHECKER(false, data, asArray, ArrayDataInformation);
0173     }
0174 
0175     QCOMPARE(data->isBitfield(), expected.isBitfield);
0176     if (expected.isBitfield) {
0177         CAST_CHECKER(true, data, asBitfield, AbstractBitfieldDataInformation);
0178     } else {
0179         CAST_CHECKER(false, data, asBitfield, AbstractBitfieldDataInformation);
0180     }
0181 
0182     QCOMPARE(data->isEnum(), expected.isEnum);
0183     if (expected.isEnum) {
0184         CAST_CHECKER(true, data, asEnum, EnumDataInformation);
0185     } else {
0186         CAST_CHECKER(false, data, asEnum, EnumDataInformation);
0187     }
0188 
0189     QCOMPARE(data->isPrimitive(), expected.isPrimitive);
0190     if (expected.isPrimitive) {
0191         CAST_CHECKER(true, data, asPrimitive, PrimitiveDataInformation);
0192     } else {
0193         CAST_CHECKER(false, data, asPrimitive, PrimitiveDataInformation);
0194     }
0195 
0196     QCOMPARE(data->isStruct(), expected.isStruct);
0197     if (expected.isStruct) {
0198         CAST_CHECKER(true, data, asStruct, StructureDataInformation);
0199     } else {
0200         CAST_CHECKER(false, data, asStruct, StructureDataInformation);
0201     }
0202 
0203     QCOMPARE(data->isUnion(), expected.isUnion);
0204     if (expected.isUnion) {
0205         CAST_CHECKER(true, data, asUnion, UnionDataInformation);
0206     } else {
0207         CAST_CHECKER(false, data, asUnion, UnionDataInformation);
0208     }
0209 
0210     QCOMPARE(data->isString(), expected.isString);
0211     if (expected.isString) {
0212         CAST_CHECKER(true, data, asString, StringDataInformation);
0213     } else {
0214         CAST_CHECKER(false, data, asString, StringDataInformation);
0215     }
0216 
0217     QCOMPARE(data->isDummy(), expected.isDummy);
0218     if (expected.isDummy) {
0219         CAST_CHECKER(true, data, asDummy, DummyDataInformation);
0220         return; // the other checks cannot be done with a dummy
0221     }
0222 
0223     CAST_CHECKER(false, data, asDummy, DummyDataInformation);
0224 
0225     DataInformation* dataInf = data->asDataInformation();
0226     QVERIFY(dataInf);
0227 
0228     QCOMPARE(dataInf->size(), expected.size);
0229     QCOMPARE(dataInf->parent(), expected.parent);
0230 
0231     DataInformation* clone1 = (dataInf->clone());
0232     QScopedPointer<TopLevelDataInformation> top(new TopLevelDataInformation(clone1));
0233     QCOMPARE(clone1->parent(), top.data()); // top takes ownership of clone1
0234     QCOMPARE(top->actualDataInformation(), clone1);
0235 
0236     QScopedPointer<DataInformation> clone2(clone1->clone());
0237     QVERIFY(clone2->parent() == nullptr); // cloning should reset parent to NULL, else we get dangling pointers
0238 
0239     QCOMPARE(dataInf->flags(DataInformation::ColumnName, true), expected.columnFlags[DataInformation::ColumnName]);
0240     QCOMPARE(dataInf->flags(DataInformation::ColumnType, true), expected.columnFlags[DataInformation::ColumnType]);
0241     QCOMPARE(dataInf->flags(DataInformation::ColumnValue, true), expected.columnFlags[DataInformation::ColumnValue]);
0242 
0243     QCOMPARE(dataInf->flags(DataInformation::ColumnName, false), expected.noFileColumnFlags[DataInformation::ColumnName]);
0244     QCOMPARE(dataInf->flags(DataInformation::ColumnType, false), expected.noFileColumnFlags[DataInformation::ColumnType]);
0245     QCOMPARE(dataInf->flags(DataInformation::ColumnValue, false), expected.noFileColumnFlags[DataInformation::ColumnValue]);
0246 }
0247 
0248 void BasicDataInformationTest::initTestCase()
0249 {
0250 //  qRegisterMetaType<const DataInformation*>();
0251 //  qRegisterMetaType<DataInformation*>();
0252     LoggerWithContext lwc(nullptr, QString());
0253 
0254     PrimitiveDataType type = PrimitiveDataType::START;
0255     while (type < PrimitiveDataType::Bitfield) {
0256         primitives.append(PrimitiveFactory::newInstance(QStringLiteral("prim"), type, lwc));
0257         type = static_cast<PrimitiveDataType>(static_cast<int>(type) + 1);
0258     }
0259     QCOMPARE(PrimitiveFactory::newInstance(QStringLiteral("invalid"), PrimitiveDataType::Bitfield, lwc), static_cast<PrimitiveDataInformation*>(nullptr));
0260     QCOMPARE(PrimitiveFactory::newInstance(QStringLiteral("invalid"), QStringLiteral("invalid_type"), lwc), static_cast<PrimitiveDataInformation*>(nullptr));
0261     bitfields.append(new BoolBitfieldDataInformation(QStringLiteral("bitfield"), 24));
0262     bitfields.append(new UnsignedBitfieldDataInformation(QStringLiteral("bitfield"), 24));
0263     bitfields.append(new SignedBitfieldDataInformation(QStringLiteral("bitfield"), 24));
0264 
0265     emptyStruct = new StructureDataInformation(QStringLiteral("emptyStruct"));
0266     QVector<DataInformation*> structChildren;
0267     structChildren << PrimitiveFactory::newInstance(QStringLiteral("prim"), PrimitiveDataType::UInt32, lwc)
0268                    << PrimitiveFactory::newInstance(QStringLiteral("prim2"), PrimitiveDataType::UInt64, lwc);
0269     structWithChildren = new StructureDataInformation(QStringLiteral("structWithChildren"), structChildren);
0270 
0271     emptyUnion = new UnionDataInformation(QStringLiteral("emptyUnion"));
0272     QVector<DataInformation*> unionChildren;
0273     unionChildren << PrimitiveFactory::newInstance(QStringLiteral("prim"), PrimitiveDataType::UInt32, lwc)
0274                   << PrimitiveFactory::newInstance(QStringLiteral("prim2"), PrimitiveDataType::UInt64, lwc);
0275     unionWithChildren = new UnionDataInformation(QStringLiteral("unionWithChildren"), unionChildren);
0276 
0277     emptyPrimitiveArray = new ArrayDataInformation(QStringLiteral("emptyPrimitiveArray"), 0, PrimitiveFactory::newInstance(QStringLiteral("prim"), PrimitiveDataType::UInt32, lwc));
0278     emptyComplexArray = new ArrayDataInformation(QStringLiteral("emptyComplexArray"), 0, structWithChildren->clone());
0279     primitiveArrayWithChildren = new ArrayDataInformation(QStringLiteral("primitiveArrayWithChildren"), 2, PrimitiveFactory::newInstance(QStringLiteral("prim"), PrimitiveDataType::UInt32, lwc));
0280     complexArrayWithChildren = new ArrayDataInformation(QStringLiteral("complexArrayWithChildren"), 2, structWithChildren->clone());
0281 
0282     QMap<AllPrimitiveTypes, QString> enumVals;
0283     enumVals[1] = QStringLiteral("one");
0284     enumVals[2] = QStringLiteral("two");
0285     enumVals[4] = QStringLiteral("four");
0286     EnumDefinition::Ptr edef(new EnumDefinition(enumVals, QStringLiteral("eDef"), PrimitiveDataType::UInt32));
0287     flagData = new FlagDataInformation(QStringLiteral("flagData"), PrimitiveFactory::newInstance(QStringLiteral("prim"), PrimitiveDataType::UInt32, lwc), edef);
0288     enumData = new EnumDataInformation(QStringLiteral("enumData"), PrimitiveFactory::newInstance(QStringLiteral("prim"), PrimitiveDataType::UInt32, lwc), edef);
0289     emptyString = new StringDataInformation(QStringLiteral("string"), StringDataInformation::StringType::ASCII);
0290     dummy = new DummyDataInformation(nullptr);
0291     topLevel = new TopLevelDataInformation(new DummyDataInformation(nullptr));
0292 }
0293 
0294 void BasicDataInformationTest::testBitfields()
0295 {
0296     ExpectedResults exp;
0297     exp.isPrimitive = true;
0298     exp.isBitfield = true;
0299     exp.size = 24;
0300     exp.columnFlags[DataInformation::ColumnValue] = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
0301     for (auto* bitField : qAsConst(bitfields)) {
0302         basicTest(bitField, exp);
0303     }
0304 }
0305 
0306 void BasicDataInformationTest::testPrimitives()
0307 {
0308     ExpectedResults exp;
0309     exp.isPrimitive = true;
0310     exp.columnFlags[DataInformation::ColumnValue] = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
0311     for (int i = 0; i < primitives.size(); ++i) {
0312         PrimitiveDataInformation* data = primitives.at(i);
0313         PrimitiveDataType t = data->type();
0314         QCOMPARE(t, PrimitiveDataType(static_cast<PrimitiveDataType>(i)));
0315         QCOMPARE(data->type(), PrimitiveDataType(static_cast<PrimitiveDataType>(i)));
0316         if (t == PrimitiveDataType::Bool8 || t ==  PrimitiveDataType::Int8 || t == PrimitiveDataType::UInt8 || t == PrimitiveDataType::Char) {
0317             exp.size = 8;
0318         } else if (t == PrimitiveDataType::Bool16 || t == PrimitiveDataType::Int16 || t == PrimitiveDataType::UInt16) {
0319             exp.size = 16;
0320         } else if (t == PrimitiveDataType::Bool32 || t == PrimitiveDataType::Int32 || t == PrimitiveDataType::UInt32 || t == PrimitiveDataType::Float) {
0321             exp.size = 32;
0322         } else if (t == PrimitiveDataType::Bool64 || t == PrimitiveDataType::Int64 || t == PrimitiveDataType::UInt64 || t == PrimitiveDataType::Double) {
0323             exp.size = 64;
0324         } else {
0325             QVERIFY(false);
0326         }
0327         basicTest(data, exp);
0328     }
0329 }
0330 
0331 void BasicDataInformationTest::testArrays()
0332 {
0333     ExpectedResults exp;
0334     exp.isArray = true;
0335     exp.size = 0;
0336     basicTest(emptyComplexArray, exp);
0337     basicTest(emptyPrimitiveArray, exp);
0338     exp.hasChildren = true;
0339     exp.size = 64;
0340     basicTest(primitiveArrayWithChildren, exp);
0341     exp.size = 96 * 2;
0342     basicTest(complexArrayWithChildren, exp);
0343 }
0344 
0345 void BasicDataInformationTest::testEnums()
0346 {
0347     ExpectedResults exp;
0348     exp.isPrimitive = true;
0349     exp.isEnum = true;
0350     exp.size = 32;
0351     exp.columnFlags[DataInformation::ColumnValue] = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
0352     basicTest(enumData, exp);
0353     basicTest(flagData, exp);
0354 }
0355 
0356 void BasicDataInformationTest::testStructs()
0357 {
0358     ExpectedResults exp;
0359     exp.isStruct = true;
0360     exp.size = 0;
0361     basicTest(emptyStruct, exp);
0362     exp.hasChildren = true;
0363     exp.size = 96;
0364     basicTest(structWithChildren, exp);
0365 }
0366 
0367 void BasicDataInformationTest::testUnions()
0368 {
0369     ExpectedResults exp;
0370     exp.isUnion = true;
0371     exp.size = 0;
0372     basicTest(emptyUnion, exp);
0373     exp.hasChildren = true;
0374     exp.size = 64;
0375     basicTest(unionWithChildren, exp);
0376 }
0377 
0378 void BasicDataInformationTest::testDummy()
0379 {
0380     ExpectedResults exp;
0381     exp.isDummy = true;
0382     exp.size = 0;
0383     basicTest(dummy, exp);
0384 }
0385 
0386 void BasicDataInformationTest::testString()
0387 {
0388     ExpectedResults exp;
0389     exp.isString = true;
0390     exp.size = 0;
0391     basicTest(emptyString, exp);
0392 }
0393 
0394 void BasicDataInformationTest::testTopLevel()
0395 {
0396     ExpectedResults exp;
0397     exp.isTopLevel = true;
0398     basicTest(topLevel, exp);
0399 }
0400 
0401 void BasicDataInformationTest::cleanupTestCase()
0402 {
0403     qDeleteAll(primitives);
0404     qDeleteAll(bitfields);
0405     delete emptyStruct;
0406     delete structWithChildren;
0407     delete emptyUnion;
0408     delete unionWithChildren;
0409     delete emptyPrimitiveArray;
0410     delete emptyComplexArray;
0411     delete primitiveArrayWithChildren;
0412     delete complexArrayWithChildren;
0413     delete flagData;
0414     delete enumData;
0415     delete emptyString;
0416     delete dummy;
0417     delete topLevel;
0418 }
0419 
0420 QTEST_GUILESS_MAIN(BasicDataInformationTest)
0421 
0422 #include "basicdatainformationtest.moc"