| 1 | // what:  unit tests for variant type boost::any | 
|---|
| 2 | // who:   contributed by Kevlin Henney | 
|---|
| 3 | // when:  July 2001 | 
|---|
| 4 | // where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95 | 
|---|
| 5 |  | 
|---|
| 6 | #include <cstdlib> | 
|---|
| 7 | #include <string> | 
|---|
| 8 | #include <utility> | 
|---|
| 9 |  | 
|---|
| 10 | #include "boost/any.hpp" | 
|---|
| 11 | #include "test.hpp" | 
|---|
| 12 |  | 
|---|
| 13 | namespace any_tests | 
|---|
| 14 | { | 
|---|
| 15 |     typedef test<const char *, void (*)()> test_case; | 
|---|
| 16 |     typedef const test_case * test_case_iterator; | 
|---|
| 17 |  | 
|---|
| 18 |     extern const test_case_iterator begin, end; | 
|---|
| 19 | } | 
|---|
| 20 |  | 
|---|
| 21 | int main() | 
|---|
| 22 | { | 
|---|
| 23 |     using namespace any_tests; | 
|---|
| 24 |     tester<test_case_iterator> test_suite(begin, end); | 
|---|
| 25 |     return test_suite() ? EXIT_SUCCESS : EXIT_FAILURE; | 
|---|
| 26 | } | 
|---|
| 27 |  | 
|---|
| 28 | namespace any_tests // test suite | 
|---|
| 29 | { | 
|---|
| 30 |     void test_default_ctor(); | 
|---|
| 31 |     void test_converting_ctor(); | 
|---|
| 32 |     void test_copy_ctor(); | 
|---|
| 33 |     void test_copy_assign(); | 
|---|
| 34 |     void test_converting_assign(); | 
|---|
| 35 |     void test_bad_cast(); | 
|---|
| 36 |     void test_swap(); | 
|---|
| 37 |     void test_null_copying(); | 
|---|
| 38 |     void test_cast_to_reference(); | 
|---|
| 39 |  | 
|---|
| 40 |     const test_case test_cases[] = | 
|---|
| 41 |     { | 
|---|
| 42 |         { "default construction",           test_default_ctor      }, | 
|---|
| 43 |         { "single argument construction",   test_converting_ctor   }, | 
|---|
| 44 |         { "copy construction",              test_copy_ctor         }, | 
|---|
| 45 |         { "copy assignment operator",       test_copy_assign       }, | 
|---|
| 46 |         { "converting assignment operator", test_converting_assign }, | 
|---|
| 47 |         { "failed custom keyword cast",     test_bad_cast          }, | 
|---|
| 48 |         { "swap member function",           test_swap              }, | 
|---|
| 49 |         { "copying operations on a null",   test_null_copying      } | 
|---|
| 50 |     }; | 
|---|
| 51 |  | 
|---|
| 52 |     const test_case_iterator begin = test_cases; | 
|---|
| 53 |     const test_case_iterator end = | 
|---|
| 54 |         test_cases + (sizeof test_cases / sizeof *test_cases); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | namespace any_tests // test definitions | 
|---|
| 58 | { | 
|---|
| 59 |     using namespace boost; | 
|---|
| 60 |  | 
|---|
| 61 |     void test_default_ctor() | 
|---|
| 62 |     { | 
|---|
| 63 |         const any value; | 
|---|
| 64 |  | 
|---|
| 65 |         check_true(value.empty(), "empty"); | 
|---|
| 66 |         check_null(any_cast<int>(&value), "any_cast<int>"); | 
|---|
| 67 |         check_equal(value.type(), typeid(void), "type"); | 
|---|
| 68 |     } | 
|---|
| 69 |  | 
|---|
| 70 |     void test_converting_ctor() | 
|---|
| 71 |     { | 
|---|
| 72 |         std::string text = "test message"; | 
|---|
| 73 |         any value = text; | 
|---|
| 74 |  | 
|---|
| 75 |         check_false(value.empty(), "empty"); | 
|---|
| 76 |         check_equal(value.type(), typeid(std::string), "type"); | 
|---|
| 77 |         check_null(any_cast<int>(&value), "any_cast<int>"); | 
|---|
| 78 |         check_non_null(any_cast<std::string>(&value), "any_cast<std::string>"); | 
|---|
| 79 |         check_equal( | 
|---|
| 80 |             any_cast<std::string>(value), text, | 
|---|
| 81 |             "comparing cast copy against original text"); | 
|---|
| 82 |         check_unequal( | 
|---|
| 83 |             any_cast<std::string>(&value), &text, | 
|---|
| 84 |             "comparing address in copy against original text"); | 
|---|
| 85 |     } | 
|---|
| 86 |  | 
|---|
| 87 |     void test_copy_ctor() | 
|---|
| 88 |     { | 
|---|
| 89 |         std::string text = "test message"; | 
|---|
| 90 |         any original = text, copy = original; | 
|---|
| 91 |  | 
|---|
| 92 |         check_false(copy.empty(), "empty"); | 
|---|
| 93 |         check_equal(original.type(), copy.type(), "type"); | 
|---|
| 94 |         check_equal( | 
|---|
| 95 |             any_cast<std::string>(original), any_cast<std::string>(copy), | 
|---|
| 96 |             "comparing cast copy against original"); | 
|---|
| 97 |         check_equal( | 
|---|
| 98 |             text, any_cast<std::string>(copy), | 
|---|
| 99 |             "comparing cast copy against original text"); | 
|---|
| 100 |         check_unequal( | 
|---|
| 101 |             any_cast<std::string>(&original), | 
|---|
| 102 |             any_cast<std::string>(©), | 
|---|
| 103 |             "comparing address in copy against original"); | 
|---|
| 104 |     } | 
|---|
| 105 |  | 
|---|
| 106 |     void test_copy_assign() | 
|---|
| 107 |     { | 
|---|
| 108 |         std::string text = "test message"; | 
|---|
| 109 |         any original = text, copy; | 
|---|
| 110 |         any * assign_result = &(copy = original); | 
|---|
| 111 |  | 
|---|
| 112 |         check_false(copy.empty(), "empty"); | 
|---|
| 113 |         check_equal(original.type(), copy.type(), "type"); | 
|---|
| 114 |         check_equal( | 
|---|
| 115 |             any_cast<std::string>(original), any_cast<std::string>(copy), | 
|---|
| 116 |             "comparing cast copy against cast original"); | 
|---|
| 117 |         check_equal( | 
|---|
| 118 |             text, any_cast<std::string>(copy), | 
|---|
| 119 |             "comparing cast copy against original text"); | 
|---|
| 120 |         check_unequal( | 
|---|
| 121 |             any_cast<std::string>(&original), | 
|---|
| 122 |             any_cast<std::string>(©), | 
|---|
| 123 |             "comparing address in copy against original"); | 
|---|
| 124 |         check_equal(assign_result, ©, "address of assignment result"); | 
|---|
| 125 |     } | 
|---|
| 126 |  | 
|---|
| 127 |     void test_converting_assign() | 
|---|
| 128 |     { | 
|---|
| 129 |         std::string text = "test message"; | 
|---|
| 130 |         any value; | 
|---|
| 131 |         any * assign_result = &(value = text); | 
|---|
| 132 |  | 
|---|
| 133 |         check_false(value.empty(), "type"); | 
|---|
| 134 |         check_equal(value.type(), typeid(std::string), "type"); | 
|---|
| 135 |         check_null(any_cast<int>(&value), "any_cast<int>"); | 
|---|
| 136 |         check_non_null(any_cast<std::string>(&value), "any_cast<std::string>"); | 
|---|
| 137 |         check_equal( | 
|---|
| 138 |             any_cast<std::string>(value), text, | 
|---|
| 139 |             "comparing cast copy against original text"); | 
|---|
| 140 |         check_unequal( | 
|---|
| 141 |             any_cast<std::string>(&value), | 
|---|
| 142 |             &text, | 
|---|
| 143 |             "comparing address in copy against original text"); | 
|---|
| 144 |         check_equal(assign_result, &value, "address of assignment result"); | 
|---|
| 145 |     } | 
|---|
| 146 |  | 
|---|
| 147 |     void test_bad_cast() | 
|---|
| 148 |     { | 
|---|
| 149 |         std::string text = "test message"; | 
|---|
| 150 |         any value = text; | 
|---|
| 151 |  | 
|---|
| 152 |         TEST_CHECK_THROW( | 
|---|
| 153 |             any_cast<const char *>(value), | 
|---|
| 154 |             bad_any_cast, | 
|---|
| 155 |             "any_cast to incorrect type"); | 
|---|
| 156 |     } | 
|---|
| 157 |  | 
|---|
| 158 |     void test_swap() | 
|---|
| 159 |     { | 
|---|
| 160 |         std::string text = "test message"; | 
|---|
| 161 |         any original = text, swapped; | 
|---|
| 162 |         std::string * original_ptr = any_cast<std::string>(&original); | 
|---|
| 163 |         any * swap_result = &original.swap(swapped); | 
|---|
| 164 |  | 
|---|
| 165 |         check_true(original.empty(), "empty on original"); | 
|---|
| 166 |         check_false(swapped.empty(), "empty on swapped"); | 
|---|
| 167 |         check_equal(swapped.type(), typeid(std::string), "type"); | 
|---|
| 168 |         check_equal( | 
|---|
| 169 |             text, any_cast<std::string>(swapped), | 
|---|
| 170 |             "comparing swapped copy against original text"); | 
|---|
| 171 |         check_non_null(original_ptr, "address in pre-swapped original"); | 
|---|
| 172 |         check_equal( | 
|---|
| 173 |             original_ptr, | 
|---|
| 174 |             any_cast<std::string>(&swapped), | 
|---|
| 175 |             "comparing address in swapped against original"); | 
|---|
| 176 |         check_equal(swap_result, &original, "address of swap result"); | 
|---|
| 177 |     } | 
|---|
| 178 |  | 
|---|
| 179 |     void test_null_copying() | 
|---|
| 180 |     { | 
|---|
| 181 |         const any null; | 
|---|
| 182 |         any copied = null, assigned; | 
|---|
| 183 |         assigned = null; | 
|---|
| 184 |  | 
|---|
| 185 |         check_true(null.empty(), "empty on null"); | 
|---|
| 186 |         check_true(copied.empty(), "empty on copied"); | 
|---|
| 187 |         check_true(assigned.empty(), "empty on copied"); | 
|---|
| 188 |     } | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | // Copyright Kevlin Henney, 2000, 2001. All rights reserved. | 
|---|
| 192 | // | 
|---|
| 193 | // Distributed under the Boost Software License, Version 1.0. (See | 
|---|
| 194 | // accompanying file LICENSE_1_0.txt or copy at | 
|---|
| 195 | // http://www.boost.org/LICENSE_1_0.txt) | 
|---|
| 196 | // | 
|---|