From 1ce3b4dd056f25066c950779654f954a1dc81cfe Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Sun, 7 Jan 2024 12:30:05 +0300 Subject: [PATCH 1/2] Switch to Boost.Core lightweight testing --- test/basic_test.hpp | 276 ++++++++++++++++++++------------------------ test/move_test.hpp | 9 +- 2 files changed, 129 insertions(+), 156 deletions(-) diff --git a/test/basic_test.hpp b/test/basic_test.hpp index 662ac7f..b72dc4a 100644 --- a/test/basic_test.hpp +++ b/test/basic_test.hpp @@ -19,10 +19,9 @@ #include #include +#include #include -#include "test.hpp" - namespace any_tests { struct huge_structure { @@ -50,9 +49,9 @@ struct basic_tests // test definitions { const Any value; - check_true(value.empty(), "empty"); - check_null(boost::any_cast(&value), "any_cast"); - check_equal(value.type(), boost::typeindex::type_id(), "type"); + BOOST_TEST(value.empty()); + BOOST_TEST_EQ(boost::any_cast(&value), nullptr); + BOOST_TEST(value.type() == boost::typeindex::type_id()); } static void test_converting_ctor() @@ -60,16 +59,12 @@ struct basic_tests // test definitions std::string text = "test message"; Any value = text; - check_false(value.empty(), "empty"); - check_equal(value.type(), boost::typeindex::type_id(), "type"); - check_null(boost::any_cast(&value), "any_cast"); - check_non_null(boost::any_cast(&value), "any_cast"); - check_equal( - boost::any_cast(value), text, - "comparing cast copy against original text"); - check_unequal( - boost::any_cast(&value), &text, - "comparing address in copy against original text"); + BOOST_TEST(!value.empty()); + BOOST_TEST(value.type() == boost::typeindex::type_id()); + BOOST_TEST_EQ(boost::any_cast(&value), nullptr); + BOOST_TEST_NE(boost::any_cast(&value), nullptr); + BOOST_TEST_EQ(boost::any_cast(value), text); + BOOST_TEST_NE(boost::any_cast(&value), &text); } static void test_copy_ctor() @@ -77,18 +72,17 @@ struct basic_tests // test definitions std::string text = "test message"; Any original = text, copy = original; - check_false(copy.empty(), "empty"); - check_equal(boost::typeindex::type_index(original.type()), copy.type(), "type"); - check_equal( - boost::any_cast(original), boost::any_cast(copy), - "comparing cast copy against original"); - check_equal( - text, boost::any_cast(copy), - "comparing cast copy against original text"); - check_unequal( + BOOST_TEST(!copy.empty()); + BOOST_TEST(boost::typeindex::type_index(original.type()) == copy.type()); + BOOST_TEST_EQ( + boost::any_cast(original), + boost::any_cast(copy) + ); + BOOST_TEST_EQ(text, boost::any_cast(copy)); + BOOST_TEST_NE( boost::any_cast(&original), - boost::any_cast(©), - "comparing address in copy against original"); + boost::any_cast(©) + ); } static void test_copy_assign() @@ -97,19 +91,18 @@ struct basic_tests // test definitions Any original = text, copy; Any * assign_result = &(copy = original); - check_false(copy.empty(), "empty"); - check_equal(boost::typeindex::type_index(original.type()), copy.type(), "type"); - check_equal( - boost::any_cast(original), boost::any_cast(copy), - "comparing cast copy against cast original"); - check_equal( - text, boost::any_cast(copy), - "comparing cast copy against original text"); - check_unequal( + BOOST_TEST(!copy.empty()); + BOOST_TEST(boost::typeindex::type_index(original.type()) == copy.type()); + BOOST_TEST_EQ( + boost::any_cast(original), + boost::any_cast(copy) + ); + BOOST_TEST_EQ(text, boost::any_cast(copy)); + BOOST_TEST_NE( boost::any_cast(&original), - boost::any_cast(©), - "comparing address in copy against original"); - check_equal(assign_result, ©, "address of assignment result"); + boost::any_cast(©) + ); + BOOST_TEST_EQ(assign_result, ©); } static void test_converting_assign() @@ -118,18 +111,13 @@ struct basic_tests // test definitions Any value; Any * assign_result = &(value = text); - check_false(value.empty(), "type"); - check_equal(value.type(), boost::typeindex::type_id(), "type"); - check_null(boost::any_cast(&value), "any_cast"); - check_non_null(boost::any_cast(&value), "any_cast"); - check_equal( - boost::any_cast(value), text, - "comparing cast copy against original text"); - check_unequal( - boost::any_cast(&value), - &text, - "comparing address in copy against original text"); - check_equal(assign_result, &value, "address of assignment result"); + BOOST_TEST(!value.empty()); + BOOST_TEST(value.type() == boost::typeindex::type_id()); + BOOST_TEST_EQ(boost::any_cast(&value), nullptr); + BOOST_TEST_NE(boost::any_cast(&value), nullptr); + BOOST_TEST_EQ(boost::any_cast(value), text); + BOOST_TEST_NE(boost::any_cast(&value), &text); + BOOST_TEST_EQ(assign_result, &value); } static void test_bad_cast() @@ -137,10 +125,10 @@ struct basic_tests // test definitions std::string text = "test message"; Any value = text; - TEST_CHECK_THROW( + BOOST_TEST_THROWS( boost::any_cast(value), - boost::bad_any_cast, - "any_cast to incorrect type"); + boost::bad_any_cast + ); } static void test_swap() @@ -153,45 +141,41 @@ struct basic_tests // test definitions huge_structure * original_ptr = boost::any_cast(&original); Any * swap_result = &original.swap(swapped); - check_true(original.empty(), "empty on original"); - check_false(swapped.empty(), "empty on swapped"); - check_equal(swapped.type(), boost::typeindex::type_id(), "type"); - check_equal( - stored.text, boost::any_cast(swapped).text, - "comparing swapped copy against original text"); - check_non_null(original_ptr, "address in pre-swapped original"); - check_equal( + BOOST_TEST(original.empty()); + BOOST_TEST(!swapped.empty()); + BOOST_TEST(swapped.type() == boost::typeindex::type_id()); + BOOST_TEST_EQ(stored.text, boost::any_cast(swapped).text); + BOOST_TEST_NE(original_ptr, nullptr); + BOOST_TEST_EQ( original_ptr, - boost::any_cast(&swapped), - "comparing address in swapped against original"); - check_equal(swap_result, &original, "address of swap result"); + boost::any_cast(&swapped) + ); + BOOST_TEST_EQ(swap_result, &original); swap(swapped, swapped); - check_false(swapped.empty(), "empty on self swap"); - check_equal( - swapped.type(), boost::typeindex::type_id(), - "type mismatch on self swap"); - check_equal( - stored.text, boost::any_cast(swapped).text, - "comparing against original text on self swap"); + BOOST_TEST(!swapped.empty()); + BOOST_TEST( + swapped.type() == boost::typeindex::type_id() + ); + BOOST_TEST_EQ( + stored.text, boost::any_cast(swapped).text + ); Any copy1 = copy_counter(); Any copy2 = copy_counter(); int count = copy_counter::get_count(); swap(copy1, copy2); - check_equal(count, copy_counter::get_count(), "checking that free swap doesn't make any copies."); + BOOST_TEST_EQ(count, copy_counter::get_count()); Any any_char = '1'; swap(any_char, swapped); - check_equal( - stored.text, boost::any_cast(any_char).text, - "comparing against original text on swap with small type"); - check_equal( - swapped.type(), boost::typeindex::type_id(), - "comparing type on swap with small type"); - check_equal( - '1', boost::any_cast(swapped), - "comparing small type swapped value"); + BOOST_TEST_EQ( + stored.text, boost::any_cast(any_char).text + ); + BOOST_TEST( + swapped.type() == boost::typeindex::type_id() + ); + BOOST_TEST_EQ('1', boost::any_cast(swapped)); } static void test_null_copying() @@ -200,9 +184,9 @@ struct basic_tests // test definitions Any copied = null, assigned; assigned = null; - check_true(null.empty(), "empty on null"); - check_true(copied.empty(), "empty on copied"); - check_true(assigned.empty(), "empty on copied"); + BOOST_TEST(null.empty()); + BOOST_TEST(copied.empty()); + BOOST_TEST(assigned.empty()); } static void test_cast_to_reference() @@ -215,41 +199,37 @@ struct basic_tests // test definitions int volatile & ra_v = boost::any_cast(a); int const volatile & ra_cv = boost::any_cast(a); - check_true( - &ra == &ra_c && &ra == &ra_v && &ra == &ra_cv, - "cv references to same obj"); + BOOST_TEST(&ra == &ra_c && &ra == &ra_v && &ra == &ra_cv); int const & rb_c = boost::any_cast(b); int const volatile & rb_cv = boost::any_cast(b); - check_true(&rb_c == &rb_cv, "cv references to copied const obj"); - check_true(&ra != &rb_c, "copies hold different objects"); + BOOST_TEST(&rb_c == &rb_cv); + BOOST_TEST(&ra != &rb_c); ++ra; int incremented = boost::any_cast(a); - check_true(incremented == 138, "increment by reference changes value"); + BOOST_TEST(incremented == 138); - TEST_CHECK_THROW( + BOOST_TEST_THROWS( boost::any_cast(a), - boost::bad_any_cast, - "any_cast to incorrect reference type"); + boost::bad_any_cast + ); - TEST_CHECK_THROW( + BOOST_TEST_THROWS( boost::any_cast(b), - boost::bad_any_cast, - "any_cast to incorrect const reference type"); + boost::bad_any_cast + ); } static void test_bad_any_cast() { - check_true( - boost::is_base_and_derived::value, - "bad_any_cast base class check" - ); + BOOST_TEST(( + boost::is_base_and_derived::value + )); - check_true( - std::string(boost::bad_any_cast().what()).find("any") != std::string::npos, - "bad_any_cast notes any in excaption" + BOOST_TEST( + std::string(boost::bad_any_cast().what()).find("any") != std::string::npos ); } @@ -259,14 +239,14 @@ struct basic_tests // test definitions Any value2; value2 = "Char array"; - check_false(value1.empty(), "type"); - check_false(value2.empty(), "type"); + BOOST_TEST(!value1.empty()); + BOOST_TEST(!value2.empty()); - check_equal(value1.type(), boost::typeindex::type_id(), "type"); - check_equal(value2.type(), boost::typeindex::type_id(), "type"); + BOOST_TEST(value1.type() == boost::typeindex::type_id()); + BOOST_TEST(value2.type() == boost::typeindex::type_id()); - check_non_null(boost::any_cast(&value1), "any_cast"); - check_non_null(boost::any_cast(&value2), "any_cast"); + BOOST_TEST(boost::any_cast(&value1)); + BOOST_TEST(boost::any_cast(&value2)); } static void test_clear() @@ -274,19 +254,19 @@ struct basic_tests // test definitions std::string text = "test message"; Any value = text; - check_false(value.empty(), "empty"); + BOOST_TEST(!value.empty()); value.clear(); - check_true(value.empty(), "non-empty after clear"); + BOOST_TEST(value.empty()); value.clear(); - check_true(value.empty(), "non-empty after second clear"); + BOOST_TEST(value.empty()); value = text; - check_false(value.empty(), "empty"); + BOOST_TEST(!value.empty()); value.clear(); - check_true(value.empty(), "non-empty after clear"); + BOOST_TEST(value.empty()); } // Following tests cover the case from #9462 @@ -299,14 +279,14 @@ struct basic_tests // test definitions static void test_vectors() { const std::vector& vec = boost::any_cast >(makeVec()); - check_equal(vec.size(), 100u, "size of vector extracted from boost::any"); - check_equal(vec.back(), 7, "back value of vector extracted from boost::any"); - check_equal(vec.front(), 7, "front value of vector extracted from boost::any"); + BOOST_TEST_EQ(vec.size(), 100u); + BOOST_TEST_EQ(vec.back(), 7); + BOOST_TEST_EQ(vec.front(), 7); std::vector vec1 = boost::any_cast >(makeVec()); - check_equal(vec1.size(), 100u, "size of second vector extracted from boost::any"); - check_equal(vec1.back(), 7, "back value of second vector extracted from boost::any"); - check_equal(vec1.front(), 7, "front value of second vector extracted from boost::any"); + BOOST_TEST_EQ(vec1.size(), 100u); + BOOST_TEST_EQ(vec1.back(), 7); + BOOST_TEST_EQ(vec1.front(), 7); } template @@ -339,55 +319,49 @@ struct basic_tests // test definitions Any test_val(obj); class_with_address_op returned_obj = boost::any_cast >(test_val); - check_equal(&val, returned_obj.get(), "any_cast incorrectly works with type that has operator&(): addresses differ"); + BOOST_TEST_EQ(&val, returned_obj.get()); - check_true(!!boost::any_cast >(&test_val), "any_cast incorrectly works with type that has operator&()"); - check_equal(boost::unsafe_any_cast >(&test_val)->get(), ptr, "unsafe_any_cast incorrectly works with type that has operator&()"); + BOOST_TEST(!!boost::any_cast >(&test_val)); + BOOST_TEST_EQ(boost::unsafe_any_cast >(&test_val)->get(), ptr); } static void test_multiple_assign() { Any test_val = 10; - check_true(!!boost::any_cast(&test_val), "any_cast"); + BOOST_TEST(!!boost::any_cast(&test_val)); test_val = '0'; - check_true(!!boost::any_cast(&test_val), "any_cast"); + BOOST_TEST(!!boost::any_cast(&test_val)); test_val = huge_structure(); - check_true(!!boost::any_cast(&test_val), "any_cast"); + BOOST_TEST(!!boost::any_cast(&test_val)); test_val = '0'; - check_true(!!boost::any_cast(&test_val), "any_cast"); + BOOST_TEST(!!boost::any_cast(&test_val)); test_val = Any(huge_structure()); - check_true(!!boost::any_cast(&test_val), "any_cast"); + BOOST_TEST(!!boost::any_cast(&test_val)); } static int run_tests() { - typedef test test_case; - const test_case test_cases[] = - { - { "default construction", test_default_ctor }, - { "single argument construction", test_converting_ctor }, - { "copy construction", test_copy_ctor }, - { "copy assignment operator", test_copy_assign }, - { "converting assignment operator", test_converting_assign }, - { "failed custom keyword cast", test_bad_cast }, - { "swap member function", test_swap }, - { "copying operations on a null", test_null_copying }, - { "cast to reference types", test_cast_to_reference }, - { "bad_any_cast exception", test_bad_any_cast }, - { "storing an array inside", test_with_array }, - { "clear() methods", test_clear }, - { "testing with vectors", test_vectors }, - { "class with operator&()", test_addressof }, - { "multiple assignments", test_multiple_assign }, - }; - typedef const test_case * test_case_iterator; - - tester test_suite(test_cases, test_cases + sizeof(test_cases) / sizeof(test_cases[0])); - return test_suite() ? EXIT_SUCCESS : EXIT_FAILURE; + test_default_ctor(); + test_converting_ctor(); + test_copy_ctor(); + test_copy_assign(); + test_converting_assign(); + test_bad_cast(); + test_swap(); + test_null_copying(); + test_cast_to_reference(); + test_bad_any_cast(); + test_with_array(); + test_clear(); + test_vectors(); + test_addressof(); + test_multiple_assign(); + + return boost::report_errors(); } }; diff --git a/test/move_test.hpp b/test/move_test.hpp index 16d50fd..70fd4dd 100644 --- a/test/move_test.hpp +++ b/test/move_test.hpp @@ -15,7 +15,6 @@ #include #include "test.hpp" -#include #include namespace any_tests { @@ -28,7 +27,7 @@ struct move_tests // test definitions Any value0 = move_copy_conting_class(); move_copy_conting_class::copy_count = 0; move_copy_conting_class::moves_count = 0; - Any value(boost::move(value0)); + Any value(std::move(value0)); check(value0.empty(), "moved away value is empty"); check_false(value.empty(), "empty"); @@ -48,7 +47,7 @@ struct move_tests // test definitions Any value = move_copy_conting_class(); move_copy_conting_class::copy_count = 0; move_copy_conting_class::moves_count = 0; - value = boost::move(value0); + value = std::move(value0); check(value0.empty(), "moved away is empty"); check_false(value.empty(), "empty"); @@ -107,7 +106,7 @@ struct move_tests // test definitions move_copy_conting_class::copy_count = 0; move_copy_conting_class::moves_count = 0; - Any value(boost::move(value0)); + Any value(std::move(value0)); check_false(value.empty(), "empty"); check_equal(value.type(), boost::typeindex::type_id(), "type"); @@ -127,7 +126,7 @@ struct move_tests // test definitions Any value; move_copy_conting_class::copy_count = 0; move_copy_conting_class::moves_count = 0; - value = boost::move(value0); + value = std::move(value0); check_false(value.empty(), "empty"); check_equal(value.type(), boost::typeindex::type_id(), "type"); From da5b664ced93b3b690eca058997755087e247313 Mon Sep 17 00:00:00 2001 From: Antony Polukhin Date: Sun, 7 Jan 2024 12:47:13 +0300 Subject: [PATCH 2/2] removed test.hpp --- test/any_test_cv_to_rv_failed.cpp | 5 +- test/any_test_temporary_to_ref_failed.cpp | 5 +- test/basic_any_test_cv_to_rv_failed.cpp | 5 +- ...basic_any_test_temporary_to_ref_failed.cpp | 5 +- test/basic_test.hpp | 4 +- test/move_test.hpp | 173 ++++------ test/test.hpp | 321 ------------------ 7 files changed, 69 insertions(+), 449 deletions(-) delete mode 100644 test/test.hpp diff --git a/test/any_test_cv_to_rv_failed.cpp b/test/any_test_cv_to_rv_failed.cpp index c474883..6d16d52 100644 --- a/test/any_test_cv_to_rv_failed.cpp +++ b/test/any_test_cv_to_rv_failed.cpp @@ -13,13 +13,10 @@ #include #include -#include "test.hpp" -int main() -{ +int main() { boost::any const cvalue(10); int i = boost::any_cast(cvalue); (void)i; - return EXIT_SUCCESS; } diff --git a/test/any_test_temporary_to_ref_failed.cpp b/test/any_test_temporary_to_ref_failed.cpp index 73f0af3..733eaff 100644 --- a/test/any_test_temporary_to_ref_failed.cpp +++ b/test/any_test_temporary_to_ref_failed.cpp @@ -13,13 +13,10 @@ #include #include -#include "test.hpp" -int main() -{ +int main() { int i = boost::any_cast(10); (void)i; - return EXIT_SUCCESS; } diff --git a/test/basic_any_test_cv_to_rv_failed.cpp b/test/basic_any_test_cv_to_rv_failed.cpp index 291c32f..18472f6 100644 --- a/test/basic_any_test_cv_to_rv_failed.cpp +++ b/test/basic_any_test_cv_to_rv_failed.cpp @@ -14,14 +14,11 @@ #include #include -#include "test.hpp" -int main() -{ +int main() { boost::anys::basic_any<> const cvalue(10); int i = boost::any_cast(cvalue); (void)i; - return EXIT_SUCCESS; } diff --git a/test/basic_any_test_temporary_to_ref_failed.cpp b/test/basic_any_test_temporary_to_ref_failed.cpp index 3e3ea7d..d759d8c 100644 --- a/test/basic_any_test_temporary_to_ref_failed.cpp +++ b/test/basic_any_test_temporary_to_ref_failed.cpp @@ -14,12 +14,9 @@ #include #include -#include "test.hpp" -int main() -{ +int main() { int i = boost::any_cast(boost::anys::basic_any<>(10)); (void)i; - return EXIT_SUCCESS; } diff --git a/test/basic_test.hpp b/test/basic_test.hpp index b72dc4a..e3dbf62 100644 --- a/test/basic_test.hpp +++ b/test/basic_test.hpp @@ -18,9 +18,9 @@ #include #include #include +#include #include -#include namespace any_tests { @@ -225,7 +225,7 @@ struct basic_tests // test definitions static void test_bad_any_cast() { BOOST_TEST(( - boost::is_base_and_derived::value + std::is_base_of::value )); BOOST_TEST( diff --git a/test/move_test.hpp b/test/move_test.hpp index 70fd4dd..31a3d98 100644 --- a/test/move_test.hpp +++ b/test/move_test.hpp @@ -14,7 +14,7 @@ #include #include -#include "test.hpp" +#include #include namespace any_tests { @@ -29,16 +29,12 @@ struct move_tests // test definitions move_copy_conting_class::moves_count = 0; Any value(std::move(value0)); - check(value0.empty(), "moved away value is empty"); - check_false(value.empty(), "empty"); - check_equal(value.type(), boost::typeindex::type_id(), "type"); - check_non_null(boost::any_cast(&value), "any_cast"); - check_equal( - move_copy_conting_class::copy_count, 0u, - "checking copy counts"); - check_equal( - move_copy_conting_class::moves_count, 0u, - "checking move counts"); + BOOST_TEST(value0.empty()); + BOOST_TEST(!value.empty()); + BOOST_TEST(value.type() == boost::typeindex::type_id()); + BOOST_TEST(boost::any_cast(&value)); + BOOST_TEST_EQ(move_copy_conting_class::copy_count, 0u); + BOOST_TEST_EQ(move_copy_conting_class::moves_count, 0u); } static void test_move_assignment() @@ -49,16 +45,12 @@ struct move_tests // test definitions move_copy_conting_class::moves_count = 0; value = std::move(value0); - check(value0.empty(), "moved away is empty"); - check_false(value.empty(), "empty"); - check_equal(value.type(), boost::typeindex::type_id(), "type"); - check_non_null(boost::any_cast(&value), "any_cast"); - check_equal( - move_copy_conting_class::copy_count, 0u, - "checking copy counts"); - check_equal( - move_copy_conting_class::moves_count, 0u, - "checking move counts"); + BOOST_TEST(value0.empty()); + BOOST_TEST(!value.empty()); + BOOST_TEST(value.type() == boost::typeindex::type_id()); + BOOST_TEST(boost::any_cast(&value)); + BOOST_TEST_EQ(move_copy_conting_class::copy_count, 0u); + BOOST_TEST_EQ(move_copy_conting_class::moves_count, 0u); } static void test_copy_construction() @@ -68,16 +60,12 @@ struct move_tests // test definitions move_copy_conting_class::moves_count = 0; Any value(value0); - check_false(value0.empty(), "copied value is not empty"); - check_false(value.empty(), "empty"); - check_equal(value.type(), boost::typeindex::type_id(), "type"); - check_non_null(boost::any_cast(&value), "any_cast"); - check_equal( - move_copy_conting_class::copy_count, 1u, - "checking copy counts"); - check_equal( - move_copy_conting_class::moves_count, 0u, - "checking move counts"); + BOOST_TEST(!value0.empty()); + BOOST_TEST(!value.empty()); + BOOST_TEST(value.type() == boost::typeindex::type_id()); + BOOST_TEST(boost::any_cast(&value)); + BOOST_TEST_EQ(move_copy_conting_class::copy_count, 1u); + BOOST_TEST_EQ(move_copy_conting_class::moves_count, 0u); } static void test_copy_assignment() @@ -88,16 +76,12 @@ struct move_tests // test definitions move_copy_conting_class::moves_count = 0; value = value0; - check_false(value0.empty(), "copied value is not empty"); - check_false(value.empty(), "empty"); - check_equal(value.type(), boost::typeindex::type_id(), "type"); - check_non_null(boost::any_cast(&value), "any_cast"); - check_equal( - move_copy_conting_class::copy_count, 1u, - "checking copy counts"); - check_equal( - move_copy_conting_class::moves_count, 0u, - "checking move counts"); + BOOST_TEST(!value0.empty()); + BOOST_TEST(!value.empty()); + BOOST_TEST(value.type() == boost::typeindex::type_id()); + BOOST_TEST(boost::any_cast(&value)); + BOOST_TEST_EQ(move_copy_conting_class::copy_count, 1u); + BOOST_TEST_EQ(move_copy_conting_class::moves_count, 0u); } static void test_move_construction_from_value() @@ -108,16 +92,12 @@ struct move_tests // test definitions Any value(std::move(value0)); - check_false(value.empty(), "empty"); - check_equal(value.type(), boost::typeindex::type_id(), "type"); - check_non_null(boost::any_cast(&value), "any_cast"); + BOOST_TEST(!value.empty()); + BOOST_TEST(value.type() == boost::typeindex::type_id()); + BOOST_TEST(boost::any_cast(&value)); - check_equal( - move_copy_conting_class::copy_count, 0u, - "checking copy counts"); - check_equal( - move_copy_conting_class::moves_count, 1u, - "checking move counts"); + BOOST_TEST_EQ(move_copy_conting_class::copy_count, 0u); + BOOST_TEST_EQ(move_copy_conting_class::moves_count, 1u); } static void test_move_assignment_from_value() @@ -128,16 +108,12 @@ struct move_tests // test definitions move_copy_conting_class::moves_count = 0; value = std::move(value0); - check_false(value.empty(), "empty"); - check_equal(value.type(), boost::typeindex::type_id(), "type"); - check_non_null(boost::any_cast(&value), "any_cast"); + BOOST_TEST(!value.empty()); + BOOST_TEST(value.type() == boost::typeindex::type_id()); + BOOST_TEST(boost::any_cast(&value)); - check_equal( - move_copy_conting_class::copy_count, 0u, - "checking copy counts"); - check_equal( - move_copy_conting_class::moves_count, 1u, - "checking move counts"); + BOOST_TEST_EQ(move_copy_conting_class::copy_count, 0u); + BOOST_TEST_EQ(move_copy_conting_class::moves_count, 1u); } static void test_copy_construction_from_value() @@ -147,16 +123,12 @@ struct move_tests // test definitions move_copy_conting_class::moves_count = 0; Any value(value0); - check_false(value.empty(), "empty"); - check_equal(value.type(), boost::typeindex::type_id(), "type"); - check_non_null(boost::any_cast(&value), "any_cast"); + BOOST_TEST(!value.empty()); + BOOST_TEST(value.type() == boost::typeindex::type_id()); + BOOST_TEST(boost::any_cast(&value)); - check_equal( - move_copy_conting_class::copy_count, 1u, - "checking copy counts"); - check_equal( - move_copy_conting_class::moves_count, 0u, - "checking move counts"); + BOOST_TEST_EQ(move_copy_conting_class::copy_count, 1u); + BOOST_TEST_EQ(move_copy_conting_class::moves_count, 0u); } static void test_copy_assignment_from_value() @@ -167,16 +139,12 @@ struct move_tests // test definitions move_copy_conting_class::moves_count = 0; value = value0; - check_false(value.empty(), "empty"); - check_equal(value.type(), boost::typeindex::type_id(), "type"); - check_non_null(boost::any_cast(&value), "any_cast"); + BOOST_TEST(!value.empty()); + BOOST_TEST(value.type() == boost::typeindex::type_id()); + BOOST_TEST(boost::any_cast(&value)); - check_equal( - move_copy_conting_class::copy_count, 1u, - "checking copy counts"); - check_equal( - move_copy_conting_class::moves_count, 0u, - "checking move counts"); + BOOST_TEST_EQ(move_copy_conting_class::copy_count, 1u); + BOOST_TEST_EQ(move_copy_conting_class::moves_count, 0u); } static const Any helper_method() { @@ -203,28 +171,19 @@ struct move_tests // test definitions move_copy_conting_class value1 = boost::any_cast(value); - check_equal( - move_copy_conting_class::copy_count, 0u, - "checking copy counts"); - check_equal( - move_copy_conting_class::moves_count, 1u, - "checking move counts"); + BOOST_TEST_EQ(move_copy_conting_class::copy_count, 0u); + BOOST_TEST_EQ(move_copy_conting_class::moves_count, 1u); (void)value1; -// Following code shall fail to compile + const Any cvalue = value0; move_copy_conting_class::copy_count = 0; move_copy_conting_class::moves_count = 0; move_copy_conting_class value2 = boost::any_cast(cvalue); - check_equal( - move_copy_conting_class::copy_count, 1u, - "checking copy counts"); - check_equal( - move_copy_conting_class::moves_count, 0u, - "checking move counts"); + BOOST_TEST_EQ(move_copy_conting_class::copy_count, 1u); + BOOST_TEST_EQ(move_copy_conting_class::moves_count, 0u); (void)value2; -// } class move_copy_conting_class { @@ -252,25 +211,19 @@ struct move_tests // test definitions }; static int run_tests() { - typedef test test_case; - const test_case test_cases[] = - { - { "move construction of any", test_move_construction }, - { "move assignment of any", test_move_assignment }, - { "copy construction of any", test_copy_construction }, - { "copy assignment of any", test_copy_assignment }, - - { "move construction from value", test_move_construction_from_value }, - { "move assignment from value", test_move_assignment_from_value }, - { "copy construction from value", test_copy_construction_from_value }, - { "copy assignment from value", test_copy_assignment_from_value }, - { "constructing from const any&&", test_construction_from_const_any_rv }, - { "casting to rvalue reference", test_cast_to_rv } - }; - typedef const test_case * test_case_iterator; - - tester test_suite(test_cases, test_cases + sizeof(test_cases) / sizeof(test_cases[0])); - return test_suite() ? EXIT_SUCCESS : EXIT_FAILURE; + test_move_construction(); + test_move_assignment(); + test_copy_construction(); + test_copy_assignment(); + + test_move_construction_from_value(); + test_move_assignment_from_value(); + test_copy_construction_from_value(); + test_copy_assignment_from_value(); + test_construction_from_const_any_rv(); + test_cast_to_rv(); + + return boost::report_errors(); } }; // struct move_tests diff --git a/test/test.hpp b/test/test.hpp deleted file mode 100644 index e74e296..0000000 --- a/test/test.hpp +++ /dev/null @@ -1,321 +0,0 @@ -// what: simple unit test framework -// who: developed by Kevlin Henney -// when: November 2000 -// where: tested with BCC 5.5, MSVC 6.0, and g++ 2.91 - -#ifndef TEST_INCLUDED -#define TEST_INCLUDED - -#include -#include -#include -#ifdef BOOST_NO_STRINGSTREAM -#include // for out-of-the-box g++ pre-2.95.3 -#else -#include -#endif -#include - -namespace any_tests // test tuple comprises name and nullary function (object) -{ - template - struct test - { - string_type name; - function_type action; - - static test make(string_type name, function_type action) - { - test result; // MSVC aggregate initializer bugs - result.name = name; - result.action = action; - return result; - } - }; -} - -namespace any_tests // failure exception used to indicate checked test failures -{ - class failure : public std::exception - { - public: // struction (default cases are OK) - - failure(const std::string & why) throw() - : reason(why) - { - } - - ~failure() throw() {} - - public: // usage - - virtual const char * what() const throw() - { - return reason.c_str(); - } - - private: // representation - - std::string reason; - - }; -} - -namespace any_tests // not_implemented exception used to mark unimplemented tests -{ - class not_implemented : public std::exception - { - public: // usage (default ctor and dtor are OK) - - virtual const char * what() const throw() - { - return "not implemented"; - } - - }; -} - -namespace any_tests // test utilities -{ - inline void check(bool condition, const std::string & description) - { - if(!condition) - { - throw failure(description); - } - } - - inline void check_true(bool value, const std::string & description) - { - check(value, "expected true: " + description); - } - - inline void check_false(bool value, const std::string & description) - { - check(!value, "expected false: " + description); - } - - template - void check_equal( - const lhs_type & lhs, const rhs_type & rhs, - const std::string & description) - { - check(lhs == rhs, "expected equal values: " + description); - } - - template - void check_unequal( - const lhs_type & lhs, const rhs_type & rhs, - const std::string & description) - { - check(lhs != rhs, "expected unequal values: " + description); - } - - inline void check_null(const void * ptr, const std::string & description) - { - check(!ptr, "expected null pointer: " + description); - } - - inline void check_non_null(const void * ptr, const std::string & description) - { - check(ptr != 0, "expected non-null pointer: " + description); - } -} - -#define TEST_CHECK_THROW(expression, exception, description) \ - try \ - { \ - expression; \ - throw ::any_tests::failure(description); \ - } \ - catch(exception &) \ - { \ - } - -namespace any_tests // memory tracking (enabled if test new and delete linked in) -{ - class allocations - { - public: // singleton access - - static allocations & instance() - { - static allocations singleton; - return singleton; - } - - public: // logging - - void clear() - { - alloc_count = dealloc_count = 0; - } - - void allocation() - { - ++alloc_count; - } - - void deallocation() - { - ++dealloc_count; - } - - public: // reporting - - unsigned long allocated() const - { - return alloc_count; - } - - unsigned long deallocated() const - { - return dealloc_count; - } - - bool balanced() const - { - return alloc_count == dealloc_count; - } - - private: // structors (default dtor is fine) - - allocations() - : alloc_count(0), dealloc_count(0) - { - } - - private: // prevention - - allocations(const allocations &); - allocations & operator=(const allocations &); - - private: // state - - unsigned long alloc_count, dealloc_count; - - }; -} - -namespace any_tests // tester is the driver class for a sequence of tests -{ - template - class tester - { - public: // structors (default destructor is OK) - - tester(test_iterator first_test, test_iterator after_last_test) - : begin(first_test), end(after_last_test) - { - } - - public: // usage - - bool operator()(); // returns true if all tests passed - - private: // representation - - test_iterator begin, end; - - private: // prevention - - tester(const tester &); - tester &operator=(const tester &); - - }; - -#if defined(__GNUC__) && defined(__SGI_STL_PORT) && (__GNUC__ < 3) - // function scope using declarations don't work: - using namespace std; -#endif - - template - bool tester::operator()() - { - using std::cerr; - using std::endl; - using std::ends; - using std::exception; - using std::flush; - using std::string; - - unsigned long passed = 0, failed = 0, unimplemented = 0; - - for(test_iterator current = begin; current != end; ++current) - { - cerr << "[" << current->name << "] " << flush; - string result = "passed"; // optimistic - - try - { - allocations::instance().clear(); - current->action(); - - if(!allocations::instance().balanced()) - { - unsigned long allocated = allocations::instance().allocated(); - unsigned long deallocated = allocations::instance().deallocated(); -#ifdef BOOST_NO_STRINGSTREAM - std::ostrstream report; -#else - std::ostringstream report; -#endif - report << "new/delete (" - << allocated << " allocated, " - << deallocated << " deallocated)" - << ends; - const string text = report.str(); -#ifdef BOOST_NO_STRINGSTREAM - report.freeze(false); -#endif - throw failure(text); - } - - ++passed; - } - catch(const failure & caught) - { - (result = "failed: ") += caught.what(); - ++failed; - } - catch(const not_implemented &) - { - result = "not implemented"; - ++unimplemented; - } - catch(const exception & caught) - { - (result = "exception: ") += caught.what(); - ++failed; - } - catch(...) - { - result = "failed with unknown exception"; - ++failed; - } - - cerr << result << endl; - } - - cerr << (passed + failed) << " tests: " - << passed << " passed, " - << failed << " failed"; - - if(unimplemented) - { - cerr << " (" << unimplemented << " not implemented)"; - } - - cerr << endl; - - return failed == 0; - } -} - -#endif - -// Copyright Kevlin Henney, 2000. All rights reserved. -// -// 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)