aboutsummaryrefslogtreecommitdiff
path: root/test/libcxx/experimental/any/small_type.pass.cpp
blob: e6595d4a4ab3303d4921efde50cdfad25915f048 (plain) (blame)
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
//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++98, c++03, c++11

// <experimental/any>

// Check that the size and alignment of any are what we expect.

#include <experimental/any>
#include "any_helpers.h"

constexpr std::size_t BufferSize = (sizeof(void*) * 3);
constexpr std::size_t BufferAlignment = alignof(void*);
// Clang doesn't like "alignof(BufferAlignment * 2)" due to PR13986.
// So we create "DoubleBufferAlignment" instead.
constexpr std::size_t DoubleBufferAlignment = BufferAlignment * 2;

class SmallThrowsDtor
{
public:
    SmallThrowsDtor() {}
    SmallThrowsDtor(SmallThrowsDtor const &) noexcept {}
    SmallThrowsDtor(SmallThrowsDtor &&) noexcept {}
    ~SmallThrowsDtor() noexcept(false) {}
};


struct alignas(1) MaxSizeType {
    char buff[BufferSize];
};

struct alignas(BufferAlignment) MaxAlignType {
};

struct alignas(BufferAlignment) MaxSizeAndAlignType {
    char buff[BufferSize];
};


struct alignas(1) OverSizeType {
    char buff[BufferSize + 1];
};

struct alignas(DoubleBufferAlignment) OverAlignedType {
};

struct alignas(DoubleBufferAlignment) OverSizeAndAlignedType {
    char buff[BufferSize + 1];
};

int main()
{
    using std::experimental::any;
    using std::experimental::__any_imp::_IsSmallObject;
    static_assert(_IsSmallObject<small>::value, "");
    static_assert(_IsSmallObject<void*>::value, "");
    static_assert(!_IsSmallObject<SmallThrowsDtor>::value, "");
    static_assert(!_IsSmallObject<large>::value, "");
    {
        // Check a type that meets the size requirement *exactly* and has
        // a lesser alignment requirement is considered small.
        typedef MaxSizeType T;
        static_assert(sizeof(T) == BufferSize, "");
        static_assert(alignof(T) < BufferAlignment,   "");
        static_assert(_IsSmallObject<T>::value, "");
    }
    {
        // Check a type that meets the alignment requirement *exactly* and has
        // a lesser size is considered small.
        typedef MaxAlignType T;
        static_assert(sizeof(T) < BufferSize, "");
        static_assert(alignof(T) == BufferAlignment,   "");
        static_assert(_IsSmallObject<T>::value, "");
    }
    {
        // Check a type that meets the size and alignment requirements *exactly*
        // is considered small.
        typedef MaxSizeAndAlignType T;
        static_assert(sizeof(T) == BufferSize, "");
        static_assert(alignof(T) == BufferAlignment,   "");
        static_assert(_IsSmallObject<T>::value, "");
    }
    {
        // Check a type that meets the alignment requirements but is over-sized
        // is not considered small.
        typedef OverSizeType T;
        static_assert(sizeof(T) > BufferSize, "");
        static_assert(alignof(T) < BufferAlignment, "");
        static_assert(!_IsSmallObject<T>::value, "");
    }
    {
        // Check a type that meets the size requirements but is over-aligned
        // is not considered small.
        typedef OverAlignedType T;
        static_assert(sizeof(T) < BufferSize, "");
        static_assert(alignof(T) > BufferAlignment, "");
        static_assert(!_IsSmallObject<T>::value, "");
    }
    {
        // Check a type that exceeds both the size an alignment requirements
        // is not considered small.
        typedef OverSizeAndAlignedType T;
        static_assert(sizeof(T) > BufferSize, "");
        static_assert(alignof(T) > BufferAlignment, "");
        static_assert(!_IsSmallObject<T>::value, "");
    }
}