| 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 | #ifndef LIBCPP_TESTS
|
|---|
| 30 | #define LIBCPP_TESTS
|
|---|
| 31 |
|
|---|
| 32 | #include <cstdio>
|
|---|
| 33 | #include <internal/test/test.hpp>
|
|---|
| 34 | #include <vector>
|
|---|
| 35 |
|
|---|
| 36 | namespace std::test
|
|---|
| 37 | {
|
|---|
| 38 | class test_set
|
|---|
| 39 | {
|
|---|
| 40 | public:
|
|---|
| 41 | test_set() = default;
|
|---|
| 42 |
|
|---|
| 43 | template<class T>
|
|---|
| 44 | void add()
|
|---|
| 45 | {
|
|---|
| 46 | tests_.push_back(new T{});
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | bool run(bool report)
|
|---|
| 50 | {
|
|---|
| 51 | bool res{true};
|
|---|
| 52 | unsigned int succeeded{};
|
|---|
| 53 | unsigned int failed{};
|
|---|
| 54 |
|
|---|
| 55 | for (auto test: tests_)
|
|---|
| 56 | {
|
|---|
| 57 | res &= test->run(report);
|
|---|
| 58 | succeeded += test->get_succeeded();
|
|---|
| 59 | failed += test->get_failed();
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | if (report)
|
|---|
| 63 | {
|
|---|
| 64 | std::printf("\n");
|
|---|
| 65 | if (res)
|
|---|
| 66 | std::printf("[TESTS SUCCEEDED!]");
|
|---|
| 67 | else
|
|---|
| 68 | std::printf("[TESTS FAILED]");
|
|---|
| 69 | std::printf("[%u OK][%u FAIL][%u TOTAL]\n",
|
|---|
| 70 | succeeded, failed, (succeeded + failed));
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | return res;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | ~test_set()
|
|---|
| 77 | {
|
|---|
| 78 | // TODO: Gimme unique_ptr!
|
|---|
| 79 | for (auto ptr: tests_)
|
|---|
| 80 | delete ptr;
|
|---|
| 81 | }
|
|---|
| 82 | private:
|
|---|
| 83 | std::vector<test_suite*> tests_{};
|
|---|
| 84 | };
|
|---|
| 85 |
|
|---|
| 86 | class array_test: public test_suite
|
|---|
| 87 | {
|
|---|
| 88 | public:
|
|---|
| 89 | bool run(bool) override;
|
|---|
| 90 | const char* name() override;
|
|---|
| 91 |
|
|---|
| 92 | array_test() = default;
|
|---|
| 93 | ~array_test() = default;
|
|---|
| 94 | };
|
|---|
| 95 |
|
|---|
| 96 | class vector_test: public test_suite
|
|---|
| 97 | {
|
|---|
| 98 | public:
|
|---|
| 99 | bool run(bool) override;
|
|---|
| 100 | const char* name() override;
|
|---|
| 101 |
|
|---|
| 102 | vector_test() = default;
|
|---|
| 103 | ~vector_test() = default;
|
|---|
| 104 |
|
|---|
| 105 | private:
|
|---|
| 106 | void test_construction_and_assignment();
|
|---|
| 107 | void test_insert();
|
|---|
| 108 | void test_erase();
|
|---|
| 109 | };
|
|---|
| 110 |
|
|---|
| 111 | class string_test: public test_suite
|
|---|
| 112 | {
|
|---|
| 113 | public:
|
|---|
| 114 | bool run(bool) override;
|
|---|
| 115 | const char* name() override;
|
|---|
| 116 |
|
|---|
| 117 | private:
|
|---|
| 118 | void test_construction_and_assignment();
|
|---|
| 119 | void test_append();
|
|---|
| 120 | void test_insert();
|
|---|
| 121 | void test_erase();
|
|---|
| 122 | void test_replace();
|
|---|
| 123 | void test_copy();
|
|---|
| 124 | void test_find();
|
|---|
| 125 | void test_substr();
|
|---|
| 126 | void test_compare();
|
|---|
| 127 | };
|
|---|
| 128 |
|
|---|
| 129 | class bitset_test: public test_suite
|
|---|
| 130 | {
|
|---|
| 131 | public:
|
|---|
| 132 | bool run(bool) override;
|
|---|
| 133 | const char* name() override;
|
|---|
| 134 |
|
|---|
| 135 | private:
|
|---|
| 136 | void test_constructors_and_assignment();
|
|---|
| 137 | void test_strings();
|
|---|
| 138 | void test_operations();
|
|---|
| 139 | };
|
|---|
| 140 |
|
|---|
| 141 | class deque_test: public test_suite
|
|---|
| 142 | {
|
|---|
| 143 | public:
|
|---|
| 144 | bool run(bool) override;
|
|---|
| 145 | const char* name() override;
|
|---|
| 146 |
|
|---|
| 147 | private:
|
|---|
| 148 | void test_constructors_and_assignment();
|
|---|
| 149 | void test_resizing();
|
|---|
| 150 | void test_push_pop();
|
|---|
| 151 | void test_operations();
|
|---|
| 152 | };
|
|---|
| 153 |
|
|---|
| 154 | class tuple_test: public test_suite
|
|---|
| 155 | {
|
|---|
| 156 | public:
|
|---|
| 157 | bool run(bool) override;
|
|---|
| 158 | const char* name() override;
|
|---|
| 159 |
|
|---|
| 160 | private:
|
|---|
| 161 | void test_constructors_and_assignment();
|
|---|
| 162 | void test_creation();
|
|---|
| 163 | void test_tie_and_structured_bindings();
|
|---|
| 164 | void test_tuple_ops();
|
|---|
| 165 | };
|
|---|
| 166 |
|
|---|
| 167 | class map_test: public test_suite
|
|---|
| 168 | {
|
|---|
| 169 | public:
|
|---|
| 170 | bool run(bool) override;
|
|---|
| 171 | const char* name() override;
|
|---|
| 172 |
|
|---|
| 173 | private:
|
|---|
| 174 | void test_constructors_and_assignment();
|
|---|
| 175 | void test_histogram();
|
|---|
| 176 | void test_emplace_insert();
|
|---|
| 177 | void test_bounds_and_ranges();
|
|---|
| 178 | void test_multi();
|
|---|
| 179 | void test_reverse_iterators();
|
|---|
| 180 | void test_multi_bounds_and_ranges();
|
|---|
| 181 | };
|
|---|
| 182 |
|
|---|
| 183 | class set_test: public test_suite
|
|---|
| 184 | {
|
|---|
| 185 | public:
|
|---|
| 186 | bool run(bool) override;
|
|---|
| 187 | const char* name() override;
|
|---|
| 188 |
|
|---|
| 189 | private:
|
|---|
| 190 | void test_constructors_and_assignment();
|
|---|
| 191 | void test_emplace_insert();
|
|---|
| 192 | void test_bounds_and_ranges();
|
|---|
| 193 | void test_multi();
|
|---|
| 194 | void test_reverse_iterators();
|
|---|
| 195 | void test_multi_bounds_and_ranges();
|
|---|
| 196 | };
|
|---|
| 197 |
|
|---|
| 198 | class unordered_map_test: public test_suite
|
|---|
| 199 | {
|
|---|
| 200 | public:
|
|---|
| 201 | bool run(bool) override;
|
|---|
| 202 | const char* name() override;
|
|---|
| 203 |
|
|---|
| 204 | private:
|
|---|
| 205 | void test_constructors_and_assignment();
|
|---|
| 206 | void test_histogram();
|
|---|
| 207 | void test_emplace_insert();
|
|---|
| 208 | void test_multi();
|
|---|
| 209 |
|
|---|
| 210 | template<class Iterator, class Container>
|
|---|
| 211 | void test_contains(const char* tname, Iterator first,
|
|---|
| 212 | Iterator last, const Container& cont)
|
|---|
| 213 | {
|
|---|
| 214 | if (!assert_contains(first, last, cont))
|
|---|
| 215 | {
|
|---|
| 216 | report(false, tname);
|
|---|
| 217 | ++failed_;
|
|---|
| 218 | ok_ = false;
|
|---|
| 219 | }
|
|---|
| 220 | else
|
|---|
| 221 | {
|
|---|
| 222 | report(true, tname);
|
|---|
| 223 | ++succeeded_;
|
|---|
| 224 | }
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | template<class Iterator1, class Iterator2, class Container>
|
|---|
| 228 | void test_contains_multi(const char* tname, Iterator1 first1,
|
|---|
| 229 | Iterator1 last1, Iterator2 first2,
|
|---|
| 230 | const Container& cont)
|
|---|
| 231 | {
|
|---|
| 232 | if (!assert_contains_multi(first1, last1, first2, cont))
|
|---|
| 233 | {
|
|---|
| 234 | report(false, tname);
|
|---|
| 235 | ++failed_;
|
|---|
| 236 | ok_ = false;
|
|---|
| 237 | }
|
|---|
| 238 | else
|
|---|
| 239 | {
|
|---|
| 240 | report(true, tname);
|
|---|
| 241 | ++succeeded_;
|
|---|
| 242 | }
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | template<class Iterator, class Container>
|
|---|
| 246 | bool assert_contains(Iterator first, Iterator last, const Container& cont)
|
|---|
| 247 | {
|
|---|
| 248 | while (first != last)
|
|---|
| 249 | {
|
|---|
| 250 | if (cont.find(*first++) == cont.end())
|
|---|
| 251 | return false;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | return true;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | template<class Iterator1, class Iterator2, class Container>
|
|---|
| 258 | bool assert_contains_multi(Iterator1 first1, Iterator1 last1,
|
|---|
| 259 | Iterator2 first2, const Container& cont)
|
|---|
| 260 | {
|
|---|
| 261 | while (first1 != last1)
|
|---|
| 262 | {
|
|---|
| 263 | if (cont.count(*first1++) != *first2++)
|
|---|
| 264 | return false;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | return true;
|
|---|
| 268 | }
|
|---|
| 269 | };
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | #endif
|
|---|