| 1 | /*
|
|---|
| 2 | * Copyright (c) 2018 Jaroslav Jindrak
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include <__bits/test/tests.hpp>
|
|---|
| 30 | #include <cstdlib>
|
|---|
| 31 | #include <deque>
|
|---|
| 32 | #include <functional>
|
|---|
| 33 | #include <initializer_list>
|
|---|
| 34 | #include <iterator>
|
|---|
| 35 | #include <queue>
|
|---|
| 36 | #include <stack>
|
|---|
| 37 | #include <vector>
|
|---|
| 38 |
|
|---|
| 39 | namespace std::test
|
|---|
| 40 | {
|
|---|
| 41 | namespace aux
|
|---|
| 42 | {
|
|---|
| 43 | template<class T, class Comp = std::less<T>>
|
|---|
| 44 | class priority_queue_iterator
|
|---|
| 45 | {
|
|---|
| 46 | public:
|
|---|
| 47 | using value_type = T;
|
|---|
| 48 | using reference = const T&;
|
|---|
| 49 | using size_type = size_t;
|
|---|
| 50 | using pointer = value_type*;
|
|---|
| 51 | using difference_type = size_t;
|
|---|
| 52 |
|
|---|
| 53 | using iterator_category = forward_iterator_tag;
|
|---|
| 54 |
|
|---|
| 55 | priority_queue_iterator(
|
|---|
| 56 | priority_queue<value_type, std::vector<value_type>, Comp> q,
|
|---|
| 57 | bool end = false
|
|---|
| 58 | )
|
|---|
| 59 | : queue_{q}, end_{end}
|
|---|
| 60 | { /* DUMMY BODY */ }
|
|---|
| 61 |
|
|---|
| 62 | priority_queue_iterator(const priority_queue_iterator& other)
|
|---|
| 63 | : queue_{other.queue_}, end_{other.end_}
|
|---|
| 64 | { /* DUMMY BODY */ }
|
|---|
| 65 |
|
|---|
| 66 | reference operator*()
|
|---|
| 67 | {
|
|---|
| 68 | return queue_.top();
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | priority_queue_iterator& operator++()
|
|---|
| 72 | {
|
|---|
| 73 | queue_.pop();
|
|---|
| 74 |
|
|---|
| 75 | if (queue_.empty())
|
|---|
| 76 | end_ = true;
|
|---|
| 77 |
|
|---|
| 78 | return *this;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | priority_queue_iterator operator++(int)
|
|---|
| 82 | {
|
|---|
| 83 | auto old = *this;
|
|---|
| 84 | ++(*this);
|
|---|
| 85 |
|
|---|
| 86 | return old;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | bool operator==(const priority_queue_iterator& rhs) const
|
|---|
| 90 | {
|
|---|
| 91 | return end_ == rhs.end_;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | bool operator!=(const priority_queue_iterator& rhs) const
|
|---|
| 95 | {
|
|---|
| 96 | return !(*this == rhs);
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | private:
|
|---|
| 100 | priority_queue<value_type, std::vector<value_type>, Comp> queue_;
|
|---|
| 101 | bool end_;
|
|---|
| 102 | };
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | bool adaptors_test::run(bool report)
|
|---|
| 106 | {
|
|---|
| 107 | report_ = report;
|
|---|
| 108 | start();
|
|---|
| 109 |
|
|---|
| 110 | test_queue();
|
|---|
| 111 | test_priority_queue();
|
|---|
| 112 | test_stack();
|
|---|
| 113 |
|
|---|
| 114 | return end();
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | const char* adaptors_test::name()
|
|---|
| 118 | {
|
|---|
| 119 | return "adaptors";
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | void adaptors_test::test_queue()
|
|---|
| 123 | {
|
|---|
| 124 | std::queue<int> q{std::deque<int>{1}};
|
|---|
| 125 |
|
|---|
| 126 | test_eq("queue initialized from deque not empty", q.empty(), false);
|
|---|
| 127 | test_eq("queue initialized form queue size", q.size(), 1U);
|
|---|
| 128 | test_eq("single element queue front == back", q.front(), q.back());
|
|---|
| 129 |
|
|---|
| 130 | q.push(2);
|
|---|
| 131 | test_eq("queue push", q.back(), 2);
|
|---|
| 132 | test_eq("queue size", q.size(), 2U);
|
|---|
| 133 |
|
|---|
| 134 | q.pop();
|
|---|
| 135 | test_eq("queue pop", q.front(), 2);
|
|---|
| 136 |
|
|---|
| 137 | q.emplace(4);
|
|---|
| 138 | test_eq("queue emplace", q.back(), 4);
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | void adaptors_test::test_priority_queue()
|
|---|
| 142 | {
|
|---|
| 143 | auto check1 = {9, 8, 5, 4, 2, 1};
|
|---|
| 144 | std::vector<int> data{5, 4, 2, 8, 1};
|
|---|
| 145 | std::priority_queue<int> q1{data.begin(), data.end()};
|
|---|
| 146 |
|
|---|
| 147 | test_eq("priority_queue initialized from iterator range not empty", q1.empty(), false);
|
|---|
| 148 | test_eq("priority_queue initialized from iterator range size", q1.size(), 5U);
|
|---|
| 149 |
|
|---|
| 150 | q1.push(9);
|
|---|
| 151 | test_eq("priority_queue push pt1", q1.size(), 6U);
|
|---|
| 152 | test_eq("priority_queue push pt2", q1.top(), 9);
|
|---|
| 153 |
|
|---|
| 154 | test_eq(
|
|---|
| 155 | "priority_queue initialized from iterator range ops",
|
|---|
| 156 | check1.begin(), check1.end(),
|
|---|
| 157 | aux::priority_queue_iterator<int>{q1},
|
|---|
| 158 | aux::priority_queue_iterator<int>{q1, true}
|
|---|
| 159 | );
|
|---|
| 160 |
|
|---|
| 161 | auto check2 = {1, 2, 3, 4, 5, 8};
|
|---|
| 162 | std::priority_queue<int, std::vector<int>, std::greater<int>> q2{std::greater<int>{}, data};
|
|---|
| 163 |
|
|---|
| 164 | test_eq("priority_queue initialized from vector and compare not empty", q2.empty(), false);
|
|---|
| 165 | test_eq("priority_queue initialized from vector and compare size", q2.size(), 5U);
|
|---|
| 166 |
|
|---|
| 167 | q2.push(3);
|
|---|
| 168 | test_eq("priority_queue push pt1", q2.size(), 6U);
|
|---|
| 169 | test_eq("priority_queue push pt2", q2.top(), 1);
|
|---|
| 170 |
|
|---|
| 171 | test_eq(
|
|---|
| 172 | "priority_queue initialized from vector and compare ops",
|
|---|
| 173 | check2.begin(), check2.end(),
|
|---|
| 174 | aux::priority_queue_iterator<int, std::greater<int>>{q2},
|
|---|
| 175 | aux::priority_queue_iterator<int, std::greater<int>>{q2, true}
|
|---|
| 176 | );
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | void adaptors_test::test_stack()
|
|---|
| 180 | {
|
|---|
| 181 | std::stack<int> s{std::deque<int>{1}};
|
|---|
| 182 |
|
|---|
| 183 | test_eq("stack initialized from deque top", s.top(), 1);
|
|---|
| 184 | test_eq("stack initialized from deque size", s.size(), 1U);
|
|---|
| 185 | test_eq("stack initialized from deque not empty", s.empty(), false);
|
|---|
| 186 |
|
|---|
| 187 | s.push(2);
|
|---|
| 188 | test_eq("stack push top", s.top(), 2);
|
|---|
| 189 | test_eq("stack push size", s.size(), 2U);
|
|---|
| 190 |
|
|---|
| 191 | s.pop();
|
|---|
| 192 | test_eq("stack pop top", s.top(), 1);
|
|---|
| 193 | test_eq("stack pop size", s.size(), 1U);
|
|---|
| 194 | }
|
|---|
| 195 | }
|
|---|