| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2018 Jaroslav Jindrak
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | #ifndef LIBCPP_BITS_TEST
|
|---|
| 8 | #define LIBCPP_BITS_TEST
|
|---|
| 9 |
|
|---|
| 10 | #include <utility>
|
|---|
| 11 | #include <iterator>
|
|---|
| 12 | #include <type_traits>
|
|---|
| 13 |
|
|---|
| 14 | namespace std::test
|
|---|
| 15 | {
|
|---|
| 16 | class test_suite
|
|---|
| 17 | {
|
|---|
| 18 | public:
|
|---|
| 19 | virtual bool run(bool) = 0;
|
|---|
| 20 | virtual const char* name() = 0;
|
|---|
| 21 |
|
|---|
| 22 | virtual ~test_suite() = default;
|
|---|
| 23 |
|
|---|
| 24 | unsigned int get_failed() const noexcept;
|
|---|
| 25 |
|
|---|
| 26 | unsigned int get_succeeded() const noexcept;
|
|---|
| 27 |
|
|---|
| 28 | protected:
|
|---|
| 29 | void report(bool, const char*);
|
|---|
| 30 | void start();
|
|---|
| 31 | bool end();
|
|---|
| 32 |
|
|---|
| 33 | unsigned int failed_{};
|
|---|
| 34 | unsigned int succeeded_{};
|
|---|
| 35 | bool ok_{true};
|
|---|
| 36 | bool report_{true};
|
|---|
| 37 |
|
|---|
| 38 | void test(const char* tname, bool expr)
|
|---|
| 39 | {
|
|---|
| 40 | test_eq(tname, expr, true);
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | template<class... Args>
|
|---|
| 44 | void test_eq(const char* tname, Args&&... args)
|
|---|
| 45 | {
|
|---|
| 46 | if (!assert_eq(std::forward<Args>(args)...))
|
|---|
| 47 | {
|
|---|
| 48 | report(false, tname);
|
|---|
| 49 | ++failed_;
|
|---|
| 50 | ok_ = false;
|
|---|
| 51 | }
|
|---|
| 52 | else
|
|---|
| 53 | {
|
|---|
| 54 | report(true, tname);
|
|---|
| 55 | ++succeeded_;
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | template<class T, class U>
|
|---|
| 60 | bool assert_eq(const T& lhs, const U& rhs)
|
|---|
| 61 | {
|
|---|
| 62 | return lhs == rhs;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | template<class Iterator1, class Iterator2>
|
|---|
| 66 | bool assert_eq(Iterator1 first1, Iterator1 last1,
|
|---|
| 67 | Iterator2 first2, Iterator2 last2)
|
|---|
| 68 | {
|
|---|
| 69 | auto len1 = std::distance(first1, last1);
|
|---|
| 70 | auto len2 = std::distance(first2, last2);
|
|---|
| 71 |
|
|---|
| 72 | using common_t = std::common_type_t<decltype(len1), decltype(len2)>;
|
|---|
| 73 | auto len1_common = static_cast<common_t>(len1);
|
|---|
| 74 | auto len2_common = static_cast<common_t>(len2);
|
|---|
| 75 |
|
|---|
| 76 | if (len1_common != len2_common)
|
|---|
| 77 | return false;
|
|---|
| 78 |
|
|---|
| 79 | while (first1 != last1)
|
|---|
| 80 | {
|
|---|
| 81 | if (*first1++ != *first2++)
|
|---|
| 82 | return false;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | return true;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | template<class Iterator, class Container>
|
|---|
| 89 | void test_contains(const char* tname, Iterator first,
|
|---|
| 90 | Iterator last, const Container& cont)
|
|---|
| 91 | {
|
|---|
| 92 | if (!assert_contains(first, last, cont))
|
|---|
| 93 | {
|
|---|
| 94 | report(false, tname);
|
|---|
| 95 | ++failed_;
|
|---|
| 96 | ok_ = false;
|
|---|
| 97 | }
|
|---|
| 98 | else
|
|---|
| 99 | {
|
|---|
| 100 | report(true, tname);
|
|---|
| 101 | ++succeeded_;
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | template<class Iterator1, class Iterator2, class Container>
|
|---|
| 106 | void test_contains_multi(const char* tname, Iterator1 first1,
|
|---|
| 107 | Iterator1 last1, Iterator2 first2,
|
|---|
| 108 | const Container& cont)
|
|---|
| 109 | {
|
|---|
| 110 | if (!assert_contains_multi(first1, last1, first2, cont))
|
|---|
| 111 | {
|
|---|
| 112 | report(false, tname);
|
|---|
| 113 | ++failed_;
|
|---|
| 114 | ok_ = false;
|
|---|
| 115 | }
|
|---|
| 116 | else
|
|---|
| 117 | {
|
|---|
| 118 | report(true, tname);
|
|---|
| 119 | ++succeeded_;
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | template<class Iterator, class Container>
|
|---|
| 124 | bool assert_contains(Iterator first, Iterator last, const Container& cont)
|
|---|
| 125 | {
|
|---|
| 126 | while (first != last)
|
|---|
| 127 | {
|
|---|
| 128 | if (cont.find(*first++) == cont.end())
|
|---|
| 129 | return false;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | return true;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | template<class Iterator1, class Iterator2, class Container>
|
|---|
| 136 | bool assert_contains_multi(Iterator1 first1, Iterator1 last1,
|
|---|
| 137 | Iterator2 first2, const Container& cont)
|
|---|
| 138 | {
|
|---|
| 139 | while (first1 != last1)
|
|---|
| 140 | {
|
|---|
| 141 | if (cont.count(*first1++) != *first2++)
|
|---|
| 142 | return false;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | return true;
|
|---|
| 146 | }
|
|---|
| 147 | };
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | #endif
|
|---|