Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorMartin Hořeňovský2023-02-05 16:40:38 +0100
committerMartin Hořeňovský2023-02-06 15:29:01 +0100
commit772fa3f7902b534d59e5fe4306e3cc6a255a6508 (patch)
tree8d2b82030f4c091a982c970e999416bf1310ba97 /src
parentf3c0a3cd091aba8341a77e5c8580e5697f0e6892 (diff)
downloadcatch3-772fa3f7902b534d59e5fe4306e3cc6a255a6508.tar.gz
Add Catch::Detail::is_permutation that supports sentinels
Also split out helpers for testing matcher ranges (types whose begin/end/empty/etc require ADL lookup, types whose iteration uses iterator + sentinel pair, etc) into their own file.
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/catch2/catch_all.hpp1
-rw-r--r--src/catch2/internal/catch_is_permutation.hpp138
-rw-r--r--src/catch2/meson.build1
4 files changed, 141 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 1e1e31b7..fd05dbdd 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -88,6 +88,7 @@ set(IMPL_HEADERS
${SOURCES_DIR}/internal/catch_floating_point_helpers.hpp
${SOURCES_DIR}/internal/catch_getenv.hpp
${SOURCES_DIR}/internal/catch_istream.hpp
+ ${SOURCES_DIR}/internal/catch_is_permutation.hpp
${SOURCES_DIR}/internal/catch_lazy_expr.hpp
${SOURCES_DIR}/internal/catch_leak_detector.hpp
${SOURCES_DIR}/internal/catch_list.hpp
diff --git a/src/catch2/catch_all.hpp b/src/catch2/catch_all.hpp
index a0bf7605..be146421 100644
--- a/src/catch2/catch_all.hpp
+++ b/src/catch2/catch_all.hpp
@@ -70,6 +70,7 @@
#include <catch2/internal/catch_fatal_condition_handler.hpp>
#include <catch2/internal/catch_floating_point_helpers.hpp>
#include <catch2/internal/catch_getenv.hpp>
+#include <catch2/internal/catch_is_permutation.hpp>
#include <catch2/internal/catch_istream.hpp>
#include <catch2/internal/catch_lazy_expr.hpp>
#include <catch2/internal/catch_leak_detector.hpp>
diff --git a/src/catch2/internal/catch_is_permutation.hpp b/src/catch2/internal/catch_is_permutation.hpp
new file mode 100644
index 00000000..708053d3
--- /dev/null
+++ b/src/catch2/internal/catch_is_permutation.hpp
@@ -0,0 +1,138 @@
+
+// Copyright Catch2 Authors
+// Distributed under the Boost Software License, Version 1.0.
+// (See accompanying file LICENSE.txt or copy at
+// https://www.boost.org/LICENSE_1_0.txt)
+
+// SPDX-License-Identifier: BSL-1.0
+#ifndef CATCH_IS_PERMUTATION_HPP_INCLUDED
+#define CATCH_IS_PERMUTATION_HPP_INCLUDED
+
+#include <algorithm>
+#include <iterator>
+
+namespace Catch {
+ namespace Detail {
+
+ template <typename ForwardIter,
+ typename Sentinel,
+ typename T,
+ typename Comparator>
+ ForwardIter find_sentinel( ForwardIter start,
+ Sentinel sentinel,
+ T const& value,
+ Comparator cmp ) {
+ while ( start != sentinel ) {
+ if ( cmp( *start, value ) ) { break; }
+ ++start;
+ }
+ return start;
+ }
+
+ template <typename ForwardIter,
+ typename Sentinel,
+ typename T,
+ typename Comparator>
+ std::ptrdiff_t count_sentinel( ForwardIter start,
+ Sentinel sentinel,
+ T const& value,
+ Comparator cmp ) {
+ std::ptrdiff_t count = 0;
+ while ( start != sentinel ) {
+ if ( cmp( *start, value ) ) { ++count; }
+ ++start;
+ }
+ return count;
+ }
+
+ template <typename ForwardIter, typename Sentinel>
+ std::enable_if_t<!std::is_same<ForwardIter, Sentinel>::value,
+ std::ptrdiff_t>
+ sentinel_distance( ForwardIter iter, const Sentinel sentinel ) {
+ std::ptrdiff_t dist = 0;
+ while ( iter != sentinel ) {
+ ++iter;
+ ++dist;
+ }
+ return dist;
+ }
+
+ template <typename ForwardIter>
+ std::ptrdiff_t sentinel_distance( ForwardIter first,
+ ForwardIter last ) {
+ return std::distance( first, last );
+ }
+
+ template <typename ForwardIter1,
+ typename Sentinel1,
+ typename ForwardIter2,
+ typename Sentinel2,
+ typename Comparator>
+ bool check_element_counts( ForwardIter1 first_1,
+ const Sentinel1 end_1,
+ ForwardIter2 first_2,
+ const Sentinel2 end_2,
+ Comparator cmp ) {
+ auto cursor = first_1;
+ while ( cursor != end_1 ) {
+ if ( find_sentinel( first_1, cursor, *cursor, cmp ) ==
+ cursor ) {
+ // we haven't checked this element yet
+ const auto count_in_range_2 =
+ count_sentinel( first_2, end_2, *cursor, cmp );
+ // Not a single instance in 2nd range, so it cannot be a
+ // permutation of 1st range
+ if ( count_in_range_2 == 0 ) { return false; }
+
+ const auto count_in_range_1 =
+ count_sentinel( cursor, end_1, *cursor, cmp );
+ if ( count_in_range_1 != count_in_range_2 ) {
+ return false;
+ }
+ }
+
+ ++cursor;
+ }
+
+ return true;
+ }
+
+ template <typename ForwardIter1,
+ typename Sentinel1,
+ typename ForwardIter2,
+ typename Sentinel2,
+ typename Comparator>
+ bool is_permutation( ForwardIter1 first_1,
+ const Sentinel1 end_1,
+ ForwardIter2 first_2,
+ const Sentinel2 end_2,
+ Comparator cmp ) {
+ // TODO: no optimization for stronger iterators, because we would also have to constrain on sentinel vs not sentinel types
+ // TODO: Comparator has to be "both sides", e.g. a == b => b == a
+ // This skips shared prefix of the two ranges
+ while (first_1 != end_1 && first_2 != end_2 && cmp(*first_1, *first_2)) {
+ ++first_1;
+ ++first_2;
+ }
+
+ // We need to handle case where at least one of the ranges has no more elements
+ if (first_1 == end_1 || first_2 == end_2) {
+ return first_1 == end_1 && first_2 == end_2;
+ }
+
+ // pair counting is n**2, so we pay linear walk to compare the sizes first
+ auto dist_1 = sentinel_distance( first_1, end_1 );
+ auto dist_2 = sentinel_distance( first_2, end_2 );
+
+ if (dist_1 != dist_2) { return false; }
+
+ // Since we do not try to handle stronger iterators pair (e.g.
+ // bidir) optimally, the only thing left to do is to check counts in
+ // the remaining ranges.
+ return check_element_counts( first_1, end_1, first_2, end_2, cmp );
+ }
+
+ } // namespace Detail
+} // namespace Catch
+
+#endif // CATCH_IS_PERMUTATION_HPP_INCLUDED
diff --git a/src/catch2/meson.build b/src/catch2/meson.build
index b32f72d2..0e114065 100644
--- a/src/catch2/meson.build
+++ b/src/catch2/meson.build
@@ -93,6 +93,7 @@ internal_headers = [
'internal/catch_floating_point_helpers.hpp',
'internal/catch_getenv.hpp',
'internal/catch_istream.hpp',
+ 'internal/catch_is_permutation.hpp',
'internal/catch_lazy_expr.hpp',
'internal/catch_leak_detector.hpp',
'internal/catch_list.hpp',