|
Boost-Commit : |
From: daniel_james_at_[hidden]
Date: 2008-01-10 18:32:30
Author: danieljames
Date: 2008-01-10 18:32:30 EST (Thu, 10 Jan 2008)
New Revision: 42669
URL: http://svn.boost.org/trac/boost/changeset/42669
Log:
Quickly hack together exception testing based on the lightweight test header.
There are several compilers which Boost.Test doesn't work on and I'd like to
run the exception tests on them. But having said that, Boost.Test offers much
that this doesn't so I'm implementing it so that, hopefully, compilers which
can use Boost.Test can be switched back in the future.
But this needs more testing before I add it to the boost trunk.
Text files modified:
branches/unordered/trunk/libs/unordered/test/exception/Jamfile.v2 | 3
branches/unordered/trunk/libs/unordered/test/exception/erase_exception_tests.cpp | 2
branches/unordered/trunk/libs/unordered/test/exception/insert_exception_tests.cpp | 16 +++---
branches/unordered/trunk/libs/unordered/test/exception/swap_exception_tests.cpp | 4
branches/unordered/trunk/libs/unordered/test/objects/exception.hpp | 84 +++++++++++++++++++++++++++++++++++++--
branches/unordered/trunk/libs/unordered/test/objects/memory.hpp | 26 +++++++----
6 files changed, 107 insertions(+), 28 deletions(-)
Modified: branches/unordered/trunk/libs/unordered/test/exception/Jamfile.v2
==============================================================================
--- branches/unordered/trunk/libs/unordered/test/exception/Jamfile.v2 (original)
+++ branches/unordered/trunk/libs/unordered/test/exception/Jamfile.v2 2008-01-10 18:32:30 EST (Thu, 10 Jan 2008)
@@ -5,7 +5,8 @@
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
: requirements
Modified: branches/unordered/trunk/libs/unordered/test/exception/erase_exception_tests.cpp
==============================================================================
--- branches/unordered/trunk/libs/unordered/test/exception/erase_exception_tests.cpp (original)
+++ branches/unordered/trunk/libs/unordered/test/exception/erase_exception_tests.cpp 2008-01-10 18:32:30 EST (Thu, 10 Jan 2008)
@@ -25,7 +25,7 @@
void check(T const& x) const {
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 == "operator==(object, object)");
Modified: branches/unordered/trunk/libs/unordered/test/exception/insert_exception_tests.cpp
==============================================================================
--- branches/unordered/trunk/libs/unordered/test/exception/insert_exception_tests.cpp (original)
+++ branches/unordered/trunk/libs/unordered/test/exception/insert_exception_tests.cpp 2008-01-10 18:32:30 EST (Thu, 10 Jan 2008)
@@ -9,7 +9,7 @@
#include "../helpers/invariants.hpp"
#include "../helpers/strong.hpp"
#include "../helpers/input_iterator.hpp"
-
+#include <boost/utility.hpp>
#include <cmath>
test::seed_t seed(747373);
@@ -108,10 +108,10 @@
size_type bucket_count = x.bucket_count();
size_type initial_elements = static_cast<size_type>(
std::ceil(bucket_count * x.max_load_factor()) - 1);
- BOOST_REQUIRE(initial_elements < this->values.size());
+ UNORDERED_REQUIRE(initial_elements < this->values.size());
x.insert(this->values.begin(),
boost::next(this->values.begin(), initial_elements));
- BOOST_REQUIRE(bucket_count == x.bucket_count());
+ UNORDERED_REQUIRE(bucket_count == x.bucket_count());
return x;
}
@@ -130,7 +130,7 @@
// This isn't actually a failure, but it means the test isn't doing its
// job.
- BOOST_REQUIRE(x.bucket_count() != bucket_count);
+ UNORDERED_REQUIRE(x.bucket_count() != bucket_count);
}
};
@@ -153,7 +153,7 @@
// This isn't actually a failure, but it means the test isn't doing its
// job.
- BOOST_REQUIRE(x.bucket_count() != bucket_count);
+ UNORDERED_REQUIRE(x.bucket_count() != bucket_count);
}
};
@@ -176,10 +176,10 @@
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(),
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;
}
@@ -191,7 +191,7 @@
// This isn't actually a failure, but it means the test isn't doing its
// job.
- BOOST_REQUIRE(x.bucket_count() != bucket_count);
+ UNORDERED_REQUIRE(x.bucket_count() != bucket_count);
}
void check(T const& x) const {
Modified: branches/unordered/trunk/libs/unordered/test/exception/swap_exception_tests.cpp
==============================================================================
--- branches/unordered/trunk/libs/unordered/test/exception/swap_exception_tests.cpp (original)
+++ branches/unordered/trunk/libs/unordered/test/exception/swap_exception_tests.cpp 2008-01-10 18:32:30 EST (Thu, 10 Jan 2008)
@@ -22,7 +22,7 @@
std::string scope(test::scope);
#if BOOST_UNORDERED_SWAP_METHOD != 2
- HASH_CHECK(
+ UNORDERED_CHECK(
scope == "hash::operator(hash)" ||
scope == "hash::operator=(hash)" ||
scope == "equal_to::operator(equal_to)" ||
@@ -73,7 +73,7 @@
std::string scope(test::scope);
#if BOOST_UNORDERED_SWAP_METHOD != 2
- HASH_CHECK(
+ UNORDERED_CHECK(
scope == "hash::operator(hash)" ||
scope == "hash::operator=(hash)" ||
scope == "equal_to::operator(equal_to)" ||
Modified: branches/unordered/trunk/libs/unordered/test/objects/exception.hpp
==============================================================================
--- branches/unordered/trunk/libs/unordered/test/objects/exception.hpp (original)
+++ branches/unordered/trunk/libs/unordered/test/objects/exception.hpp 2008-01-10 18:32:30 EST (Thu, 10 Jan 2008)
@@ -6,14 +6,17 @@
#if !defined(BOOST_UNORDERED_TEST_OBJECTS_HEADER)
#define BOOST_UNORDERED_TEST_OBJECTS_HEADER
+#if defined(BOOST_UNORDERED_EXCEPTION_USE_TEST)
#define BOOST_TEST_MAIN
-#include <boost/test/unit_test.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 <boost/limits.hpp>
-#include <boost/test/test_tools.hpp>
-#include <boost/test/exception_safety.hpp>
#include <boost/preprocessor/seq/for_each_product.hpp>
#include <boost/preprocessor/seq/elem.hpp>
#include <boost/preprocessor/cat.hpp>
@@ -24,10 +27,12 @@
#include <map>
#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) \
- RUN_EXCEPTION_TESTS_OP2( \
+ UNORDERED_EXCEPTION_TEST_CASE( \
BOOST_PP_CAT(BOOST_PP_SEQ_ELEM(0, product), \
BOOST_PP_CAT(_, BOOST_PP_SEQ_ELEM(1, product)) \
), \
@@ -35,12 +40,25 @@
BOOST_PP_SEQ_ELEM(1, product) \
)
-#define RUN_EXCEPTION_TESTS_OP2(name, test_func, type) \
+
+#if defined(BOOST_UNORDERED_EXCEPTION_USE_TEST)
+#define UNORDERED_EXCEPTION_TEST_PREFIX
+#define UNORDERED_EXCEPTION_TEST_CASE(name, test_func, type) \
BOOST_AUTO_TEST_CASE(name) \
{ \
test_func< type > fixture; \
::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) \
for(::test::scope_guard unordered_test_guard( \
@@ -48,10 +66,17 @@
!unordered_test_guard.dismissed(); \
unordered_test_guard.dismiss())
+#if defined(BOOST_UNORDERED_EXCEPTION_USE_TEST)
#define EPOINT(name) \
if(::test::exceptions_enabled) { \
BOOST_ITEST_EPOINT(name); \
}
+#else
+#define EPOINT(name) \
+ if(::test::exceptions_enabled) { \
+ ::test::lightweight::epoint(name); \
+ }
+#endif
#define ENABLE_EXCEPTIONS \
::test::exceptions_enable BOOST_PP_CAT(ENABLE_EXCEPTIONS_, __LINE__)(true)
@@ -169,11 +194,58 @@
}
};
+#if defined(BOOST_UNORDERED_EXCEPTION_USE_TEST)
template <class Test>
void exception_safety(Test const& f, char const* name) {
test_runner<Test> runner(f);
::boost::itest::exception_safety(runner, name);
}
+#else
+ // Quick and dirty 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
Modified: branches/unordered/trunk/libs/unordered/test/objects/memory.hpp
==============================================================================
--- branches/unordered/trunk/libs/unordered/test/objects/memory.hpp (original)
+++ branches/unordered/trunk/libs/unordered/test/objects/memory.hpp 2008-01-10 18:32:30 EST (Thu, 10 Jan 2008)
@@ -6,7 +6,13 @@
#if !defined(BOOST_UNORDERED_TEST_MEMORY_HEADER)
#define BOOST_UNORDERED_TEST_MEMORY_HEADER
-#define HASH_CHECK(test) if(!(test)) BOOST_ERROR(BOOST_STRINGIZE(test))
+#if defined(BOOST_UNORDERED_EXCEPTION_USE_TEST)
+#define UNORDERED_CHECK(x) BOOST_CHECK(x)
+#define UNORDERED_REQUIRE(x) BOOST_REQUIRE(x)
+#else
+#define UNORDERED_CHECK(x) BOOST_TEST(x)
+#define UNORDERED_REQUIRE(x) if(!(x)) { BOOST_ERROR(BOOST_STRINGIZE(x)); throw test::lightweight::test_failure(); }
+#endif
namespace test
{
@@ -84,7 +90,7 @@
void allocator_unref()
{
- HASH_CHECK(count_allocators > 0);
+ UNORDERED_CHECK(count_allocators > 0);
if(count_allocators > 0) {
--count_allocators;
if(count_allocators == 0) {
@@ -97,9 +103,9 @@
count_constructions = 0;
allocated_memory.clear();
- HASH_CHECK(no_allocations_left);
- HASH_CHECK(no_constructions_left);
- HASH_CHECK(allocated_memory_empty);
+ UNORDERED_CHECK(no_allocations_left);
+ UNORDERED_CHECK(no_constructions_left);
+ UNORDERED_CHECK(allocated_memory_empty);
}
}
}
@@ -123,12 +129,12 @@
if(pos == allocated_memory.end()) {
BOOST_ERROR("Deallocating unknown pointer.");
} else {
- HASH_CHECK(pos->first.start == ptr);
- HASH_CHECK(pos->first.end == (char*) ptr + n * size);
- HASH_CHECK(pos->second.tag_ == tag);
+ UNORDERED_CHECK(pos->first.start == ptr);
+ UNORDERED_CHECK(pos->first.end == (char*) ptr + n * size);
+ UNORDERED_CHECK(pos->second.tag_ == tag);
allocated_memory.erase(pos);
}
- HASH_CHECK(count_allocations > 0);
+ UNORDERED_CHECK(count_allocations > 0);
if(count_allocations > 0) --count_allocations;
}
@@ -139,7 +145,7 @@
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;
}
};
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk