-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathexec-get-allocator.pass.cpp
87 lines (73 loc) · 2.52 KB
/
exec-get-allocator.pass.cpp
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
// src/Beman/Execution26/tests/exec-get-allocator.pass.cpp -*-C++-*-
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include <Beman/Execution26/execution.hpp>
#include <test/execution.hpp>
#include <concepts>
// ----------------------------------------------------------------------------
namespace
{
struct non_allocator
{
};
struct allocator
{
int tag{};
using value_type = int;
auto allocate(std::size_t n) -> int* { return new value_type[n]; }
auto deallocate(int* ptr, std::size_t) -> void { delete[] ptr; }
auto operator== (allocator const&) const -> bool = default;
};
struct no_allocator
{
int tag{};
};
template <bool Noexcept>
struct throwing_allocator
{
auto query(test_std::get_allocator_t const&) const noexcept(Noexcept) -> allocator
{
return {};
}
};
template <typename Result>
struct allocator_object
{
int tag{};
auto query(test_std::get_allocator_t const&) const noexcept -> Result
{
return Result{this->tag};
}
};
struct non_const_get_allocator
{
auto query(test_std::get_allocator_t const&) noexcept -> allocator
{
return {};
}
};
template <bool Expect, typename Object>
auto test_get_allocator(Object&& object) -> void
{
static_assert(Expect == requires{ test_std::get_allocator(::std::forward<Object>(object)); });
}
}
auto main() -> int
{
static_assert(std::same_as<test_std::get_allocator_t const,
decltype(test_std::get_allocator)>);
static_assert(test_std::forwarding_query(test_std::get_allocator));
test_get_allocator<false>(no_allocator());
test_get_allocator<true>(throwing_allocator<true>());
test_get_allocator<false>(throwing_allocator<false>());
static_assert(test_detail::simple_allocator<allocator>);
static_assert(not test_detail::simple_allocator<non_allocator>);
test_get_allocator<true>(allocator_object<allocator>());
test_get_allocator<false>(allocator_object<non_allocator>());
test_get_allocator<false>(non_const_get_allocator());
non_const_get_allocator alloc;
test_get_allocator<false>(alloc);
non_const_get_allocator calloc;
test_get_allocator<false>(calloc);
static_assert(std::same_as<allocator, decltype(test_std::get_allocator(allocator_object<allocator>()))>);
assert(43 == test_std::get_allocator(allocator_object<allocator>{43}).tag);
}