about summary refs log tree commit diff
path: root/third_party/immer/test/util.hpp
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2020-07-15T07·20+0100
committerVincent Ambo <mail@tazj.in>2020-07-15T07·23+0100
commit1213b086a1015a662ab7ebd658f784534fd3116a (patch)
treed3bc8f3b7f40b8b60f0ef6fbd649cf765f4fdfb6 /third_party/immer/test/util.hpp
parent1390827b9ea1e04bc9863e48930bfd16db3b716e (diff)
parent7f19d641647ac4ef313ed88d6b5c140983ce5436 (diff)
merge(3p/immer): Subtree merge at 'ad3e3556d' as 'third_party/immer' r/1299
Change-Id: I9636a41ad44b4218293833fd3e9456d9b07c731b
Diffstat (limited to 'third_party/immer/test/util.hpp')
-rw-r--r--third_party/immer/test/util.hpp103
1 files changed, 103 insertions, 0 deletions
diff --git a/third_party/immer/test/util.hpp b/third_party/immer/test/util.hpp
new file mode 100644
index 000000000000..41904f5a8907
--- /dev/null
+++ b/third_party/immer/test/util.hpp
@@ -0,0 +1,103 @@
+//
+// immer: immutable data structures for C++
+// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente
+//
+// This software is distributed under the Boost Software License, Version 1.0.
+// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt
+//
+
+#pragma once
+
+#include <boost/range/irange.hpp>
+#include <boost/range/join.hpp>
+#include <cstddef>
+
+namespace {
+
+struct identity_t
+{
+    template <typename T>
+    decltype(auto) operator()(T&& x)
+    {
+        return std::forward<decltype(x)>(x);
+    }
+};
+
+template <typename Integer>
+auto test_irange(Integer from, Integer to)
+{
+#if IMMER_SLOW_TESTS
+    return boost::irange(from, to);
+#else
+    if (to - from < Integer{10})
+        return boost::join(boost::irange(Integer{}, Integer{}),
+                           boost::join(boost::irange(from, to, 1),
+                                       boost::irange(Integer{}, Integer{})));
+    else
+        return boost::join(boost::irange(from, from + Integer{2}),
+                           boost::join(boost::irange(from + Integer{2},
+                                                     to - Integer{2},
+                                                     (to - from) / Integer{5}),
+                                       boost::irange(to - Integer{2}, to)));
+#endif
+}
+
+} // anonymous namespace
+
+#if IMMER_SLOW_TESTS
+#define CHECK_SLOW(...) CHECK(__VA_ARGS__)
+#else
+#define CHECK_SLOW(...)
+#endif
+
+#if IMMER_SLOW_TESTS
+#define CHECK_VECTOR_EQUALS_RANGE_AUX(v1_, first_, last_, xf_)                 \
+    [](auto&& v1, auto&& first, auto&& last, auto&& xf) {                      \
+        auto size = std::distance(first, last);                                \
+        CHECK(static_cast<std::ptrdiff_t>(v1.size()) == size);                 \
+        if (static_cast<std::ptrdiff_t>(v1.size()) != size)                    \
+            return;                                                            \
+        for (auto j = 0u; j < size; ++j)                                       \
+            CHECK(xf(v1[j]) == xf(*first++));                                  \
+    }(v1_, first_, last_, xf_) // CHECK_EQUALS
+#else
+#define CHECK_VECTOR_EQUALS_RANGE_AUX(v1_, first_, last_, ...)                 \
+    [](auto&& v1, auto&& first, auto&& last, auto&& xf) {                      \
+        auto size = std::distance(first, last);                                \
+        CHECK(static_cast<std::ptrdiff_t>(v1.size()) == size);                 \
+        if (static_cast<std::ptrdiff_t>(v1.size()) != size)                    \
+            return;                                                            \
+        if (size > 0) {                                                        \
+            CHECK(xf(v1[0]) == xf(*(first + (0))));                            \
+            CHECK(xf(v1[size - 1]) == xf(*(first + (size - 1))));              \
+            CHECK(xf(v1[size / 2]) == xf(*(first + (size / 2))));              \
+            CHECK(xf(v1[size / 3]) == xf(*(first + (size / 3))));              \
+            CHECK(xf(v1[size / 4]) == xf(*(first + (size / 4))));              \
+            CHECK(xf(v1[size - 1 - size / 2]) ==                               \
+                  xf(*(first + (size - 1 - size / 2))));                       \
+            CHECK(xf(v1[size - 1 - size / 3]) ==                               \
+                  xf(*(first + (size - 1 - size / 3))));                       \
+            CHECK(xf(v1[size - 1 - size / 4]) ==                               \
+                  xf(*(first + (size - 1 - size / 4))));                       \
+        }                                                                      \
+        if (size > 1) {                                                        \
+            CHECK(xf(v1[1]) == xf(*(first + (1))));                            \
+            CHECK(xf(v1[size - 2]) == xf(*(first + (size - 2))));              \
+        }                                                                      \
+        if (size > 2) {                                                        \
+            CHECK(xf(v1[2]) == xf(*(first + (2))));                            \
+            CHECK(xf(v1[size - 3]) == xf(*(first + (size - 3))));              \
+        }                                                                      \
+    }(v1_, first_, last_, __VA_ARGS__) // CHECK_EQUALS
+#endif                                 // IMMER_SLOW_TESTS
+
+#define CHECK_VECTOR_EQUALS_AUX(v1_, v2_, ...)                                 \
+    [](auto&& v1, auto&& v2, auto&&... xs) {                                   \
+        CHECK_VECTOR_EQUALS_RANGE_AUX(v1, v2.begin(), v2.end(), xs...);        \
+    }(v1_, v2_, __VA_ARGS__)
+
+#define CHECK_VECTOR_EQUALS_RANGE(v1, b, e)                                    \
+    CHECK_VECTOR_EQUALS_RANGE_AUX((v1), (b), (e), identity_t{})
+
+#define CHECK_VECTOR_EQUALS(v1, v2)                                            \
+    CHECK_VECTOR_EQUALS_AUX((v1), (v2), identity_t{})