1
+ // src/Beman/Execution26/tests/exec.get.allocator.pass.cpp -*-C++-*-
2
+ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3
+
4
+ #include < Beman/Execution26/detail/get_allocator.hpp>
5
+ #include < Beman/Execution26/execution.hpp>
6
+ #include < test/execution.hpp>
7
+ #include < concepts>
8
+
9
+ // ----------------------------------------------------------------------------
10
+
11
+ namespace
12
+ {
13
+ struct non_allocator
14
+ {
15
+ };
16
+ struct allocator
17
+ {
18
+ int tag{};
19
+ using value_type = int ;
20
+ auto allocate (std::size_t n) -> int* { return new value_type[n]; }
21
+ auto deallocate (int * ptr, std::size_t ) -> void { delete[] ptr; }
22
+ auto operator == (allocator const &) const -> bool = default ;
23
+ };
24
+
25
+ struct no_allocator
26
+ {
27
+ int tag{};
28
+ };
29
+
30
+ template <bool Noexcept>
31
+ struct throwing_allocator
32
+ {
33
+ auto query (test_std::get_allocator_t const &) const noexcept (Noexcept) -> allocator
34
+ {
35
+ return {};
36
+ }
37
+ };
38
+
39
+ template <typename Result>
40
+ struct allocator_object
41
+ {
42
+ int tag{};
43
+ auto query (test_std::get_allocator_t const &) const noexcept -> Result
44
+ {
45
+ return Result{this ->tag };
46
+ }
47
+ };
48
+
49
+ struct non_const_get_allocator
50
+ {
51
+ auto query (test_std::get_allocator_t const &) noexcept -> allocator
52
+ {
53
+ return {};
54
+ }
55
+ };
56
+
57
+
58
+ template <bool Expect, typename Object>
59
+ auto test_get_allocator (Object&& object) -> void
60
+ {
61
+ static_assert (Expect == requires{ test_std::get_allocator (::std::forward<Object>(object)); });
62
+ }
63
+ }
64
+
65
+ auto main () -> int
66
+ {
67
+ static_assert (std::same_as<test_std::get_allocator_t const ,
68
+ decltype (test_std::get_allocator)>);
69
+ static_assert (test_std::forwarding_query (test_std::get_allocator));
70
+
71
+ test_get_allocator<false >(no_allocator ());
72
+ test_get_allocator<true >(throwing_allocator<true >());
73
+ test_get_allocator<false >(throwing_allocator<false >());
74
+
75
+ static_assert (test_detail::simple_allocator<allocator>);
76
+ static_assert (not test_detail::simple_allocator<non_allocator>);
77
+ test_get_allocator<true >(allocator_object<allocator>());
78
+ test_get_allocator<false >(allocator_object<non_allocator>());
79
+
80
+ test_get_allocator<false >(non_const_get_allocator ());
81
+ non_const_get_allocator alloc;
82
+ test_get_allocator<false >(alloc);
83
+ non_const_get_allocator calloc;
84
+ test_get_allocator<false >(calloc);
85
+
86
+ static_assert (std::same_as<allocator, decltype (test_std::get_allocator (allocator_object<allocator>()))>);
87
+ assert (43 == test_std::get_allocator (allocator_object<allocator>{43 }).tag );
88
+ }
0 commit comments