Merge the latest unordered changes. These are concerned with getting the tests

working on more compilers. The biggest change is that the exception tests have
been changed to use a very simple exception testing mechanism on top of
lightweight_test. This was because Boost.Test exception testing isn't working
on several platforms. I'm trying to set this up so that I can use Boost.Test on
compilers which it completely supports, and lightweight test on others.
Boost.Test tests more than my simple exception testing code ever will so it's
worth using where I can.


[SVN r42698]
This commit is contained in:
Daniel James
2008-01-12 14:43:40 +00:00
parent 9d7411840e
commit be93c29493
11 changed files with 154 additions and 67 deletions

View File

@@ -5,7 +5,8 @@
import testing ; import testing ;
alias framework : /boost/test//boost_unit_test_framework ; #alias framework : /boost/test//boost_unit_test_framework ;
alias framework : ;
project unordered-test/exception-tests project unordered-test/exception-tests
: requirements : requirements

View File

@@ -4,10 +4,6 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include "./containers.hpp" #include "./containers.hpp"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <boost/test/exception_safety.hpp>
#include "../helpers/random_values.hpp" #include "../helpers/random_values.hpp"
#include "../helpers/invariants.hpp" #include "../helpers/invariants.hpp"

View File

@@ -4,10 +4,6 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include "./containers.hpp" #include "./containers.hpp"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <boost/test/exception_safety.hpp>
#include "../helpers/random_values.hpp" #include "../helpers/random_values.hpp"
#include "../helpers/input_iterator.hpp" #include "../helpers/input_iterator.hpp"

View File

@@ -4,10 +4,6 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include "./containers.hpp" #include "./containers.hpp"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <boost/test/exception_safety.hpp>
#include "../helpers/random_values.hpp" #include "../helpers/random_values.hpp"
test::seed_t seed(73041); test::seed_t seed(73041);

View File

