File: more_expressions_tests.cpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (104 lines) | stat: -rw-r--r-- 3,463 bytes parent folder | download | duplicates (10)
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
/*=============================================================================
    Phoenix V1.2.1
    Copyright (c) 2001-2003 Joel de Guzman

    Use, modification and distribution is subject to 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)
==============================================================================*/
#include <iostream>
#include <boost/detail/lightweight_test.hpp>

#define PHOENIX_LIMIT 15
#include <boost/spirit/include/phoenix1_primitives.hpp>
#include <boost/spirit/include/phoenix1_composite.hpp>
#include <boost/spirit/include/phoenix1_functions.hpp>
#include <boost/spirit/include/phoenix1_operators.hpp>

using namespace phoenix;
using namespace std;

    ///////////////////////////////////////////////////////////////////////////////
    struct sqr_ {

        template <typename ArgT>
        struct result { typedef ArgT type; };

        template <typename ArgT>
        ArgT operator()(ArgT n) const { return n * n; }
    };

    function<sqr_> sqr;

    ///////////////////////////////////////////////////////////////////////////////
    struct adder_ {

        template <typename Arg1T, typename Arg2T, typename ArgT3>
        struct result { typedef Arg1T type; };

        template <typename Arg1T, typename Arg2T, typename ArgT3>
        Arg1T operator()(Arg1T a, Arg2T b, ArgT3 c) const { return a + b + c; }
    };

    function<adder_> adder;

///////////////////////////////////////////////////////////////////////////////
int
main()
{
    int     i2 = 2, i = 4, i50 = 50, i10 = 10, i20 = 20, i100 = 100;
    double  d5 = 5, d10 = 10;
    string hello = "hello";

///////////////////////////////////////////////////////////////////////////////
//
//  More complex expressions
//
///////////////////////////////////////////////////////////////////////////////
    BOOST_TEST((10 - arg1)(i100) == (10 - i100));
    BOOST_TEST((20 - arg1)(i100) == (20 - i100));
    BOOST_TEST((arg1 - 10)(i100) == (i100 - 10));
    BOOST_TEST((arg1 - 20)(i100) == (i100 - 20));
    BOOST_TEST((arg1 - arg2)(i100, i50) == (i100 - i50));
    BOOST_TEST((arg1 - var(i))(i10) == (i10 - i));
    BOOST_TEST((arg1 + arg2 - arg3)(i100, i50, i20) == (i100 + i50 - i20));
    BOOST_TEST((sqr(arg1) + arg2 - arg3)(i100, i50, i20) == ((i100*i100) + i50 - i20));

    int ii = i;
    BOOST_TEST((var(i) += arg1)(i2) == (ii += i2));
    BOOST_TEST((sqr(sqr(arg1)))(i100) == (i100*i100*i100*i100));


#if 0   /*** SHOULD NOT COMPILE ***/
    (val(3) += arg1)(i100);
    (val(3) = 3)();
#endif

    BOOST_TEST(((adder(arg1, arg2, arg3) + arg2 - arg3)(i100, i50, i20)) == (i100 + i50 + i20) + i50 - i20);
    BOOST_TEST((adder(arg1, arg2, arg3)(i100, i50, i20)) == (i100 + i50 + i20));
    BOOST_TEST((sqr(sqr(sqr(sqr(arg1)))))(d10) == 1e16);
    BOOST_TEST((sqr(sqr(arg1)) / arg1 / arg1)(d5) == 25);

    for (int j = 0; j < 20; ++j)
    {
        cout << (10 < arg1)(j);
        BOOST_TEST((10 < arg1)(j) == (10 < j));
    }
    cout << endl;

    for (int k = 0; k < 20; ++k)
    {
        bool r = ((arg1 % 2 == 0) && (arg1 < 15))(k);
        cout << r;
        BOOST_TEST(r == ((k % 2 == 0) && (k < 15)));
    }
    cout << endl;

///////////////////////////////////////////////////////////////////////////////
//
//  End asserts
//
///////////////////////////////////////////////////////////////////////////////

    return boost::report_errors();    
}