summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/phoenix/test/bll_compatibility/operator_tests_simple.cpp
blob: 8a533515468116d640af35d5f4b0ad8e5e4518df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//  operator_tests_simple.cpp  -- The Boost Lambda Library ---------------
//
// Copyright (C) 2000-2003 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
// Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// For more information, see www.boost.org

// -----------------------------------------------------------------------



#include <boost/core/lightweight_test.hpp>

#include "boost/lambda/lambda.hpp"

#include "boost/lambda/detail/suppress_unused.hpp"

#include <boost/shared_ptr.hpp>

#include <vector>
#include <map>
#include <set>
#include <string>

#include <iostream>

#ifndef BOOST_NO_STRINGSTREAM
#include <sstream>
#endif

using namespace std;
using namespace boost;

using namespace boost::lambda;


class unary_plus_tester {};
unary_plus_tester operator+(const unary_plus_tester& a) { return a; } 

void cout_tests()
{
#ifndef BOOST_NO_STRINGSTREAM
  using std::cout;
  ostringstream os;
  int i = 10; 
  (os << _1)(i);

  (os << constant("FOO"))();

  BOOST_TEST_EQ(os.str(), std::string("10FOO"));
  

  istringstream is("ABC 1");
  std::string s;
  int k;

  is >> s;
  is >> k;

  BOOST_TEST_EQ(s, std::string("ABC"));
  BOOST_TEST_EQ(k, 1);
  // test for constant, constant_ref and var
  i = 5;
  constant_type<int>::type ci(constant(i));
  var_type<int>::type vi(var(i)); 

  (vi = _1)(make_const(100));
  BOOST_TEST_EQ((ci)(), 5);
  BOOST_TEST_EQ(i, 100);

  int a;
  constant_ref_type<int>::type cr(constant_ref(i));
  (++vi, var(a) = cr)();
  BOOST_TEST_EQ(i, 101);
#endif
}

void arithmetic_operators() {
  int i = 1; int j = 2; int k = 3;

  using namespace std;
  using namespace boost::lambda;
   
  BOOST_TEST_EQ((_1 + 1)(i), 2);
  BOOST_TEST_EQ(((_1 + 1) * _2)(i, j), 4);
  BOOST_TEST_EQ((_1 - 1)(i), 0);

  BOOST_TEST_EQ((_1 * 2)(j), 4);
  BOOST_TEST_EQ((_1 / 2)(j), 1);

  BOOST_TEST_EQ((_1 % 2)(k), 1);

  BOOST_TEST_EQ((-_1)(i), -1);
  BOOST_TEST_EQ((+_1)(i), 1);
  
  // test that unary plus really does something
  unary_plus_tester u;
  unary_plus_tester up = (+_1)(u);

  boost::lambda::detail::suppress_unused_variable_warnings(up);
}

void bitwise_operators() {
  unsigned int ui = 2;

  BOOST_TEST_EQ((_1 << 1)(ui), (2 << 1));
  BOOST_TEST_EQ((_1 >> 1)(ui), (2 >> 1));

  BOOST_TEST_EQ((_1 & 1)(ui), (2 & 1));
  BOOST_TEST_EQ((_1 | 1)(ui), (2 | 1));
  BOOST_TEST_EQ((_1 ^ 1)(ui), (2 ^ 1));
  BOOST_TEST_EQ((~_1)(ui), ~2u);
}

void comparison_operators() {
  int i = 0, j = 1;

  BOOST_TEST((_1 < _2)(i, j));
  BOOST_TEST((_1 <= _2)(i, j));
  BOOST_TEST(!(_1 == _2)(i, j));
  BOOST_TEST((_1 != _2)(i, j));
  BOOST_TEST(!(_1 > _2)(i, j));
  BOOST_TEST(!(_1 >= _2)(i, j));

  BOOST_TEST(!(!(_1 < _2))(i, j));
  BOOST_TEST(!(!(_1 <= _2))(i, j));
  BOOST_TEST((!(_1 == _2))(i, j));
  BOOST_TEST(!(!(_1 != _2))(i, j));
  BOOST_TEST((!(_1 > _2))(i, j));
  BOOST_TEST((!(_1 >= _2))(i, j));
}