@@ -4,10 +4,6 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include "./containers.hpp" #include "./containers.hpp"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <boost/test/exception_safety.hpp>
#include "../helpers/random_values.hpp" #include "../helpers/random_values.hpp"
#include "../helpers/invariants.hpp" #include "../helpers/invariants.hpp"
#include "../helpers/helpers.hpp" #include "../helpers/helpers.hpp"
@@ -29,7 +25,7 @@ struct erase_test_base : public test::exception_base
void check(T const& x) const { void check(T const& x) const {
std::string scope(test::scope); std::string scope(test::scope);
HASH_CHECK(scope.find("hash::") != std::string::npos || UNORDERED_CHECK(scope.find("hash::") != std::string::npos ||
scope.find("equal_to::") != std::string::npos || scope.find("equal_to::") != std::string::npos ||
scope == "operator==(object, object)"); scope == "operator==(object, object)");

View File

@@ -4,16 +4,12 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include "./containers.hpp" #include "./containers.hpp"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <boost/test/exception_safety.hpp>
#include <string> #include <string>
#include "../helpers/random_values.hpp" #include "../helpers/random_values.hpp"
#include "../helpers/invariants.hpp" #include "../helpers/invariants.hpp"
#include "../helpers/strong.hpp" #include "../helpers/strong.hpp"
#include "../helpers/input_iterator.hpp" #include "../helpers/input_iterator.hpp"
#include <boost/utility.hpp>
#include <cmath> #include <cmath>
test::seed_t seed(747373); test::seed_t seed(747373);
@@ -105,17 +101,18 @@ struct insert_test_rehash1 : public insert_test_base<T>
insert_test_rehash1() : insert_test_base<T>(1000) {} insert_test_rehash1() : insert_test_base<T>(1000) {}
T init() const { T init() const {
using namespace std;
typedef BOOST_DEDUCED_TYPENAME T::size_type size_type; typedef BOOST_DEDUCED_TYPENAME T::size_type size_type;
T x; T x;
x.max_load_factor(0.25); x.max_load_factor(0.25);
size_type bucket_count = x.bucket_count(); size_type bucket_count = x.bucket_count();
size_type initial_elements = static_cast<size_type>( size_type initial_elements = static_cast<size_type>(
std::ceil(bucket_count * x.max_load_factor()) - 1); ceil(bucket_count * (double) x.max_load_factor()) - 1);
BOOST_REQUIRE(initial_elements < this->values.size()); UNORDERED_REQUIRE(initial_elements < this->values.size());
x.insert(this->values.begin(), x.insert(this->values.begin(),
boost::next(this->values.begin(), initial_elements)); boost::next(this->values.begin(), initial_elements));
BOOST_REQUIRE(bucket_count == x.bucket_count()); UNORDERED_REQUIRE(bucket_count == x.bucket_count());
return x; return x;
} }
@@ -134,7 +131,7 @@ struct insert_test_rehash1 : public insert_test_base<T>
// This isn't actually a failure, but it means the test isn't doing its // This isn't actually a failure, but it means the test isn't doing its
// job. // job.
BOOST_REQUIRE(x.bucket_count() != bucket_count); UNORDERED_REQUIRE(x.bucket_count() != bucket_count);
} }
}; };
@@ -157,7 +154,7 @@ struct insert_test_rehash2 : public insert_test_rehash1<T>
// This isn't actually a failure, but it means the test isn't doing its // This isn't actually a failure, but it means the test isn't doing its
// job. // job.
BOOST_REQUIRE(x.bucket_count() != bucket_count); UNORDERED_REQUIRE(x.bucket_count() != bucket_count);
} }
}; };
@@ -169,6 +166,7 @@ struct insert_test_rehash3 : public insert_test_base<T>
insert_test_rehash3() : insert_test_base<T>(1000) {} insert_test_rehash3() : insert_test_base<T>(1000) {}
T init() const { T init() const {
using namespace std;
typedef BOOST_DEDUCED_TYPENAME T::size_type size_type; typedef BOOST_DEDUCED_TYPENAME T::size_type size_type;
T x; T x;
@@ -176,14 +174,14 @@ struct insert_test_rehash3 : public insert_test_base<T>
original_bucket_count = x.bucket_count(); original_bucket_count = x.bucket_count();
rehash_bucket_count = static_cast<size_type>( rehash_bucket_count = static_cast<size_type>(
std::ceil(original_bucket_count * x.max_load_factor())) - 1; ceil(original_bucket_count * (double) x.max_load_factor())) - 1;
size_type initial_elements = rehash_bucket_count - 5; size_type initial_elements = rehash_bucket_count - 5;
BOOST_REQUIRE(initial_elements < this->values.size()); UNORDERED_REQUIRE(initial_elements < this->values.size());
x.insert(this->values.begin(), x.insert(this->values.begin(),
boost::next(this->values.begin(), initial_elements)); boost::next(this->values.begin(), initial_elements));
BOOST_REQUIRE(original_bucket_count == x.bucket_count()); UNORDERED_REQUIRE(original_bucket_count == x.bucket_count());
return x; return x;
} }
@@ -195,7 +193,7 @@ struct insert_test_rehash3 : public insert_test_base<T>
// This isn't actually a failure, but it means the test isn't doing its // This isn't actually a failure, but it means the test isn't doing its
// job. // job.
BOOST_REQUIRE(x.bucket_count() != bucket_count); UNORDERED_REQUIRE(x.bucket_count() != bucket_count);
} }
void check(T const& x) const { void check(T const& x) const {

View File

@@ -4,10 +4,6 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include "./containers.hpp" #include "./containers.hpp"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <boost/test/exception_safety.hpp>
#include <string> #include <string>
#include "../helpers/random_values.hpp" #include "../helpers/random_values.hpp"
#include "../helpers/invariants.hpp" #include "../helpers/invariants.hpp"

View File

@@ -4,10 +4,6 @@
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include "./containers.hpp" #include "./containers.hpp"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include <boost/test/exception_safety.hpp>
#include "../helpers/random_values.hpp" #include "../helpers/random_values.hpp"
#include "../helpers/invariants.hpp" #include "../helpers/invariants.hpp"
@@ -26,7 +22,7 @@ struct self_swap_base : public test::exception_base
std::string scope(test::scope); std::string scope(test::scope);
#if BOOST_UNORDERED_SWAP_METHOD != 2 #if BOOST_UNORDERED_SWAP_METHOD != 2
HASH_CHECK( UNORDERED_CHECK(
scope == "hash::operator(hash)" || scope == "hash::operator(hash)" ||
scope == "hash::operator=(hash)" || scope == "hash::operator=(hash)" ||
scope == "equal_to::operator(equal_to)" || scope == "equal_to::operator(equal_to)" ||
@@ -77,7 +73,7 @@ struct swap_base : public test::exception_base
std::string scope(test::scope); std::string scope(test::scope);
#if BOOST_UNORDERED_SWAP_METHOD != 2 #if BOOST_UNORDERED_SWAP_METHOD != 2
HASH_CHECK( UNORDERED_CHECK(
scope == "hash::operator(hash)" || scope == "hash::operator(hash)" ||
scope == "hash::operator=(hash)" || scope == "hash::operator=(hash)" ||
scope == "equal_to::operator(equal_to)" || scope == "equal_to::operator(equal_to)" ||

19
test/helpers/test.hpp Normal file
View File

@@ -0,0 +1,19 @@
// 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_TEST_HEADER)
#define BOOST_UNORDERED_TEST_TEST_HEADER
#if defined(BOOST_UNORDERED_USE_TEST)
#include <boost/test/test_tools.hpp>
#define UNORDERED_CHECK(x) BOOST_CHECK(x)
#define UNORDERED_REQUIRE(x) BOOST_REQUIRE(x)
#else
#include <boost/detail/lightweight_test.hpp>
#define UNORDERED_CHECK(x) BOOST_TEST(x)
#define UNORDERED_REQUIRE(x) if(!(x)) { BOOST_ERROR(BOOST_STRINGIZE(x)); throw test::lightweight::test_failure(); }
#endif
#endif

View File

@@ -6,24 +6,32 @@
#if !defined(BOOST_UNORDERED_TEST_OBJECTS_HEADER) #if !defined(BOOST_UNORDERED_TEST_OBJECTS_HEADER)
#define BOOST_UNORDERED_TEST_OBJECTS_HEADER #define BOOST_UNORDERED_TEST_OBJECTS_HEADER
#include <cstddef> #if defined(BOOST_UNORDERED_USE_TEST)
#include <boost/limits.hpp> #define BOOST_TEST_MAIN
#include <boost/test/test_tools.hpp>
#include <boost/test/exception_safety.hpp> #include <boost/test/exception_safety.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#else
#include <boost/detail/lightweight_test.hpp>
#endif
#include <cstddef>
#include <iostream>
#include <boost/limits.hpp>
#include <boost/preprocessor/seq/for_each_product.hpp> #include <boost/preprocessor/seq/for_each_product.hpp>
#include <boost/preprocessor/seq/elem.hpp> #include <boost/preprocessor/seq/elem.hpp>
#include <boost/preprocessor/cat.hpp> #include <boost/preprocessor/cat.hpp>
#include <iostream>
#include "../helpers/fwd.hpp" #include "../helpers/fwd.hpp"
#include "../helpers/allocator.hpp" #include "../helpers/allocator.hpp"
#include "memory.hpp" #include "./memory.hpp"
#include <map>
#define RUN_EXCEPTION_TESTS(test_seq, param_seq) \ #define RUN_EXCEPTION_TESTS(test_seq, param_seq) \
BOOST_PP_SEQ_FOR_EACH_PRODUCT(RUN_EXCEPTION_TESTS_OP, (test_seq)(param_seq)) UNORDERED_EXCEPTION_TEST_PREFIX \
BOOST_PP_SEQ_FOR_EACH_PRODUCT(RUN_EXCEPTION_TESTS_OP, (test_seq)(param_seq)) \
UNORDERED_EXCEPTION_TEST_POSTFIX
#define RUN_EXCEPTION_TESTS_OP(r, product) \ #define RUN_EXCEPTION_TESTS_OP(r, product) \
RUN_EXCEPTION_TESTS_OP2( \ UNORDERED_EXCEPTION_TEST_CASE( \
BOOST_PP_CAT(BOOST_PP_SEQ_ELEM(0, product), \ BOOST_PP_CAT(BOOST_PP_SEQ_ELEM(0, product), \
BOOST_PP_CAT(_, BOOST_PP_SEQ_ELEM(1, product)) \ BOOST_PP_CAT(_, BOOST_PP_SEQ_ELEM(1, product)) \
), \ ), \
@@ -31,12 +39,25 @@
BOOST_PP_SEQ_ELEM(1, product) \ BOOST_PP_SEQ_ELEM(1, product) \
) )
#define RUN_EXCEPTION_TESTS_OP2(name, test_func, type) \
#if defined(BOOST_UNORDERED_USE_TEST)
#define UNORDERED_EXCEPTION_TEST_PREFIX
#define UNORDERED_EXCEPTION_TEST_CASE(name, test_func, type) \
BOOST_AUTO_TEST_CASE(name) \ BOOST_AUTO_TEST_CASE(name) \
{ \ { \
test_func< type > fixture; \ test_func< type > fixture; \
::test::exception_safety(fixture, BOOST_STRINGIZE(test_func<type>)); \ ::test::exception_safety(fixture, BOOST_STRINGIZE(test_func<type>)); \
} }
#define UNORDERED_EXCEPTION_TEST_POSTFIX
#else
#define UNORDERED_EXCEPTION_TEST_PREFIX int main() {
#define UNORDERED_EXCEPTION_TEST_CASE(name, test_func, type) \
{ \
test_func< type > fixture; \
::test::lightweight::exception_safety(fixture, BOOST_STRINGIZE(test_func<type>)); \
}
#define UNORDERED_EXCEPTION_TEST_POSTFIX return boost::report_errors(); }
#endif
#define SCOPE(scope_name) \ #define SCOPE(scope_name) \
for(::test::scope_guard unordered_test_guard( \ for(::test::scope_guard unordered_test_guard( \
@@ -44,10 +65,17 @@
!unordered_test_guard.dismissed(); \ !unordered_test_guard.dismissed(); \
unordered_test_guard.dismiss()) unordered_test_guard.dismiss())
#if defined(BOOST_UNORDERED_USE_TEST)
#define EPOINT(name) \ #define EPOINT(name) \
if(::test::exceptions_enabled) { \ if(::test::exceptions_enabled) { \
BOOST_ITEST_EPOINT(name); \ BOOST_ITEST_EPOINT(name); \
} }
#else
#define EPOINT(name) \
if(::test::exceptions_enabled) { \
::test::lightweight::epoint(name); \
}
#endif
#define ENABLE_EXCEPTIONS \ #define ENABLE_EXCEPTIONS \
::test::exceptions_enable BOOST_PP_CAT(ENABLE_EXCEPTIONS_, __LINE__)(true) ::test::exceptions_enable BOOST_PP_CAT(ENABLE_EXCEPTIONS_, __LINE__)(true)
@@ -155,21 +183,71 @@ namespace test {
strong.store(x); strong.store(x);
try { try {
ENABLE_EXCEPTIONS; ENABLE_EXCEPTIONS;
call_ignore_extra_parameters(&Test::run, test_, x, strong); call_ignore_extra_parameters<Test, BOOST_DEDUCED_TYPENAME Test::data_type, BOOST_DEDUCED_TYPENAME Test::strong_type>(&Test::run, test_, x, strong);
} }
catch(...) { catch(...) {
call_ignore_extra_parameters(&Test::check, test_, call_ignore_extra_parameters<Test, BOOST_DEDUCED_TYPENAME Test::data_type const, BOOST_DEDUCED_TYPENAME Test::strong_type const>(&Test::check, test_,
constant(x), constant(strong)); constant(x), constant(strong));
throw; throw;
} }
} }
}; };
#if defined(BOOST_UNORDERED_USE_TEST)
template <class Test> template <class Test>
void exception_safety(Test const& f, char const* name) { void exception_safety(Test const& f, char const* name) {
test_runner<Test> runner(f); test_runner<Test> runner(f);
::boost::itest::exception_safety(runner, name); ::boost::itest::exception_safety(runner, name);
} }
#else
// Quick exception testing based on lightweight test
namespace lightweight {
static int iteration;
static int count;
struct test_exception {
char const* name;
test_exception(char const* n) : name(n) {}
};
struct test_failure {
};
void epoint(char const* name) {
++count;
if(count == iteration) {
throw test_exception(name);
}
}
template <class Test>
void exception_safety(Test const& f, char const* name) {
test_runner<Test> runner(f);
iteration = 0;
bool success = false;
do {
++iteration;
count = 0;
try {
runner();
success = true;
}
catch(test_failure) {
break;
}
catch(...) {
}
} while(!success);
}
}
#endif
} }
namespace test namespace test
@@ -550,5 +628,15 @@ namespace exception
} }
} }
// Workaround for ADL deficient compilers
#if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
namespace test
{
test::exception::object generate(test::exception::object const* x) {
return test::exception::generate(x);
}
}
#endif
#endif #endif

