File: partial_sort_copy.cpp

package info (click to toggle)
range-v3 0.12.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,620 kB
  • sloc: cpp: 76,839; xml: 226; sh: 89; python: 34; makefile: 16; perl: 15
file content (198 lines) | stat: -rw-r--r-- 5,478 bytes parent folder | download | duplicates (2)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Range v3 library
//
//  Copyright Eric Niebler 2014-present
//
//  Use, modification and distribution is subject to the
//  Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ericniebler/range-v3

//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include <algorithm>
#include <memory>
#include <random>
#include <vector>
#include <range/v3/core.hpp>
#include <range/v3/algorithm/partial_sort_copy.hpp>
#include "../simple_test.hpp"
#include "../test_utils.hpp"
#include "../test_iterators.hpp"

RANGES_DIAGNOSTIC_IGNORE_GLOBAL_CONSTRUCTORS
RANGES_DIAGNOSTIC_IGNORE_SIGN_CONVERSION

namespace
{
    std::mt19937 gen;

    template<class Iter>
    void
    test_larger_sorts(int N, int M)
    {
        auto partial_sort_copy = ::make_testable_2<true, false>(ranges::partial_sort_copy);
        int* input = new int[N];
        int* output = new int[M];
        for (int i = 0; i < N; ++i)
            input[i] = i;
        std::shuffle(input, input+N, gen);
        partial_sort_copy(Iter(input), Iter(input+N), output, output+M).check([&](int* r)
        {
            int* e = output + std::min(N, M);
            CHECK(r == e);
            int i = 0;
            for (int* x = output; x < e; ++x, ++i)
                CHECK(*x == i);
            std::shuffle(input, input+N, gen);
        });
        partial_sort_copy(Iter(input), Iter(input+N), output, output+M, std::greater<int>()).check([&](int* r)
        {
            int* e = output + std::min(N, M);
            CHECK(r == e);
            int i = N-1;
            for (int* x = output; x < e; ++x, --i)
                CHECK(*x == i);
            std::shuffle(input, input+N, gen);
        });
        delete [] output;
        delete [] input;
    }

    template<class Iter>
    void
    test_larger_sorts(int N)
    {
        test_larger_sorts<Iter>(N, 0);
        test_larger_sorts<Iter>(N, 1);
        test_larger_sorts<Iter>(N, 2);
        test_larger_sorts<Iter>(N, 3);
        test_larger_sorts<Iter>(N, N/2-1);
        test_larger_sorts<Iter>(N, N/2);
        test_larger_sorts<Iter>(N, N/2+1);
        test_larger_sorts<Iter>(N, N-2);
        test_larger_sorts<Iter>(N, N-1);
        test_larger_sorts<Iter>(N, N);
        test_larger_sorts<Iter>(N, N+1000);
    }

    template<class Iter>
    void
    test()
    {
        test_larger_sorts<Iter>(0, 100);
        test_larger_sorts<Iter>(10);
        test_larger_sorts<Iter>(256);
        test_larger_sorts<Iter>(257);
        test_larger_sorts<Iter>(499);
        test_larger_sorts<Iter>(500);
        test_larger_sorts<Iter>(997);
        test_larger_sorts<Iter>(1000);
        test_larger_sorts<Iter>(1009);
    }

    struct S
    {
        int i;
    };

    struct U
    {
        int i;
        U & operator=(S s)
        {
            i = s.i;
            return *this;
        }
    };
}

constexpr bool test_constexpr()
{
    using namespace ranges;
    using IL = std::initializer_list<int>;
    int output[9] = {0};
    int * r = partial_sort_copy(IL{5, 3, 4, 1, 8, 2, 6, 7, 0, 9}, output, less{});
    int * e = output + 9;
    STATIC_CHECK_RETURN(r == e);

    int i = 0;
    for(int * x = output; x < e; ++x, ++i)
    {
        STATIC_CHECK_RETURN(*x == i);
    }
    return true;
}

int main()
{
    int i = 0;
    int * r = ranges::partial_sort_copy(&i, &i, &i, &i+5);
    CHECK(r == &i);
    CHECK(i == 0);
    test<InputIterator<const int*> >();
    test<ForwardIterator<const int*> >();
    test<BidirectionalIterator<const int*> >();
    test<RandomAccessIterator<const int*> >();
    test<const int*>();

    // Check projections
    {
        constexpr int N = 256;
        constexpr int M = N/2-1;
        S input[N];
        U output[M];
        for (int j = 0; j < N; ++j)
            input[j].i = j;
        std::shuffle(input, input+N, gen);
        U * r2 = ranges::partial_sort_copy(input, output, std::less<int>(), &S::i, &U::i);
        U* e = output + std::min(N, M);
        CHECK(r2 == e);
        int i2 = 0;
        for (U* x = output; x < e; ++x, ++i2)
            CHECK(x->i == i2);
    }

    // Check rvalue ranges
    {
        constexpr int N = 256;
        constexpr int M = N/2-1;
        S input[N];
        U output[M];
        for (int j = 0; j < N; ++j)
            input[j].i = j;
        std::shuffle(input, input+N, gen);
        auto r0 = ranges::partial_sort_copy(input, std::move(output), std::less<int>(), &S::i, &U::i);
        U* e = output + std::min(N, M);
#ifndef RANGES_WORKAROUND_MSVC_573728
        CHECK(::is_dangling(r0));
#endif // RANGES_WORKAROUND_MSVC_573728

        int i2 = 0;
        for (U* x = output; x < e; ++x, ++i2)
            CHECK(x->i == i2);

        std::vector<U> vec(M);
        auto r1 = ranges::partial_sort_copy(input, std::move(vec), std::less<int>(), &S::i, &U::i);
        e = vec.data() + std::min(N, M);
        CHECK(::is_dangling(r1));

        i2 = 0;
        for (U* x = vec.data(); x < e; ++x, ++i2)
            CHECK(x->i == i2);
    }

    {
        STATIC_CHECK(test_constexpr());
    }

    return ::test_result();
}