• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2016-2018 T. Zachary Laine
2 //
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 #include <boost/yap/expression.hpp>
7 
8 #include <boost/test/minimal.hpp>
9 
10 #include <sstream>
11 
12 
13 template<typename T>
14 using term = boost::yap::terminal<boost::yap::expression, T>;
15 
16 template<typename T>
17 using term_ref = boost::yap::expression_ref<boost::yap::expression, term<T> &>;
18 
19 template<typename T>
20 using term_cref =
21     boost::yap::expression_ref<boost::yap::expression, term<T> const &>;
22 
23 namespace yap = boost::yap;
24 namespace bh = boost::hana;
25 
26 
27 struct callable
28 {
operator ()callable29     int operator()() { return 42; }
30 };
31 
32 struct side_effect_callable_1
33 {
operator ()side_effect_callable_134     int operator()()
35     {
36         *value_ = 1;
37         return 0;
38     }
39 
40     int * value_;
41 };
42 
43 struct side_effect_callable_2
44 {
operator ()side_effect_callable_245     int operator()()
46     {
47         *value_ = 2;
48         return 0;
49     }
50 
51     int * value_;
52 };
53 
54 
test_main(int,char * [])55 int test_main(int, char * [])
56 {
57     {
58         int one = 0;
59         int two = 0;
60 
61         auto true_nothrow_throw_expr = if_else(
62             term<bool>{{true}},
63             term<callable>{}(),
64             term<side_effect_callable_1>{{&one}}());
65 
66         BOOST_CHECK(yap::evaluate(true_nothrow_throw_expr) == 42);
67         BOOST_CHECK(one == 0);
68         BOOST_CHECK(two == 0);
69     }
70 
71     {
72         int one = 0;
73         int two = 0;
74 
75         auto false_nothrow_throw_expr = if_else(
76             term<bool>{{false}},
77             term<callable>{}(),
78             term<side_effect_callable_1>{{&one}}());
79 
80         BOOST_CHECK(yap::evaluate(false_nothrow_throw_expr) == 0);
81         BOOST_CHECK(one == 1);
82         BOOST_CHECK(two == 0);
83     }
84 
85     {
86         int one = 0;
87         int two = 0;
88 
89         auto true_throw1_throw2_expr = if_else(
90             term<bool>{{true}},
91             term<side_effect_callable_1>{{&one}}(),
92             term<side_effect_callable_2>{{&two}}());
93 
94         BOOST_CHECK(yap::evaluate(true_throw1_throw2_expr) == 0);
95         BOOST_CHECK(one == 1);
96         BOOST_CHECK(two == 0);
97     }
98 
99     {
100         int one = 0;
101         int two = 0;
102 
103         auto false_throw1_throw2_expr = if_else(
104             term<bool>{{false}},
105             term<side_effect_callable_1>{{&one}}(),
106             term<side_effect_callable_2>{{&two}}());
107 
108         BOOST_CHECK(yap::evaluate(false_throw1_throw2_expr) == 0);
109         BOOST_CHECK(one == 0);
110         BOOST_CHECK(two == 2);
111     }
112 
113     return 0;
114 }
115