| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
// <memory> |
| 10 |
|
| 11 |
// unique_ptr |
| 12 |
|
| 13 |
// test op[](size_t) |
| 14 |
|
| 15 |
#include <memory> |
| 16 |
#include <cassert> |
| 17 |
|
| 18 |
// TODO: Move TEST_IS_CONSTANT_EVALUATED into its own header |
| 19 |
#include <type_traits> |
| 20 |
|
| 21 |
#include "test_macros.h" |
| 22 |
|
| 23 |
class A { |
| 24 |
int state_; |
| 25 |
static int next_; |
| 26 |
|
| 27 |
public: |
| 28 |
TEST_CONSTEXPR_CXX23 A() : state_(0) { |
| 29 |
if (!TEST_IS_CONSTANT_EVALUATED) |
| 30 |
state_ = ++next_; |
| 31 |
} |
| 32 |
|
| 33 |
TEST_CONSTEXPR_CXX23 int get() const { return state_; } |
| 34 |
|
| 35 |
friend TEST_CONSTEXPR_CXX23 bool operator==(const A& x, int y) { return x.state_ == y; } |
| 36 |
|
| 37 |
TEST_CONSTEXPR_CXX23 A& operator=(int i) { |
| 38 |
state_ = i; |
| 39 |
return *this; |
| 40 |
} |
| 41 |
}; |
| 42 |
|
| 43 |
int A::next_ = 0; |
| 44 |
|
| 45 |
TEST_CONSTEXPR_CXX23 bool test() { |
| 46 |
std::unique_ptr<A[]> p(new A[3]); |
| 47 |
if (!TEST_IS_CONSTANT_EVALUATED) { |
| 48 |
assert(p[0] == 1); |
| 49 |
assert(p[1] == 2); |
| 50 |
assert(p[2] == 3); |
| 51 |
} |
| 52 |
p[0] = 3; |
| 53 |
p[1] = 2; |
| 54 |
p[2] = 1; |
| 55 |
assert(p[0] == 3); |
| 56 |
assert(p[1] == 2); |
| 57 |
assert(p[2] == 1); |
| 58 |
|
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
int main(int, char**) { |
| 63 |
test(); |
| 64 |
#if TEST_STD_VER >= 23 |
| 65 |
static_assert(test()); |
| 66 |
#endif |
| 67 |
|
| 68 |
return 0; |
| 69 |
} |