]> git.proxmox.com Git - ceph.git/blob - ceph/src/Beast/test/core/empty_base_optimization.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / Beast / test / core / empty_base_optimization.cpp
1 //
2 // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7
8 // Test that header file is self-contained.
9 #include <beast/core/detail/empty_base_optimization.hpp>
10
11 #include <beast/unit_test/suite.hpp>
12
13 namespace beast {
14 namespace detail {
15
16 class empty_base_optimization_test
17 : public beast::unit_test::suite
18 {
19 public:
20 template<class T>
21 class test1
22 : private empty_base_optimization<T>
23 {
24 using Base = empty_base_optimization<T>;
25 void* m_p;
26 public:
27 explicit test1 (T const& t)
28 : Base (t)
29 {}
30
31 T& member() {return Base::member();}
32 T const& member() const {return Base::member();}
33 };
34
35 template<class T>
36 class test2
37 {
38 void* m_p;
39 T m_t;
40 public:
41 explicit test2 (T const& t)
42 : m_t (t)
43 {}
44
45 T& member() {return m_t;}
46 T const& member() const {return m_t;}
47 };
48
49 struct Empty
50 {
51 operator bool() {return true;}
52 };
53
54 static
55 bool
56 test_one()
57 {
58 test1<int> t1(1);
59 test2<int> t2(2);
60 static_assert(sizeof(t1) == sizeof(t2), "don't optimize for int");
61 if(t1.member() != 1)
62 return false;
63 if(t2.member() != 2)
64 return false;
65 return true;
66 }
67
68 static
69 bool
70 test_two()
71 {
72 test1<Empty> t1((Empty()));
73 test2<Empty> t2((Empty()));
74 static_assert(sizeof(t1) < sizeof(t2), "do optimize for Empty");
75 if(t1.member() != true)
76 return false;
77 if(t2.member() != true)
78 return false;
79 return true;
80 }
81
82 void
83 run()
84 {
85 BEAST_EXPECT(test_one());
86 BEAST_EXPECT(test_two());
87 pass();
88 }
89 };
90
91 BEAST_DEFINE_TESTSUITE(empty_base_optimization,core,beast);
92
93 } // detail
94 } // beast