Debian Codegraph @

libstdc++-v3/testsuite/25_algorithms/pstl/alg_nonmodifying/mismatch.cc

raw
1 // -*- C++ -*-
2 // { dg-options "-ltbb" }
3 // { dg-do run { target c++17 } }
4 // { dg-timeout-factor 3 }
5 // { dg-timeout-factor 5 { target debug_mode } }
6 // { dg-require-effective-target tbb_backend }
7
8 //===-- mismatch.pass.cpp -------------------------------------------------===//
9 //
10 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
11 // See https://llvm.org/LICENSE.txt for license information.
12 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "pstl/pstl_test_config.h"
17
18 #ifdef PSTL_STANDALONE_TESTS
19
20 #include "pstl/execution"
21 #include "pstl/algorithm"
22 #include "pstl/numeric"
23 #include "pstl/memory"
24
25 #else
26 #include <execution>
27 #include <algorithm>
28 #endif // PSTL_STANDALONE_TESTS
29
30 #include "pstl/test_utils.h"
31
32 using namespace TestUtils;
33
34 struct test_mismatch
35 {
36 template <typename Policy, typename Iterator1, typename Iterator2>
37 void
38 operator()(Policy&& exec, Iterator1 first1, Iterator1 last1, Iterator2 first2)
39 {
40 using namespace std;
41 typedef typename iterator_traits<Iterator1>::value_type T;
42 {
43 const auto expected = std::mismatch(first1, last1, first2, std::equal_to<T>());
44 const auto res3 = mismatch(exec, first1, last1, first2, std::equal_to<T>());
45 EXPECT_TRUE(expected == res3, "wrong return result from mismatch");
46 const auto res4 = mismatch(exec, first1, last1, first2);
47 EXPECT_TRUE(expected == res4, "wrong return result from mismatch");
48 }
49 }
50 template <typename Policy, typename Iterator1, typename Iterator2>
51 void
52 operator()(Policy&& exec, Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2)
53 {
54 using namespace std;
55 typedef typename iterator_traits<Iterator1>::value_type T;
56 {
57 const auto expected = mismatch(std::execution::seq, first1, last1, first2, last2, std::equal_to<T>());
58 const auto res1 = mismatch(exec, first1, last1, first2, last2, std::equal_to<T>());
59 EXPECT_TRUE(expected == res1, "wrong return result from mismatch");
60 const auto res2 = mismatch(exec, first1, last1, first2, last2);
61 EXPECT_TRUE(expected == res2, "wrong return result from mismatch");
62 }
63 }
64 };
65
66 template <typename T>
67 void
68 test_mismatch_by_type()
69 {
70 using namespace std;
71 for (size_t size = 0; size <= 100000; size = size <= 16 ? size + 1 : size_t(3.1415 * size))
72 {
73 const T val = T(-1);
74 Sequence<T> in(size, [](size_t v) -> T { return T(v % 100); });
75 {
76 Sequence<T> in2(in);
77 invoke_on_all_policies(test_mismatch(), in.begin(), in.end(), in2.begin(), in2.end());
78 invoke_on_all_policies(test_mismatch(), in.begin(), in.end(), in2.begin());
79
80 const size_t min_size = 3;
81 if (size > min_size)
82 {
83 const size_t idx_for_1 = size / min_size;
84 in[idx_for_1] = val, in[idx_for_1 + 1] = val, in[idx_for_1 + 2] = val;
85 invoke_on_all_policies(test_mismatch(), in.begin(), in.end(), in2.begin(), in2.end());
86 invoke_on_all_policies(test_mismatch(), in.begin(), in.end(), in2.begin());
87 }
88
89 const size_t idx_for_2 = 500;
90 if (size >= idx_for_2 - 1)
91 {
92 in2[size / idx_for_2] = val;
93 invoke_on_all_policies(test_mismatch(), in.cbegin(), in.cend(), in2.cbegin(), in2.cend());
94 invoke_on_all_policies(test_mismatch(), in.cbegin(), in.cend(), in2.cbegin());
95 }
96 }
97 {
98 Sequence<T> in2(100, [](size_t v) -> T { return T(v); });
99 invoke_on_all_policies(test_mismatch(), in2.begin(), in2.end(), in.begin(), in.end());
100 // We can't call std::mismatch with semantic below when size of second sequence less than size of first sequence
101 if (in2.size() <= in.size())
102 invoke_on_all_policies(test_mismatch(), in2.begin(), in2.end(), in.begin());
103
104 const size_t idx = 97;
105 in2[idx] = val;
106 in2[idx + 1] = val;
107 invoke_on_all_policies(test_mismatch(), in.cbegin(), in.cend(), in2.cbegin(), in2.cend());
108 if (in.size() <= in2.size())
109 invoke_on_all_policies(test_mismatch(), in.cbegin(), in.cend(), in2.cbegin());
110 }
111 {
112 Sequence<T> in2({});
113 invoke_on_all_policies(test_mismatch(), in2.begin(), in2.end(), in.begin(), in.end());
114
115 invoke_on_all_policies(test_mismatch(), in.cbegin(), in.cend(), in2.cbegin(), in2.cend());
116 if (in.size() == 0)
117 invoke_on_all_policies(test_mismatch(), in.cbegin(), in.cend(), in2.cbegin());
118 }
119 }
120 }
121
122 template <typename T>
123 struct test_non_const
124 {
125 template <typename Policy, typename FirstIterator, typename SecondInterator>
126 void
127 operator()(Policy&& exec, FirstIterator first_iter, SecondInterator second_iter)
128 {
129 mismatch(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::less<T>()));
130 }
131 };
132
133 int
134 main()
135 {
136
137 test_mismatch_by_type<int32_t>();
138 test_mismatch_by_type<float64_t>();
139 test_mismatch_by_type<Wrapper<int32_t>>();
140
141 test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>());
142
143 std::cout << done() << std::endl;
144 return 0;
145 }