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