| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2018 Jaroslav Jindrak
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | #include <__bits/test/tests.hpp>
|
|---|
| 8 | #include <cstdlib>
|
|---|
| 9 | #include <deque>
|
|---|
| 10 | #include <functional>
|
|---|
| 11 | #include <initializer_list>
|
|---|
| 12 | #include <iterator>
|
|---|
| 13 | #include <queue>
|
|---|
| 14 | #include <stack>
|
|---|
| 15 | #include <vector>
|
|---|
| 16 |
|
|---|
| 17 | namespace std::test
|
|---|
| 18 | {
|
|---|
| 19 | namespace aux
|
|---|
| 20 | {
|
|---|
| 21 | template<class T, class Comp = std::less<T>>
|
|---|
| 22 | class priority_queue_iterator
|
|---|
| 23 | {
|
|---|
| 24 | public:
|
|---|
| 25 | using value_type = T;
|
|---|
| 26 | using reference = const T&;
|
|---|
| 27 | using size_type = size_t;
|
|---|
| 28 | using pointer = value_type*;
|
|---|
| 29 | using difference_type = size_t;
|
|---|
| 30 |
|
|---|
| 31 | using iterator_category = forward_iterator_tag;
|
|---|
| 32 |
|
|---|
| 33 | priority_queue_iterator(
|
|---|
| 34 | priority_queue<value_type, std::vector<value_type>, Comp> q,
|
|---|
| 35 | bool end = false
|
|---|
| 36 | )
|
|---|
| 37 | : queue_{q}, end_{end}
|
|---|
| 38 | { /* DUMMY BODY */ }
|
|---|
| 39 |
|
|---|
| 40 | priority_queue_iterator(const priority_queue_iterator& other)
|
|---|
| 41 | : queue_{other.queue_}, end_{other.end_}
|
|---|
| 42 | { /* DUMMY BODY */ }
|
|---|
| 43 |
|
|---|
| 44 | reference operator*()
|
|---|
| 45 | {
|
|---|
| 46 | return queue_.top();
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | priority_queue_iterator& operator++()
|
|---|
| 50 | {
|
|---|
| 51 | queue_.pop();
|
|---|
| 52 |
|
|---|
| 53 | if (queue_.empty())
|
|---|
| 54 | end_ = true;
|
|---|
| 55 |
|
|---|
| 56 | return *this;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | priority_queue_iterator operator++(int)
|
|---|
| 60 | {
|
|---|
| 61 | auto old = *this;
|
|---|
| 62 | ++(*this);
|
|---|
| 63 |
|
|---|
| 64 | return old;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | bool operator==(const priority_queue_iterator& rhs) const
|
|---|
| 68 | {
|
|---|
| 69 | return end_ == rhs.end_;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | bool operator!=(const priority_queue_iterator& rhs) const
|
|---|
| 73 | {
|
|---|
| 74 | return !(*this == rhs);
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | private:
|
|---|
| 78 | priority_queue<value_type, std::vector<value_type>, Comp> queue_;
|
|---|
| 79 | bool end_;
|
|---|
| 80 | };
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | bool adaptors_test::run(bool report)
|
|---|
| 84 | {
|
|---|
| 85 | report_ = report;
|
|---|
| 86 | start();
|
|---|
| 87 |
|
|---|
| 88 | test_queue();
|
|---|
| 89 | test_priority_queue();
|
|---|
| 90 | test_stack();
|
|---|
| 91 |
|
|---|
| 92 | return end();
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | const char* adaptors_test::name()
|
|---|
| 96 | {
|
|---|
| 97 | return "adaptors";
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | void adaptors_test::test_queue()
|
|---|
| 101 | {
|
|---|
| 102 | std::queue<int> q{std::deque<int>{1}};
|
|---|
| 103 |
|
|---|
| 104 | test_eq("queue initialized from deque not empty", q.empty(), false);
|
|---|
| 105 | test_eq("queue initialized form queue size", q.size(), 1U);
|
|---|
| 106 | test_eq("single element queue front == back", q.front(), q.back());
|
|---|
| 107 |
|
|---|
| 108 | q.push(2);
|
|---|
| 109 | test_eq("queue push", q.back(), 2);
|
|---|
| 110 | test_eq("queue size", q.size(), 2U);
|
|---|
| 111 |
|
|---|
| 112 | q.pop();
|
|---|
| 113 | test_eq("queue pop", q.front(), 2);
|
|---|
| 114 |
|
|---|
| 115 | q.emplace(4);
|
|---|
| 116 | test_eq("queue emplace", q.back(), 4);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | void adaptors_test::test_priority_queue()
|
|---|
| 120 | {
|
|---|
| 121 | auto check1 = {9, 8, 5, 4, 2, 1};
|
|---|
| 122 | std::vector<int> data{5, 4, 2, 8, 1};
|
|---|
| 123 | std::priority_queue<int> q1{data.begin(), data.end()};
|
|---|
| 124 |
|
|---|
| 125 | test_eq("priority_queue initialized from iterator range not empty", q1.empty(), false);
|
|---|
| 126 | test_eq("priority_queue initialized from iterator range size", q1.size(), 5U);
|
|---|
| 127 |
|
|---|
| 128 | q1.push(9);
|
|---|
| 129 | test_eq("priority_queue push pt1", q1.size(), 6U);
|
|---|
| 130 | test_eq("priority_queue push pt2", q1.top(), 9);
|
|---|
| 131 |
|
|---|
| 132 | test_eq(
|
|---|
| 133 | "priority_queue initialized from iterator range ops",
|
|---|
| 134 | check1.begin(), check1.end(),
|
|---|
| 135 | aux::priority_queue_iterator<int>{q1},
|
|---|
| 136 | aux::priority_queue_iterator<int>{q1, true}
|
|---|
| 137 | );
|
|---|
| 138 |
|
|---|
| 139 | auto check2 = {1, 2, 3, 4, 5, 8};
|
|---|
| 140 | std::priority_queue<int, std::vector<int>, std::greater<int>> q2{std::greater<int>{}, data};
|
|---|
| 141 |
|
|---|
| 142 | test_eq("priority_queue initialized from vector and compare not empty", q2.empty(), false);
|
|---|
| 143 | test_eq("priority_queue initialized from vector and compare size", q2.size(), 5U);
|
|---|
| 144 |
|
|---|
| 145 | q2.push(3);
|
|---|
| 146 | test_eq("priority_queue push pt1", q2.size(), 6U);
|
|---|
| 147 | test_eq("priority_queue push pt2", q2.top(), 1);
|
|---|
| 148 |
|
|---|
| 149 | test_eq(
|
|---|
| 150 | "priority_queue initialized from vector and compare ops",
|
|---|
| 151 | check2.begin(), check2.end(),
|
|---|
| 152 | aux::priority_queue_iterator<int, std::greater<int>>{q2},
|
|---|
| 153 | aux::priority_queue_iterator<int, std::greater<int>>{q2, true}
|
|---|
| 154 | );
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | void adaptors_test::test_stack()
|
|---|
| 158 | {
|
|---|
| 159 | std::stack<int> s{std::deque<int>{1}};
|
|---|
| 160 |
|
|---|
| 161 | test_eq("stack initialized from deque top", s.top(), 1);
|
|---|
| 162 | test_eq("stack initialized from deque size", s.size(), 1U);
|
|---|
| 163 | test_eq("stack initialized from deque not empty", s.empty(), false);
|
|---|
| 164 |
|
|---|
| 165 | s.push(2);
|
|---|
| 166 | test_eq("stack push top", s.top(), 2);
|
|---|
| 167 | test_eq("stack push size", s.size(), 2U);
|
|---|
| 168 |
|
|---|
| 169 | s.pop();
|
|---|
| 170 | test_eq("stack pop top", s.top(), 1);
|
|---|
| 171 | test_eq("stack pop size", s.size(), 1U);
|
|---|
| 172 | }
|
|---|
| 173 | }
|
|---|