1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
|
// Copyright Kevlin Henney, 2000, 2001. All rights reserved.
// Copyright Antony Polukhin, 2013-2025.
// Copyright Ruslan Arutyunyan, 2019-2021.
//
// 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)
#ifndef BOOST_ANY_TEST_BASIC_TEST_HPP
#define BOOST_ANY_TEST_BASIC_TEST_HPP
// what: unit tests for variant type boost::any
// who: contributed by Kevlin Henney
// when: July 2001, 2013, 2014
// where: tested with BCC 5.5, MSVC 6.0, and g++ 2.95
#include <cstdlib>
#include <string>
#include <vector>
#include <utility>
#include <type_traits>
#include <boost/type_index.hpp>
#include <boost/core/lightweight_test.hpp>
namespace any_tests {
struct huge_structure {
char take_place[1024];
std::string text;
};
template <typename Any>
struct basic_tests // test definitions
{
struct copy_counter
{
public:
copy_counter() {}
copy_counter(const copy_counter&) { ++count; }
copy_counter& operator=(const copy_counter&) { ++count; return *this; }
static int get_count() { return count; }
private:
static int count;
};
static void test_default_ctor()
{
const Any value;
BOOST_TEST(value.empty());
BOOST_TEST_EQ(boost::any_cast<int>(&value), nullptr);
BOOST_TEST(value.type() == boost::typeindex::type_id<void>());
}
static void test_converting_ctor()
{
std::string text = "test message";
Any value = text;
BOOST_TEST(!value.empty());
BOOST_TEST(value.type() == boost::typeindex::type_id<std::string>());
BOOST_TEST_EQ(boost::any_cast<int>(&value), nullptr);
BOOST_TEST_NE(boost::any_cast<std::string>(&value), nullptr);
BOOST_TEST_EQ(boost::any_cast<std::string>(value), text);
BOOST_TEST_NE(boost::any_cast<std::string>(&value), &text);
}
static void test_copy_ctor()
{
std::string text = "test message";
Any original = text, copy = original;
BOOST_TEST(!copy.empty());
BOOST_TEST(boost::typeindex::type_index(original.type()) == copy.type());
BOOST_TEST_EQ(
boost::any_cast<std::string>(original),
boost::any_cast<std::string>(copy)
);
BOOST_TEST_EQ(text, boost::any_cast<std::string>(copy));
BOOST_TEST_NE(
boost::any_cast<std::string>(&original),
boost::any_cast<std::string>(©)
);
}
static void test_copy_assign()
{
std::string text = "test message";
Any original = text, copy;
Any * assign_result = &(copy = original);
BOOST_TEST(!copy.empty());
BOOST_TEST(boost::typeindex::type_index(original.type()) == copy.type());
BOOST_TEST_EQ(
boost::any_cast<std::string>(original),
boost::any_cast<std::string>(copy)
);
BOOST_TEST_EQ(text, boost::any_cast<std::string>(copy));
BOOST_TEST_NE(
boost::any_cast<std::string>(&original),
boost::any_cast<std::string>(©)
);
BOOST_TEST_EQ(assign_result, ©);
}
static void test_converting_assign()
{
std::string text = "test message";
Any value;
Any * assign_result = &(value = text);
BOOST_TEST(!value.empty());
BOOST_TEST(value.type() == boost::typeindex::type_id<std::string>());
BOOST_TEST_EQ(boost::any_cast<int>(&value), nullptr);
BOOST_TEST_NE(boost::any_cast<std::string>(&value), nullptr);
BOOST_TEST_EQ(boost::any_cast<std::string>(value), text);
BOOST_TEST_NE(boost::any_cast<std::string>(&value), &text);
BOOST_TEST_EQ(assign_result, &value);
}
static void test_bad_cast()
{
std::string text = "test message";
Any value = text;
BOOST_TEST_THROWS(
boost::any_cast<const char *>(value),
boost::bad_any_cast
);
}
static void test_swap()
{
huge_structure stored;
stored.text = "test message";
Any original = stored;
Any swapped;
huge_structure * original_ptr = boost::any_cast<huge_structure>(&original);
Any * swap_result = &original.swap(swapped);
BOOST_TEST(original.empty());
BOOST_TEST(!swapped.empty());
BOOST_TEST(swapped.type() == boost::typeindex::type_id<huge_structure>());
BOOST_TEST_EQ(stored.text, boost::any_cast<huge_structure>(swapped).text);
BOOST_TEST_NE(original_ptr, nullptr);
BOOST_TEST_EQ(
original_ptr,
boost::any_cast<huge_structure>(&swapped)
);
BOOST_TEST_EQ(swap_result, &original);
swap(swapped, swapped);
BOOST_TEST(!swapped.empty());
BOOST_TEST(
swapped.type() == boost::typeindex::type_id<huge_structure>()
);
BOOST_TEST_EQ(
stored.text, boost::any_cast<huge_structure>(swapped).text
);
Any copy1 = copy_counter();
Any copy2 = copy_counter();
int count = copy_counter::get_count();
swap(copy1, copy2);
BOOST_TEST_EQ(count, copy_counter::get_count());
Any any_char = '1';
swap(any_char, swapped);
BOOST_TEST_EQ(
stored.text, boost::any_cast<huge_structure>(any_char).text
);
BOOST_TEST(
swapped.type() == boost::typeindex::type_id<char>()
);
BOOST_TEST_EQ('1', boost::any_cast<char>(swapped));
}
static void test_null_copying()
{
const Any null;
Any copied = null, assigned;
assigned = null;
BOOST_TEST(null.empty());
BOOST_TEST(copied.empty());
BOOST_TEST(assigned.empty());
}
static void test_cast_to_reference()
{
Any a(137);
const Any b(a);
int & ra = boost::any_cast<int &>(a);
int const & ra_c = boost::any_cast<int const &>(a);
int volatile & ra_v = boost::any_cast<int volatile &>(a);
int const volatile & ra_cv = boost::any_cast<int const volatile&>(a);
BOOST_TEST(&ra == &ra_c && &ra == &ra_v && &ra == &ra_cv);
int const & rb_c = boost::any_cast<int const &>(b);
int const volatile & rb_cv = boost::any_cast<int const volatile &>(b);
BOOST_TEST(&rb_c == &rb_cv);
BOOST_TEST(&ra != &rb_c);
++ra;
int incremented = boost::any_cast<int>(a);
BOOST_TEST(incremented == 138);
BOOST_TEST_THROWS(
boost::any_cast<char &>(a),
boost::bad_any_cast
);
BOOST_TEST_THROWS(
boost::any_cast<const char &>(b),
boost::bad_any_cast
);
}
static void test_bad_any_cast()
{
BOOST_TEST((
std::is_base_of<std::exception, boost::bad_any_cast>::value
));
BOOST_TEST(
std::string(boost::bad_any_cast().what()).find("any") != std::string::npos
);
}
static void test_with_array()
{
Any value1("Char array");
Any value2;
value2 = "Char array";
BOOST_TEST(!value1.empty());
BOOST_TEST(!value2.empty());
BOOST_TEST(value1.type() == boost::typeindex::type_id<const char*>());
BOOST_TEST(value2.type() == boost::typeindex::type_id<const char*>());
BOOST_TEST(boost::any_cast<const char*>(&value1));
BOOST_TEST(boost::any_cast<const char*>(&value2));
}
static void test_clear()
{
std::string text = "test message";
Any value = text;
BOOST_TEST(!value.empty());
value.clear();
BOOST_TEST(value.empty());
value.clear();
BOOST_TEST(value.empty());
value = text;
BOOST_TEST(!value.empty());
value.clear();
BOOST_TEST(value.empty());
}
// Following tests cover the case from #9462
// https://svn.boost.org/trac/boost/ticket/9462
static Any makeVec()
{
return std::vector<int>(100 /*size*/, 7 /*value*/);
}
static void test_vectors()
{
const std::vector<int>& vec = boost::any_cast<std::vector<int> >(makeVec());
BOOST_TEST_EQ(vec.size(), 100u);
BOOST_TEST_EQ(vec.back(), 7);
BOOST_TEST_EQ(vec.front(), 7);
std::vector<int> vec1 = boost::any_cast<std::vector<int> >(makeVec());
BOOST_TEST_EQ(vec1.size(), 100u);
BOOST_TEST_EQ(vec1.back(), 7);
BOOST_TEST_EQ(vec1.front(), 7);
}
template<typename T>
class class_with_address_op
{
public:
class_with_address_op(const T* p)
: ptr(p)
{}
const T** operator &()
{
return &ptr;
}
const T* get() const
{
return ptr;
}
private:
const T* ptr;
};
static void test_addressof()
{
int val = 10;
const int* ptr = &val;
class_with_address_op<int> obj(ptr);
Any test_val(obj);
class_with_address_op<int> returned_obj = boost::any_cast<class_with_address_op<int> >(test_val);
BOOST_TEST_EQ(&val, returned_obj.get());
BOOST_TEST(!!boost::any_cast<class_with_address_op<int> >(&test_val));
BOOST_TEST_EQ(boost::unsafe_any_cast<class_with_address_op<int> >(&test_val)->get(), ptr);
}
static void test_multiple_assign()
{
Any test_val = 10;
BOOST_TEST(!!boost::any_cast<int>(&test_val));
test_val = '0';
BOOST_TEST(!!boost::any_cast<char>(&test_val));
test_val = huge_structure();
BOOST_TEST(!!boost::any_cast<huge_structure>(&test_val));
test_val = '0';
BOOST_TEST(!!boost::any_cast<char>(&test_val));
test_val = Any(huge_structure());
BOOST_TEST(!!boost::any_cast<huge_structure>(&test_val));
}
static int run_tests()
{
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();
}
};
template <typename Any>
int basic_tests<Any>::copy_counter::count = 0;
}
#endif
|