File indexing completed on 2024-05-19 04:39:59

0001 /*
0002     SPDX-FileCopyrightText: 2010 Milian Wolff <mail@milianw.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #include <QObject>
0008 #include <QTest>
0009 #include <QStandardPaths>
0010 #include <QDebug>
0011 
0012 #include "../kdevvarlengtharray.h"
0013 
0014 struct TestValue
0015 {
0016     TestValue()
0017     {}
0018     TestValue(const TestValue& other)
0019     {
0020         if (other.m_index) {
0021             int mustDo = 1;
0022             ++mustDo;
0023         }
0024         m_index = other.m_index;
0025     }
0026     uint m_index = 0;
0027 };
0028 
0029 class TestKDevVarLengthArray : public QObject
0030 {
0031     Q_OBJECT
0032 
0033 private Q_SLOTS:
0034     void initTestCase() { QStandardPaths::setTestModeEnabled(true); }
0035 
0036     /**
0037      * Make sure that valgrind does not report any warnings here
0038      * about uninitialized member variables.
0039      */
0040     void appendReallocIntegrity()
0041     {
0042         KDevVarLengthArray<TestValue, 2> array;
0043         QCOMPARE(array.size(), 0);
0044         QCOMPARE(array.capacity(), 2);
0045 
0046         qDebug() << "append item 1";
0047         array << TestValue();
0048         qDebug() << "appended index is:" << array[0].m_index;
0049         QCOMPARE(array.size(), 1);
0050         QCOMPARE(array.capacity(), 2);
0051         qDebug() << "append item 2";
0052         array << TestValue(); // should trigger the realloc
0053         qDebug() << "appended index is:" << array[1].m_index;
0054         QCOMPARE(array.size(), 2);
0055         QCOMPARE(array.capacity(), 2);
0056         qDebug() << "append item 3";
0057         array << TestValue();
0058         qDebug() << "appended index is:" << array[2].m_index;
0059         QCOMPARE(array.size(), 3);
0060         QCOMPARE(array.capacity(), 4);
0061 
0062         array.clear();
0063     }
0064 
0065     void mixed()
0066     {
0067         KDevVarLengthArray<int, 10> array;
0068         array.append(1);
0069         array << 2;
0070         array.insert(0, 0);
0071         QCOMPARE(array.back(), 2);
0072         array.pop_back();
0073         QCOMPARE(array.back(), 1);
0074         array.append(1);
0075         QVERIFY(array.removeOne(1));
0076         QCOMPARE(array.toList(), QList<int>() << 0 << 1);
0077         QCOMPARE(array.toVector(), QVector<int>() << 0 << 1);
0078         array.insert(0, 42);
0079         QCOMPARE(array.toVector(), QVector<int>() << 42 << 0 << 1);
0080         array.remove(0);
0081         QCOMPARE(array.toVector(), QVector<int>() << 0 << 1);
0082         QVERIFY(array.contains(1));
0083         QVERIFY(!array.contains(42));
0084         QCOMPARE(array.back(), 1);
0085         QCOMPARE(array.indexOf(1), 1);
0086         QCOMPARE(array.indexOf(42), -1);
0087     }
0088 };
0089 
0090 QTEST_MAIN(TestKDevVarLengthArray)
0091 
0092 #include "test_kdevvarlengtharray.moc"