| 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 <initializer_list>
|
|---|
| 30 | #include <internal/test/tests.hpp>
|
|---|
| 31 | #include <unordered_set>
|
|---|
| 32 | #include <string>
|
|---|
| 33 | #include <utility>
|
|---|
| 34 |
|
|---|
| 35 | namespace std::test
|
|---|
| 36 | {
|
|---|
| 37 | bool unordered_set_test::run(bool report)
|
|---|
| 38 | {
|
|---|
| 39 | report_ = report;
|
|---|
| 40 | start();
|
|---|
| 41 |
|
|---|
| 42 | test_constructors_and_assignment();
|
|---|
| 43 | test_emplace_insert();
|
|---|
| 44 | test_multi();
|
|---|
| 45 |
|
|---|
| 46 | return end();
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | const char* unordered_set_test::name()
|
|---|
| 50 | {
|
|---|
| 51 | return "unordered_set";
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | void unordered_set_test::test_constructors_and_assignment()
|
|---|
| 55 | {
|
|---|
| 56 | auto check1 = {1, 2, 3, 4, 5, 6, 7};
|
|---|
| 57 | auto src1 = {3, 1, 5, 2, 7, 6, 4};
|
|---|
| 58 |
|
|---|
| 59 | std::unordered_set<int> s1{src1};
|
|---|
| 60 | test_contains(
|
|---|
| 61 | "initializer list initialization",
|
|---|
| 62 | check1.begin(), check1.end(), s1
|
|---|
| 63 | );
|
|---|
| 64 | test_eq("size", s1.size(), 7U);
|
|---|
| 65 |
|
|---|
| 66 | std::unordered_set<int> s2{src1.begin(), src1.end()};
|
|---|
| 67 | test_contains(
|
|---|
| 68 | "iterator range initialization",
|
|---|
| 69 | check1.begin(), check1.end(), s2
|
|---|
| 70 | );
|
|---|
| 71 |
|
|---|
| 72 | std::unordered_set<int> s3{s1};
|
|---|
| 73 | test_contains(
|
|---|
| 74 | "copy initialization",
|
|---|
| 75 | check1.begin(), check1.end(), s3
|
|---|
| 76 | );
|
|---|
| 77 |
|
|---|
| 78 | std::unordered_set<int> s4{std::move(s1)};
|
|---|
| 79 | test_contains(
|
|---|
| 80 | "move initialization",
|
|---|
| 81 | check1.begin(), check1.end(), s4
|
|---|
| 82 | );
|
|---|
| 83 | test_eq("move initialization - origin empty", s1.size(), 0U);
|
|---|
| 84 | test_eq("empty", s1.empty(), true);
|
|---|
| 85 |
|
|---|
| 86 | s1 = s4;
|
|---|
| 87 | test_contains(
|
|---|
| 88 | "copy assignment",
|
|---|
| 89 | check1.begin(), check1.end(), s1
|
|---|
| 90 | );
|
|---|
| 91 |
|
|---|
| 92 | s4 = std::move(s1);
|
|---|
| 93 | test_contains(
|
|---|
| 94 | "move assignment",
|
|---|
| 95 | check1.begin(), check1.end(), s4
|
|---|
| 96 | );
|
|---|
| 97 | test_eq("move assignment - origin empty", s1.size(), 0U);
|
|---|
| 98 |
|
|---|
| 99 | s1 = src1;
|
|---|
| 100 | test_contains(
|
|---|
| 101 | "initializer list assignment",
|
|---|
| 102 | check1.begin(), check1.end(), s1
|
|---|
| 103 | );
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | void unordered_set_test::test_emplace_insert()
|
|---|
| 107 | {
|
|---|
| 108 | std::unordered_set<int> set1{};
|
|---|
| 109 |
|
|---|
| 110 | auto res1 = set1.emplace(1);
|
|---|
| 111 | test_eq("first emplace succession", res1.second, true);
|
|---|
| 112 | test_eq("first emplace equivalence", *res1.first, 1);
|
|---|
| 113 |
|
|---|
| 114 | auto res2 = set1.emplace(1);
|
|---|
| 115 | test_eq("second emplace failure", res2.second, false);
|
|---|
| 116 | test_eq("second emplace equivalence", *res2.first, 1);
|
|---|
| 117 |
|
|---|
| 118 | auto res3 = set1.emplace_hint(set1.begin(), 2);
|
|---|
| 119 | test_eq("first emplace_hint succession", (res3 != set1.end()), true);
|
|---|
| 120 | test_eq("first emplace_hint equivalence", *res3, 2);
|
|---|
| 121 |
|
|---|
| 122 | auto res4 = set1.emplace_hint(set1.begin(), 2);
|
|---|
| 123 | test_eq("second emplace_hint failure", (res4 != set1.end()), true);
|
|---|
| 124 | test_eq("second emplace_hint equivalence", *res4, 2);
|
|---|
| 125 |
|
|---|
| 126 | std::unordered_set<std::string> set2{};
|
|---|
| 127 | auto res5 = set2.insert("A");
|
|---|
| 128 | test_eq("conversion insert succession", res5.second, true);
|
|---|
| 129 | test_eq("conversion insert equivalence", *res5.first, std::string{"A"});
|
|---|
| 130 |
|
|---|
| 131 | auto res6 = set2.insert(std::string{"B"});
|
|---|
| 132 | test_eq("first insert succession", res6.second, true);
|
|---|
| 133 | test_eq("first insert equivalence", *res6.first, std::string{"B"});
|
|---|
| 134 |
|
|---|
| 135 | auto res7 = set2.insert(std::string{"B"});
|
|---|
| 136 | test_eq("second insert failure", res7.second, false);
|
|---|
| 137 | test_eq("second insert equivalence", *res7.first, std::string{"B"});
|
|---|
| 138 |
|
|---|
| 139 | auto res10 = set1.erase(set1.find(2));
|
|---|
| 140 | test_eq("erase", set1.find(2), set1.end());
|
|---|
| 141 | test_eq("highest erased", res10, set1.end());
|
|---|
| 142 |
|
|---|
| 143 | set2.insert(std::string{"G"});
|
|---|
| 144 | set2.insert(std::string{"H"});
|
|---|
| 145 | set2.insert(std::string{"K"});
|
|---|
| 146 | auto res11 = set2.erase(std::string{"G"});
|
|---|
| 147 | test_eq("erase by key pt1", res11, 1U);
|
|---|
| 148 | auto res12 = set2.erase(std::string{"M"});
|
|---|
| 149 | test_eq("erase by key pt2", res12, 0U);
|
|---|
| 150 |
|
|---|
| 151 | std::unordered_set<int> set3{};
|
|---|
| 152 | set3.insert(1);
|
|---|
| 153 | auto res13 = set3.erase(1);
|
|---|
| 154 | test_eq("erase only element by key pt1", res13, 1U);
|
|---|
| 155 | test_eq("erase only element by key pt2", set3.empty(), true);
|
|---|
| 156 |
|
|---|
| 157 | set3.insert(3);
|
|---|
| 158 | auto res14 = set3.erase(set3.begin());
|
|---|
| 159 | test_eq("erase only element by iterator pt1", res14, set3.end());
|
|---|
| 160 | test_eq("erase only element by iterator pt2", set3.empty(), true);
|
|---|
| 161 |
|
|---|
| 162 | set2.clear();
|
|---|
| 163 | test_eq("clear", set2.empty(), true);
|
|---|
| 164 |
|
|---|
| 165 | set3.insert(1);
|
|---|
| 166 | auto res15 = set3.count(1);
|
|---|
| 167 | test_eq("count", res15, 1U);
|
|---|
| 168 |
|
|---|
| 169 | set3.insert(15);
|
|---|
| 170 | auto res16 = set3.find(15);
|
|---|
| 171 | test_eq("find", *res16, 15);
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | void unordered_set_test::test_multi()
|
|---|
| 175 | {
|
|---|
| 176 | auto check_keys = {1, 2, 3, 4, 5, 6, 7};
|
|---|
| 177 | auto check_counts = {1U, 1U, 2U, 1U, 1U, 3U, 1U};
|
|---|
| 178 | auto src1 = {3, 6, 1, 5, 6, 3, 2, 7, 6, 4};
|
|---|
| 179 |
|
|---|
| 180 | std::unordered_multiset<int> mset{src1};
|
|---|
| 181 | test_contains_multi(
|
|---|
| 182 | "multi construction",
|
|---|
| 183 | check_keys.begin(), check_keys.end(),
|
|---|
| 184 | check_counts.begin(), mset
|
|---|
| 185 | );
|
|---|
| 186 |
|
|---|
| 187 | auto res1 = mset.count(6);
|
|---|
| 188 | test_eq("multi count", res1, 3U);
|
|---|
| 189 |
|
|---|
| 190 | auto res2 = mset.emplace(7);
|
|---|
| 191 | test_eq("multi duplicit emplace pt1", *res2, 7);
|
|---|
| 192 | test_eq("multi duplicit emplace pt2", mset.count(7), 2U);
|
|---|
| 193 |
|
|---|
| 194 | auto res3 = mset.emplace(8);
|
|---|
| 195 | test_eq("multi unique emplace pt1", *res3, 8);
|
|---|
| 196 | test_eq("multi unique emplace pt2", mset.count(8), 1U);
|
|---|
| 197 |
|
|---|
| 198 | auto res4 = mset.insert(8);
|
|---|
| 199 | test_eq("multi duplicit insert pt1", *res4, 8);
|
|---|
| 200 | test_eq("multi duplicit insert pt2", mset.count(8), 2U);
|
|---|
| 201 |
|
|---|
| 202 | auto res5 = mset.insert(9);
|
|---|
| 203 | test_eq("multi unique insert pt1", *res5, 9);
|
|---|
| 204 | test_eq("multi unique insert pt2", mset.count(9), 1U);
|
|---|
| 205 |
|
|---|
| 206 | auto res6 = mset.erase(8);
|
|---|
| 207 | test_eq("multi erase by key pt1", res6, 2U);
|
|---|
| 208 | test_eq("multi erase by key pt2", mset.count(8), 0U);
|
|---|
| 209 |
|
|---|
| 210 | auto res7 = mset.erase(mset.find(7));
|
|---|
| 211 | test_eq("multi erase by iterator pt1", *res7, 7);
|
|---|
| 212 | test_eq("multi erase by iterator pt2", mset.count(7), 1U);
|
|---|
| 213 | }
|
|---|
| 214 | }
|
|---|