diff options
| author | Eric Fiselier <eric@efcs.ca> | 2015-07-31 02:24:58 +0000 |
|---|---|---|
| committer | Eric Fiselier <eric@efcs.ca> | 2015-07-31 02:24:58 +0000 |
| commit | 3461dbc0a7beb7299fcec968bba87a32393e3252 (patch) | |
| tree | 5f408f34b2629d285c0f32325f40893d60cc65ac /libcxx/test/support | |
| parent | 403cbcb84d8b8f459aaa7351472fee8b8df3e391 (diff) | |
| download | bcm5719-llvm-3461dbc0a7beb7299fcec968bba87a32393e3252.tar.gz bcm5719-llvm-3461dbc0a7beb7299fcec968bba87a32393e3252.zip | |
[libcxx] Add <experimental/any> v2.
Summary:
This patch adds the second revision of <experimental/any>.
I've been working from the LFTS draft found at this link. https://rawgit.com/cplusplus/fundamentals-ts/v1/fundamentals-ts.html#any
Reviewers: danalbert, jroelofs, K-ballo, mclow.lists
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D6762
llvm-svn: 243728
Diffstat (limited to 'libcxx/test/support')
| -rw-r--r-- | libcxx/test/support/any_helpers.h | 318 | ||||
| -rw-r--r-- | libcxx/test/support/count_new.hpp | 43 | ||||
| -rw-r--r-- | libcxx/test/support/test_macros.h | 14 |
3 files changed, 375 insertions, 0 deletions
diff --git a/libcxx/test/support/any_helpers.h b/libcxx/test/support/any_helpers.h new file mode 100644 index 00000000000..76b1922fb1f --- /dev/null +++ b/libcxx/test/support/any_helpers.h @@ -0,0 +1,318 @@ +#ifndef ANY_HELPERS_H +#define ANY_HELPERS_H + +#include <experimental/any> +#include <typeinfo> +#include <type_traits> +#include <cassert> + +#include "test_macros.h" + +#if !defined(TEST_HAS_NO_RTTI) +#define RTTI_ASSERT(X) assert(X) +#else +#define RTTI_ASSERT(X) +#endif + +template <class _Tp> + struct IsSmallObject + : public std::integral_constant<bool + , sizeof(_Tp) <= (sizeof(void*)*3) + && std::alignment_of<void*>::value + % std::alignment_of<_Tp>::value == 0 + && std::is_nothrow_move_constructible<_Tp>::value + > + {}; + + +// Return 'true' if 'Type' will be considered a small type by 'any' +template <class Type> +bool isSmallType() { +#if defined(_LIBCPP_VERSION) + return std::experimental::__any_imp::_IsSmallObject<Type>::value; +#else + return IsSmallObject<Type>::value; +#endif + +} + +// Assert that an object is empty. If the object used to contain an object +// of type 'LastType' check that it can no longer be accessed. +template <class LastType = int> +void assertEmpty(std::experimental::any const& a) { + assert(a.empty()); + RTTI_ASSERT(a.type() == typeid(void)); + assert(std::experimental::any_cast<LastType const>(&a) == nullptr); +} + +// Assert that an 'any' object stores the specified 'Type' and 'value'. +template <class Type> +void assertContains(std::experimental::any const& a, int value = 1) { + assert(!a.empty()); + RTTI_ASSERT(a.type() == typeid(Type)); + assert(std::experimental::any_cast<Type const &>(a).value == value); +} + +// Modify the value of a "test type" stored within an any to the specified +// 'value'. +template <class Type> +void modifyValue(std::experimental::any& a, int value) { + assert(!a.empty()); + RTTI_ASSERT(a.type() == typeid(Type)); + std::experimental::any_cast<Type&>(a).value = value; +} + +// A test type that will trigger the small object optimization within 'any'. +template <int Dummy = 0> +struct small_type +{ + static int count; + static int copied; + static int moved; + static int const_copied; + static int non_const_copied; + + static void reset() { + small_type::copied = 0; + small_type::moved = 0; + small_type::const_copied = 0; + small_type::non_const_copied = 0; + } + + int value; + + explicit small_type(int val) : value(val) { + ++count; + } + + small_type(small_type const & other) throw() { + value = other.value; + ++count; + ++copied; + ++const_copied; + } + + small_type(small_type& other) throw() { + value = other.value; + ++count; + ++copied; + ++non_const_copied; + } + + small_type(small_type && other) throw() { + value = other.value; + other.value = 0; + ++count; + ++moved; + } + + ~small_type() { + value = -1; + --count; + } + +private: + small_type& operator=(small_type const&) = delete; + small_type& operator=(small_type&&) = delete; +}; + +template <int Dummy> +int small_type<Dummy>::count = 0; + +template <int Dummy> +int small_type<Dummy>::copied = 0; + +template <int Dummy> +int small_type<Dummy>::moved = 0; + +template <int Dummy> +int small_type<Dummy>::const_copied = 0; + +template <int Dummy> +int small_type<Dummy>::non_const_copied = 0; + +typedef small_type<> small; +typedef small_type<1> small1; +typedef small_type<2> small2; + + +// A test type that will NOT trigger the small object optimization in any. +template <int Dummy = 0> +struct large_type +{ + static int count; + static int copied; + static int moved; + static int const_copied; + static int non_const_copied; + + static void reset() { + large_type::copied = 0; + large_type::moved = 0; + large_type::const_copied = 0; + large_type::non_const_copied = 0; + } + + int value; + + large_type(int val) : value(val) { + ++count; + data[0] = 0; + } + + large_type(large_type const & other) { + value = other.value; + ++count; + ++copied; + ++const_copied; + } + + large_type(large_type & other) { + value = other.value; + ++count; + ++copied; + ++non_const_copied; + } + + large_type(large_type && other) { + value = other.value; + other.value = 0; + ++count; + ++moved; + } + + ~large_type() { + value = 0; + --count; + } + +private: + large_type& operator=(large_type const&) = delete; + large_type& operator=(large_type &&) = delete; + int data[10]; +}; + +template <int Dummy> +int large_type<Dummy>::count = 0; + +template <int Dummy> +int large_type<Dummy>::copied = 0; + +template <int Dummy> +int large_type<Dummy>::moved = 0; + +template <int Dummy> +int large_type<Dummy>::const_copied = 0; + +template <int Dummy> +int large_type<Dummy>::non_const_copied = 0; + +typedef large_type<> large; +typedef large_type<1> large1; +typedef large_type<2> large2; + +// The exception type thrown by 'small_throws_on_copy', 'large_throws_on_copy' +// and 'throws_on_move'. +struct my_any_exception {}; + +void throwMyAnyExpression() { +#if !defined(TEST_HAS_NO_EXCEPTIONS) + throw my_any_exception(); +#else + assert(false && "Exceptions are disabled"); +#endif +} + +// A test type that will trigger the small object optimization within 'any'. +// this type throws if it is copied. +struct small_throws_on_copy +{ + static int count; + int value; + + explicit small_throws_on_copy(int val = 0) : value(val) { + ++count; + } + + small_throws_on_copy(small_throws_on_copy const &) { + throwMyAnyExpression(); + } + + small_throws_on_copy(small_throws_on_copy && other) throw() { + value = other.value; + ++count; + } + + ~small_throws_on_copy() { + --count; + } +private: + small_throws_on_copy& operator=(small_throws_on_copy const&) = delete; + small_throws_on_copy& operator=(small_throws_on_copy &&) = delete; +}; + +int small_throws_on_copy::count = 0; + +// A test type that will NOT trigger the small object optimization within 'any'. +// this type throws if it is copied. +struct large_throws_on_copy +{ + static int count; + int value = 0; + + explicit large_throws_on_copy(int val = 0) : value(val) { + data[0] = 0; + ++count; + } + + large_throws_on_copy(large_throws_on_copy const &) { + throwMyAnyExpression(); + } + + large_throws_on_copy(large_throws_on_copy && other) throw() { + value = other.value; + ++count; + } + + ~large_throws_on_copy() { + --count; + } + +private: + large_throws_on_copy& operator=(large_throws_on_copy const&) = delete; + large_throws_on_copy& operator=(large_throws_on_copy &&) = delete; + int data[10]; +}; + +int large_throws_on_copy::count = 0; + +// A test type that throws when it is moved. This object will NOT trigger +// the small object optimization in 'any'. +struct throws_on_move +{ + static int count; + int value; + + explicit throws_on_move(int val = 0) : value(val) { ++count; } + + throws_on_move(throws_on_move const & other) { + value = other.value; + ++count; + } + + throws_on_move(throws_on_move &&) { + throwMyAnyExpression(); + } + + ~throws_on_move() { + --count; + } +private: + throws_on_move& operator=(throws_on_move const&) = delete; + throws_on_move& operator=(throws_on_move &&) = delete; +}; + +int throws_on_move::count = 0; + + +#endif diff --git a/libcxx/test/support/count_new.hpp b/libcxx/test/support/count_new.hpp index ddaf036f7d1..57aca7def33 100644 --- a/libcxx/test/support/count_new.hpp +++ b/libcxx/test/support/count_new.hpp @@ -30,6 +30,10 @@ public: // All checks return true when disable_checking is enabled. static const bool disable_checking; + // Disallow any allocations from occurring. Useful for testing that + // code doesn't perform any allocations. + bool disable_allocations; + int outstanding_new; int new_called; int delete_called; @@ -43,6 +47,7 @@ public: public: void newCalled(std::size_t s) { + assert(disable_allocations == false); assert(s); ++new_called; ++outstanding_new; @@ -58,6 +63,7 @@ public: void newArrayCalled(std::size_t s) { + assert(disable_allocations == false); assert(s); ++outstanding_array_new; ++new_array_called; @@ -71,8 +77,20 @@ public: ++delete_array_called; } + void disableAllocations() + { + disable_allocations = true; + } + + void enableAllocations() + { + disable_allocations = false; + } + void reset() { + disable_allocations = false; + outstanding_new = 0; new_called = 0; delete_called = 0; @@ -203,4 +221,29 @@ void operator delete[](void* p) throw() #endif // DISABLE_NEW_COUNT + +struct DisableAllocationGuard { + explicit DisableAllocationGuard(bool disable = true) : m_disabled(disable) + { + // Don't re-disable if already disabled. + if (globalMemCounter.disable_allocations == true) m_disabled = false; + if (m_disabled) globalMemCounter.disableAllocations(); + } + + void release() { + if (m_disabled) globalMemCounter.enableAllocations(); + m_disabled = false; + } + + ~DisableAllocationGuard() { + release(); + } + +private: + bool m_disabled; + + DisableAllocationGuard(DisableAllocationGuard const&); + DisableAllocationGuard& operator=(DisableAllocationGuard const&); +}; + #endif /* COUNT_NEW_HPP */ diff --git a/libcxx/test/support/test_macros.h b/libcxx/test/support/test_macros.h index a08ca87be93..08ec28f7c7b 100644 --- a/libcxx/test/support/test_macros.h +++ b/libcxx/test/support/test_macros.h @@ -14,6 +14,12 @@ #define TEST_CONCAT1(X, Y) X##Y #define TEST_CONCAT(X, Y) TEST_CONCAT1(X, Y) +#ifdef __has_feature +#define TEST_HAS_FEATURE(X) __has_feature(X) +#else +#define TEST_HAS_FEATURE(X) 0 +#endif + #ifdef __has_extension #define TEST_HAS_EXTENSION(X) __has_extension(X) #else @@ -80,4 +86,12 @@ template <unsigned> struct static_assert_check {}; } // end namespace test_detail +#if !TEST_HAS_FEATURE(cxx_rtti) && !defined(__cxx_rtti) +#define TEST_HAS_NO_RTTI +#endif + +#if !TEST_HAS_FEATURE(cxx_exceptions) && !defined(__cxx_exceptions) +#define TEST_HAS_NO_EXCEPTIONS +#endif + #endif // SUPPORT_TEST_MACROS_HPP |

