Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/variant/test/test1.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 3.0 KB
Line 
1//-----------------------------------------------------------------------------
2// boost-libs variant/test/test1.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 "class_a.h"
17#include "jobs.h"
18
19#include <iostream>
20#include <string>
21#include <vector>
22
23
24
25void run()
26{
27
28   using boost::apply_visitor;
29   using boost::variant;
30   using std::string;
31   using std::vector;
32   using std::cout;
33   using std::endl;
34
35   typedef variant< char*, string, short > t_var0;
36   typedef variant< int, string, double > t_var1;
37   typedef variant< short, const char* > t_var2;
38   typedef variant< string, char > t_var3;   
39   typedef variant< unsigned short, const char* > t_var4;
40   typedef variant< unsigned short, const char*, t_var2 > t_var5;
41   typedef variant< unsigned short, const char*, t_var5 > t_var6;
42   typedef variant< class_a, const void* > t_var7;
43   typedef variant< t_var6, int > t_var8;
44   typedef variant< t_var8, unsigned short > t_var9;
45   typedef variant< char, unsigned char > t_var10;
46   typedef variant< short, int, vector<int>, long> t_var11;
47
48   t_var1 v1;
49   t_var0 v0;
50   t_var2 v2;
51   t_var3 v3;
52   t_var4 v4;
53   t_var5 v5;
54   t_var6 v6;
55   t_var7 v7;
56   t_var8 v8;
57   t_var9 v9;
58   t_var10 v10;
59   t_var11 v11;
60
61
62   //
63   // Check assignment rules
64   //
65
66   v2 = 4;
67   v4 = v2;
68   verify(v4, spec<unsigned short>());
69
70   v2 = "abc";
71   v4 = v2;
72   verify(v4, spec<const char*>(), "[V] abc");
73
74   v5 = "def";
75   verify(v5, spec<const char*>(), "[V] def");
76
77   v5 = v2;
78   verify(v5, spec<t_var2>(), "[V] [V] abc");
79
80   v6 = 58;
81   verify(v6, spec<unsigned short>(), "[V] 58");
82
83   v6 = v5;
84   verify(v6, spec<t_var5>(), "[V] [V] [V] abc");
85
86   v8 = v2;
87   verify(v8, spec<t_var6>(), "[V] [V] abc");
88
89   v8 = v6;
90   verify(v8, spec<t_var6>(), "[V] [V] [V] [V] abc");
91
92   v7 = v2;
93   verify(v7, spec<const void*>());
94
95   v7 = 199;
96   verify(v7, spec<class_a>(), "[V] class_a(199)");
97
98   v2 = 200;
99   v7 = v2;
100   verify(v7, spec<class_a>(), "[V] class_a(200)");
101
102
103
104   //
105   // Check sizes of held values
106   //
107   total_sizeof ts;
108
109   v1 = 5.9;
110   apply_visitor(ts, v1);
111
112   v1 = 'B';
113   apply_visitor(ts, v1);
114
115   v1 = 3.4f;
116   apply_visitor(ts, v1);
117
118   BOOST_CHECK(ts.result() == sizeof(int) + sizeof(double)*2);
119
120   v11 = 5;
121   string res_s = apply_visitor(int_printer(), v11);
122   BOOST_CHECK(res_s == "5");
123
124   //
125   // A variant object holding an std::vector
126   //
127   vector<int> int_vec_1;
128   int_vec_1.push_back(512);
129   int_vec_1.push_back(256);
130   int_vec_1.push_back(128);
131   int_vec_1.push_back(64);
132
133   v11 = int_vec_1;
134   res_s = apply_visitor(int_printer(), v11);
135   BOOST_CHECK(res_s == ",512,256,128,64");
136}
137
138
139
140int test_main(int , char* [])
141{
142   run();
143   return 0;
144}
Note: See TracBrowser for help on using the repository browser.