File: inplace_merge.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 (142 lines) | stat: -rw-r--r-- 3,792 bytes parent folder | download | duplicates (3)
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
// 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 <random>
#include <range/v3/core.hpp>
#include <range/v3/algorithm/inplace_merge.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, typename Sent = Iter>
    void
    test_one_iter(unsigned N, unsigned M)
    {
        RANGES_ENSURE(M <= N);
        int* ia = new int[N];
        for (unsigned i = 0; i < N; ++i)
            ia[i] = i;
        std::shuffle(ia, ia+N, gen);
        std::sort(ia, ia+M);
        std::sort(ia+M, ia+N);
        auto res = ranges::inplace_merge(Iter(ia), Iter(ia+M), Sent(ia+N));
        CHECK(res == Iter(ia+N));
        if(N > 0)
        {
            CHECK(ia[0] == 0);
            CHECK(ia[N-1] == (int)N-1);
            CHECK(std::is_sorted(ia, ia+N));
        }
        delete [] ia;
    }

    template<class Iter, typename Sent = Iter>
    void
    test_one_rng(unsigned N, unsigned M)
    {
        RANGES_ENSURE(M <= N);
        int* ia = new int[N];
        for (unsigned i = 0; i < N; ++i)
            ia[i] = i;
        std::shuffle(ia, ia+N, gen);
        std::sort(ia, ia+M);
        std::sort(ia+M, ia+N);
        auto res = ranges::inplace_merge(ranges::make_subrange(Iter(ia), Sent(ia+N)), Iter(ia+M));
        CHECK(res == Iter(ia+N));
        if(N > 0)
        {
            CHECK(ia[0] == 0);
            CHECK(ia[N-1] == (int)N-1);
            CHECK(std::is_sorted(ia, ia+N));
        }

        std::shuffle(ia, ia+N, gen);
        std::sort(ia, ia+M);
        std::sort(ia+M, ia+N);
        auto res2 = ranges::inplace_merge(::MakeTestRange(Iter(ia), Sent(ia+N)), Iter(ia+M));
        CHECK(::is_dangling(res2));
        if(N > 0)
        {
            CHECK(ia[0] == 0);
            CHECK(ia[N-1] == (int)N-1);
            CHECK(std::is_sorted(ia, ia+N));
        }

        delete [] ia;
    }

    template<class Iter>
    void
    test_one(unsigned N, unsigned M)
    {
        test_one_iter<Iter>(N, M);
        test_one_iter<Iter, typename sentinel_type<Iter>::type>(N, M);
        test_one_rng<Iter>(N, M);
        test_one_rng<Iter, typename sentinel_type<Iter>::type>(N, M);
    }

    template<class Iter>
    void
    test(unsigned N)
    {
        test_one<Iter>(N, 0);
        test_one<Iter>(N, N/4);
        test_one<Iter>(N, N/2);
        test_one<Iter>(N, 3*N/4);
        test_one<Iter>(N, N);
    }

    template<class Iter>
    void
    test()
    {
        test_one<Iter>(0, 0);
        test_one<Iter>(1, 0);
        test_one<Iter>(1, 1);
        test_one<Iter>(2, 0);
        test_one<Iter>(2, 1);
        test_one<Iter>(2, 2);
        test_one<Iter>(3, 0);
        test_one<Iter>(3, 1);
        test_one<Iter>(3, 2);
        test_one<Iter>(3, 3);
        test<Iter>(4);
        test<Iter>(100);
        test<Iter>(1000);
    }
}

int main()
{
    // test<ForwardIterator<int*> >();
    test<BidirectionalIterator<int*> >();
    test<RandomAccessIterator<int*> >();
    test<int*>();

    return ::test_result();
}