// what: unit tests for variant type any // who: contributed by Kevlin Henney // when: October 2000 // where: tested with BCC 5.5 and MSVC 6.0 #include #include #include #include "any.hpp" #include "test.hpp" namespace any_tests { typedef test::test test_case; typedef const test_case * test_case_iterator; extern const test_case_iterator begin, end; } int main() { using namespace any_tests; test::tester test_suite(begin, end); return test_suite() ? EXIT_SUCCESS : EXIT_FAILURE; } namespace any_tests // test suite { void test_default_ctor(); void test_converting_ctor(); void test_copy_ctor(); void test_copy_assign(); void test_converting_assign(); void test_bad_cast(); void test_comparisons(); void test_swap(); void test_null_copying(); 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 }, { "boolean comparisons", test_comparisons }, { "swap member function", test_swap }, { "copying operations on a null", test_null_copying } }; const test_case_iterator begin = test_cases; const test_case_iterator end = test_cases + (sizeof test_cases / sizeof *test_cases); } namespace any_tests // test definitions { using namespace test; using namespace boost; void test_default_ctor() { const any value; std::string copied_text; bool copied = value.copy_to(copied_text); check_false(value, "operator _opaque_bool *"); check_null( value.to_ptr MSVC_EXCLUDE()(MSVC_INCLUDE(static_cast(0))), "to_ptr"); check_equal(value.type(), typeid(void), "type_info"); check_false(copied, "copy_to result"); } void test_converting_ctor() { std::string text = "test message", copied_text; any value = text; bool copied = value.copy_to(copied_text); check_true(value, "operator _opaque_bool *"); check_equal(value.type(), typeid(std::string), "type_info"); check_null( value.to_ptr MSVC_EXCLUDE()(MSVC_INCLUDE(static_cast(0))), "to_ptr"); check_non_null( value.to_ptr MSVC_EXCLUDE()(MSVC_INCLUDE(static_cast(0))), "to_ptr"); check_true(copied, "copy_to result"); check_equal( any_cast(value), text, "comparing cast copy against original text"); check_equal( copied_text, text, "comparing copied text against original text"); check_unequal( value.to_ptr MSVC_EXCLUDE()(MSVC_INCLUDE(static_cast(0))), &text, "comparing address in copy against original text"); } void test_copy_ctor() { std::string text = "test message", copied_text; any original = text, copy = original; bool copied = copy.copy_to(copied_text); check_true(copy, "operator _opaque_bool *"); check_equal(original.type(), copy.type(), "type_info"); check_equal( any_cast(original), any_cast(copy), "comparing cast copy against original"); check_true(copied, "copy_to result"); check_equal( text, any_cast(copy), "comparing cast copy against original text"); check_equal( copied_text, text, "comparing copied text against original text"); check_unequal( original.to_ptr MSVC_EXCLUDE()(MSVC_INCLUDE(static_cast(0))), copy.to_ptr MSVC_EXCLUDE()(MSVC_INCLUDE(static_cast(0))), "comparing address in copy against original"); } void test_copy_assign() { std::string text = "test message", copied_text; any original = text, copy; any * assign_result = &(copy = original); bool copied = copy.copy_to(copied_text); check_true(copy, "operator _opaque_bool *"); check_equal(original.type(), copy.type(), "type_info"); check_equal( any_cast(original), any_cast(copy), "comparing cast copy against original"); check_true(copied, "copy_to result"); check_equal( text, any_cast(copy), "comparing cast copy against original text"); check_equal( copied_text, text, "comparing copied text against original text"); check_unequal( original.to_ptr MSVC_EXCLUDE()(MSVC_INCLUDE(static_cast(0))), copy.to_ptr MSVC_EXCLUDE()(MSVC_INCLUDE(static_cast(0))), "comparing address in copy against original"); check_equal(assign_result, ©, "address of assignment result"); } void test_converting_assign() { std::string text = "test message"; any value; any * assign_result = &(value = text); check_true(value, "operator _opaque_bool *"); check_equal(value.type(), typeid(std::string), "type_info"); check_null( value.to_ptr MSVC_EXCLUDE()(MSVC_INCLUDE(static_cast(0))), "to_ptr"); check_non_null( value.to_ptr MSVC_EXCLUDE()(MSVC_INCLUDE(static_cast(0))), "to_ptr"); check_equal( any_cast(value), text, "comparing cast copy against original text"); check_unequal( value.to_ptr MSVC_EXCLUDE()(MSVC_INCLUDE(static_cast(0))), &text, "comparing address in copy against original text"); check_equal(assign_result, &value, "address of assignment result"); } void test_bad_cast() { std::string text = "test message"; any value = text; TEST_CHECK_THROW( any_cast(value), bad_any_cast, "any_cast to incorrect type"); } void test_comparisons() { const any empty, also_empty, zero = 0, one = static_cast("1"); check_equal(empty, also_empty, "comparing empty equal to empty"); check_equal(zero, one, "comparing non-empty equal to non-empty"); MSVC_EXCLUDE(check_true(empty < zero, "comparing empty less than non-empty")); } void test_swap() { std::string text = "test message", copied_text; any original = text, swapped; std::string * original_to_ptr = original.to_ptr MSVC_EXCLUDE()(MSVC_INCLUDE(static_cast(0))); any * swap_result = &original.swap(swapped); bool copied = swapped.copy_to(copied_text); check_false(original, "operator _opaque_bool * on original"); check_true(swapped, "operator _opaque_bool * on swapped"); check_equal(swapped.type(), typeid(std::string), "type_info"); check_true(copied, "copy_to result"); check_equal( text, any_cast(swapped), "comparing swapped copy against original text"); check_equal( copied_text, text, "comparing copied text against original text"); check_non_null(original_to_ptr, "address in pre-swapped original"); check_equal( original_to_ptr, swapped.to_ptr MSVC_EXCLUDE()(MSVC_INCLUDE(static_cast(0))), "comparing address in swapped against original"); check_equal(swap_result, &original, "address of swap result"); } void test_null_copying() { const any null; any copied = null, assigned; assigned = null; check_false(null, "operator const void * on null"); check_false(copied, "operator const void * on copied"); check_false(assigned, "operator const void * on copied"); } } // Copyright Kevlin Henney, 2000. All rights reserved. // // Permission to use, copy, modify, and distribute this software for any // purpose is hereby granted without fee, provided that this copyright and // permissions notice appear in all copies and derivatives, and that no // charge may be made for the software and its documentation except to cover // cost of distribution. // // This software is provided "as is" without express or implied warranty.