1// Boost.TypeErasure library
2//
3// Copyright 2011 Steven Watanabe
4//
5// Distributed under the Boost Software License Version 1.0. (See
6// accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9// $Id$
10
11#include <boost/type_erasure/any.hpp>
12#include <boost/type_erasure/builtin.hpp>
13#include <boost/type_erasure/operators.hpp>
14#include <boost/type_erasure/any_cast.hpp>
15#include <boost/mpl/vector.hpp>
16
17#define BOOST_TEST_MAIN
18#include <boost/test/unit_test.hpp>
19
20using namespace boost::type_erasure;
21
22template<class T = _self>
23struct common : ::boost::mpl::vector<
24 copy_constructible<T>,
25 typeid_<T>
26> {};
27
28BOOST_AUTO_TEST_CASE(test_basic)
29{
30 typedef ::boost::mpl::vector<common<>, subscriptable<int&> > test_concept;
31 int i[5];
32 any<test_concept> x(&i[0]);
33 BOOST_CHECK_EQUAL(&x[0], &i[0]);
34}
35
36BOOST_AUTO_TEST_CASE(test_basic_const)
37{
38 typedef ::boost::mpl::vector<common<>, subscriptable<int&, const _self> > test_concept;
39 int i[5];
40 const any<test_concept> x(&i[0]);
41 BOOST_CHECK_EQUAL(&x[0], &i[0]);
42}
43
44BOOST_AUTO_TEST_CASE(test_any_result)
45{
46 typedef ::boost::mpl::vector<common<>, common<_a>, subscriptable<_a&, const _self> > test_concept;
47 typedef ::boost::mpl::map<
48 ::boost::mpl::pair<_self, int*>,
49 ::boost::mpl::pair<_a, int>
50 > types;
51 int i[5];
52 any<test_concept> x(&i[0], make_binding<types>());
53 any<test_concept, _a&> y(x[0]);
54 BOOST_CHECK_EQUAL(any_cast<int*>(&y), &i[0]);
55}
56
57BOOST_AUTO_TEST_CASE(test_any_result_const)
58{
59 typedef ::boost::mpl::vector<common<>, common<_a>, subscriptable<const _a&, const _self> > test_concept;
60 typedef ::boost::mpl::map<
61 ::boost::mpl::pair<_self, const int*>,
62 ::boost::mpl::pair<_a, int>
63 > types;
64 const int i[5] = { 0, 0, 0, 0, 0 };
65 any<test_concept> x(&i[0], make_binding<types>());
66 any<test_concept, const _a&> y(x[0]);
67 BOOST_CHECK_EQUAL(any_cast<const int*>(&y), &i[0]);
68}
69

source code of boost/libs/type_erasure/test/test_subscript.cpp