File: test_value_params.cc

package info (click to toggle)
eckit 1.32.4-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 305,876 kB
  • sloc: cpp: 111,654; ansic: 2,826; yacc: 590; lex: 361; python: 237; sh: 202; makefile: 42
file content (367 lines) | stat: -rw-r--r-- 11,880 bytes parent folder | download | duplicates (2)
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
/*
 * (C) Copyright 1996- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation nor
 * does it submit to any jurisdiction.
 */
#include <sys/types.h>
#include <limits>

#include "eckit/filesystem/PathName.h"
#include "eckit/io/AutoCloser.h"
#include "eckit/serialisation/FileStream.h"
#include "eckit/value/CompositeParams.h"
#include "eckit/value/DispatchParams.h"
#include "eckit/value/Params.h"
#include "eckit/value/Properties.h"
#include "eckit/value/ScopeParams.h"

#include "eckit/testing/Test.h"

#include "AnyKeyParams.h"

using namespace std;
using namespace eckit;
using namespace eckit::testing;

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

namespace eckit_test {

const int imax                  = numeric_limits<int>::max();
const unsigned int uimax        = numeric_limits<unsigned int>::max();
const long long llmax           = numeric_limits<long long>::max();
const unsigned long long ullmax = numeric_limits<unsigned long long>::max();
const double dmax               = numeric_limits<double>::max();

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

class TestParams : public DispatchParams<TestParams> {
public:

    TestParams(const std::string& payload) : payload_(payload) { dispatch_["foo"] = &TestParams::getPayload; }
    TestParams(Stream& s) {
        dispatch_["foo"] = &TestParams::getPayload;
        s >> payload_;
    }

private:

    Params::value_t getPayload(const Params::key_t& key) const { return payload_; }

    friend void encode(const TestParams&, Stream&);

    string payload_;
};

void encode(const TestParams& p, Stream& s) {
    s << p.payload_;
}

Params::Factory<TestParams> testParamsFactory;

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

struct PropertiesFixture {
    PropertiesFixture() :
        p(Properties()
              .set("bool", true)
              .set("int", imax)
              .set("unsigned int", uimax)
              .set("long long", llmax)
              .set("unsigned long long", ullmax)
              .set("double", dmax)
              .set("string", "foo")
              .set("Length", Length(42))
              .set("Date", Date(2015, 2, 1))
              .set("PathName", PathName("/var/tmp"))) {}

    Params p;
};

struct CompositeParamsFixture {
    CompositeParamsFixture() :
        p(CompositeParams()
              .push_back(Params(Properties().set("bool", true)))
              .push_back(Params(Properties().set("int", imax)))
              .push_back(Params(Properties().set("unsigned int", uimax)))
              .push_back(Params(Properties().set("long long", llmax)))
              .push_back(Params(Properties().set("unsigned long long", ullmax)))
              .push_back(Params(Properties().set("double", dmax)))
              .push_back(Params(Properties().set("string", "foo")))
              .push_back(Params(Properties().set("Length", Length(42))))
              .push_back(Params(Properties().set("Date", Date(2015, 2, 1))))
              .push_back(Params(Properties().set("PathName", PathName("/var/tmp"))))) {}

    Params p;
};

struct ListParamsFixture {
    ListParamsFixture() : p(CompositeParams(ListParamsFixture::pl())) {}

    static Params::List& pl() {
        static Params::List l;
        if (!l.size()) {
            l.push_back(Params(Properties().set("bool", true)));
            l.push_back(Params(Properties().set("int", imax)));
            l.push_back(Params(Properties().set("unsigned int", uimax)));
            l.push_back(Params(Properties().set("long long", llmax)));
            l.push_back(Params(Properties().set("unsigned long long", ullmax)));
            l.push_back(Params(Properties().set("double", dmax)));
            l.push_back(Params(Properties().set("string", "foo")));
            l.push_back(Params(Properties().set("Length", Length(42))));
            l.push_back(Params(Properties().set("Date", Date(2015, 2, 1))));
            l.push_back(Params(Properties().set("PathName", PathName("/var/tmp"))));
        }
        return l;
    }

