| 1 | // Unit test for boost::any. |
| 2 | // |
| 3 | // See http://www.boost.org for most recent version, including documentation. |
| 4 | // |
| 5 | // Copyright Antony Polukhin, 2013. |
| 6 | // |
| 7 | // Distributed under the Boost |
| 8 | // Software License, Version 1.0. (See accompanying file |
| 9 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt). |
| 10 | |
| 11 | #include <cstdlib> |
| 12 | #include <string> |
| 13 | #include <utility> |
| 14 | |
| 15 | #include "boost/any.hpp" |
| 16 | #include "../test.hpp" |
| 17 | |
| 18 | namespace any_tests |
| 19 | { |
| 20 | typedef test<const char *, void (*)()> test_case; |
| 21 | typedef const test_case * test_case_iterator; |
| 22 | |
| 23 | extern const test_case_iterator begin, end; |
| 24 | } |
| 25 | |
| 26 | int main() |
| 27 | { |
| 28 | using namespace any_tests; |
| 29 | tester<test_case_iterator> test_suite(begin, end); |
| 30 | return test_suite() ? EXIT_SUCCESS : EXIT_FAILURE; |
| 31 | } |
| 32 | |
| 33 | namespace any_tests // test suite |
| 34 | { |
| 35 | void test_move_construction(); |
| 36 | void test_move_assignment(); |
| 37 | void test_copy_construction(); |
| 38 | void test_copy_assignment(); |
| 39 | |
| 40 | void test_move_construction_from_value(); |
| 41 | void test_move_assignment_from_value(); |
| 42 | void test_copy_construction_from_value(); |
| 43 | void test_copy_assignment_from_value(); |
| 44 | |
| 45 | |
| 46 | const test_case test_cases[] = |
| 47 | { |
| 48 | { "move construction of any", test_move_construction }, |
| 49 | { "move assignment of any", test_move_assignment }, |
| 50 | { "copy construction of any", test_copy_construction }, |
| 51 | { "copy assignment of any", test_copy_assignment }, |
| 52 | |
| 53 | { "move construction from value", test_move_construction_from_value }, |
| 54 | { "move assignment from value", test_move_assignment_from_value }, |
| 55 | { "copy construction from value", test_copy_construction_from_value }, |
| 56 | { "copy assignment from value", test_copy_assignment_from_value } |
| 57 | }; |
| 58 | |
| 59 | const test_case_iterator begin = test_cases; |
| 60 | const test_case_iterator end = |
| 61 | test_cases + (sizeof test_cases / sizeof *test_cases); |
| 62 | |
| 63 | |
| 64 | class move_copy_conting_class { |
| 65 | BOOST_COPYABLE_AND_MOVABLE(move_copy_conting_class) |
| 66 | public: |
| 67 | static unsigned int moves_count; |
| 68 | static unsigned int copy_count; |
| 69 | |
| 70 | move_copy_conting_class(){} |
| 71 | move_copy_conting_class(BOOST_RV_REF(move_copy_conting_class) /*param*/) { |
| 72 | ++ moves_count; |
| 73 | } |
| 74 | |
| 75 | move_copy_conting_class& operator=(BOOST_RV_REF(move_copy_conting_class) /*param*/) { |
| 76 | ++ moves_count; |
| 77 | return *this; |
| 78 | } |
| 79 | |
| 80 | move_copy_conting_class(const move_copy_conting_class&) { |
| 81 | ++ copy_count; |
| 82 | } |
| 83 | move_copy_conting_class& operator=(BOOST_COPY_ASSIGN_REF(move_copy_conting_class) /*param*/) { |
| 84 | ++ copy_count; |
| 85 | return *this; |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | unsigned int move_copy_conting_class::moves_count = 0; |
| 90 | unsigned int move_copy_conting_class::copy_count = 0; |
| 91 | } |
| 92 | |
| 93 | namespace any_tests // test definitions |
| 94 | { |
| 95 | using namespace boost; |
| 96 | |
| 97 | void test_move_construction() |
| 98 | { |
| 99 | any value0 = move_copy_conting_class(); |
| 100 | move_copy_conting_class::copy_count = 0; |
| 101 | move_copy_conting_class::moves_count = 0; |
| 102 | any value(boost::move(value0)); |
| 103 | |
| 104 | check(value0.empty(), "moved away value is empty"); |
| 105 | check_false(value.empty(), "empty"); |
| 106 | check_equal(value.type(), typeid(move_copy_conting_class), "type"); |
| 107 | check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>"); |
| 108 | check_equal( |
| 109 | move_copy_conting_class::copy_count, 0u, |
| 110 | "checking copy counts"); |
| 111 | check_equal( |
| 112 | move_copy_conting_class::moves_count, 0u, |
| 113 | "checking move counts"); |
| 114 | } |
| 115 | |
| 116 | void test_move_assignment() |
| 117 | { |
| 118 | any value0 = move_copy_conting_class(); |
| 119 | any value = move_copy_conting_class(); |
| 120 | move_copy_conting_class::copy_count = 0; |
| 121 | move_copy_conting_class::moves_count = 0; |
| 122 | value = boost::move(value0); |
| 123 | |
| 124 | check(value0.empty(), "moved away is empty"); |
| 125 | check_false(value.empty(), "empty"); |
| 126 | check_equal(value.type(), typeid(move_copy_conting_class), "type"); |
| 127 | check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>"); |
| 128 | check_equal( |
| 129 | move_copy_conting_class::copy_count, 0u, |
| 130 | "checking copy counts"); |
| 131 | check_equal( |
| 132 | move_copy_conting_class::moves_count, 0u, |
| 133 | "checking move counts"); |
| 134 | } |
| 135 | |
| 136 | void test_copy_construction() |
| 137 | { |
| 138 | any value0 = move_copy_conting_class(); |
| 139 | move_copy_conting_class::copy_count = 0; |
| 140 | move_copy_conting_class::moves_count = 0; |
| 141 | any value(value0); |
| 142 | |
| 143 | check_false(value0.empty(), "copyed value is not empty"); |
| 144 | check_false(value.empty(), "empty"); |
| 145 | check_equal(value.type(), typeid(move_copy_conting_class), "type"); |
| 146 | check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>"); |
| 147 | check_equal( |
| 148 | move_copy_conting_class::copy_count, 1u, |
| 149 | "checking copy counts"); |
| 150 | check_equal( |
| 151 | move_copy_conting_class::moves_count, 0u, |
| 152 | "checking move counts"); |
| 153 | } |
| 154 | |
| 155 | void test_copy_assignment() |
| 156 | { |
| 157 | any value0 = move_copy_conting_class(); |
| 158 | any value = move_copy_conting_class(); |
| 159 | move_copy_conting_class::copy_count = 0; |
| 160 | move_copy_conting_class::moves_count = 0; |
| 161 | value = value0; |
| 162 | |
| 163 | check_false(value0.empty(), "copyied value is not empty"); |
| 164 | check_false(value.empty(), "empty"); |
| 165 | check_equal(value.type(), typeid(move_copy_conting_class), "type"); |
| 166 | check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>"); |
| 167 | check_equal( |
| 168 | move_copy_conting_class::copy_count, 1u, |
| 169 | "checking copy counts"); |
| 170 | check_equal( |
| 171 | move_copy_conting_class::moves_count, 0u, |
| 172 | "checking move counts"); |
| 173 | } |
| 174 | |
| 175 | void test_move_construction_from_value() |
| 176 | { |
| 177 | move_copy_conting_class value0; |
| 178 | move_copy_conting_class::copy_count = 0; |
| 179 | move_copy_conting_class::moves_count = 0; |
| 180 | #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION |
| 181 | any value(boost::move(value0)); |
| 182 | #else |
| 183 | any value(value0); |
| 184 | #endif |
| 185 | |
| 186 | check_false(value.empty(), "empty"); |
| 187 | check_equal(value.type(), typeid(move_copy_conting_class), "type"); |
| 188 | check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>"); |
| 189 | |
| 190 | #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION |
| 191 | check_equal( |
| 192 | move_copy_conting_class::copy_count, 0u, |
| 193 | "checking copy counts"); |
| 194 | check_equal( |
| 195 | move_copy_conting_class::moves_count, 1u, |
| 196 | "checking move counts"); |
| 197 | #endif |
| 198 | |
| 199 | } |
| 200 | |
| 201 | void test_move_assignment_from_value() |
| 202 | { |
| 203 | move_copy_conting_class value0; |
| 204 | any value; |
| 205 | move_copy_conting_class::copy_count = 0; |
| 206 | move_copy_conting_class::moves_count = 0; |
| 207 | #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION |
| 208 | value = boost::move(value0); |
| 209 | #else |
| 210 | value = value0; |
| 211 | #endif |
| 212 | |
| 213 | check_false(value.empty(), "empty"); |
| 214 | check_equal(value.type(), typeid(move_copy_conting_class), "type"); |
| 215 | check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>"); |
| 216 | |
| 217 | #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION |
| 218 | check_equal( |
| 219 | move_copy_conting_class::copy_count, 0u, |
| 220 | "checking copy counts"); |
| 221 | check_equal( |
| 222 | move_copy_conting_class::moves_count, 1u, |
| 223 | "checking move counts"); |
| 224 | #endif |
| 225 | |
| 226 | } |
| 227 | |
| 228 | void test_copy_construction_from_value() |
| 229 | { |
| 230 | move_copy_conting_class value0; |
| 231 | move_copy_conting_class::copy_count = 0; |
| 232 | move_copy_conting_class::moves_count = 0; |
| 233 | any value(value0); |
| 234 | |
| 235 | check_false(value.empty(), "empty"); |
| 236 | check_equal(value.type(), typeid(move_copy_conting_class), "type"); |
| 237 | check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>"); |
| 238 | |
| 239 | check_equal( |
| 240 | move_copy_conting_class::copy_count, 1u, |
| 241 | "checking copy counts"); |
| 242 | check_equal( |
| 243 | move_copy_conting_class::moves_count, 0u, |
| 244 | "checking move counts"); |
| 245 | } |
| 246 | |
| 247 | void test_copy_assignment_from_value() |
| 248 | { |
| 249 | move_copy_conting_class value0; |
| 250 | any value; |
| 251 | move_copy_conting_class::copy_count = 0; |
| 252 | move_copy_conting_class::moves_count = 0; |
| 253 | value = value0; |
| 254 | |
| 255 | check_false(value.empty(), "empty"); |
| 256 | check_equal(value.type(), typeid(move_copy_conting_class), "type"); |
| 257 | check_non_null(any_cast<move_copy_conting_class>(&value), "any_cast<move_copy_conting_class>"); |
| 258 | |
| 259 | check_equal( |
| 260 | move_copy_conting_class::copy_count, 1u, |
| 261 | "checking copy counts"); |
| 262 | check_equal( |
| 263 | move_copy_conting_class::moves_count, 0u, |
| 264 | "checking move counts"); |
| 265 | } |
| 266 | } |