PageRenderTime 43ms CodeModel.GetById 1ms app.highlight 37ms RepoModel.GetById 2ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/phoenix/test/scope/lambda_tests.cpp

http://hadesmem.googlecode.com/
C++ | 208 lines | 166 code | 34 blank | 8 comment | 18 complexity | ce0ac39affcda321a1fcb425bc4983b7 MD5 | raw file
  1/*=============================================================================
  2    Copyright (c) 2001-2007 Joel de Guzman
  3
  4    Distributed under the Boost Software License, Version 1.0. (See accompanying
  5    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6==============================================================================*/
  7#include <iostream>
  8#include <cmath>
  9#include <algorithm>
 10#include <vector>
 11
 12#include <typeinfo>
 13
 14#include <boost/detail/lightweight_test.hpp>
 15#include <boost/phoenix/core.hpp>
 16#include <boost/phoenix/operator.hpp>
 17#include <boost/phoenix/function.hpp>
 18#include <boost/phoenix/bind.hpp>
 19#include <boost/phoenix/scope.hpp>
 20
 21namespace boost { namespace phoenix
 22{
 23    struct for_each_impl
 24    {
 25        template <typename Sig>
 26        struct result;
 27
 28        template <typename This, typename C, typename F>
 29        struct result<This(C,F)>
 30        {
 31            typedef void type;
 32        };
 33
 34        template <typename C, typename F>
 35        void operator()(C& c, F f) const
 36        {
 37            std::for_each(c.begin(), c.end(), f);
 38        }
 39    };
 40
 41    function<for_each_impl> const for_each = for_each_impl();
 42
 43    struct push_back_impl
 44    {
 45        template <typename Sig>
 46        struct result;
 47
 48        template <typename This, typename C, typename T>
 49        struct result<This(C,T)>
 50        {
 51            typedef void type;
 52        };
 53
 54        template <typename C, typename T>
 55        void operator()(C& c, T& x) const
 56        {
 57            c.push_back(x);
 58        }
 59    };
 60
 61    function<push_back_impl> const push_back = push_back_impl();
 62}}
 63
 64struct zzz {};
 65
 66int
 67main()
 68{
 69    using boost::phoenix::lambda;
 70    using boost::phoenix::let;
 71    using boost::phoenix::ref;
 72    using boost::phoenix::val;
 73    using boost::phoenix::arg_names::_1;
 74    using boost::phoenix::arg_names::_2;
 75    using boost::phoenix::local_names::_a;
 76    using boost::phoenix::local_names::_b;
 77    using boost::phoenix::placeholders::arg1;
 78
 79    {
 80        int x = 1;
 81        int y = lambda[_1]()(x);
 82        BOOST_TEST(x == y);
 83    }
 84
 85    {
 86        int x = 1, y = 10;
 87        BOOST_TEST(
 88            (_1 + lambda[_1 + 2])(x)(y) == 1+10+2
 89        );
 90        BOOST_TEST(
 91            (_1 + lambda[-_1])(x)(y) == 1+-10
 92        );
 93    }
 94
 95    {
 96        int x = 1, y = 10, z = 13;
 97        BOOST_TEST(
 98            lambda(_a = _1, _b = _2)
 99            [
100                _1 + _a + _b
101            ]
102            (x, z)(y) == x + y + z
103        );
104    }
105
106    {
107        int x = 4;
108        int y = 5;
109        lambda(_a = _1)[_a = 555](x)();
110        BOOST_TEST(x == 555);
111        (void)y;
112    }
113
114    {
115        int x = 1;
116        long x2 = 2;
117        short x3 = 3;
118        char const* y = "hello";
119        zzz z;
120
121        BOOST_TEST(lambda[_1](x)(y) == y);
122        BOOST_TEST(lambda(_a = _1)[_a](x)(y) == x);
123        BOOST_TEST(lambda(_a = _1)[lambda[_a]](x)(y)(z) == x);
124        BOOST_TEST(lambda(_a = _1)[lambda[_a + _1]](x)(y)(x) == 2);
125        BOOST_TEST(lambda(_a = _1)[lambda(_b = _1)[_a + _b + _1]](x)(x2)(x3) == 6);
126    }
127
128    {
129        int x = 1, y = 10;
130        BOOST_TEST(
131            (_1 + lambda(_a = _1)[_a + _1 + 2])(x)(y) == 1+1+10+2
132        );
133    }
134
135    {
136        int x = 1, y = 10;
137        BOOST_TEST(
138        (
139                _1 + 
140                lambda(_a = _1)
141                [
142                    _a + lambda[_a + 2]
143                ]
144            )
145            (x)(y)(y) == 1+1+1+2
146        );
147    }
148
149    {
150        using boost::phoenix::for_each;
151
152        int init[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
153        std::vector<int> v(init, init+10);
154
155        int x = 0;
156        for_each(_1, lambda(_a = _2)[_a += _1])(v, x);
157        BOOST_TEST(x == 55);
158    }
159
160    {
161        using boost::phoenix::for_each;
162        using boost::phoenix::push_back;
163
164        int x = 10;
165        std::vector<std::vector<int> > v(10);
166
167        for_each(_1, lambda(_a = _2)[push_back(_1, _a)])(v, x);
168
169        int y = 0;
170        for_each(arg1, lambda[ref(y) += _1[0]])(v);
171        BOOST_TEST(y == 100);
172    }
173
174    {
175        int x = 1, y = 10, z = 13;
176    
177        BOOST_TEST(
178            lambda(_a = _1, _b = _2)
179            [
180                _1 + _a + _b
181            ]
182            (x, z)(y) == x + y + z
183        );
184    }
185
186    {
187		  {
188            // $$$ Fixme. This should not be failing $$$
189            int x = (let(_a = lambda[val(1)])[_a])()();
190            //BOOST_TEST(x == 1);
191		  }
192
193		  {
194            int x = (let(_a = lambda[val(1)])[bind(_a)])();
195            BOOST_TEST(x == 1);
196		  }
197    }
198
199    {
200        int i = 0;
201        int j = 2;
202        BOOST_TEST(lambda[let(_a = _1)[_a = _2]]()(i, j) == j);
203        BOOST_TEST(i == j);
204    }
205
206    return boost::report_errors();
207}
208