View File

@@ -6,7 +6,11 @@
#if !defined(BOOST_UNORDERED_TEST_MEMORY_HEADER) #if !defined(BOOST_UNORDERED_TEST_MEMORY_HEADER)
#define BOOST_UNORDERED_TEST_MEMORY_HEADER #define BOOST_UNORDERED_TEST_MEMORY_HEADER
#define HASH_CHECK(test) if(!(test)) BOOST_ERROR(BOOST_STRINGIZE(test)) #include <memory>
#include <map>
#include <boost/mpl/apply.hpp>
#include <boost/assert.hpp>
#include "../helpers/test.hpp"
namespace test namespace test
{ {
@@ -58,8 +62,7 @@ namespace test
template <class AllocatorHolder = default_allocator_holder> template <class AllocatorHolder = default_allocator_holder>
struct memory_tracker { struct memory_tracker {
typedef std::map<memory_area, memory_track, memory_area_compare, typedef std::map<memory_area, memory_track, memory_area_compare,
BOOST_DEDUCED_TYPENAME AllocatorHolder:: BOOST_DEDUCED_TYPENAME boost::mpl::apply1<AllocatorHolder, std::pair<memory_area const, memory_track> >::type
template apply<std::pair<memory_area const, memory_track> >::type
> allocated_memory_type; > allocated_memory_type;
allocated_memory_type allocated_memory; allocated_memory_type allocated_memory;
@@ -84,7 +87,7 @@ namespace test
void allocator_unref() void allocator_unref()
{ {
HASH_CHECK(count_allocators > 0); UNORDERED_CHECK(count_allocators > 0);
if(count_allocators > 0) { if(count_allocators > 0) {
--count_allocators; --count_allocators;
if(count_allocators == 0) { if(count_allocators == 0) {
@@ -97,9 +100,9 @@ namespace test
count_constructions = 0; count_constructions = 0;
allocated_memory.clear(); allocated_memory.clear();
HASH_CHECK(no_allocations_left); UNORDERED_CHECK(no_allocations_left);
HASH_CHECK(no_constructions_left); UNORDERED_CHECK(no_constructions_left);
HASH_CHECK(allocated_memory_empty); UNORDERED_CHECK(allocated_memory_empty);
} }
} }
} }
@@ -111,8 +114,10 @@ namespace test
} }
else { else {
++count_allocations; ++count_allocations;
allocated_memory[memory_area(ptr, (char*) ptr + n * size)] = allocated_memory.insert(
memory_track(tag); std::pair<memory_area const, memory_track>(
memory_area(ptr, (char*) ptr + n * size),
memory_track(tag)));
} }
} }
@@ -123,12 +128,12 @@ namespace test
if(pos == allocated_memory.end()) { if(pos == allocated_memory.end()) {
BOOST_ERROR("Deallocating unknown pointer."); BOOST_ERROR("Deallocating unknown pointer.");
} else { } else {
HASH_CHECK(pos->first.start == ptr); UNORDERED_CHECK(pos->first.start == ptr);
HASH_CHECK(pos->first.end == (char*) ptr + n * size); UNORDERED_CHECK(pos->first.end == (char*) ptr + n * size);
HASH_CHECK(pos->second.tag_ == tag); UNORDERED_CHECK(pos->second.tag_ == tag);
allocated_memory.erase(pos); allocated_memory.erase(pos);
} }
HASH_CHECK(count_allocations > 0); UNORDERED_CHECK(count_allocations > 0);
if(count_allocations > 0) --count_allocations; if(count_allocations > 0) --count_allocations;
} }
@@ -139,7 +144,7 @@ namespace test
void track_destroy(void* ptr, std::size_t /*size*/, int tag) void track_destroy(void* ptr, std::size_t /*size*/, int tag)
{ {
HASH_CHECK(count_constructions > 0); UNORDERED_CHECK(count_constructions > 0);
if(count_constructions > 0) --count_constructions; if(count_constructions > 0) --count_constructions;
} }
}; };