Warning, file /utilities/okteta/kasten/controllers/test/uniondatainformationtest.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 <Okteta/ByteArrayModel>
0012 #include <Okteta/Byte>
0013 
0014 #include "testutils.hpp"
0015 
0016 #include "view/structures/datatypes/uniondatainformation.hpp"
0017 #include "view/structures/datatypes/topleveldatainformation.hpp"
0018 #include "view/structures/datatypes/primitivefactory.hpp"
0019 #include "view/structures/datatypes/primitive/bitfield/unsignedbitfielddatainformation.hpp"
0020 
0021 class UnionDataInformationTest : public QObject
0022 {
0023     Q_OBJECT
0024 
0025 private Q_SLOTS:
0026     void initTestCase();
0027     void testSize();
0028     void testReadData1();
0029     void testReadData1_data();
0030 
0031 private:
0032     QScopedPointer<Okteta::ByteArrayModel> model;
0033 };
0034 
0035 /** In binary:
0036  *  00010010 00110100 01010110 01111000
0037  *  10011010 10111100 11011110 11110000
0038  */
0039 static constexpr Okteta::Byte testData[8] = {
0040     0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0
0041 };
0042 
0043 void UnionDataInformationTest::initTestCase()
0044 {
0045     model.reset(new Okteta::ByteArrayModel(testData, sizeof(testData)));
0046     model->setAutoDelete(false);
0047     QCOMPARE(model->size(), Okteta::Size(8));
0048 }
0049 
0050 void UnionDataInformationTest::testSize()
0051 {
0052     LoggerWithContext lwc(nullptr, QString());
0053 
0054     UnionDataInformation empty(QStringLiteral("empty"));
0055     QCOMPARE(empty.size(), BitCount32(0));
0056     QVector<DataInformation*> size54;
0057     size54 << PrimitiveFactory::newInstance(QStringLiteral("8"), PrimitiveDataType::Bool8, lwc);
0058     size54 << PrimitiveFactory::newInstance(QStringLiteral("16"), PrimitiveDataType::Int16, lwc);
0059     size54 << PrimitiveFactory::newInstance(QStringLiteral("32"), PrimitiveDataType::Float, lwc);
0060     size54 << new UnsignedBitfieldDataInformation(QStringLiteral("54"), 54);
0061     UnionDataInformation fourChildren(QStringLiteral("four"), size54);
0062     QCOMPARE(fourChildren.size(), BitCount32(54));
0063 }
0064 
0065 void UnionDataInformationTest::testReadData1()
0066 {
0067     LoggerWithContext lwc(nullptr, QString());
0068 
0069     PrimitiveDataInformation* b8 = PrimitiveFactory::newInstance(QStringLiteral("8"), PrimitiveDataType::Bool8, lwc);
0070     PrimitiveDataInformation* u32 = PrimitiveFactory::newInstance(QStringLiteral("32"), PrimitiveDataType::UInt32, lwc);
0071     PrimitiveDataInformation* i16 = PrimitiveFactory::newInstance(QStringLiteral("16"), PrimitiveDataType::Int16, lwc);
0072     auto* u54 = new UnsignedBitfieldDataInformation(QStringLiteral("54"), 54);
0073     QVector<DataInformation*> children;
0074     children << b8 << u32 << i16 << u54;
0075     auto* un = new UnionDataInformation(QStringLiteral("un"), children);
0076     un->setByteOrder(DataInformation::DataInformationEndianess::EndianessLittle);
0077     TopLevelDataInformation top(un);
0078     // read from bit 0
0079     QFETCH(uint, address);
0080     QFETCH(quint8, offset);
0081     QFETCH(quint64, bitsRemaining);
0082     // update needed before read
0083     top.scriptHandler()->updateDataInformation(un);
0084     qint64 read = un->readData(model.data(), address, bitsRemaining, &offset);
0085     QFETCH(qint64, readBitsTotal);
0086     QCOMPARE(read, readBitsTotal);
0087     QFETCH(quint8, offsetAfterRead);
0088     QCOMPARE(offset, offsetAfterRead);
0089     QFETCH(bool, unWasAble);
0090     QCOMPARE(un->wasAbleToRead(), unWasAble);
0091 
0092     QFETCH(quint8, b8Expected);
0093     QFETCH(bool, b8WasAble);
0094     QFETCH(qint16, i16Expected);
0095     QFETCH(bool, i16WasAble);
0096     QFETCH(quint32, u32Expected);
0097     QFETCH(bool, u32WasAble);
0098     QFETCH(quint64, u54Expected);
0099     QFETCH(bool, u54WasAble);
0100 
0101     QCOMPARE(b8->value().value<quint8>(), b8Expected);
0102     QCOMPARE(b8->wasAbleToRead(), b8WasAble);
0103     QCOMPARE(i16->value().value<qint16>(), i16Expected);
0104     QCOMPARE(i16->wasAbleToRead(), i16WasAble);
0105     QCOMPARE(u32->value().value<quint32>(), u32Expected);
0106     QCOMPARE(u32->wasAbleToRead(), u32WasAble);
0107     QCOMPARE(u54->value().value<quint64>(), u54Expected);
0108     QCOMPARE(u54->wasAbleToRead(), u54WasAble);
0109 }
0110 
0111 void UnionDataInformationTest::testReadData1_data()
0112 {
0113     QTest::addColumn<uint>("address");
0114     QTest::addColumn<quint8>("offset");
0115     QTest::addColumn<quint64>("bitsRemaining"); // XXX get rid of this argument?
0116 
0117     QTest::addColumn<qint64>("readBitsTotal");
0118     QTest::addColumn<quint8>("offsetAfterRead");
0119     QTest::addColumn<bool>("unWasAble");
0120 
0121     QTest::addColumn<quint8>("b8Expected");
0122     QTest::addColumn<bool>("b8WasAble");
0123     QTest::addColumn<qint16>("i16Expected");
0124     QTest::addColumn<bool>("i16WasAble");
0125     QTest::addColumn<quint32>("u32Expected");
0126     QTest::addColumn<bool>("u32WasAble");
0127     QTest::addColumn<quint64>("u54Expected");
0128     QTest::addColumn<bool>("u54WasAble");
0129 
0130     /* In little endian:
0131      *  11110000 11011110 10111100 10011010
0132      *  01111000 01010110 00110100 00010010
0133      */
0134     QTest::newRow("0 bytes 0 bits") << 0u << quint8(0) << quint64(64) << qint64(54) << quint8(6) << true
0135         /* now b8 and i16 */ << quint8(0x12u) << true << qint16(0x3412) << true
0136         /* u32 and u54 */ << quint32(0x78563412u) << true << Q_UINT64_C(0x1ebc9a78563412) << true;
0137     // shifted by two bytes in little endian:
0138     // 11110000 11011110 10111100 10011010 01111000 01010110 00110100 000100xx
0139     QTest::newRow("0 bytes 2 bits") << 0u << quint8(2) << quint64(62) << qint64(54) << quint8(0) << true
0140         /* b8 */ << Utils::binary<quint8>("00 000100") << true
0141         /* i16 */ << Utils::binary<qint16>("10 00110100 000100") << true
0142         /* u32 */ << Utils::binary<quint32>("10 01111000 01010110 00110100 000100") << true
0143         /* u54 */ << Utils::binary<quint64>("11011110 10111100 10011010 01111000 01010110 00110100 000100") << true;
0144 
0145     // now so that the 54bit value fits in exactly, i.e. 10 bits shifted
0146     // shifted by 10 bytes in little endian:
0147     // 11110000 11011110 10111100 10011010 01111000 01010110 001101xx xxxxxxxx
0148     QTest::newRow("1 bytes 2 bits") << 1u << quint8(2) << quint64(54) << qint64(54) << quint8(0) << true
0149         /* b8 */ << Utils::binary<quint8>("10 001101") << true
0150         /* i16 */ << Utils::binary<qint16>("00 01010110 001101") << true
0151         /* u32 */ << Utils::binary<quint32>("00 10011010 01111000 01010110 001101") << true
0152         /* u54 */ << Utils::binary<quint64>("11110000 11011110 10111100 10011010 01111000 01010110 001101") << true;
0153 
0154     // now make the 54 bit value go past eof (11 bits)
0155     // shifted by 10 bytes in little endian:
0156     // 11110000 11011110 10111100 10011010 01111000 01010110 00110xxx xxxxxxxx
0157     QTest::newRow("1 bytes 3 bits") << 1u << quint8(3) << quint64(53) << qint64(-1) << quint8(3) << false
0158         /* b8 */ << Utils::binary<quint8>("110 00110") << true
0159         /* i16 */ << Utils::binary<qint16>("000 01010110 00110") << true
0160         /* u32 */ << Utils::binary<quint32>("100 10011010 01111000 01010110 00110") << true
0161         /* u54 */ << Utils::binary<quint64>("0") << false;
0162 }
0163 
0164 QTEST_GUILESS_MAIN(UnionDataInformationTest)
0165 
0166 #include "uniondatainformationtest.moc"