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 // UNSUPPORTED: c++03, c++11, c++14
10
11 // <any>
12
13 // Check that the size and alignment of any are what we expect.
14
15 #include <any>
16 #include "test_macros.h"
17 #include "any_helpers.h"
18
19 constexpr std::size_t BufferSize = (sizeof(void*) * 3);
20 constexpr std::size_t BufferAlignment = alignof(void*);
21 // Clang doesn't like "alignof(BufferAlignment * 2)" due to PR13986.
22 // So we create "DoubleBufferAlignment" instead.
23 constexpr std::size_t DoubleBufferAlignment = BufferAlignment * 2;
24
25 class SmallThrowsDtor
26 {
27 public:
SmallThrowsDtor()28 SmallThrowsDtor() {}
SmallThrowsDtor(SmallThrowsDtor const &)29 SmallThrowsDtor(SmallThrowsDtor const &) noexcept {}
SmallThrowsDtor(SmallThrowsDtor &&)30 SmallThrowsDtor(SmallThrowsDtor &&) noexcept {}
~SmallThrowsDtor()31 ~SmallThrowsDtor() noexcept(false) {}
32 };
33
34
35 struct alignas(1) MaxSizeType {
36 char buff[BufferSize];
37 };
38
39 struct alignas(BufferAlignment) MaxAlignType {
40 };
41
42 struct alignas(BufferAlignment) MaxSizeAndAlignType {
43 char buff[BufferSize];
44 };
45
46
47 struct alignas(1) OverSizeType {
48 char buff[BufferSize + 1];
49 };
50
51 struct alignas(DoubleBufferAlignment) OverAlignedType {
52 };
53
54 struct alignas(DoubleBufferAlignment) OverSizeAndAlignedType {
55 char buff[BufferSize + 1];
56 };
57
main(int,char **)58 int main(int, char**)
59 {
60 using std::any;
61 using std::__any_imp::_IsSmallObject;
62 static_assert(_IsSmallObject<small>::value, "");
63 static_assert(_IsSmallObject<void*>::value, "");
64 static_assert(!_IsSmallObject<SmallThrowsDtor>::value, "");
65 static_assert(!_IsSmallObject<large>::value, "");
66 {
67 // Check a type that meets the size requirement *exactly* and has
68 // a lesser alignment requirement is considered small.
69 typedef MaxSizeType T;
70 static_assert(sizeof(T) == BufferSize, "");
71 static_assert(alignof(T) < BufferAlignment, "");
72 static_assert(_IsSmallObject<T>::value, "");
73 }
74 {
75 // Check a type that meets the alignment requirement *exactly* and has
76 // a lesser size is considered small.
77 typedef MaxAlignType T;
78 static_assert(sizeof(T) < BufferSize, "");
79 static_assert(alignof(T) == BufferAlignment, "");
80 static_assert(_IsSmallObject<T>::value, "");
81 }
82 {
83 // Check a type that meets the size and alignment requirements *exactly*
84 // is considered small.
85 typedef MaxSizeAndAlignType T;
86 static_assert(sizeof(T) == BufferSize, "");
87 static_assert(alignof(T) == BufferAlignment, "");
88 static_assert(_IsSmallObject<T>::value, "");
89 }
90 {
91 // Check a type that meets the alignment requirements but is over-sized
92 // is not considered small.
93 typedef OverSizeType T;
94 static_assert(sizeof(T) > BufferSize, "");
95 static_assert(alignof(T) < BufferAlignment, "");
96 static_assert(!_IsSmallObject<T>::value, "");
97 }
98 {
99 // Check a type that meets the size requirements but is over-aligned
100 // is not considered small.
101 typedef OverAlignedType T;
102 static_assert(sizeof(T) < BufferSize, "");
103 static_assert(alignof(T) > BufferAlignment, "");
104 static_assert(!_IsSmallObject<T>::value, "");
105 }
106 {
107 // Check a type that exceeds both the size an alignment requirements
108 // is not considered small.
109 typedef OverSizeAndAlignedType T;
110 static_assert(sizeof(T) > BufferSize, "");
111 static_assert(alignof(T) > BufferAlignment, "");
112 static_assert(!_IsSmallObject<T>::value, "");
113 }
114
115 return 0;
116 }
117