diff --git a/libcxx/test/support/test_allocator.h b/libcxx/test/support/test_allocator.h --- a/libcxx/test/support/test_allocator.h +++ b/libcxx/test/support/test_allocator.h @@ -21,42 +21,41 @@ #include "test_macros.h" template -inline typename std::allocator_traits::size_type -alloc_max_size(Alloc const &a) { +inline typename std::allocator_traits::size_type alloc_max_size(Alloc const& a) { typedef std::allocator_traits AT; return AT::max_size(a); } -class test_alloc_base -{ +class test_alloc_base { protected: - static int time_to_throw; + static int time_to_throw; + public: - static int throw_after; - static int count; - static int alloc_count; - static int copied; - static int moved; - static int converted; - - const static int destructed_value = -1; - const static int default_value = 0; - const static int moved_value = INT_MAX; - - static void clear() { - assert(count == 0 && "clearing leaking allocator data?"); - count = 0; - time_to_throw = 0; - alloc_count = 0; - throw_after = INT_MAX; - clear_ctor_counters(); - } + static int throw_after; + static int count; + static int alloc_count; + static int copied; + static int moved; + static int converted; + + const static int destructed_value = -1; + const static int default_value = 0; + const static int moved_value = INT_MAX; + + static void clear() { + assert(count == 0 && "clearing leaking allocator data?"); + count = 0; + time_to_throw = 0; + alloc_count = 0; + throw_after = INT_MAX; + clear_ctor_counters(); + } - static void clear_ctor_counters() { - copied = 0; - moved = 0; - converted = 0; - } + static void clear_ctor_counters() { + copied = 0; + moved = 0; + converted = 0; + } }; int test_alloc_base::count = 0; @@ -68,234 +67,235 @@ int test_alloc_base::converted = 0; template -class test_allocator - : public test_alloc_base -{ - int data_; // participates in equality - int id_; // unique identifier, doesn't participate in equality - template friend class test_allocator; -public: +class test_allocator : public test_alloc_base { + int data_; // participates in equality + int id_; // unique identifier, doesn't participate in equality + template + friend class test_allocator; - typedef unsigned size_type; - typedef int difference_type; - typedef T value_type; - typedef value_type* pointer; - typedef const value_type* const_pointer; - typedef typename std::add_lvalue_reference::type reference; - typedef typename std::add_lvalue_reference::type const_reference; - - template struct rebind {typedef test_allocator other;}; - - test_allocator() TEST_NOEXCEPT : data_(0), id_(0) {++count;} - explicit test_allocator(int i, int id = 0) TEST_NOEXCEPT : data_(i), id_(id) - {++count;} - test_allocator(const test_allocator& a) TEST_NOEXCEPT : data_(a.data_), - id_(a.id_) { - ++count; - ++copied; - assert(a.data_ != destructed_value && a.id_ != destructed_value && - "copying from destroyed allocator"); - } +public: + typedef unsigned size_type; + typedef int difference_type; + typedef T value_type; + typedef value_type* pointer; + typedef const value_type* const_pointer; + typedef typename std::add_lvalue_reference::type reference; + typedef typename std::add_lvalue_reference::type const_reference; + + template + struct rebind { + typedef test_allocator other; + }; + + test_allocator() TEST_NOEXCEPT : data_(0), id_(0) { ++count; } + explicit test_allocator(int i, int id = 0) TEST_NOEXCEPT : data_(i), id_(id) { ++count; } + test_allocator(const test_allocator& a) TEST_NOEXCEPT : data_(a.data_), id_(a.id_) { + ++count; + ++copied; + assert(a.data_ != destructed_value && a.id_ != destructed_value && "copying from destroyed allocator"); + } #if TEST_STD_VER >= 11 - test_allocator(test_allocator&& a) TEST_NOEXCEPT : data_(a.data_), - id_(a.id_) { - ++count; - ++moved; - assert(a.data_ != destructed_value && a.id_ != destructed_value && - "moving from destroyed allocator"); - a.data_ = moved_value; - a.id_ = moved_value; - } + test_allocator(test_allocator&& a) TEST_NOEXCEPT : data_(a.data_), id_(a.id_) { + ++count; + ++moved; + assert(a.data_ != destructed_value && a.id_ != destructed_value && "moving from destroyed allocator"); + a.data_ = moved_value; + a.id_ = moved_value; + } #endif - template - test_allocator(const test_allocator& a) TEST_NOEXCEPT : data_(a.data_), - id_(a.id_) { - ++count; - ++converted; - } - ~test_allocator() TEST_NOEXCEPT { - assert(data_ >= 0); assert(id_ >= 0); - --count; - data_ = destructed_value; - id_ = destructed_value; - } - pointer address(reference x) const {return &x;} - const_pointer address(const_reference x) const {return &x;} - pointer allocate(size_type n, const void* = 0) - { - assert(data_ >= 0); - if (time_to_throw >= throw_after) { + template + test_allocator(const test_allocator& a) TEST_NOEXCEPT : data_(a.data_), id_(a.id_) { + ++count; + ++converted; + } + ~test_allocator() TEST_NOEXCEPT { + assert(data_ >= 0); + assert(id_ >= 0); + --count; + data_ = destructed_value; + id_ = destructed_value; + } + pointer address(reference x) const { return &x; } + const_pointer address(const_reference x) const { return &x; } + pointer allocate(size_type n, const void* = 0) { + assert(data_ >= 0); + if (time_to_throw >= throw_after) { #ifndef TEST_HAS_NO_EXCEPTIONS - throw std::bad_alloc(); + throw std::bad_alloc(); #else - std::terminate(); + std::terminate(); #endif - } - ++time_to_throw; - ++alloc_count; - return (pointer)::operator new(n * sizeof(T)); - } - void deallocate(pointer p, size_type) - {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p);} - size_type max_size() const TEST_NOEXCEPT - {return UINT_MAX / sizeof(T);} + } + ++time_to_throw; + ++alloc_count; + return (pointer)::operator new(n * sizeof(T)); + } + void deallocate(pointer p, size_type) { + assert(data_ >= 0); + --alloc_count; + ::operator delete((void*)p); + } + size_type max_size() const TEST_NOEXCEPT { return UINT_MAX / sizeof(T); } #if TEST_STD_VER < 11 - void construct(pointer p, const T& val) - {::new(static_cast(p)) T(val);} + void construct(pointer p, const T& val) { ::new (static_cast(p)) T(val); } #else - template void construct(pointer p, U&& val) - {::new(static_cast(p)) T(std::forward(val));} + template + void construct(pointer p, U&& val) { + ::new (static_cast(p)) T(std::forward(val)); + } #endif - void destroy(pointer p) - {p->~T();} - friend bool operator==(const test_allocator& x, const test_allocator& y) - {return x.data_ == y.data_;} - friend bool operator!=(const test_allocator& x, const test_allocator& y) - {return !(x == y);} - - int get_data() const { return data_; } - int get_id() const { return id_; } + void destroy(pointer p) { p->~T(); } + friend bool operator==(const test_allocator& x, const test_allocator& y) { return x.data_ == y.data_; } + friend bool operator!=(const test_allocator& x, const test_allocator& y) { return !(x == y); } + + int get_data() const { return data_; } + int get_id() const { return id_; } }; template -class non_default_test_allocator - : public test_alloc_base -{ - int data_; +class non_default_test_allocator : public test_alloc_base { + int data_; - template friend class non_default_test_allocator; -public: + template + friend class non_default_test_allocator; - typedef unsigned size_type; - typedef int difference_type; - typedef T value_type; - typedef value_type* pointer; - typedef const value_type* const_pointer; - typedef typename std::add_lvalue_reference::type reference; - typedef typename std::add_lvalue_reference::type const_reference; - - template struct rebind {typedef non_default_test_allocator other;}; - -// non_default_test_allocator() TEST_NOEXCEPT : data_(0) {++count;} - explicit non_default_test_allocator(int i) TEST_NOEXCEPT : data_(i) {++count;} - non_default_test_allocator(const non_default_test_allocator& a) TEST_NOEXCEPT - : data_(a.data_) {++count;} - template non_default_test_allocator(const non_default_test_allocator& a) TEST_NOEXCEPT - : data_(a.data_) {++count;} - ~non_default_test_allocator() TEST_NOEXCEPT {assert(data_ >= 0); --count; data_ = -1;} - pointer address(reference x) const {return &x;} - const_pointer address(const_reference x) const {return &x;} - pointer allocate(size_type n, const void* = 0) - { - assert(data_ >= 0); - if (time_to_throw >= throw_after) { +public: + typedef unsigned size_type; + typedef int difference_type; + typedef T value_type; + typedef value_type* pointer; + typedef const value_type* const_pointer; + typedef typename std::add_lvalue_reference::type reference; + typedef typename std::add_lvalue_reference::type const_reference; + + template + struct rebind { + typedef non_default_test_allocator other; + }; + + // non_default_test_allocator() TEST_NOEXCEPT : data_(0) {++count;} + explicit non_default_test_allocator(int i) TEST_NOEXCEPT : data_(i) { ++count; } + non_default_test_allocator(const non_default_test_allocator& a) TEST_NOEXCEPT : data_(a.data_) { ++count; } + template + non_default_test_allocator(const non_default_test_allocator& a) TEST_NOEXCEPT : data_(a.data_) { + ++count; + } + ~non_default_test_allocator() TEST_NOEXCEPT { + assert(data_ >= 0); + --count; + data_ = -1; + } + pointer address(reference x) const { return &x; } + const_pointer address(const_reference x) const { return &x; } + pointer allocate(size_type n, const void* = 0) { + assert(data_ >= 0); + if (time_to_throw >= throw_after) { #ifndef TEST_HAS_NO_EXCEPTIONS - throw std::bad_alloc(); + throw std::bad_alloc(); #else - std::terminate(); + std::terminate(); #endif - } - ++time_to_throw; - ++alloc_count; - return (pointer)::operator new (n * sizeof(T)); - } - void deallocate(pointer p, size_type) - {assert(data_ >= 0); --alloc_count; ::operator delete((void*)p); } - size_type max_size() const TEST_NOEXCEPT - {return UINT_MAX / sizeof(T);} + } + ++time_to_throw; + ++alloc_count; + return (pointer)::operator new(n * sizeof(T)); + } + void deallocate(pointer p, size_type) { + assert(data_ >= 0); + --alloc_count; + ::operator delete((void*)p); + } + size_type max_size() const TEST_NOEXCEPT { return UINT_MAX / sizeof(T); } #if TEST_STD_VER < 11 - void construct(pointer p, const T& val) - {::new(static_cast(p)) T(val);} + void construct(pointer p, const T& val) { ::new (static_cast(p)) T(val); } #else - template void construct(pointer p, U&& val) - {::new(static_cast(p)) T(std::forward(val));} + template + void construct(pointer p, U&& val) { + ::new (static_cast(p)) T(std::forward(val)); + } #endif - void destroy(pointer p) {p->~T();} + void destroy(pointer p) { p->~T(); } - friend bool operator==(const non_default_test_allocator& x, const non_default_test_allocator& y) - {return x.data_ == y.data_;} - friend bool operator!=(const non_default_test_allocator& x, const non_default_test_allocator& y) - {return !(x == y);} + friend bool operator==(const non_default_test_allocator& x, const non_default_test_allocator& y) { + return x.data_ == y.data_; + } + friend bool operator!=(const non_default_test_allocator& x, const non_default_test_allocator& y) { return !(x == y); } }; template <> -class test_allocator - : public test_alloc_base -{ - int data_; - int id_; +class test_allocator : public test_alloc_base { + int data_; + int id_; + + template + friend class test_allocator; - template friend class test_allocator; public: + typedef unsigned size_type; + typedef int difference_type; + typedef void value_type; + typedef value_type* pointer; + typedef const value_type* const_pointer; + + template + struct rebind { + typedef test_allocator other; + }; + + test_allocator() TEST_NOEXCEPT : data_(0), id_(0) {} + explicit test_allocator(int i, int id = 0) TEST_NOEXCEPT : data_(i), id_(id) {} + test_allocator(const test_allocator& a) TEST_NOEXCEPT : data_(a.data_), id_(a.id_) {} + template + test_allocator(const test_allocator& a) TEST_NOEXCEPT : data_(a.data_), id_(a.id_) {} + ~test_allocator() TEST_NOEXCEPT { + data_ = -1; + id_ = -1; + } - typedef unsigned size_type; - typedef int difference_type; - typedef void value_type; - typedef value_type* pointer; - typedef const value_type* const_pointer; - - template struct rebind {typedef test_allocator other;}; - - test_allocator() TEST_NOEXCEPT : data_(0), id_(0) {} - explicit test_allocator(int i, int id = 0) TEST_NOEXCEPT : data_(i), id_(id) {} - test_allocator(const test_allocator& a) TEST_NOEXCEPT - : data_(a.data_), id_(a.id_) {} - template test_allocator(const test_allocator& a) TEST_NOEXCEPT - : data_(a.data_), id_(a.id_) {} - ~test_allocator() TEST_NOEXCEPT {data_ = -1; id_ = -1; } - - int get_id() const { return id_; } - int get_data() const { return data_; } - - friend bool operator==(const test_allocator& x, const test_allocator& y) - {return x.data_ == y.data_;} - friend bool operator!=(const test_allocator& x, const test_allocator& y) - {return !(x == y);} + int get_id() const { return id_; } + int get_data() const { return data_; } + + friend bool operator==(const test_allocator& x, const test_allocator& y) { return x.data_ == y.data_; } + friend bool operator!=(const test_allocator& x, const test_allocator& y) { return !(x == y); } }; template -class other_allocator -{ - int data_; +class other_allocator { + int data_; - template friend class other_allocator; + template + friend class other_allocator; public: - typedef T value_type; + typedef T value_type; - other_allocator() : data_(-1) {} - explicit other_allocator(int i) : data_(i) {} - template other_allocator(const other_allocator& a) - : data_(a.data_) {} - T* allocate(std::size_t n) - {return (T*)::operator new(n * sizeof(T));} - void deallocate(T* p, std::size_t) - {::operator delete((void*)p);} + other_allocator() : data_(-1) {} + explicit other_allocator(int i) : data_(i) {} + template + other_allocator(const other_allocator& a) : data_(a.data_) {} + T* allocate(std::size_t n) { return (T*)::operator new(n * sizeof(T)); } + void deallocate(T* p, std::size_t) { ::operator delete((void*)p); } - other_allocator select_on_container_copy_construction() const - {return other_allocator(-2);} + other_allocator select_on_container_copy_construction() const { return other_allocator(-2); } - friend bool operator==(const other_allocator& x, const other_allocator& y) - {return x.data_ == y.data_;} - friend bool operator!=(const other_allocator& x, const other_allocator& y) - {return !(x == y);} + friend bool operator==(const other_allocator& x, const other_allocator& y) { return x.data_ == y.data_; } + friend bool operator!=(const other_allocator& x, const other_allocator& y) { return !(x == y); } - typedef std::true_type propagate_on_container_copy_assignment; - typedef std::true_type propagate_on_container_move_assignment; - typedef std::true_type propagate_on_container_swap; + typedef std::true_type propagate_on_container_copy_assignment; + typedef std::true_type propagate_on_container_move_assignment; + typedef std::true_type propagate_on_container_swap; #if TEST_STD_VER < 11 - std::size_t max_size() const - {return UINT_MAX / sizeof(T);} + std::size_t max_size() const { return UINT_MAX / sizeof(T); } #endif - }; #if TEST_STD_VER >= 11 struct Ctor_Tag {}; -template class TaggingAllocator; +template +class TaggingAllocator; struct Tag_X { // All constructors must be passed the Tag type. @@ -308,8 +308,8 @@ Tag_X(Ctor_Tag, Tag_X&&) {} // EmplaceConstructible into vector> from args. - template - Tag_X(Ctor_Tag, Args&&...) { } + template + Tag_X(Ctor_Tag, Args&&...) {} // not DefaultConstructible, CopyConstructible or MoveConstructible. Tag_X() = delete; @@ -324,48 +324,50 @@ private: // Not Destructible. - ~Tag_X() { } + ~Tag_X() {} // Erasable from vector>. friend class TaggingAllocator; }; - -template +template class TaggingAllocator { public: - using value_type = T; - TaggingAllocator() = default; + using value_type = T; + TaggingAllocator() = default; - template - TaggingAllocator(const TaggingAllocator&) { } + template + TaggingAllocator(const TaggingAllocator&) {} - T* allocate(std::size_t n) { return std::allocator{}.allocate(n); } + T* allocate(std::size_t n) { return std::allocator{}.allocate(n); } - void deallocate(T* p, std::size_t n) { std::allocator{}.deallocate(p, n); } + void deallocate(T* p, std::size_t n) { std::allocator{}.deallocate(p, n); } - template - void construct(Tag_X* p, Args&&... args) - { ::new((void*)p) Tag_X(Ctor_Tag{}, std::forward(args)...); } + template + void construct(Tag_X* p, Args&&... args) { + ::new ((void*)p) Tag_X(Ctor_Tag{}, std::forward(args)...); + } - template - void construct(U* p, Args&&... args) - { ::new((void*)p) U(std::forward(args)...); } + template + void construct(U* p, Args&&... args) { + ::new ((void*)p) U(std::forward(args)...); + } - template - void destroy(U* p) - { p->~U(); } + template + void destroy(U* p) { + p->~U(); + } }; -template -bool -operator==(const TaggingAllocator&, const TaggingAllocator&) -{ return true; } +template +bool operator==(const TaggingAllocator&, const TaggingAllocator&) { + return true; +} -template -bool -operator!=(const TaggingAllocator&, const TaggingAllocator&) -{ return false; } +template +bool operator!=(const TaggingAllocator&, const TaggingAllocator&) { + return false; +} #endif template @@ -376,10 +378,10 @@ limited_alloc_handle() : outstanding_(0), last_alloc_(nullptr) {} template - T *allocate(std::size_t N) { + T* allocate(std::size_t N) { if (N + outstanding_ > MaxAllocs) TEST_THROW(std::bad_alloc()); - last_alloc_ = ::operator new(N*sizeof(T)); + last_alloc_ = ::operator new(N * sizeof(T)); outstanding_ += N; return static_cast(last_alloc_); } @@ -395,50 +397,50 @@ }; template -class limited_allocator -{ - template friend class limited_allocator; - typedef limited_alloc_handle BuffT; - std::shared_ptr handle_; +class limited_allocator { + template + friend class limited_allocator; + typedef limited_alloc_handle BuffT; + std::shared_ptr handle_; + public: - typedef T value_type; - typedef value_type* pointer; - typedef const value_type* const_pointer; - typedef value_type& reference; - typedef const value_type& const_reference; - typedef std::size_t size_type; - typedef std::ptrdiff_t difference_type; + typedef T value_type; + typedef value_type* pointer; + typedef const value_type* const_pointer; + typedef value_type& reference; + typedef const value_type& const_reference; + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; - template struct rebind { typedef limited_allocator other; }; + template + struct rebind { + typedef limited_allocator other; + }; - limited_allocator() : handle_(new BuffT) {} + limited_allocator() : handle_(new BuffT) {} - limited_allocator(limited_allocator const& other) : handle_(other.handle_) {} + limited_allocator(limited_allocator const& other) : handle_(other.handle_) {} - template - explicit limited_allocator(limited_allocator const& other) - : handle_(other.handle_) {} + template + explicit limited_allocator(limited_allocator const& other) : handle_(other.handle_) {} - limited_allocator& operator=(const limited_allocator&) = delete; + limited_allocator& operator=(const limited_allocator&) = delete; - pointer allocate(size_type n) { return handle_->template allocate(n); } - void deallocate(pointer p, size_type n) { handle_->deallocate(p, n); } - size_type max_size() const {return N;} + pointer allocate(size_type n) { return handle_->template allocate(n); } + void deallocate(pointer p, size_type n) { handle_->deallocate(p, n); } + size_type max_size() const { return N; } - BuffT* getHandle() const { return handle_.get(); } + BuffT* getHandle() const { return handle_.get(); } }; template -inline bool operator==(limited_allocator const& LHS, - limited_allocator const& RHS) { +inline bool operator==(limited_allocator const& LHS, limited_allocator const& RHS) { return LHS.getHandle() == RHS.getHandle(); } template -inline bool operator!=(limited_allocator const& LHS, - limited_allocator const& RHS) { +inline bool operator!=(limited_allocator const& LHS, limited_allocator const& RHS) { return !(LHS == RHS); } - #endif // TEST_ALLOCATOR_H