From 10a05bda054cf0ab849e7e3d29e25aa861b68960 Mon Sep 17 00:00:00 2001 From: Daniel James Date: Sun, 1 Jul 2007 11:54:22 +0000 Subject: [PATCH] Move malloc_allocator into its own header so that the normal tests don't have to depend on the exception testing code. [SVN r7332] --- test/helpers/allocator.hpp | 53 +++++++++++++++++++++++++++++++++++++ test/helpers/invariants.hpp | 6 ++--- test/objects/exception.hpp | 41 ++-------------------------- 3 files changed, 58 insertions(+), 42 deletions(-) create mode 100644 test/helpers/allocator.hpp diff --git a/test/helpers/allocator.hpp b/test/helpers/allocator.hpp new file mode 100644 index 00000000..e78280ba --- /dev/null +++ b/test/helpers/allocator.hpp @@ -0,0 +1,53 @@ + +// Copyright 2006-2007 Daniel James. +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#if !defined(BOOST_UNORDERED_TEST_MALLOC_ALLOCATOR_HEADER) +#define BOOST_UNORDERED_TEST_MALLOC_ALLOCATOR_HEADER + +#include +#include +#include + +namespace test +{ + template + struct malloc_allocator + { + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + typedef T* pointer; + typedef T const* const_pointer; + typedef T& reference; + typedef T const& const_reference; + typedef T value_type; + + template struct rebind { typedef malloc_allocator other; }; + + malloc_allocator() {} + template malloc_allocator(malloc_allocator const& x) {} + malloc_allocator(malloc_allocator const& x) {} + + pointer address(reference r) { return &r; } + const_pointer address(const_reference r) { return &r; } + + pointer allocate(size_type n) { + return static_cast(malloc(n * sizeof(T))); + } + + pointer allocate(size_type n, const_pointer u) { return allocate(n); } + void deallocate(pointer p, size_type n) { free(p); } + void construct(pointer p, T const& t) { new(p) T(t); } + void destroy(pointer p) { p->~T(); } + + size_type max_size() const { + return (std::numeric_limits::max)(); + } + + bool operator==(malloc_allocator const& x) const { return true; } + bool operator!=(malloc_allocator const& x) const { return false; } + }; +} + +#endif diff --git a/test/helpers/invariants.hpp b/test/helpers/invariants.hpp index 91d5b2d6..a4eca0b0 100644 --- a/test/helpers/invariants.hpp +++ b/test/helpers/invariants.hpp @@ -13,6 +13,7 @@ #include #include "./metafunctions.hpp" #include "./helpers.hpp" +#include "./allocator.hpp" namespace test { @@ -23,7 +24,7 @@ namespace test typedef typename X::key_type key_type; // Boost.Test was reporting memory leaks for std::set on g++-3.3. // So I work around it by using malloc. - std::set, test::exception::detail::malloc_allocator > found_; + std::set, test::malloc_allocator > found_; typename X::const_iterator it = x1.begin(), end = x1.end(); typename X::size_type size = 0; @@ -32,9 +33,8 @@ namespace test // to test either that keys are unique or that equivalent keys are // adjacent. (6.3.1/6) key_type key = get_key(*it); - if(found_.find(key) != found_.end()) + if(!found_.insert(key).second) BOOST_ERROR("Elements with equivalent keys aren't adjacent."); - found_.insert(key); // Iterate over equivalent keys, counting them. unsigned int count = 0; diff --git a/test/objects/exception.hpp b/test/objects/exception.hpp index 6506b3d8..5cb90c18 100644 --- a/test/objects/exception.hpp +++ b/test/objects/exception.hpp @@ -14,8 +14,8 @@ #include #include #include -#include #include "../helpers/fwd.hpp" +#include "../helpers/allocator.hpp" #include #define RUN_EXCEPTION_TESTS(test_seq, param_seq) \ @@ -185,43 +185,6 @@ namespace exception // the most convenient way. namespace { - template - struct malloc_allocator - { - typedef std::size_t size_type; - typedef std::ptrdiff_t difference_type; - typedef T* pointer; - typedef T const* const_pointer; - typedef T& reference; - typedef T const& const_reference; - typedef T value_type; - - template struct rebind { typedef malloc_allocator other; }; - - malloc_allocator() {} - template malloc_allocator(malloc_allocator const& x) {} - malloc_allocator(malloc_allocator const& x) {} - - pointer address(reference r) { return &r; } - const_pointer address(const_reference r) { return &r; } - - pointer allocate(size_type n) { - return static_cast(malloc(n * sizeof(T))); - } - - pointer allocate(size_type n, const_pointer u) { return allocate(n); } - void deallocate(pointer p, size_type n) { free(p); } - void construct(pointer p, T const& t) { new(p) T(t); } - void destroy(pointer p) { p->~T(); } - - size_type max_size() const { - return (std::numeric_limits::max)(); - } - - bool operator==(malloc_allocator const& x) const { return true; } - bool operator!=(malloc_allocator const& x) const { return false; } - }; - struct memory_area { void const* start; void const* end; @@ -252,7 +215,7 @@ namespace exception }; typedef std::map, - malloc_allocator > > + test::malloc_allocator > > allocated_memory_type; allocated_memory_type allocated_memory; unsigned int count_allocators = 0;