File indexing completed on 2024-05-12 05:55:43

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 #include <QSignalSpy>
0011 
0012 #include "view/structures/datatypes/array/arraydatainformation.hpp"
0013 #include "view/structures/datatypes/array/primitivearraydata.hpp"
0014 #include "view/structures/datatypes/topleveldatainformation.hpp"
0015 #include "view/structures/datatypes/primitive/primitivetemplateinfo.hpp"
0016 #include "view/structures/datatypes/primitivefactory.hpp"
0017 #include "view/structures/datatypes/structuredatainformation.hpp"
0018 
0019 class ArrayDataInformationTest : public QObject
0020 {
0021     Q_OBJECT
0022 
0023 private:
0024     void testResizeHelper(ArrayDataInformation* array, TopLevelDataInformation* top);
0025 
0026 private Q_SLOTS:
0027     void initTestCase();
0028     void testResize();
0029     void testResize_data();
0030     void cleanupTestCase();
0031 
0032 private:
0033     ArrayDataInformation* primitive;
0034     ArrayDataInformation* complex;
0035     TopLevelDataInformation* primitiveTop;
0036     TopLevelDataInformation* complexTop;
0037     uint primitiveSize;
0038     uint complexSize;
0039 };
0040 
0041 void ArrayDataInformationTest::initTestCase()
0042 {
0043     qRegisterMetaType<const DataInformation*>();
0044     qRegisterMetaType<DataInformation*>();
0045     LoggerWithContext lwc(nullptr, QString());
0046 
0047     primitive = new ArrayDataInformation(QStringLiteral("primitives"), 0,
0048                                          PrimitiveFactory::newInstance(QStringLiteral("child"), PrimitiveDataType::UInt32, lwc));
0049     primitiveSize = 32;
0050     primitiveTop = new TopLevelDataInformation(primitive);
0051 
0052     QCOMPARE(primitive->isArray(), true);
0053     QCOMPARE(primitive->isBitfield(), false);
0054     QCOMPARE(primitive->isDummy(), false);
0055     QCOMPARE(primitive->isEnum(), false);
0056     QCOMPARE(primitive->isEnum(), false);
0057     QCOMPARE(primitive->isString(), false);
0058     QCOMPARE(primitive->isUnion(), false);
0059     QCOMPARE(primitive->mainStructure(), primitive);
0060     QCOMPARE(primitive->childCount(), 0U);
0061     QCOMPARE(primitive->length(), 0U);
0062     QCOMPARE(primitive->size(), 0U);
0063     QCOMPARE(primitive->isTopLevel(), false);
0064     QCOMPARE(primitiveTop->isTopLevel(), true);
0065     QCOMPARE(primitive->positionInFile(3), BitCount64(24));
0066     QCOMPARE(primitive->topLevelDataInformation(), primitiveTop);
0067 
0068     QVector<DataInformation*> structsChildren;
0069     structsChildren << PrimitiveFactory::newInstance(QStringLiteral("first"), PrimitiveDataType::UInt32, lwc)
0070                     << PrimitiveFactory::newInstance(QStringLiteral("second"), PrimitiveDataType::UInt32, lwc);
0071 
0072     auto* structs = new StructureDataInformation(QStringLiteral("vals"), structsChildren);
0073 
0074     complexSize = 64;
0075     complex = new ArrayDataInformation(QStringLiteral("complex"), 0, structs);
0076     complexTop = new TopLevelDataInformation(complex);
0077 
0078     QCOMPARE(complex->isArray(), true);
0079     QCOMPARE(complex->isBitfield(), false);
0080     QCOMPARE(complex->isDummy(), false);
0081     QCOMPARE(complex->isEnum(), false);
0082     QCOMPARE(complex->isEnum(), false);
0083     QCOMPARE(complex->isString(), false);
0084     QCOMPARE(complex->isUnion(), false);
0085     QCOMPARE(complex->mainStructure(), complex);
0086     QCOMPARE(complex->childCount(), 0U);
0087     QCOMPARE(complex->length(), 0U);
0088     QCOMPARE(complex->size(), 0U);
0089     QCOMPARE(complex->isTopLevel(), false);
0090     QCOMPARE(complexTop->isTopLevel(), true);
0091     QCOMPARE(complex->positionInFile(3), BitCount64(24));
0092     QCOMPARE(complex->topLevelDataInformation(), complexTop);
0093 }
0094 
0095 void ArrayDataInformationTest::testResizeHelper(ArrayDataInformation* array, TopLevelDataInformation* top)
0096 {
0097     QSignalSpy spyAboutToInsert(top, SIGNAL(childrenAboutToBeInserted(DataInformation*,uint,uint)));
0098     QSignalSpy spyInserted(top, SIGNAL(childrenInserted(const DataInformation*,uint,uint)));
0099     QSignalSpy spyAboutToRemove(top, SIGNAL(childrenAboutToBeRemoved(DataInformation*,uint,uint)));
0100     QSignalSpy spyRemoved(top, SIGNAL(childrenRemoved(const DataInformation*,uint,uint)));
0101 
0102     QFETCH(uint, newsize);
0103     QFETCH(uint, postsize);
0104     QFETCH(bool, insertCalled);
0105     QFETCH(bool, removeCalled);
0106 
0107     array->setArrayLength(newsize);
0108     QCOMPARE(array->length(), postsize);
0109     if (insertCalled) {
0110         QCOMPARE(spyAboutToInsert.size(), 1);
0111         QCOMPARE(spyInserted.size(), 1);
0112 
0113         QFETCH(uint, insertFirstArg);
0114         QFETCH(uint, insertSecondArg);
0115 
0116         QCOMPARE(spyAboutToInsert[0][1].toUInt(), insertFirstArg);
0117         QCOMPARE(spyAboutToInsert[0][2].toUInt(), insertSecondArg);
0118         QCOMPARE(spyInserted[0][1].toUInt(), insertFirstArg);
0119         QCOMPARE(spyInserted[0][2].toUInt(), insertSecondArg);
0120     }
0121     if (removeCalled) {
0122         QCOMPARE(spyAboutToRemove.size(), 1);
0123         QCOMPARE(spyRemoved.size(), 1);
0124 
0125         QFETCH(uint, removeFirstArg);
0126         QFETCH(uint, removeSecondArg);
0127 
0128         QCOMPARE(spyAboutToRemove[0][1].toUInt(), removeFirstArg);
0129         QCOMPARE(spyAboutToRemove[0][2].toUInt(), removeSecondArg);
0130         QCOMPARE(spyRemoved[0][1].toUInt(), removeFirstArg);
0131         QCOMPARE(spyRemoved[0][2].toUInt(), removeSecondArg);
0132     }
0133 }
0134 
0135 void ArrayDataInformationTest::testResize()
0136 {
0137     testResizeHelper(primitive, primitiveTop);
0138     testResizeHelper(complex, complexTop);
0139 }
0140 
0141 void ArrayDataInformationTest::testResize_data()
0142 {
0143     QTest::addColumn<uint>("newsize");
0144     QTest::addColumn<uint>("postsize");
0145     QTest::addColumn<bool>("insertCalled");
0146     QTest::addColumn<uint>("insertFirstArg");
0147     QTest::addColumn<uint>("insertSecondArg");
0148     QTest::addColumn<bool>("removeCalled");
0149     QTest::addColumn<uint>("removeFirstArg");
0150     QTest::addColumn<uint>("removeSecondArg");
0151     QTest::newRow("1. (10)") << 10U << 10U << true << 0U << 9U << false << -1U << -1U;
0152     QTest::newRow("2. (10)") << 10U << 10U << false << -1U << -1U << false << -1U << -1U;
0153     QTest::newRow("3. (9)") << 9U << 9U << false << -1U << -1U << true << 9U << 9U;
0154     QTest::newRow("4. (18)") << 18U << 18U << true << 9U << 17U << false << -1U << -1U;
0155     QTest::newRow("5. (0)") << 0U << 0U << false << -1U << -1U << true << 0U << 17U;
0156     QTest::newRow("6. (1)") << 1U << 1U << true << 0U << 0U << false << -1U << -1U;
0157     QTest::newRow("7. (0)") << 0U << 0U << false << -1U << -1U << true << 0U << 0U;
0158     QTest::newRow("8. (0)") << 0U << 0U << false << -1U << -1U << false << -1U << -1U;
0159 
0160 }
0161 
0162 void ArrayDataInformationTest::cleanupTestCase()
0163 {
0164     delete complexTop;
0165     delete primitiveTop;
0166 }
0167 
0168 QTEST_GUILESS_MAIN(ArrayDataInformationTest)
0169 
0170 #include "arraydatainformationtest.moc"