void logical_operators() {

  bool t = true, f = false;
  BOOST_TEST((_1 && _2)(t, t));
  BOOST_TEST(!(_1 && _2)(t, f));
  BOOST_TEST(!(_1 && _2)(f, t));
  BOOST_TEST(!(_1 && _2)(f, f));

  BOOST_TEST((_1 || _2)(t, t));
  BOOST_TEST((_1 || _2)(t, f));
  BOOST_TEST((_1 || _2)(f, t));
  BOOST_TEST(!(_1 || _2)(f, f));

  BOOST_TEST(!(!_1)(t));
  BOOST_TEST((!_1)(f));

  // test short circuiting
  int i=0;

  (false && ++_1)(i);
  BOOST_TEST_EQ(i, 0);
  i = 0;

  (true && ++_1)(i);
  BOOST_TEST_EQ(i, 1);
  i = 0;

  (false || ++_1)(i);
  BOOST_TEST_EQ(i, 1);
  i = 0;

  (true || ++_1)(i);
  BOOST_TEST_EQ(i, 0);
  i = 0;
}

void unary_incs_and_decs() {
  int i = 0;

  BOOST_TEST_EQ(_1++(i), 0);
  BOOST_TEST_EQ(i, 1);
  i = 0;

  BOOST_TEST_EQ(_1--(i), 0);
  BOOST_TEST_EQ(i, -1);
  i = 0;

  BOOST_TEST_EQ((++_1)(i), 1);
  BOOST_TEST_EQ(i, 1);
  i = 0;

  BOOST_TEST_EQ((--_1)(i), -1);
  BOOST_TEST_EQ(i, -1);
  i = 0;

  // the result of prefix -- and ++ are lvalues
  (++_1)(i) = 10;
  BOOST_TEST_EQ(i, 10);
  i = 0;

  (--_1)(i) = 10;
  BOOST_TEST_EQ(i, 10);
  i = 0;
}

void compound_operators() { 

  int i = 1; 

  // normal variable as the left operand
  (i += _1)(make_const(1));
  BOOST_TEST_EQ(i, 2);

  (i -= _1)(make_const(1));
  BOOST_TEST_EQ(i, 1);

  (i *= _1)(make_const(10));
  BOOST_TEST_EQ(i, 10);

  (i /= _1)(make_const(2));
  BOOST_TEST_EQ(i, 5);

  (i %= _1)(make_const(2));
  BOOST_TEST_EQ(i, 1);

  // lambda expression as a left operand
  (_1 += 1)(i);
  BOOST_TEST_EQ(i, 2);

  (_1 -= 1)(i);
  BOOST_TEST_EQ(i, 1);

  (_1 *= 10)(i);
  BOOST_TEST_EQ(i, 10);

  (_1 /= 2)(i);
  BOOST_TEST_EQ(i, 5);

  (_1 %= 2)(i);
  BOOST_TEST_EQ(i, 1);
  
  // lambda expression as a left operand with rvalue on RHS
  (_1 += (0 + 1))(i);
  BOOST_TEST_EQ(i, 2);

  (_1 -= (0 + 1))(i);
  BOOST_TEST_EQ(i, 1);

  (_1 *= (0 + 10))(i);
  BOOST_TEST_EQ(i, 10);

  (_1 /= (0 + 2))(i);
  BOOST_TEST_EQ(i, 5);

  (_1 %= (0 + 2))(i);
  BOOST_TEST_EQ(i, 1);
  
  // shifts
  unsigned int ui = 2;
  (_1 <<= 1)(ui);
  BOOST_TEST_EQ(ui, (2 << 1));

  ui = 2;
  (_1 >>= 1)(ui);
  BOOST_TEST_EQ(ui, (2 >> 1));

  ui = 2;
  (ui <<= _1)(make_const(1));
  BOOST_TEST_EQ(ui, (2 << 1));

  ui = 2;
  (ui >>= _1)(make_const(1));
  BOOST_TEST_EQ(ui, (2 >> 1));

  // and, or, xor
  ui = 2;
  (_1 &= 1)(ui);
  BOOST_TEST_EQ(ui, (2 & 1));

  ui = 2;
  (_1 |= 1)(ui);
  BOOST_TEST_EQ(ui, (2 | 1));

  ui = 2;
  (_1 ^= 1)(ui);
  BOOST_TEST_EQ(ui, (2 ^ 1));

  ui = 2;
  (ui &= _1)(make_const(1));
  BOOST_TEST_EQ(ui, (2 & 1));

  ui = 2;
  (ui |= _1)(make_const(1));
  BOOST_TEST_EQ(ui, (2 | 1));

  ui = 2;
  (ui ^= _1)(make_const(1));
  BOOST_TEST_EQ(ui, (2 ^ 1));
    
}

