forked from boostorg/unordered
Improved support for testing on older versions of gcc.
Link tests. Plus more... [SVN r2980]
This commit is contained in:
@@ -6,9 +6,12 @@
|
||||
#if !defined(BOOST_UNORDERED_TEST_OBJECTS_HEADER)
|
||||
#define BOOST_UNORDERED_TEST_OBJECTS_HEADER
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/limits.hpp>
|
||||
#include <cstddef>
|
||||
#include "../helpers/fwd.hpp"
|
||||
#include <ostream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
namespace test
|
||||
{
|
||||
@@ -144,6 +147,136 @@ namespace test
|
||||
}
|
||||
};
|
||||
|
||||
namespace detail
|
||||
{
|
||||
namespace {
|
||||
struct memory_area {
|
||||
void const* start;
|
||||
void const* end;
|
||||
|
||||
memory_area(void const* s, void const* e)
|
||||
: start(s), end(e)
|
||||
{
|
||||
}
|
||||
|
||||
// This is a bit dodgy as it defines overlapping
|
||||
// areas as 'equal', so this isn't a total ordering.
|
||||
// But it is for non-overlapping memory regions - which
|
||||
// is what'll be stored.
|
||||
//
|
||||
// All searches will be for areas entirely contained by
|
||||
// a member of the set - so it should find the area that contains
|
||||
// the region that is searched for.
|
||||
bool operator<(memory_area const& other) const {
|
||||
return end < other.start;
|
||||
}
|
||||
};
|
||||
|
||||
struct memory_track {
|
||||
explicit memory_track(int tag = -1) :
|
||||
constructed_(0),
|
||||
tag_(tag) {}
|
||||
|
||||
int constructed_;
|
||||
int tag_;
|
||||
};
|
||||
|
||||
std::map<memory_area, memory_track> allocated_memory;
|
||||
unsigned int count_allocators = 0;
|
||||
unsigned int count_allocations = 0;
|
||||
unsigned int count_constructions = 0;
|
||||
}
|
||||
|
||||
void allocator_ref()
|
||||
{
|
||||
if(count_allocators == 0) {
|
||||
count_allocations = 0;
|
||||
count_constructions = 0;
|
||||
allocated_memory.clear();
|
||||
}
|
||||
++count_allocators;
|
||||
}
|
||||
|
||||
void allocator_unref()
|
||||
{
|
||||
BOOST_TEST(count_allocators > 0);
|
||||
if(count_allocators > 0) {
|
||||
--count_allocators;
|
||||
if(count_allocators == 0) {
|
||||
bool no_allocations_left = (count_allocations == 0);
|
||||
bool no_constructions_left = (count_constructions == 0);
|
||||
bool allocated_memory_empty = allocated_memory.empty();
|
||||
|
||||
// Clearing the data before the checks terminate the tests.
|
||||
count_allocations = 0;
|
||||
count_constructions = 0;
|
||||
allocated_memory.clear();
|
||||
|
||||
BOOST_TEST(no_allocations_left);
|
||||
BOOST_TEST(no_constructions_left);
|
||||
BOOST_TEST(allocated_memory_empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void track_allocate(void *ptr, std::size_t n, std::size_t size, int tag)
|
||||
{
|
||||
if(n == 0) {
|
||||
// TODO: This is unspecified - not undefined, so what to do?
|
||||
}
|
||||
else {
|
||||
++count_allocations;
|
||||
allocated_memory[memory_area(ptr, (char*) ptr + n * size)] =
|
||||
memory_track(tag);
|
||||
}
|
||||
}
|
||||
|
||||
void track_deallocate(void* ptr, std::size_t n, std::size_t size, int tag)
|
||||
{
|
||||
std::map<memory_area, memory_track>::iterator pos
|
||||
= allocated_memory.find(memory_area(ptr, ptr));
|
||||
if(pos == allocated_memory.end()) {
|
||||
BOOST_ERROR("Deallocating unknown pointer.");
|
||||
} else {
|
||||
// TODO: Not exception safe.
|
||||
BOOST_TEST(pos->first.start == ptr);
|
||||
BOOST_TEST(pos->first.end == (char*) ptr + n * size);
|
||||
BOOST_TEST(pos->second.tag_ == tag);
|
||||
BOOST_TEST(pos->second.constructed_ == 0);
|
||||
allocated_memory.erase(pos);
|
||||
}
|
||||
BOOST_TEST(count_allocations > 0);
|
||||
if(count_allocations > 0) --count_allocations;
|
||||
}
|
||||
|
||||
void track_construct(void* ptr, std::size_t size, int tag)
|
||||
{
|
||||
std::map<memory_area, memory_track>::iterator pos
|
||||
= allocated_memory.find(memory_area(ptr, ptr));
|
||||
if(pos == allocated_memory.end())
|
||||
BOOST_ERROR("Constructing unknown pointer.");
|
||||
BOOST_TEST(pos->second.tag_ == tag);
|
||||
//TODO: Track the number of allocations, and make sure the number
|
||||
// of constructions doesn't exceed it. If you're feeling keen,
|
||||
// perhaps track the individual objects in the array.
|
||||
++count_constructions;
|
||||
++pos->second.constructed_;
|
||||
}
|
||||
|
||||
void track_destroy(void* ptr, std::size_t size, int tag)
|
||||
{
|
||||
std::map<memory_area, memory_track>::iterator pos
|
||||
= allocated_memory.find(memory_area(ptr, ptr));
|
||||
if(pos == allocated_memory.end())
|
||||
BOOST_ERROR("Destroying unknown pointer.");
|
||||
BOOST_TEST(count_constructions > 0);
|
||||
BOOST_TEST(pos->second.tag_ == tag);
|
||||
BOOST_TEST(pos->second.constructed_ > 0);
|
||||
if(count_constructions > 0) --count_constructions;
|
||||
if(pos->second.constructed_ > 0) --pos->second.constructed_;
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
class allocator
|
||||
{
|
||||
@@ -164,30 +297,43 @@ namespace test
|
||||
|
||||
template <class U> struct rebind { typedef allocator<U> other; };
|
||||
|
||||
explicit allocator(int t = 0) : tag_(t) {}
|
||||
template <class Y> allocator(allocator<Y> const& x) : tag_(x.tag_) {}
|
||||
allocator(allocator const& x) : tag_(x.tag_) {}
|
||||
~allocator() {}
|
||||
explicit allocator(int t = 0) : tag_(t) { detail::allocator_ref(); }
|
||||
template <class Y> allocator(allocator<Y> const& x) : tag_(x.tag_) { detail::allocator_ref(); }
|
||||
allocator(allocator const& x) : tag_(x.tag_) { detail::allocator_ref(); }
|
||||
~allocator() { detail::allocator_unref(); }
|
||||
|
||||
// TODO: Shall I check these?
|
||||
pointer address(reference r) { return pointer(&r); }
|
||||
const_pointer address(const_reference r) { return const_pointer(&r); }
|
||||
|
||||
pointer allocate(size_type n) {
|
||||
return pointer(static_cast<T*>(::operator new(n * sizeof(T))));
|
||||
pointer ptr(static_cast<T*>(::operator new(n * sizeof(T))));
|
||||
detail::track_allocate((void*) ptr, n, sizeof(T), tag_);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
pointer allocate(size_type n, const_pointer u)
|
||||
{
|
||||
return pointer(static_cast<T*>(::operator new(n * sizeof(T))));
|
||||
pointer ptr(static_cast<T*>(::operator new(n * sizeof(T))));
|
||||
detail::track_allocate((void*) ptr, n, sizeof(T), tag_);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void deallocate(pointer p, size_type n)
|
||||
{
|
||||
detail::track_deallocate((void*) p, n, sizeof(T), tag_);
|
||||
::operator delete((void*) p);
|
||||
}
|
||||
|
||||
void construct(pointer p, T const& t) { new(p) T(t); }
|
||||
void destroy(pointer p) { p->~T(); }
|
||||
void construct(pointer p, T const& t) {
|
||||
detail::track_construct((void*) p, sizeof(T), tag_);
|
||||
new(p) T(t);
|
||||
}
|
||||
|
||||
void destroy(pointer p) {
|
||||
detail::track_destroy((void*) p, sizeof(T), tag_);
|
||||
p->~T();
|
||||
}
|
||||
|
||||
size_type max_size() const {
|
||||
return (std::numeric_limits<size_type>::max)();
|
||||
|
||||
Reference in New Issue
Block a user