File: ranges_destroy.pass.cpp

package info (click to toggle)
llvm-toolchain-16 1%3A16.0.6-15~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,634,792 kB
  • sloc: cpp: 6,179,261; ansic: 1,216,205; asm: 741,319; python: 196,614; objc: 75,325; f90: 49,640; lisp: 32,396; pascal: 12,286; sh: 9,394; perl: 7,442; ml: 5,494; awk: 3,523; makefile: 2,723; javascript: 1,206; xml: 886; fortran: 581; cs: 573
file content (221 lines) | stat: -rw-r--r-- 6,151 bytes parent folder | download | duplicates (13)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//===----------------------------------------------------------------------===//
//
// 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, c++17

// <memory>
//
// namespace ranges {
//   template<nothrow-input-iterator InputIterator, nothrow-sentinel-for<InputIterator> Sentinel>
//     requires destructible<iter_value_t<InputIterator>>
//     constexpr InputIterator destroy(InputIterator first, Sentinel last) noexcept; // since C++20
//   template<nothrow-input-range InputRange>
//     requires destructible<range_value_t<InputRange>>
//     constexpr borrowed_iterator_t<InputRange> destroy(InputRange&& range) noexcept; // since C++20
// }

#include <cassert>
#include <memory>
#include <ranges>
#include <type_traits>

#include "test_iterators.h"
#include "test_macros.h"

// TODO(varconst): consolidate the ADL checks into a single file.
// Because this is a variable and not a function, it's guaranteed that ADL won't be used. However,
// implementations are allowed to use a different mechanism to achieve this effect, so this check is
// libc++-specific.
LIBCPP_STATIC_ASSERT(std::is_class_v<decltype(std::ranges::destroy)>);

struct NotNothrowDtrable {
  ~NotNothrowDtrable() noexcept(false) {}
};
static_assert(!std::is_invocable_v<decltype(std::ranges::destroy), NotNothrowDtrable*, NotNothrowDtrable*>);

struct Counted {
  int& count;

  constexpr Counted(int& count_ref) : count(count_ref) { ++count; }
  constexpr Counted(const Counted& rhs) : count(rhs.count) { ++count; }
  constexpr ~Counted() { --count; }

  friend void operator&(Counted) = delete;
};

template <class Iterator>
constexpr void test() {
  // (iterator + sentinel) overload.
  {
    constexpr int N = 5;
    std::allocator<Counted> alloc;
    using Traits = std::allocator_traits<decltype(alloc)>;
    int counter = 0;

    Counted* out = Traits::allocate(alloc, N);
    for (int i = 0; i != N; ++i) {
      Traits::construct(alloc, out + i, counter);
    }
    assert(counter == N);

    std::ranges::destroy(Iterator(out), Iterator(out + N));
    assert(counter == 0);

    Traits::deallocate(alloc, out, N);
  }

  // (range) overload.
  {
    constexpr int N = 5;
    std::allocator<Counted> alloc;
    using Traits = std::allocator_traits<decltype(alloc)>;
    int counter = 0;

    Counted* out = Traits::allocate(alloc, N);
    for (int i = 0; i != N; ++i) {
      Traits::construct(alloc, out + i, counter);
    }
    assert(counter == N);

    auto range = std::ranges::subrange(Iterator(out), Iterator(out + N));
    std::ranges::destroy(range);
    assert(counter == 0);

    Traits::deallocate(alloc, out, N);
  }
}

constexpr bool tests() {
  test<Counted*>();
  test<forward_iterator<Counted*>>();

  return true;
}

constexpr bool test_arrays() {
  // One-dimensional array, (iterator + sentinel) overload.
  {
    constexpr int N = 5;
    constexpr int M = 3;

    using Array = Counted[M];
    std::allocator<Array> alloc;
    using Traits = std::allocator_traits<decltype(alloc)>;
    int counter = 0;

    Array* buffer = Traits::allocate(alloc, N);
    for (int i = 0; i != N; ++i) {
      Array& array_ref = *(buffer + i);
      for (int j = 0; j != M; ++j) {
        Traits::construct(alloc, std::addressof(array_ref[j]), counter);
      }
    }
    assert(counter == N * M);

    std::ranges::destroy(buffer, buffer + N);
    assert(counter == 0);

    Traits::deallocate(alloc, buffer, N);
  }

  // One-dimensional array, (range) overload.
  {
    constexpr int N = 5;
    constexpr int A = 3;

    using Array = Counted[A];
    std::allocator<Array> alloc;
    using Traits = std::allocator_traits<decltype(alloc)>;
    int counter = 0;

    Array* buffer = Traits::allocate(alloc, N);
    for (int i = 0; i != N; ++i) {
      Array& array_ref = *(buffer + i);
      for (int j = 0; j != A; ++j) {
        Traits::construct(alloc, std::addressof(array_ref[j]), counter);
      }
    }
    assert(counter == N * A);

    auto range = std::ranges::subrange(buffer, buffer + N);
    std::ranges::destroy(range);
    assert(counter == 0);

    Traits::deallocate(alloc, buffer, N);
  }

  // Multidimensional array, (iterator + sentinel ) overload.
  {
    constexpr int N = 5;
    constexpr int A = 3;
    constexpr int B = 3;

    using Array = Counted[A][B];
    std::allocator<Array> alloc;
    using Traits = std::allocator_traits<decltype(alloc)>;
    int counter = 0;

    Array* buffer = Traits::allocate(alloc, N);
    for (int i = 0; i != N; ++i) {
      Array& array_ref = *(buffer + i);
      for (int j = 0; j != A; ++j) {
        for (int k = 0; k != B; ++k) {
          Traits::construct(alloc, std::addressof(array_ref[j][k]), counter);
        }
      }
    }
    assert(counter == N * A * B);

    std::ranges::destroy(buffer, buffer + N);
    assert(counter == 0);

    Traits::deallocate(alloc, buffer, N);
  }

  // Multidimensional array, (range) overload.
  {
    constexpr int N = 5;
    constexpr int A = 3;
    constexpr int B = 3;

    using Array = Counted[A][B];
    std::allocator<Array> alloc;
    using Traits = std::allocator_traits<decltype(alloc)>;
    int counter = 0;

    Array* buffer = Traits::allocate(alloc, N);
    for (int i = 0; i != N; ++i) {
      Array& array_ref = *(buffer + i);
      for (int j = 0; j != A; ++j) {
        for (int k = 0; k != B; ++k) {
          Traits::construct(alloc, std::addressof(array_ref[j][k]), counter);
        }
      }
    }
    assert(counter == N * A * B);

    std::ranges::destroy(buffer, buffer + N);
    assert(counter == 0);

    Traits::deallocate(alloc, buffer, N);
  }

  return true;
}

int main(int, char**) {
  tests();
  test_arrays();

  static_assert(tests());
  // TODO: Until std::construct_at has support for arrays, it's impossible to test this
  //       in a constexpr context (see https://reviews.llvm.org/D114903).
  // static_assert(test_arrays());

  return 0;
}