File: env_var.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (297 lines) | stat: -rw-r--r-- 11,851 bytes parent folder | download | duplicates (19)
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
///////////////////////////////////////////////////////////////////////////////
// env_var.cpp
//
//  Copyright 2012 Eric Niebler. 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)

#include <cstring>
#include <sstream>
#include <boost/config.hpp>
#include <boost/detail/workaround.hpp>
#include <boost/proto/proto.hpp>
#include <boost/test/unit_test.hpp>

namespace proto = boost::proto;

BOOST_PROTO_DEFINE_ENV_VAR(tag0_type, tag0);

struct abstract
{
    virtual ~abstract() = 0;
};

abstract::~abstract() {}

struct concrete : abstract
{
    ~concrete() {}
};

template<typename Tag, typename Env>
void assert_has_env_var(Env const &)
{
    BOOST_MPL_ASSERT((proto::result_of::has_env_var<Env, Tag>));
}

template<typename Tag, typename Env>
void assert_has_env_var_not(Env const &)
{
    BOOST_MPL_ASSERT_NOT((proto::result_of::has_env_var<Env, Tag>));
}

void test_is_env()
{
    BOOST_MPL_ASSERT_NOT((proto::is_env<int>));
    BOOST_MPL_ASSERT_NOT((proto::is_env<int &>));
    BOOST_MPL_ASSERT_NOT((proto::is_env<int const &>));

    BOOST_MPL_ASSERT_NOT((proto::is_env<abstract>));
    BOOST_MPL_ASSERT_NOT((proto::is_env<abstract &>));
    BOOST_MPL_ASSERT_NOT((proto::is_env<abstract const &>));

    BOOST_MPL_ASSERT((proto::is_env<proto::empty_env>));
    BOOST_MPL_ASSERT((proto::is_env<proto::empty_env &>));
    BOOST_MPL_ASSERT((proto::is_env<proto::empty_env const &>));

    BOOST_MPL_ASSERT((proto::is_env<proto::env<tag0_type, int> >));
    BOOST_MPL_ASSERT((proto::is_env<proto::env<tag0_type, int> &>));
    BOOST_MPL_ASSERT((proto::is_env<proto::env<tag0_type, int> const &>));
}

void test_as_env()
{
    proto::env<proto::data_type, int> e0 = proto::as_env(2);
    BOOST_CHECK_EQUAL(e0[proto::data], 2);
    assert_has_env_var<proto::data_type>(e0);
    assert_has_env_var_not<tag0_type>(e0);

    int i = 39;
    proto::env<proto::data_type, int &> e1 = proto::as_env(boost::ref(i));
    assert_has_env_var<proto::data_type>(i);
    assert_has_env_var_not<tag0_type>(i);
    BOOST_CHECK_EQUAL(e1[proto::data], 39);
    BOOST_CHECK_EQUAL(&e1[proto::data], &i);

    proto::empty_env e2 = proto::as_env(proto::empty_env());
    proto::env<proto::data_type, int &> e3 = proto::as_env(e1);
    proto::env<proto::data_type, int &> & e4 = proto::as_env(boost::ref(e1));
    BOOST_CHECK_EQUAL(&e4, &e1);

    concrete c;
    abstract &a = c;
    std::stringstream sout;
    int rgi[2] = {};
    proto::env<proto::data_type, abstract &> e5 = proto::as_env(a);
    proto::env<proto::data_type, std::stringstream &> e6 = proto::as_env(sout);
    BOOST_CHECK_EQUAL(&e6[proto::data], &sout);
    proto::env<proto::data_type, int(&)[2]> e7 = proto::as_env(rgi);
    BOOST_CHECK_EQUAL(&e7[proto::data][0], &rgi[0]);
    proto::env<proto::data_type, void(&)()> e8 = proto::as_env(test_as_env);
    BOOST_CHECK_EQUAL(&e8[proto::data], &test_as_env);
}

