1 /*=============================================================================
2 Copyright (c) 1998-2003 Joel de Guzman
3 http://spirit.sourceforge.net/
4
5 Use, modification and distribution is subject to the Boost Software
6 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 #include <iostream>
10 #include <boost/detail/lightweight_test.hpp>
11 #include <string>
12
13 using namespace std;
14
15 #include <boost/spirit/include/classic_core.hpp>
16 using namespace BOOST_SPIRIT_CLASSIC_NS;
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //
20 // Match tests
21 //
22 ///////////////////////////////////////////////////////////////////////////////
23 struct X {};
YY24 struct Y { Y(int) {} }; // not default constructible
ZZ25 struct Z { Z(double n):n(n){} double n; }; // implicitly convertible from double
26
27 void
match_tests()28 match_tests()
29 {
30 match<> m0;
31 BOOST_TEST(!m0.has_valid_attribute());
32
33 match<int> m1(m0);
34 m1.value(123);
35 BOOST_TEST(m1.has_valid_attribute());
36 BOOST_TEST(m1.value() == 123);
37
38 match<double> m2(m1);
39 BOOST_TEST(m2.has_valid_attribute());
40 BOOST_TEST(m1.value() == int(m2.value()));
41 m2.value(456);
42
43 *&m0 = m0; // match<nil> = match<nil>
44 m0 = m1; // match<nil> = match<int>
45 m0 = m2; // match<nil> = match<double>
46 m1 = m0; // match<int> = match<nil>
47 BOOST_TEST(!m1);
48 BOOST_TEST(!m1.has_valid_attribute());
49
50 *&m1 = m1; // match<int> = match<int>
51 m1.value(int(m2.value()));
52 BOOST_TEST(m1.has_valid_attribute());
53 BOOST_TEST(m1.value() == int(m2.value()));
54
55 m2.value(123.456);
56 match<Z> mz(m2); // copy from match<double>
57 mz = m2; // assign from match<double>
58 BOOST_TEST(mz.value().n == 123.456);
59
60 m1.value(123);
61 m2 = m0;
62 BOOST_TEST(!m2);
63 BOOST_TEST(!m2.has_valid_attribute());
64
65 m2 = m1; // match<double> = match<int>
66 BOOST_TEST(m2.has_valid_attribute());
67 BOOST_TEST(m1.value() == int(m2.value()));
68 *&m2 = m2; // match<double> = match<double>
69
70 cout << "sizeof(int) == " << sizeof(int) << '\n';
71 cout << "sizeof(match<>) == " << sizeof(m0) << '\n';
72 cout << "sizeof(match<int>) == " << sizeof(m1) << '\n';
73 cout << "sizeof(match<double>) == " << sizeof(m2) << '\n';
74
75 match<int&> mr;
76 BOOST_TEST(!mr.has_valid_attribute());
77
78 match<int&> mrr(4);
79 BOOST_TEST(!mrr.has_valid_attribute());
80
81 int ri = 3;
82 match<int&> mr2(4, ri);
83 BOOST_TEST(mr2.has_valid_attribute());
84 mr = mr2;
85 BOOST_TEST(mr.has_valid_attribute());
86
87 match<int&> mr3(mrr);
88 BOOST_TEST(!mr3.has_valid_attribute());
89 mr2 = mr3;
90 BOOST_TEST(!mr2.has_valid_attribute());
91
92 match<X> mx;
93 m1 = mx;
94 m0 = mx;
95 BOOST_TEST(!mx.has_valid_attribute());
96 BOOST_TEST(!m0.has_valid_attribute());
97 BOOST_TEST(!m1.has_valid_attribute());
98
99 match<Y> my;
100 BOOST_TEST(!my.has_valid_attribute());
101
102 match<std::string> ms;
103 BOOST_TEST(!ms.has_valid_attribute());
104 ms.value("Kimpo Ponchwayla");
105 BOOST_TEST(ms.has_valid_attribute());
106 BOOST_TEST(ms.value() == "Kimpo Ponchwayla");
107 ms = match<>();
108 BOOST_TEST(!ms.has_valid_attribute());
109
110 // let's try a match with a reference:
111 int i;
112 match<int&> mr4(4, i);
113 BOOST_TEST(mr4.has_valid_attribute());
114 mr4.value(3);
115 BOOST_TEST(mr4.value() == 3);
116 BOOST_TEST(i == 3);
117 (void)i;
118
119 int x = 456;
120 match<int&> mref(1, x);
121 BOOST_TEST(mref.value() == 456);
122 mref.value(123);
123 BOOST_TEST(mref.value() == 123);
124 x = mref.value();
125 BOOST_TEST(x == 123);
126 mref.value() = 986;
127 BOOST_TEST(x == 986);
128
129 std::string s("hello");
130 match<int> mint(1, x);
131 BOOST_TEST(mint.value() == x);
132 match<std::string> mstr(1, s);
133 BOOST_TEST(mstr.value() == "hello");
134 mstr = mint;
135 mint = mstr;
136 }
137
138 ///////////////////////////////////////////////////////////////////////////////
139 //
140 // Match Policy tests
141 //
142 ///////////////////////////////////////////////////////////////////////////////
143 void
match_policy_tests()144 match_policy_tests()
145 {
146 match<> m0;
147 match<int> m1;
148 match<double> m2;
149 match_policy mp;
150
151 m0 = mp.no_match(); BOOST_TEST(!m0);
152 m1 = mp.no_match(); BOOST_TEST(!m1);
153 m0 = mp.empty_match(); BOOST_TEST(!!m0);
154 m2 = mp.empty_match(); BOOST_TEST(!!m2);
155
156 m1 = mp.create_match(5, 100, 0, 0);
157 m2 = mp.create_match(5, 10.5, 0, 0);
158
159 mp.concat_match(m1, m2);
160 BOOST_TEST(m1.length() == 10);
161 }
162
163 ///////////////////////////////////////////////////////////////////////////////
164 //
165 // Main
166 //
167 ///////////////////////////////////////////////////////////////////////////////
168 int
main()169 main()
170 {
171 match_tests();
172 match_policy_tests();
173 return boost::report_errors();
174 }
175
176