1 // what: unit tests for variant type boost::any
2 // who: contributed by Kevlin Henney
4 // where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95
10 #include "boost/any.hpp"
15 typedef test<const char *, void (*)()> test_case;
16 typedef const test_case * test_case_iterator;
18 extern const test_case_iterator begin, end;
23 using namespace any_tests;
24 tester<test_case_iterator> test_suite(begin, end);
25 return test_suite() ? EXIT_SUCCESS : EXIT_FAILURE;
28 namespace any_tests // test suite
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();
37 void test_null_copying();
38 void test_cast_to_reference();
40 const test_case test_cases[] =
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 { "cast to reference types", test_cast_to_reference }
53 const test_case_iterator begin = test_cases;
54 const test_case_iterator end =
55 test_cases + (sizeof test_cases / sizeof *test_cases);
58 namespace any_tests // test definitions
60 using namespace boost;
62 void test_default_ctor()
66 check_true(value.empty(), "empty");
67 check_null(any_cast<int>(&value), "any_cast<int>");
68 check_equal(value.type(), typeid(void), "type");
71 void test_converting_ctor()
73 std::string text = "test message";
76 check_false(value.empty(), "empty");
77 check_equal(value.type(), typeid(std::string), "type");
78 check_null(any_cast<int>(&value), "any_cast<int>");
79 check_non_null(any_cast<std::string>(&value), "any_cast<std::string>");
81 any_cast<std::string>(value), text,
82 "comparing cast copy against original text");
84 any_cast<std::string>(&value), &text,
85 "comparing address in copy against original text");
90 std::string text = "test message";
91 any original = text, copy = original;
93 check_false(copy.empty(), "empty");
94 check_equal(original.type(), copy.type(), "type");
96 any_cast<std::string>(original), any_cast<std::string>(copy),
97 "comparing cast copy against original");
99 text, any_cast<std::string>(copy),
100 "comparing cast copy against original text");
102 any_cast<std::string>(&original),
103 any_cast<std::string>(©),
104 "comparing address in copy against original");
107 void test_copy_assign()
109 std::string text = "test message";
110 any original = text, copy;
111 any * assign_result = &(copy = original);
113 check_false(copy.empty(), "empty");
114 check_equal(original.type(), copy.type(), "type");
116 any_cast<std::string>(original), any_cast<std::string>(copy),
117 "comparing cast copy against cast original");
119 text, any_cast<std::string>(copy),
120 "comparing cast copy against original text");
122 any_cast<std::string>(&original),
123 any_cast<std::string>(©),
124 "comparing address in copy against original");
125 check_equal(assign_result, ©, "address of assignment result");
128 void test_converting_assign()
130 std::string text = "test message";
132 any * assign_result = &(value = text);
134 check_false(value.empty(), "type");
135 check_equal(value.type(), typeid(std::string), "type");
136 check_null(any_cast<int>(&value), "any_cast<int>");
137 check_non_null(any_cast<std::string>(&value), "any_cast<std::string>");
139 any_cast<std::string>(value), text,
140 "comparing cast copy against original text");
142 any_cast<std::string>(&value),
144 "comparing address in copy against original text");
145 check_equal(assign_result, &value, "address of assignment result");
150 std::string text = "test message";
154 any_cast<const char *>(value),
156 "any_cast to incorrect type");
161 std::string text = "test message";
162 any original = text, swapped;
163 std::string * original_ptr = any_cast<std::string>(&original);
164 any * swap_result = &original.swap(swapped);
166 check_true(original.empty(), "empty on original");
167 check_false(swapped.empty(), "empty on swapped");
168 check_equal(swapped.type(), typeid(std::string), "type");
170 text, any_cast<std::string>(swapped),
171 "comparing swapped copy against original text");
172 check_non_null(original_ptr, "address in pre-swapped original");
175 any_cast<std::string>(&swapped),
176 "comparing address in swapped against original");
177 check_equal(swap_result, &original, "address of swap result");
180 void test_null_copying()
183 any copied = null, assigned;
186 check_true(null.empty(), "empty on null");
187 check_true(copied.empty(), "empty on copied");
188 check_true(assigned.empty(), "empty on copied");
191 void test_cast_to_reference()
196 int & ra = any_cast<int &>(a);
197 int const & ra_c = any_cast<int const &>(a);
198 int volatile & ra_v = any_cast<int volatile &>(a);
199 int const volatile & ra_cv = any_cast<int const volatile&>(a);
202 &ra == &ra_c && &ra == &ra_v && &ra == &ra_cv,
203 "cv references to same obj");
205 int const & rb_c = any_cast<int const &>(b);
206 int const volatile & rb_cv = any_cast<int const volatile &>(b);
208 check_true(&rb_c == &rb_cv, "cv references to copied const obj");
209 check_true(&ra != &rb_c, "copies hold different objects");
212 int incremented = any_cast<int>(a);
213 check_true(incremented == 138, "increment by reference changes value");
218 "any_cast to incorrect reference type");
221 any_cast<const char &>(b),
223 "any_cast to incorrect const reference type");
228 // Copyright Kevlin Henney, 2000, 2001. All rights reserved.
230 // Distributed under the Boost Software License, Version 1.0. (See
231 // accompanying file LICENSE_1_0.txt or copy at
232 // http://www.boost.org/LICENSE_1_0.txt)