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

source code of pstl/test/std/algorithms/alg.nonmodifying/mismatch.pass.cpp