]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/lambda/test/operator_tests_simple.cpp
1 // operator_tests_simple.cpp -- The Boost Lambda Library ---------------
3 // Copyright (C) 2000-2003 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
4 // Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
10 // For more information, see www.boost.org
12 // -----------------------------------------------------------------------
16 #include <boost/core/lightweight_test.hpp>
17 #define BOOST_CHECK BOOST_TEST
19 #include "boost/lambda/lambda.hpp"
21 #include "boost/lambda/detail/suppress_unused.hpp"
23 #include <boost/shared_ptr.hpp>
32 #ifndef BOOST_NO_STRINGSTREAM
37 using namespace boost
;
39 using namespace boost::lambda
;
42 class unary_plus_tester
{};
43 unary_plus_tester
operator+(const unary_plus_tester
& a
) { return a
; }
47 #ifndef BOOST_NO_STRINGSTREAM
53 (os
<< constant("FOO"))();
55 BOOST_CHECK(os
.str() == std::string("10FOO"));
58 istringstream
is("ABC 1");
65 BOOST_CHECK(s
== std::string("ABC"));
67 // test for constant, constant_ref and var
69 constant_type
<int>::type
ci(constant(i
));
70 var_type
<int>::type
vi(var(i
));
72 (vi
= _1
)(make_const(100));
73 BOOST_CHECK((ci
)() == 5);
74 BOOST_CHECK(i
== 100);
77 constant_ref_type
<int>::type
cr(constant_ref(i
));
78 (++vi
, var(a
) = cr
)();
79 BOOST_CHECK(i
== 101);
83 void arithmetic_operators() {
84 int i
= 1; int j
= 2; int k
= 3;
87 using namespace boost::lambda
;
89 BOOST_CHECK((_1
+ 1)(i
)==2);
90 BOOST_CHECK(((_1
+ 1) * _2
)(i
, j
)==4);
91 BOOST_CHECK((_1
- 1)(i
)==0);
93 BOOST_CHECK((_1
* 2)(j
)==4);
94 BOOST_CHECK((_1
/ 2)(j
)==1);
96 BOOST_CHECK((_1
% 2)(k
)==1);
98 BOOST_CHECK((-_1
)(i
) == -1);
99 BOOST_CHECK((+_1
)(i
) == 1);
101 // test that unary plus really does something
103 unary_plus_tester up
= (+_1
)(u
);
105 boost::lambda::detail::suppress_unused_variable_warnings(up
);
108 void bitwise_operators() {
111 BOOST_CHECK((_1
<< 1)(ui
)==(2 << 1));
112 BOOST_CHECK((_1
>> 1)(ui
)==(2 >> 1));
114 BOOST_CHECK((_1
& 1)(ui
)==(2 & 1));
115 BOOST_CHECK((_1
| 1)(ui
)==(2 | 1));
116 BOOST_CHECK((_1
^ 1)(ui
)==(2 ^ 1));
117 BOOST_CHECK((~_1
)(ui
)==~2u);
120 void comparison_operators() {
123 BOOST_CHECK((_1
< _2
)(i
, j
) == true);
124 BOOST_CHECK((_1
<= _2
)(i
, j
) == true);
125 BOOST_CHECK((_1
== _2
)(i
, j
) == false);
126 BOOST_CHECK((_1
!= _2
)(i
, j
) == true);
127 BOOST_CHECK((_1
> _2
)(i
, j
) == false);
128 BOOST_CHECK((_1
>= _2
)(i
, j
) == false);
130 BOOST_CHECK((!(_1
< _2
))(i
, j
) == false);
131 BOOST_CHECK((!(_1
<= _2
))(i
, j
) == false);
132 BOOST_CHECK((!(_1
== _2
))(i
, j
) == true);
133 BOOST_CHECK((!(_1
!= _2
))(i
, j
) == false);
134 BOOST_CHECK((!(_1
> _2
))(i
, j
) == true);
135 BOOST_CHECK((!(_1
>= _2
))(i
, j
) == true);
138 void logical_operators() {
140 bool t
= true, f
= false;
141 BOOST_CHECK((_1
&& _2
)(t
, t
) == true);
142 BOOST_CHECK((_1
&& _2
)(t
, f
) == false);
143 BOOST_CHECK((_1
&& _2
)(f
, t
) == false);
144 BOOST_CHECK((_1
&& _2
)(f
, f
) == false);
146 BOOST_CHECK((_1
|| _2
)(t
, t
) == true);
147 BOOST_CHECK((_1
|| _2
)(t
, f
) == true);
148 BOOST_CHECK((_1
|| _2
)(f
, t
) == true);
149 BOOST_CHECK((_1
|| _2
)(f
, f
) == false);
151 BOOST_CHECK((!_1
)(t
) == false);
152 BOOST_CHECK((!_1
)(f
) == true);
154 // test short circuiting
174 void unary_incs_and_decs() {
177 BOOST_CHECK(_1
++(i
) == 0);
181 BOOST_CHECK(_1
--(i
) == 0);
182 BOOST_CHECK(i
== -1);
185 BOOST_CHECK((++_1
)(i
) == 1);
189 BOOST_CHECK((--_1
)(i
) == -1);
190 BOOST_CHECK(i
== -1);
193 // the result of prefix -- and ++ are lvalues
203 void compound_operators() {
207 // normal variable as the left operand
208 (i
+= _1
)(make_const(1));
211 (i
-= _1
)(make_const(1));
214 (i
*= _1
)(make_const(10));
215 BOOST_CHECK(i
== 10);
217 (i
/= _1
)(make_const(2));
220 (i
%= _1
)(make_const(2));
223 // lambda expression as a left operand
231 BOOST_CHECK(i
== 10);
239 // lambda expression as a left operand with rvalue on RHS
247 BOOST_CHECK(i
== 10);
258 BOOST_CHECK(ui
==(2 << 1));
262 BOOST_CHECK(ui
==(2 >> 1));
265 (ui
<<= _1
)(make_const(1));
266 BOOST_CHECK(ui
==(2 << 1));
269 (ui
>>= _1
)(make_const(1));
270 BOOST_CHECK(ui
==(2 >> 1));
275 BOOST_CHECK(ui
==(2 & 1));
279 BOOST_CHECK(ui
==(2 | 1));
283 BOOST_CHECK(ui
==(2 ^ 1));
286 (ui
&= _1
)(make_const(1));
287 BOOST_CHECK(ui
==(2 & 1));
290 (ui
|= _1
)(make_const(1));
291 BOOST_CHECK(ui
==(2 | 1));
294 (ui
^= _1
)(make_const(1));
295 BOOST_CHECK(ui
==(2 ^ 1));
299 void assignment_and_subscript() {
301 // assignment and subscript need to be defined as member functions.
302 // Hence, if you wish to use a normal variable as the left hand argument,
303 // you must wrap it with var to turn it into a lambda expression
309 BOOST_CHECK(s
== string("one"));
312 BOOST_CHECK(s
== string("two"));
314 BOOST_CHECK((var(s
)[_1
])(make_const(2)) == 'o');
315 BOOST_CHECK((_1
[2])(s
) == 'o');
316 BOOST_CHECK((_1
[_2
])(s
, make_const(2)) == 'o');
318 // subscript returns lvalue
319 (var(s
)[_1
])(make_const(1)) = 'o';
320 BOOST_CHECK(s
== "too");
323 BOOST_CHECK(s
== "tao");
325 (_1
[_2
])(s
, make_const(0)) = 'm';
326 BOOST_CHECK(s
== "mao");
328 // TODO: tests for vector, set, map, multimap
333 void address_of_and_dereference() {
337 BOOST_CHECK((&_1
)(a
) == &a
);
338 BOOST_CHECK((*&_1
)(i
) == 42);
340 std::vector
<int> vi
; vi
.push_back(1);
341 std::vector
<int>::iterator it
= vi
.begin();
344 BOOST_CHECK(vi
[0] == 7);
345 const std::vector
<int>::iterator
cit(it
);
347 BOOST_CHECK(vi
[0] == 8);
349 // TODO: Add tests for more complex iterator types
351 boost::shared_ptr
<int> ptr(new int(0));
353 BOOST_CHECK(*ptr
== 7);
354 const boost::shared_ptr
<int> cptr(ptr
);
356 BOOST_CHECK(*ptr
== 8);
364 BOOST_CHECK((_1
= 10, 2 * _1
)(i
) == 20);
366 // TODO: that the return type is the exact type of the right argument
367 // (that r/l valueness is preserved)
371 void pointer_arithmetic() {
373 int ia
[4] = { 1, 2, 3, 4 };
375 int* ia_last
= &ia
[3];
377 const int cia
[4] = { 1, 2, 3, 4 };
378 const int* cip
= cia
;
379 const int* cia_last
= &cia
[3];
383 BOOST_CHECK((*(_1
+ 1))(ia
) == 2);
386 BOOST_CHECK((*(_1
+ 1))(ip
) == 2);
388 BOOST_CHECK((*(_1
- 1))(ia_last
) == 3);
391 BOOST_CHECK((*(_1
+ 1))(cia
) == 2);
393 BOOST_CHECK((*(_1
+ 1))(cip
) == 2);
394 BOOST_CHECK((*(_1
- 1))(cia_last
) == 3);
396 // pointer arithmetic should not make non-consts const
400 BOOST_CHECK(ia
[2] == 0);
401 BOOST_CHECK(ia
[3] == 0);
404 BOOST_CHECK((_1
- _2
)(ia_last
, ia
) == 3);
405 BOOST_CHECK((_1
- _2
)(cia_last
, cia
) == 3);
406 BOOST_CHECK((ia_last
- _1
)(ia
) == 3);
407 BOOST_CHECK((cia_last
- _1
)(cia
) == 3);
408 BOOST_CHECK((cia_last
- _1
)(cip
) == 3);
414 arithmetic_operators();
416 comparison_operators();
418 unary_incs_and_decs();
419 compound_operators();
420 assignment_and_subscript();
421 address_of_and_dereference();
423 pointer_arithmetic();
425 return boost::report_errors();