File: test_id_queue.cpp

package info (click to toggle)
dnf5 5.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,960 kB
  • sloc: cpp: 94,312; python: 3,370; xml: 1,073; ruby: 600; sql: 250; ansic: 232; sh: 104; perl: 62; makefile: 30
file content (184 lines) | stat: -rw-r--r-- 5,472 bytes parent folder | download
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
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: GPL-2.0-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
//
// Libdnf is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Libdnf is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libdnf.  If not, see <https://www.gnu.org/licenses/>.


#include "test_id_queue.hpp"

#include "solv/id_queue.hpp"

#include <iterator>

CPPUNIT_TEST_SUITE_REGISTRATION(IdQueueTest);


void IdQueueTest::setUp() {}


void IdQueueTest::tearDown() {}


// Test push_back(), operator[], size(), and clear()
void IdQueueTest::test_push_back() {
    libdnf5::solv::IdQueue id_queue;
    CPPUNIT_ASSERT(id_queue.size() == 0);

    id_queue.push_back(4);
    CPPUNIT_ASSERT(id_queue.size() == 1);

    id_queue.push_back(3, 2);
    CPPUNIT_ASSERT(id_queue.size() == 3);

    // insert same value like it is in queue will result in increase of elements
    id_queue.push_back(3);
    CPPUNIT_ASSERT(id_queue.size() == 4);

    // check the order of inserted values
    CPPUNIT_ASSERT(id_queue[0] == 4);
    CPPUNIT_ASSERT(id_queue[1] == 3);
    CPPUNIT_ASSERT(id_queue[2] == 2);
    CPPUNIT_ASSERT(id_queue[3] == 3);
    id_queue.clear();
    CPPUNIT_ASSERT(id_queue.size() == 0);
}

/// Test operators, copy and move constructors
void IdQueueTest::test_operators() {
    libdnf5::solv::IdQueue id_queue_same1;
    libdnf5::solv::IdQueue id_queue_same2;
    libdnf5::solv::IdQueue id_queue_different3;
    libdnf5::solv::IdQueue id_queue_different4;
    id_queue_same1.push_back(3, 2);
    id_queue_same2.push_back(3, 2);
    id_queue_different3.push_back(2, 5);
    id_queue_different4.push_back(2);

    CPPUNIT_ASSERT(id_queue_same1 == id_queue_same2);
    CPPUNIT_ASSERT(id_queue_same1 == id_queue_same1);
    CPPUNIT_ASSERT(id_queue_same1 != id_queue_different3);
    CPPUNIT_ASSERT(id_queue_different4 != id_queue_different3);
    CPPUNIT_ASSERT(id_queue_different3 != id_queue_different4);

    // test copy constructor
    auto copy = id_queue_same1;
    CPPUNIT_ASSERT(id_queue_same1 == copy);
    CPPUNIT_ASSERT(id_queue_same1.size() == 2);

    // test move constructor
    auto move = std::move(id_queue_same1);
    CPPUNIT_ASSERT(id_queue_same1 != move);
    CPPUNIT_ASSERT(id_queue_same1.size() == 0);
    CPPUNIT_ASSERT(move.size() == 2);

    /// test += operator
    CPPUNIT_ASSERT(id_queue_same2.size() == 2);
    CPPUNIT_ASSERT(id_queue_different3.size() == 2);
    id_queue_same2 += id_queue_different3;
    CPPUNIT_ASSERT(id_queue_same2.size() == 4);
    CPPUNIT_ASSERT(id_queue_different3.size() == 2);
    CPPUNIT_ASSERT(id_queue_same2[0] == 3);
    CPPUNIT_ASSERT(id_queue_same2[1] == 2);
    CPPUNIT_ASSERT(id_queue_same2[2] == 2);
    CPPUNIT_ASSERT(id_queue_same2[3] == 5);
}

void IdQueueTest::test_iterator_empty() {
    std::vector<Id> expected = {};
    std::vector<Id> result;
    libdnf5::solv::IdQueue queue;

    for (auto it = queue.begin(); it != queue.end(); it++) {
        result.push_back(*it);
    }
    CPPUNIT_ASSERT(result == expected);
}

void IdQueueTest::test_iterator_full() {
    std::vector<Id> expected = {5, 6, 8, 10, 14};
    libdnf5::solv::IdQueue queue;
    queue.push_back(5, 6);
    queue.push_back(8, 10);
    queue.push_back(14);

    // test begin
    auto it1 = queue.begin();
    CPPUNIT_ASSERT_EQUAL(*it1, 5);

    // test pre-increment operator
    auto it2 = ++it1;
    CPPUNIT_ASSERT_EQUAL(*it1, 6);
    CPPUNIT_ASSERT_EQUAL(*it2, 6);

    // test post-increment operator
    auto it3 = it2++;
    CPPUNIT_ASSERT_EQUAL(*it2, 8);
    CPPUNIT_ASSERT_EQUAL(*it3, 6);

    // test move back to the begin
    it2.begin();
    CPPUNIT_ASSERT_EQUAL(*it2, 5);
    CPPUNIT_ASSERT(it2 == queue.begin());

    // test increment after begin
    ++it2;
    CPPUNIT_ASSERT_EQUAL(*it2, 6);

    auto it4 = std::next(it2);
    CPPUNIT_ASSERT_EQUAL(*it2, 6);
    CPPUNIT_ASSERT_EQUAL(*it4, 8);

    std::advance(it4, 2);
    CPPUNIT_ASSERT_EQUAL(*it4, 14);

    // test move to the end
    it2.end();
    CPPUNIT_ASSERT(it2 == queue.end());

    // test loop with pre-increment operator
    {
        std::vector<Id> result;
        for (auto it = queue.begin(), end = queue.end(); it != end; ++it) {
            result.push_back(*it);
        }
        CPPUNIT_ASSERT(result == expected);
    }

    // test loop with post-increment operator
    {
        std::vector<Id> result;
        for (auto it = queue.begin(), end = queue.end(); it != end; it++) {
            result.push_back(*it);
        }
        CPPUNIT_ASSERT(result == expected);
    }
}

void IdQueueTest::test_iterator_performance() {
    constexpr int max = 1000000;
    libdnf5::solv::IdQueue queue;
    for (int i = 0; i < max; i++) {
        queue.push_back(i);
    }

    for (int i = 0; i < 10; i++) {
        std::vector<Id> result;
        for (auto it = queue.begin(); it != queue.end(); it++) {
            result.push_back(*it);
        }
    }
}