1 // -*- C++ -*-
2 //===-- includes.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 <algorithm>
15 #include <cmath>
16 #include <execution>
17 #include <functional>
18
19 #include "support/utils.h"
20
21 using namespace TestUtils;
22
23 template <typename T>
24 struct Num
25 {
26 T val;
NumNum27 explicit Num(const T& v) : val(v) {}
28
29 //for "includes" checks
30 template <typename T1>
31 bool
operator <Num32 operator<(const Num<T1>& v1) const
33 {
34 return val < v1.val;
35 }
36
37 //The types Type1 and Type2 must be such that an object of type InputIt can be dereferenced and then implicitly converted to both of them
38 template <typename T1>
operator Num<T1>Num39 operator Num<T1>() const
40 {
41 return Num<T1>((T1)val);
42 }
43 };
44
45 struct test_one_policy
46 {
47 template <typename Policy, typename InputIterator1, typename InputIterator2, typename Compare>
48 typename std::enable_if<!TestUtils::isReverse<InputIterator1>::value, void>::type
operator ()test_one_policy49 operator()(Policy&& exec, InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2,
50 Compare comp)
51 {
52
53 auto expect_res = std::includes(first1, last1, first2, last2, comp);
54 auto res = std::includes(exec, first1, last1, first2, last2, comp);
55
56 EXPECT_TRUE(expect_res == res, "wrong result for includes");
57 }
58
59 template <typename Policy, typename InputIterator1, typename InputIterator2, typename Compare>
60 typename std::enable_if<TestUtils::isReverse<InputIterator1>::value, void>::type
operator ()test_one_policy61 operator()(Policy&&, InputIterator1, InputIterator1, InputIterator2, InputIterator2, Compare)
62 {
63 }
64 };
65
66 template <typename T1, typename T2, typename Compare>
67 void
test_includes(Compare compare)68 test_includes(Compare compare)
69 {
70
71 const std::size_t n_max = 1000000;
72
73 // The rand()%(2*n+1) encourages generation of some duplicates.
74 std::srand(42);
75
76 for (std::size_t n = 0; n < n_max; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
77 {
78 for (std::size_t m = 0; m < n_max; m = m <= 16 ? m + 1 : size_t(2.71828 * m))
79 {
80 //prepare the input ranges
81 Sequence<T1> in1(n, [](std::size_t k) { return rand() % (2 * k + 1); });
82 Sequence<T2> in2(m, [](std::size_t k) { return rand() % (k + 1); });
83
84 std::sort(in1.begin(), in1.end(), compare);
85 std::sort(in2.begin(), in2.end(), compare);
86
87 invoke_on_all_policies(test_one_policy(), in1.begin(), in1.end(), in2.cbegin(), in2.cend(), compare);
88
89 //test w/ non constant predicate
90 if (n < 5 && m < 5)
91 invoke_on_all_policies(test_one_policy(), in1.begin(), in1.end(), in2.cbegin(), in2.cend(),
92 non_const(compare));
93 }
94 }
95 }
96
97 int
main()98 main()
99 {
100
101 test_includes<float64_t, float64_t>(std::less<>());
102 test_includes<Num<int64_t>, Num<int32_t>>([](const Num<int64_t>& x, const Num<int32_t>& y) { return x < y; });
103 std::cout << done() << std::endl;
104
105 return 0;
106 }
107