void test_comma()
{
    proto::env<proto::data_type, int> e0 = (proto::data = 1);
    BOOST_CHECK_EQUAL(e0[proto::data], 1);

    int i = 39;
    proto::env<proto::data_type, int &> e1 = (proto::data = boost::ref(i));
    BOOST_CHECK_EQUAL(e1[proto::data], 39);
    BOOST_CHECK_EQUAL(&e1[proto::data], &i);

    concrete c;
    abstract &a = c;
    std::stringstream sout;
    int rgi[2] = {};
    proto::env<proto::data_type, abstract &> e5 = (proto::data = a);
    proto::env<proto::data_type, std::stringstream &> e6 = (proto::data = sout);
    BOOST_CHECK_EQUAL(&e6[proto::data], &sout);
    proto::env<proto::data_type, int(&)[2]> e7 = (proto::data = rgi);
    BOOST_CHECK_EQUAL(&e7[proto::data][0], &rgi[0]);
    // The test below fails on msvc due to a compiler bug
    // note: <https://connect.microsoft.com/VisualStudio/feedback/details/754684/premature-decay-of-function-types-in-overloaded-assignment-operator>
#if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
    proto::env<proto::data_type, void(&)()> e8 = (proto::data = boost::ref(test_as_env));
    BOOST_CHECK_EQUAL(&e8[proto::data], &test_as_env);
#else
    proto::env<proto::data_type, void(&)()> e8 = (proto::data = test_as_env);
    BOOST_CHECK_EQUAL(&e8[proto::data], &test_as_env);
#endif

    proto::env<
        tag0_type
      , char const (&)[6]
      , proto::env<proto::data_type, int>
    > e9 = (proto::data = 1, tag0 = "hello");
    BOOST_CHECK_EQUAL(e9[proto::data], 1);
    BOOST_CHECK_EQUAL(0, std::strcmp(e9[tag0], "hello"));

    proto::env<
        tag0_type
      , int
      , proto::env<
            tag0_type
          , char const (&)[6]
          , proto::env<proto::data_type, int>
        >
    > e10 = (proto::data = 1, tag0 = "hello", tag0 = 42);
    BOOST_CHECK_EQUAL(e10[proto::data], 1);
    BOOST_CHECK_EQUAL(e10[tag0], 42);

    proto::env<
        tag0_type
      , char const (&)[6]
      , proto::env<proto::data_type, abstract &>
    > e11 = (a, tag0 = "hello");
    BOOST_CHECK_EQUAL(&e11[proto::data], &a);
    BOOST_CHECK_EQUAL(0, std::strcmp(e11[tag0], "hello"));

    proto::env<
        tag0_type
      , int
      , proto::env<
            tag0_type
          , char const (&)[6]
          , proto::env<proto::data_type, abstract &>
        >
    > e12 = (a, tag0 = "hello", tag0 = 42);
    BOOST_CHECK_EQUAL(&e12[proto::data], &a);
    BOOST_CHECK_EQUAL(e12[tag0], 42);

    proto::env<tag0_type, int> e13 = (proto::empty_env(), tag0 = 42);
    BOOST_CHECK_EQUAL(e13[tag0], 42);
    assert_has_env_var<tag0_type>(e13);
    assert_has_env_var_not<proto::data_type>(e13);

    proto::empty_env empty;
    proto::env<tag0_type, int> e14 = (boost::ref(empty), tag0 = 42);
    BOOST_CHECK_EQUAL(e14[tag0], 42);

    proto::env<
        proto::data_type
      , char const (&)[6]
      , proto::env<tag0_type, int>
    > e15 = (boost::ref(e14), proto::data = "hello");
    BOOST_CHECK_EQUAL(e15[tag0], 42);
    BOOST_CHECK_EQUAL(0, std::strcmp(e15[proto::data], "hello"));

    proto::env<
        proto::data_type
      , char const (&)[6]
      , proto::env<tag0_type, int>
    > e16 = (proto::as_env(boost::ref(e14)), proto::data = "hello");
    BOOST_CHECK_EQUAL(e16[tag0], 42);
    BOOST_CHECK_EQUAL(0, std::strcmp(e16[proto::data], "hello"));
}

void test_result_of_env_var()
{
    typedef proto::empty_env env0_type;
    BOOST_MPL_ASSERT((boost::is_same<proto::result_of::env_var<env0_type, proto::data_type>::type, proto::key_not_found>));
    BOOST_MPL_ASSERT((boost::is_same<proto::result_of::env_var<env0_type &, proto::data_type>::type, proto::key_not_found>));
    BOOST_MPL_ASSERT((boost::is_same<proto::result_of::env_var<env0_type const &, proto::data_type>::type, proto::key_not_found>));

    typedef proto::env<proto::data_type, int> env1_type;
    BOOST_MPL_ASSERT((boost::is_same<proto::result_of::env_var<env1_type, proto::data_type>::type, int>));
    BOOST_MPL_ASSERT((boost::is_same<proto::result_of::env_var<env1_type &, proto::data_type>::type, int>));
    BOOST_MPL_ASSERT((boost::is_same<proto::result_of::env_var<env1_type const &, proto::data_type>::type, int>));

    typedef proto::env<proto::data_type, int &> env2_type;
    BOOST_MPL_ASSERT((boost::is_same<proto::result_of::env_var<env2_type, proto::data_type>::type, int &>));
    BOOST_MPL_ASSERT((boost::is_same<proto::result_of::env_var<env2_type &, proto::data_type>::type, int &>));
    BOOST_MPL_ASSERT((boost::is_same<proto::result_of::env_var<env2_type const &, proto::data_type>::type, int &>));

    typedef proto::env<proto::data_type, double, proto::env<tag0_type, abstract &> > env3_type;
    BOOST_MPL_ASSERT((boost::is_same<proto::result_of::env_var<env3_type, proto::data_type>::type, double>));
    BOOST_MPL_ASSERT((boost::is_same<proto::result_of::env_var<env3_type, tag0_type>::type, abstract &>));
    BOOST_MPL_ASSERT((boost::is_same<proto::result_of::env_var<env3_type &, proto::data_type>::type, double>));
    BOOST_MPL_ASSERT((boost::is_same<proto::result_of::env_var<env3_type &, tag0_type>::type, abstract &>));
    BOOST_MPL_ASSERT((boost::is_same<proto::result_of::env_var<env3_type const &, proto::data_type>::type, double>));
    BOOST_MPL_ASSERT((boost::is_same<proto::result_of::env_var<env3_type const &, tag0_type>::type, abstract &>));

    typedef proto::env<tag0_type, double, proto::env<tag0_type, abstract &> > env4_type;
    BOOST_MPL_ASSERT((boost::is_same<proto::result_of::env_var<env4_type, tag0_type>::type, double>));
    BOOST_MPL_ASSERT((boost::is_same<proto::result_of::env_var<env4_type &, tag0_type>::type, double>));
    BOOST_MPL_ASSERT((boost::is_same<proto::result_of::env_var<env4_type const &, tag0_type>::type, double>));
}

