summaryrefslogtreecommitdiff
path: root/pstl/test/std/algorithms/alg.modifying.operations/rotate.pass.cpp
blob: 5402f792dfb2c226df5cfaee8e761498672cc49b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// -*- C++ -*-
//===-- rotate.pass.cpp ---------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14

#include "support/pstl_test_config.h"

#include <iterator>
#include <execution>
#include <algorithm>

#include "support/utils.h"

using namespace TestUtils;

template <typename T>
struct wrapper
{
    T t;
    int move_count;
    explicit wrapper(T t_) : t(t_), move_count(0) {}
    wrapper&
    operator=(const T& t_)
    {
        t = t_;
        return *this;
    }

    wrapper(const wrapper<T>& a) : move_count(0) { t = a.t; }

    wrapper<T>&
    operator=(wrapper<T>& a)
    {
        t = a.t;
        return *this;
    }

    wrapper<T>&
    operator=(wrapper<T>&& a)
    {
        t = a.t;
        move_count += 1;
        return *this;
    }
};

template <typename T>
struct compare
{
    bool
    operator()(const T& a, const T& b)
    {
        return a == b;
    }
};

template <typename T>
struct compare<wrapper<T>>
{
    bool
    operator()(const wrapper<T>& a, const wrapper<T>& b)
    {
        return a.t == b.t;
    }
};
#include <typeinfo>

struct test_one_policy
{

#if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN ||                                                             \
    _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN // dummy specializations to skip testing in case of broken configuration
    template <typename Iterator, typename Size>
    void
    operator()(pstl::execution::unsequenced_policy, Iterator data_b, Iterator data_e, Iterator actual_b,
               Iterator actual_e, Size shift)
    {
    }
    template <typename Iterator, typename Size>
    void
    operator()(pstl::execution::parallel_unsequenced_policy, Iterator data_b, Iterator data_e, Iterator actual_b,
               Iterator actual_e, Size shift)
    {
    }
#endif

    template <typename ExecutionPolicy, typename Iterator, typename Size>
    void
    operator()(ExecutionPolicy&& exec, Iterator data_b, Iterator data_e, Iterator actual_b, Iterator actual_e,
               Size shift)
    {
        using namespace std;
        using T = typename iterator_traits<Iterator>::value_type;
        Iterator actual_m = std::next(actual_b, shift);

        copy(data_b, data_e, actual_b);
        Iterator actual_return = rotate(exec, actual_b, actual_m, actual_e);

        EXPECT_TRUE(actual_return == std::next(actual_b, std::distance(actual_m, actual_e)), "wrong result of rotate");
        auto comparator = compare<T>();
        bool check = std::equal(actual_return, actual_e, data_b, comparator);
        check = check && std::equal(actual_b, actual_return, std::next(data_b, shift), comparator);

        EXPECT_TRUE(check, "wrong effect of rotate");
        EXPECT_TRUE(check_move(exec, actual_b, actual_e, shift), "wrong move test of rotate");
    }

    template <typename ExecutionPolicy, typename Iterator, typename Size>
    typename std::enable_if<
        is_same_iterator_category<Iterator, std::random_access_iterator_tag>::value &&
            !std::is_same<ExecutionPolicy, std::execution::sequenced_policy>::value &&
            std::is_same<typename std::iterator_traits<Iterator>::value_type, wrapper<float32_t>>::value,
        bool>::type
    check_move(ExecutionPolicy&&, Iterator b, Iterator e, Size shift)
    {
        bool result = all_of(b, e, [](wrapper<float32_t>& a) {
            bool temp = a.move_count > 0;
            a.move_count = 0;
            return temp;
        });
        return shift == 0 || result;
    }

    template <typename ExecutionPolicy, typename Iterator, typename Size>
    typename std::enable_if<
        !(is_same_iterator_category<Iterator, std::random_access_iterator_tag>::value &&
          !std::is_same<ExecutionPolicy, std::execution::sequenced_policy>::value &&
          std::is_same<typename std::iterator_traits<Iterator>::value_type, wrapper<float32_t>>::value),
        bool>::type
    check_move(ExecutionPolicy&&, Iterator, Iterator, Size)
    {
        return true;
    }
};

template <typename T>
void
test()
{
    const int32_t max_len = 100000;

    Sequence<T> actual(max_len, [](std::size_t i) { return T(i); });
    Sequence<T> data(max_len, [](std::size_t i) { return T(i); });

    for (int32_t len = 0; len < max_len; len = len <= 16 ? len + 1 : int32_t(3.1415 * len))
    {
        int32_t shifts[] = {0, 1, 2, len / 3, (2 * len) / 3, len - 1};
        for (auto shift : shifts)
        {
            if (shift >= 0 && shift < len)
            {
                invoke_on_all_policies(test_one_policy(), data.begin(), data.begin() + len, actual.begin(),
                                       actual.begin() + len, shift);
            }
        }
    }
}

int
main()
{
    test<int32_t>();
    test<wrapper<float64_t>>();
    test<MemoryChecker>();
    EXPECT_FALSE(MemoryChecker::alive_objects() < 0, "wrong effect from rotate: number of ctors calls < num of dtors calls");
    EXPECT_FALSE(MemoryChecker::alive_objects() > 0, "wrong effect from rotate: number of ctors calls > num of dtors calls");

    std::cout << done() << std::endl;
    return 0;
}