    Params p;
};

struct ScopedParamsFixture {
    ScopedParamsFixture() : p(ScopeParams("scope", Params(Properties().set("foo", "bar")))) {}

    Params p;
};

struct CompositeScopedParamsFixture {
    CompositeScopedParamsFixture() :
        p(CompositeParams()
              .push_back(Params(ScopeParams("user", Params(Properties().set("resol", 100)))))
              .push_back(Params(ScopeParams("default", Params(Properties().set("resol", 200)))))) {}

    Params p;
};

struct DispatchParamsFixture {
    DispatchParamsFixture() : p(TestParams("bar")) {}

    Params p;
};

struct AnyKeyParamsFixture {
    AnyKeyParamsFixture() : p(AnyKeyParams("foo")) {}

    Params p;
};

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

void test_keys(const Params& p) {
    EXPECT(p.has("bool"));
    EXPECT(p.has("int"));
    EXPECT(p.has("unsigned int"));
    EXPECT(p.has("long long"));
    EXPECT(p.has("unsigned long long"));
    EXPECT(p.has("double"));
    EXPECT(p.has("string"));
    EXPECT(p.has("Length"));
    EXPECT(p.has("Date"));
    EXPECT(p.has("PathName"));
    EXPECT(!p.has("foo"));
    EXPECT_THROWS_AS(p["foo"], BadParameter);
}

void test_vals(const Params& p) {
    EXPECT((bool)p["bool"] == true);
    EXPECT((int)p["int"] == imax);
    EXPECT((unsigned int)p["unsigned int"] == uimax);
    EXPECT((long long)p["long long"] == llmax);
    EXPECT((unsigned long long)p["unsigned long long"] == ullmax);
    EXPECT((double)p["double"] == dmax);
    EXPECT(p["string"] == "foo");
    EXPECT(p["Length"] == Length(42));
    EXPECT(p["Date"] == (Date(2015, 2, 1)));
    EXPECT(p["PathName"] == PathName("/var/tmp"));
}

void test_scope(const Params& p) {
    EXPECT(p.has("scope.foo"));
    EXPECT(!p.has("foo"));
    EXPECT(p["scope.foo"] == "bar");
    EXPECT_THROWS_AS(p["foo"], BadParameter);
}

void test_composite_scope(const Params& p) {
    EXPECT(p.has("user.resol"));
    EXPECT(p.has("default.resol"));
    EXPECT(!p.has("resol"));
    EXPECT((uint)p["user.resol"] == 100);
    EXPECT((uint)p["default.resol"] == 200);
}

void test_dispatch(const Params& p) {
    EXPECT(p.has("foo"));
    EXPECT(!p.has("bar"));
    EXPECT(p["foo"] == "bar");
}

void test_custom(const Params& p) {
    EXPECT(p.has("foo"));
    EXPECT(p.has("bar"));
    EXPECT(p["foo"] == "foo");
    EXPECT(p["bar"] == "foo");
}

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

Params stream_to_from_file(const Params& p) {
    PathName filename    = PathName::unique("data");
    std::string filepath = filename.asString();
    {
        FileStream sout(filepath.c_str(), "w");
        auto c = closer(sout);
        sout << p;
    }
    {
        FileStream sin(filepath.c_str(), "r");
        auto c = closer(sin);
        return Params(Params::decode(sin));
    }
}

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

CASE("test_properties") {

    PropertiesFixture fix;

    SECTION("test_properties_params_init") {
        eckit::Log::info() << "Initialize Properties" << std::endl;
        eckit::Log::info() << "Params: " << fix.p << std::endl;
        test_keys(fix.p);
        test_vals(fix.p);
    }

    SECTION("test_properties_params_streaming") {
        eckit::Log::info() << "Stream Properties" << std::endl;
        eckit::Log::info() << "original: " << fix.p << std::endl;
        Params params = stream_to_from_file(fix.p);
        eckit::Log::info() << "streamed: " << params << std::endl;
        test_keys(params);
        test_vals(params);
    }
}

CASE("test_composite_params") {

    CompositeParamsFixture fix;

    SECTION("test_composite_params") {
        eckit::Log::info() << "Initialize CompositeParams from Properties" << std::endl;
        eckit::Log::info() << "Params: " << fix.p << std::endl;
        test_keys(fix.p);
        test_vals(fix.p);
    }

    SECTION("test_composite_params_streaming") {
        eckit::Log::info() << "Stream CompositeParams initialised from Properties" << std::endl;
        eckit::Log::info() << "original: " << fix.p << std::endl;
        Params params = stream_to_from_file(fix.p);
        eckit::Log::info() << "streamed: " << params << std::endl;
        test_keys(params);
        test_vals(params);
    }
}


CASE("test_composite_params_list") {

    ListParamsFixture fix;

    SECTION("test_composite_params_list_init") {
        eckit::Log::info() << "Initialize CompositeParams from list of Properties" << std::endl;
        eckit::Log::info() << "Params: " << fix.p << std::endl;
        test_keys(fix.p);
        test_vals(fix.p);
    }

    SECTION("test_composite_params_list_streaming") {
        eckit::Log::info() << "Stream CompositeParams initialised from list of Properties" << std::endl;
        eckit::Log::info() << "original: " << fix.p << std::endl;
        Params params = stream_to_from_file(fix.p);
        eckit::Log::info() << "streamed: " << params << std::endl;
        test_keys(params);
        test_vals(params);
    }
}

CASE("test_scoped_params") {

    ScopedParamsFixture fix;

    SECTION("test_scope_params_init") {
        eckit::Log::info() << "Initialize ScopedParams" << std::endl;
        eckit::Log::info() << "Params: " << fix.p << std::endl;
        test_scope(fix.p);
    }

    SECTION("test_scope_params_streaming") {
        eckit::Log::info() << "Stream ScopedParams" << std::endl;
        eckit::Log::info() << "original: " << fix.p << std::endl;
        Params params = stream_to_from_file(fix.p);
        eckit::Log::info() << "streamed: " << params << std::endl;
        test_scope(params);
    }
}

CASE("test_composite_scope_params") {

    CompositeScopedParamsFixture fix;

    SECTION("test_composite_scope_params_init") {
        eckit::Log::info() << "Initialize CompositeParams from ScopedParams" << std::endl;
        eckit::Log::info() << "Params: " << fix.p << std::endl;
        test_composite_scope(fix.p);
    }

    SECTION("test_composite_scope_params_streaming") {
        eckit::Log::info() << "Stream CompositeParams initialised from ScopedParams" << std::endl;
        eckit::Log::info() << "original: " << fix.p << std::endl;
        Params params = stream_to_from_file(fix.p);
        eckit::Log::info() << "streamed: " << params << std::endl;
        test_composite_scope(params);
    }
}

CASE("test_dispatch_params") {

    DispatchParamsFixture fix;

    SECTION("test_dispatch_params_init") {
        eckit::Log::info() << "Initialize DispatchParams" << std::endl;
        test_dispatch(fix.p);
    }

    SECTION("test_dispatch_params_streaming") {
        eckit::Log::info() << "Stream DispatchParams" << std::endl;
        Params params = stream_to_from_file(fix.p);
        test_dispatch(params);
    }
}

CASE("test_custom_params") {

    AnyKeyParamsFixture fix;

    SECTION("test_custom_params_init") {
        eckit::Log::info() << "Initialize custom AnyKeyParams" << std::endl;
        test_custom(fix.p);
    }

    SECTION("test_custom_params_streaming") {
        eckit::Log::info() << "Stream custom AnyKeyParams" << std::endl;
        Params params = stream_to_from_file(fix.p);
        test_custom(params);
    }
}

}  // namespace eckit_test

int main(int argc, char* argv[]) {
    return run_tests(argc, argv);
}