void test_env_var()
{
    proto::key_not_found x0 = proto::env_var<proto::data_type>(proto::empty_env());
    proto::key_not_found x1 = proto::env_var<proto::data_type>(tag0 = 42);
    int x2 = proto::env_var<tag0_type>(tag0 = 42);
    BOOST_CHECK_EQUAL(x2, 42);
    int x3 = proto::functional::env_var<tag0_type>()(tag0 = 42);
    BOOST_CHECK_EQUAL(x3, 42);

    int i = 43;
    int & x4 = proto::env_var<tag0_type>(tag0 = boost::ref(i));
    BOOST_CHECK_EQUAL(&x4, &i);
    int & x5 = proto::functional::env_var<tag0_type>()(tag0 = boost::ref(i));
    BOOST_CHECK_EQUAL(&x5, &i);

    concrete c;
    abstract &a = c;
    abstract &x6 = proto::env_var<tag0_type>(tag0 = a);
    BOOST_CHECK_EQUAL(&x6, &a);
    abstract &x7 = proto::functional::env_var<tag0_type>()(tag0 = a);
    BOOST_CHECK_EQUAL(&x7, &a);

    abstract &x8 = proto::env_var<tag0_type>((42, tag0 = a));
    BOOST_CHECK_EQUAL(&x8, &a);
    abstract &x9 = proto::functional::env_var<tag0_type>()((42, tag0 = a));
    BOOST_CHECK_EQUAL(&x9, &a);
}

void test_env_var_tfx()
{
    typedef proto::terminal<int>::type int_;
    int_ i = {42};

    // tests for _env
    BOOST_MPL_ASSERT((boost::is_same<boost::result_of<proto::_env(int_ &)>::type, proto::empty_env>));
    BOOST_MPL_ASSERT((boost::is_same<boost::result_of<proto::_env(int_ &, int)>::type, proto::empty_env>));
    BOOST_MPL_ASSERT((boost::is_same<boost::result_of<proto::_env(int_ &, int &, float &)>::type, float &>));

    // Bummer, is there any way around this?
#ifdef BOOST_RESULT_OF_USE_DECLTYPE
    BOOST_MPL_ASSERT((boost::is_same<boost::result_of<proto::_env(int_ &, int &, float)>::type, float const &>));
    BOOST_MPL_ASSERT((boost::is_same<boost::tr1_result_of<proto::_env(int_ &, int &, float)>::type, float>));
#else
    BOOST_MPL_ASSERT((boost::is_same<boost::result_of<proto::_env(int_ &, int &, float)>::type, float>));
    BOOST_MPL_ASSERT((boost::is_same<boost::tr1_result_of<proto::_env(int_ &, int &, float)>::type, float>));
#endif

    double d = 3.14;
    double & rd = proto::_env()(i, 0, d);
    BOOST_CHECK_EQUAL(&d, &rd);

    proto::env<proto::data_type, int> e0 = proto::_env()(i, 0, proto::as_env(42));
    BOOST_CHECK_EQUAL(e0[proto::data], 42);

    proto::env<proto::data_type, int> e1 = proto::_env()(i, 0, proto::functional::as_env()(42));
    BOOST_CHECK_EQUAL(e1[proto::data], 42);

    proto::env<proto::data_type, int> e2 = proto::_env()(i, 0, (proto::data = 42));
    BOOST_CHECK_EQUAL(e2[proto::data], 42);

    proto::env<proto::data_type, int, proto::env<proto::data_type, int> > e3 = proto::_env()(i, 0, (42, proto::data = 43));
    BOOST_CHECK_EQUAL(e3[proto::data], 43);
}

using namespace boost::unit_test;
///////////////////////////////////////////////////////////////////////////////
// init_unit_test_suite
//
test_suite* init_unit_test_suite( int argc, char* argv[] )
{
    test_suite *test = BOOST_TEST_SUITE("test for environment variables");
    test->add(BOOST_TEST_CASE(&test_as_env));
    test->add(BOOST_TEST_CASE(&test_comma));
    test->add(BOOST_TEST_CASE(&test_result_of_env_var));
    test->add(BOOST_TEST_CASE(&test_env_var));
    test->add(BOOST_TEST_CASE(&test_env_var_tfx));
    return test;
}