aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/utils/indexedcontainerproxyconstiterator/tst_indexedcontainerproxyconstiterator.cpp
blob: 2d739a9b294f27401ae58c0f292a48eb9c814402 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include <utils/indexedcontainerproxyconstiterator.h>

#include <QtTest>

#include <string>
#include <vector>

using namespace Utils;

class tst_IndexedContainerProxyConstIterator : public QObject
{
    Q_OBJECT

private slots:
    void initTestCase();
    void testConstruction();
    void testDereference();
    void testIndexing();
    void testComparisons();
    void testIncrement();
    void testDecrement();
    void testPlus();
    void testMinus();
    void testIteration();

private:
    using StringContainer = std::vector<std::string>;
    using StringIterator = IndexedContainerProxyConstIterator<StringContainer>;
    StringContainer strings;

    using BoolContainer = std::vector<bool>;
    using BoolIterator = IndexedContainerProxyConstIterator<BoolContainer>;
    BoolContainer bools;
};

void tst_IndexedContainerProxyConstIterator::initTestCase()
{
    strings = {"abc", "defgh", "ijk"};
    bools = {false, true, false};
}

void tst_IndexedContainerProxyConstIterator::testConstruction()
{
    StringIterator strIt(strings, 0);
    QCOMPARE(*strIt, "abc");

    StringIterator strIt2(strings, 1);
    QCOMPARE(*strIt2, "defgh");

    BoolIterator boolIt(bools, 0);
    QCOMPARE(*boolIt, false);

    BoolIterator boolIt2(bools, 1);
    QCOMPARE(*boolIt2, true);
}

void tst_IndexedContainerProxyConstIterator::testDereference()
{
    StringIterator strIt(strings, 0);
    QCOMPARE(*strIt, "abc");
    QCOMPARE(int(strIt->length()), 3);

    BoolIterator boolIt(bools, 0);
    QCOMPARE(*boolIt, false);
}

void tst_IndexedContainerProxyConstIterator::testIndexing()
{
    StringIterator strIt(strings, 0);
    QCOMPARE(strIt[2], "ijk");

    BoolIterator boolIt(bools, 0);
    QCOMPARE(boolIt[2], false);
}

void tst_IndexedContainerProxyConstIterator::testComparisons()
{
    StringIterator strIt(strings, 0);
    StringIterator strIt2(strings, 0);
    StringIterator strIt3(strings, 1);

    QVERIFY(strIt == strIt);
    QVERIFY(!(strIt != strIt));
    QVERIFY(!(strIt < strIt));
    QVERIFY(strIt <= strIt);
    QVERIFY(!(strIt > strIt));
    QVERIFY(strIt >= strIt);

    QVERIFY(strIt == strIt2);
    QVERIFY(!(strIt != strIt2));
    QVERIFY(!(strIt < strIt2));
    QVERIFY(strIt <= strIt2);
    QVERIFY(!(strIt > strIt2));
    QVERIFY(strIt >= strIt2);

    QVERIFY(!(strIt == strIt3));
    QVERIFY(strIt != strIt3);
    QVERIFY(strIt < strIt3);
    QVERIFY(strIt <= strIt3);
    QVERIFY(!(strIt > strIt3));
    QVERIFY(!(strIt >= strIt3));

    QVERIFY(!(strIt3 == strIt));
    QVERIFY(strIt3 != strIt);
    QVERIFY(!(strIt3 < strIt));
    QVERIFY(!(strIt3 <= strIt));
    QVERIFY(strIt3 > strIt);
    QVERIFY(strIt3 >= strIt);
}

void tst_IndexedContainerProxyConstIterator::testIncrement()
{
    StringIterator strIt(strings, 0);
    QCOMPARE(*(++strIt), "defgh");
    QCOMPARE(*(strIt++), "defgh");
    QCOMPARE(*strIt, "ijk");

    BoolIterator boolIt(bools, 0);
    QCOMPARE(*(++boolIt), true);
    QCOMPARE(*(boolIt++), true);
    QCOMPARE(*boolIt, false);
}

void tst_IndexedContainerProxyConstIterator::testDecrement()
{
    StringIterator strIt(strings, 3);
    QCOMPARE(*(--strIt), "ijk");
    QCOMPARE(*(strIt--), "ijk");
    QCOMPARE(*strIt, "defgh");

    BoolIterator boolIt(bools, 3);
    QCOMPARE(*(--boolIt), false);
    QCOMPARE(*(boolIt--), false);
    QCOMPARE(*boolIt, true);
}

void tst_IndexedContainerProxyConstIterator::testPlus()
{
    StringIterator strIt(strings, 1);
    QCOMPARE(*(strIt + 1), "ijk");
    QCOMPARE(*(1 + strIt), "ijk");
    strIt += 1;
    QCOMPARE(*strIt, "ijk");

    BoolIterator boolIt(bools, 1);
    QCOMPARE(*(boolIt + 1), false);
    QCOMPARE(*(1 + boolIt), false);
    boolIt += 1;
    QCOMPARE(*boolIt, false);
}

void tst_IndexedContainerProxyConstIterator::testMinus()
{
    StringIterator strIt(strings, 1);
    QCOMPARE(*(strIt - 1), "abc");
    strIt -= 1;
    QCOMPARE(*strIt, "abc");

    BoolIterator boolIt(bools, 1);
    QCOMPARE(*(boolIt - 1), false);
    boolIt -= 1;
    QCOMPARE(*boolIt, false);
}

void tst_IndexedContainerProxyConstIterator::testIteration()
{
    StringIterator strBegin(strings, 0);
    StringIterator strEnd(strings, strings.size());
    StringContainer stringsCopy;
    for (StringIterator it = strBegin; it != strEnd; ++it)
        stringsCopy.push_back(*it);
    QCOMPARE(stringsCopy, strings);

    BoolIterator boolBegin(bools, 0);
    BoolIterator boolEnd(bools, bools.size());
    BoolContainer boolsCopy;
    for (BoolIterator it = boolBegin; it != boolEnd; ++it)
        boolsCopy.push_back(*it);
    QCOMPARE(boolsCopy, bools);
}

QTEST_GUILESS_MAIN(tst_IndexedContainerProxyConstIterator)

#include "tst_indexedcontainerproxyconstiterator.moc"