~ubuntu-branches/debian/sid/boost1.49/sid

« back to all changes in this revision

Viewing changes to libs/any/any_test.cpp

  • Committer: Package Import Robot
  • Author(s): Steve M. Robbins
  • Date: 2012-02-26 00:31:44 UTC
  • Revision ID: package-import@ubuntu.com-20120226003144-eaytp12cbf6ubpms
Tags: upstream-1.49.0
ImportĀ upstreamĀ versionĀ 1.49.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        { "cast to reference types",        test_cast_to_reference }
 
51
    };
 
52
 
 
53
    const test_case_iterator begin = test_cases;
 
54
    const test_case_iterator end =
 
55
        test_cases + (sizeof test_cases / sizeof *test_cases);
 
56
}
 
57
 
 
58
namespace any_tests // test definitions
 
59
{
 
60
    using namespace boost;
 
61
 
 
62
    void test_default_ctor()
 
63
    {
 
64
        const any value;
 
65
 
 
66
        check_true(value.empty(), "empty");
 
67
        check_null(any_cast<int>(&value), "any_cast<int>");
 
68
        check_equal(value.type(), typeid(void), "type");
 
69
    }
 
70
 
 
71
    void test_converting_ctor()
 
72
    {
 
73
        std::string text = "test message";
 
74
        any value = text;
 
75
 
 
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>");
 
80
        check_equal(
 
81
            any_cast<std::string>(value), text,
 
82
            "comparing cast copy against original text");
 
83
        check_unequal(
 
84
            any_cast<std::string>(&value), &text,
 
85
            "comparing address in copy against original text");
 
86
    }
 
87
 
 
88
    void test_copy_ctor()
 
89
    {
 
90
        std::string text = "test message";
 
91
        any original = text, copy = original;
 
92
 
 
93
        check_false(copy.empty(), "empty");
 
94
        check_equal(original.type(), copy.type(), "type");
 
95
        check_equal(
 
96
            any_cast<std::string>(original), any_cast<std::string>(copy),
 
97
            "comparing cast copy against original");
 
98
        check_equal(
 
99
            text, any_cast<std::string>(copy),
 
100
            "comparing cast copy against original text");
 
101
        check_unequal(
 
102
            any_cast<std::string>(&original),
 
103
            any_cast<std::string>(&copy),
 
104
            "comparing address in copy against original");
 
105
    }
 
106
 
 
107
    void test_copy_assign()
 
108
    {
 
109
        std::string text = "test message";
 
110
        any original = text, copy;
 
111
        any * assign_result = &(copy = original);
 
112
 
 
113
        check_false(copy.empty(), "empty");
 
114
        check_equal(original.type(), copy.type(), "type");
 
115
        check_equal(
 
116
            any_cast<std::string>(original), any_cast<std::string>(copy),
 
117
            "comparing cast copy against cast original");
 
118
        check_equal(
 
119
            text, any_cast<std::string>(copy),
 
120
            "comparing cast copy against original text");
 
121
        check_unequal(
 
122
            any_cast<std::string>(&original),
 
123
            any_cast<std::string>(&copy),
 
124
            "comparing address in copy against original");
 
125
        check_equal(assign_result, &copy, "address of assignment result");
 
126
    }
 
127
 
 
128
    void test_converting_assign()
 
129
    {
 
130
        std::string text = "test message";
 
131
        any value;
 
132
        any * assign_result = &(value = text);
 
133
 
 
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>");
 
138
        check_equal(
 
139
            any_cast<std::string>(value), text,
 
140
            "comparing cast copy against original text");
 
141
        check_unequal(
 
142
            any_cast<std::string>(&value),
 
143
            &text,
 
144
            "comparing address in copy against original text");
 
145
        check_equal(assign_result, &value, "address of assignment result");
 
146
    }
 
147
 
 
148
    void test_bad_cast()
 
149
    {
 
150
        std::string text = "test message";
 
151
        any value = text;
 
152
 
 
153
        TEST_CHECK_THROW(
 
154
            any_cast<const char *>(value),
 
155
            bad_any_cast,
 
156
            "any_cast to incorrect type");
 
157
    }
 
158
 
 
159
    void test_swap()
 
160
    {
 
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);
 
165
 
 
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");
 
169
        check_equal(
 
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");
 
173
        check_equal(
 
174
            original_ptr,
 
175
            any_cast<std::string>(&swapped),
 
176
            "comparing address in swapped against original");
 
177
        check_equal(swap_result, &original, "address of swap result");
 
178
    }
 
179
 
 
180
    void test_null_copying()
 
181
    {
 
182
        const any null;
 
183
        any copied = null, assigned;
 
184
        assigned = null;
 
185
 
 
186
        check_true(null.empty(), "empty on null");
 
187
        check_true(copied.empty(), "empty on copied");
 
188
        check_true(assigned.empty(), "empty on copied");
 
189
    }
 
190
 
 
191
    void test_cast_to_reference()
 
192
    {
 
193
        any a(137);
 
194
        const any b(a);
 
195
 
 
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);
 
200
 
 
201
        check_true(
 
202
            &ra == &ra_c && &ra == &ra_v && &ra == &ra_cv,
 
203
            "cv references to same obj");
 
204
 
 
205
        int const &          rb_c  = any_cast<int const &>(b);
 
206
        int const volatile & rb_cv = any_cast<int const volatile &>(b);
 
207
 
 
208
        check_true(&rb_c == &rb_cv, "cv references to copied const obj");
 
209
        check_true(&ra != &rb_c, "copies hold different objects");
 
210
 
 
211
        ++ra;
 
212
        int incremented = any_cast<int>(a);
 
213
        check_true(incremented == 138, "increment by reference changes value");
 
214
 
 
215
        TEST_CHECK_THROW(
 
216
            any_cast<char &>(a),
 
217
            bad_any_cast,
 
218
            "any_cast to incorrect reference type");
 
219
 
 
220
        TEST_CHECK_THROW(
 
221
            any_cast<const char &>(b),
 
222
            bad_any_cast,
 
223
            "any_cast to incorrect const reference type");
 
224
    }
 
225
 
 
226
}
 
227
 
 
228
// Copyright Kevlin Henney, 2000, 2001. All rights reserved.
 
229
//
 
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)
 
233
//