Debian Codegraph @

src/libs/store/tests/TestKoXmlVector.cpp

raw
1 /* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2015 Friedrich W. H. Kossebau <[email protected]>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 // clazy:excludeall=qstring-arg
8 #include "TestKoXmlVector.h"
9
10 #include <KoXmlVector.h>
11
12 #include <QTest>
13 #include <QDebug>
14
15
16 enum TestEnum
17 {
18 FirstType = 0,
19 SecondType = 1,
20 ThirdType = 2,
21 FourthType = 3,
22 FifthType = 4
23 };
24
25 class TestStruct
26 {
27 public:
28 bool attr: 1;
29 TestEnum type: 4; // seems msvc needs an extra bit
30 unsigned int number: 28;
31 QString string;
32 };
33
34 Q_DECLARE_TYPEINFO(TestStruct, Q_MOVABLE_TYPE);
35
36 static QDataStream& operator<<(QDataStream& s, const TestStruct& item)
37 {
38 quint8 flag = item.attr ? 1 : 0;
39
40 s << flag;
41 s << (quint8) item.type;
42 s << item.number;
43 s << item.string;
44
45 return s;
46 }
47
48 static QDataStream& operator>>(QDataStream& s, TestStruct& item)
49 {
50 quint8 flag;
51 quint8 type;
52 int number;
53 QString string;
54
55 s >> flag;
56 s >> type;
57 s >> number;
58 s >> string;
59
60 item.attr = (flag != 0);
61 item.type = (TestEnum) type;
62 item.number = number;
63 item.string = string;
64
65 return s;
66 }
67
68 void TestKoXmlVector::simpleConstructor()
69 {
70 KoXmlVector<TestStruct> vector;
71
72 QCOMPARE(vector.count(), 0);
73 QCOMPARE(vector.size(), 0);
74 QCOMPARE(vector.isEmpty(), true);
75 }
76
77
78 static const int writeAndReadUncompressedCount = 5;
79
80 void TestKoXmlVector::writeAndRead_data()
81 {
82 QTest::addColumn<unsigned int>("itemCount");
83 for(unsigned int i = 0; i < writeAndReadUncompressedCount*3+1; ++i) {
84 QTest::newRow(QByteArray::number(i).data()) << i;
85 }
86 }
87
88
89 void TestKoXmlVector::writeAndRead()
90 {
91 QFETCH(unsigned int, itemCount);
92
93 KoXmlVector<TestStruct, writeAndReadUncompressedCount+1> vector;
94
95 // add 3x items than what would not be compressed
96 for (unsigned int i = 0; i < itemCount; ++i) {
97 // test adding
98 TestStruct &item = vector.newItem();
99
100 const bool attr = (i % 2) == 0;
101 const TestEnum type = (TestEnum)(i % 5);
102 const unsigned int number = i;
103 const QString string = QString::number(i);
104
105 item.attr = attr;
106 item.type = type;
107 item.number = number;
108 item.string = string;
109
110 QCOMPARE(vector.count(), (signed)i+1);
111 QCOMPARE(vector.size(), (signed)i+1);
112 QCOMPARE(vector.isEmpty(), false);
113 }
114
115 vector.squeeze();
116
117 // now check all in a row again, so including all the uncompressed
118 for (unsigned int i = 0; i < itemCount; ++i) {
119 const bool attr = (i % 2) == 0;
120 const TestEnum type = (TestEnum)(i % 5);
121 const unsigned int number = i;
122 const QString string = QString::number(i);
123
124 const TestStruct &readItem = vector[i];
125
126 QCOMPARE(readItem.attr, attr);
127 QCOMPARE(readItem.type, type);
128 QCOMPARE(readItem.number, number);
129 QCOMPARE(readItem.string, string);
130 }
131 // and backwards
132 for (unsigned int ri = itemCount; ri > 0; --ri) {
133 const unsigned int i = ri-1;
134 const bool attr = (i % 2) == 0;
135 const TestEnum type = (TestEnum)(i % 5);
136 const unsigned int number = i;
137 const QString string = QString::number(i);
138
139 const TestStruct &readItem = vector[i];
140
141 QCOMPARE(readItem.attr, attr);
142 QCOMPARE(readItem.type, type);
143 QCOMPARE(readItem.number, number);
144 QCOMPARE(readItem.string, string);
145 }
146 }
147
148
149
150 QTEST_GUILESS_MAIN(TestKoXmlVector)