void assignment_and_subscript() {

  // assignment and subscript need to be defined as member functions.
  // Hence, if you wish to use a normal variable as the left hand argument, 
  // you must wrap it with var to turn it into a lambda expression

  using std::string;
  string s;

  (_1 = "one")(s);
  BOOST_TEST_EQ(s, string("one"));

  (var(s) = "two")();
  BOOST_TEST_EQ(s, string("two"));

  BOOST_TEST_EQ((var(s)[_1])(make_const(2)), 'o');
  BOOST_TEST_EQ((_1[2])(s), 'o');
  BOOST_TEST_EQ((_1[_2])(s, make_const(2)), 'o');

  // subscript returns lvalue
  (var(s)[_1])(make_const(1)) = 'o';
  BOOST_TEST_EQ(s, "too");
 
  (_1[1])(s) = 'a';
  BOOST_TEST_EQ(s, "tao");

  (_1[_2])(s, make_const(0)) = 'm';
  BOOST_TEST_EQ(s, "mao");

  // TODO: tests for vector, set, map, multimap
}

class A {};

void address_of_and_dereference() {
  
  A a; int i = 42;  
  
  BOOST_TEST_EQ((&_1)(a), &a);
  BOOST_TEST_EQ((*&_1)(i), 42);

  std::vector<int> vi; vi.push_back(1);
  std::vector<int>::iterator it = vi.begin();
  
  (*_1 = 7)(it);
  BOOST_TEST_EQ(vi[0], 7);
  const std::vector<int>::iterator cit(it);
  (*_1 = 8)(cit);
  BOOST_TEST_EQ(vi[0], 8);

  // TODO: Add tests for more complex iterator types

  boost::shared_ptr<int> ptr(new int(0));
  (*_1 = 7)(ptr);
  BOOST_TEST_EQ(*ptr, 7);
  const boost::shared_ptr<int> cptr(ptr);
  (*_1 = 8)(cptr);
  BOOST_TEST_EQ(*ptr, 8);
}



void comma() {

  int i = 100;
  BOOST_TEST_EQ((_1 = 10, 2 * _1)(i), 20);

  // TODO: that the return type is the exact type of the right argument
  // (that r/l valueness is preserved)

}

void pointer_arithmetic() {

  int ia[4] = { 1, 2, 3, 4 };
  int* ip = ia;
  int* ia_last = &ia[3];

  const int cia[4] = { 1, 2, 3, 4 };
  const int* cip = cia;
  const int* cia_last = &cia[3];

 
  // non-const array
  BOOST_TEST_EQ((*(_1 + 1))(ia), 2);

  // non-const pointer
  BOOST_TEST_EQ((*(_1 + 1))(ip), 2);

  BOOST_TEST_EQ((*(_1 - 1))(ia_last), 3);

  // const array
  BOOST_TEST_EQ((*(_1 + 1))(cia), 2);
  // const pointer
  BOOST_TEST_EQ((*(_1 + 1))(cip), 2);
  BOOST_TEST_EQ((*(_1 - 1))(cia_last), 3);
 
  // pointer arithmetic should not make non-consts const
    (*(_1 + 2))(ia) = 0;
    (*(_1 + 3))(ip) = 0;

  BOOST_TEST_EQ(ia[2], 0);
  BOOST_TEST_EQ(ia[3], 0);

  // pointer - pointer
  BOOST_TEST_EQ((_1 - _2)(ia_last, ia), 3);
  BOOST_TEST_EQ((_1 - _2)(cia_last, cia), 3);
  BOOST_TEST_EQ((ia_last - _1)(ia), 3);
  BOOST_TEST_EQ((cia_last - _1)(cia), 3);
  BOOST_TEST_EQ((cia_last - _1)(cip), 3);

}

int main()
{
  arithmetic_operators();
  bitwise_operators();
  comparison_operators();
  logical_operators();
  unary_incs_and_decs();
  compound_operators();
  assignment_and_subscript();
  address_of_and_dereference();
  comma();
  pointer_arithmetic();
  cout_tests();
  return boost::report_errors();
}