1 | //----------------------------------------------------------------------------- |
---|
2 | // boost-libs variant/test/test5.cpp header file |
---|
3 | // See http://www.boost.org for updates, documentation, and revision history. |
---|
4 | //----------------------------------------------------------------------------- |
---|
5 | // |
---|
6 | // Copyright (c) 2003 |
---|
7 | // Eric Friedman, Itay Maman |
---|
8 | // |
---|
9 | // Distributed under the Boost Software License, Version 1.0. (See |
---|
10 | // accompanying file LICENSE_1_0.txt or copy at |
---|
11 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
12 | |
---|
13 | #include "boost/test/minimal.hpp" |
---|
14 | #include "boost/variant.hpp" |
---|
15 | |
---|
16 | #include "jobs.h" |
---|
17 | |
---|
18 | #include <assert.h> |
---|
19 | #include <iostream> |
---|
20 | #include <string> |
---|
21 | #include <vector> |
---|
22 | |
---|
23 | |
---|
24 | void run() |
---|
25 | { |
---|
26 | using std::string; |
---|
27 | using boost::variant; |
---|
28 | using boost::apply_visitor; |
---|
29 | |
---|
30 | typedef variant<int, float, unsigned short, unsigned char> t_var1; |
---|
31 | typedef variant<int, t_var1, unsigned short, unsigned char> t_var2; |
---|
32 | typedef variant<string, int, t_var2> t_var3; |
---|
33 | |
---|
34 | t_var1 v1; |
---|
35 | t_var2 v2; |
---|
36 | t_var2 v2_second; |
---|
37 | t_var3 v3; |
---|
38 | |
---|
39 | const char c0 = 'x'; |
---|
40 | v1 = c0; |
---|
41 | |
---|
42 | //v2 and v3 are holding (aka: containing) a variant |
---|
43 | v2 = v1; |
---|
44 | v3 = v2; |
---|
45 | |
---|
46 | verify(v1, spec<int>()); |
---|
47 | verify(v2, spec<t_var1>()); |
---|
48 | verify(v3, spec<t_var2>()); |
---|
49 | |
---|
50 | |
---|
51 | // |
---|
52 | // assignment from const char (Converted to int) |
---|
53 | // |
---|
54 | v2 = c0; |
---|
55 | v3 = c0; |
---|
56 | |
---|
57 | verify(v2, spec<int>()); |
---|
58 | verify(v3, spec<int>()); |
---|
59 | |
---|
60 | |
---|
61 | BOOST_CHECK(apply_visitor(sum_int(), v2) == c0); |
---|
62 | BOOST_CHECK(apply_visitor(sum_int(), v3) == c0); |
---|
63 | |
---|
64 | sum_int adder; |
---|
65 | apply_visitor(adder, v2); |
---|
66 | apply_visitor(adder, v3); |
---|
67 | |
---|
68 | BOOST_CHECK(adder.result() == 2*c0); |
---|
69 | |
---|
70 | // |
---|
71 | // A variant holding a variant |
---|
72 | // |
---|
73 | typedef variant<unsigned char, float> t_var4; |
---|
74 | typedef variant<string, t_var4> t_var5; |
---|
75 | |
---|
76 | t_var4 v4; |
---|
77 | t_var5 v5; |
---|
78 | |
---|
79 | v5 = 22.5f; |
---|
80 | verify(v5, spec<t_var4>(), "[V] [V] 22.5"); |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | |
---|
85 | int test_main(int , char* []) |
---|
86 | { |
---|
87 | run(); |
---|
88 | return 0; |
---|
89 | } |
---|
90 | |
---|