File: test_resource.cc

package info (click to toggle)
eckit 1.32.4-2
  • links: PTS
  • area: main
  • in suites: forky
  • size: 600,644 kB
  • sloc: cpp: 111,654; ansic: 2,826; yacc: 590; lex: 361; python: 237; sh: 202; makefile: 41
file content (107 lines) | stat: -rw-r--r-- 3,118 bytes parent folder | download | duplicates (4)
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
/*
 * (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 <cmath>

#include "eckit/config/LibEcKit.h"
#include "eckit/config/Resource.h"
#include "eckit/config/ResourceMgr.h"
#include "eckit/log/Bytes.h"
#include "eckit/log/Log.h"
#include "eckit/runtime/Tool.h"
#include "eckit/testing/Test.h"
#include "eckit/types/FloatCompare.h"
#include "eckit/types/Types.h"

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

namespace eckit::test {

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

CASE("test_default") {
    std::string s = Resource<std::string>("s", "some");

    EXPECT(s == "some");

    double d = Resource<double>("d", 777.7);

    EXPECT(is_approximately_equal(d, 777.7, 0.0001));  // accept 0.0001% tolerance
}

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

CASE("test_vector_long") {
    std::vector<long> def(3, 77);
    std::vector<long> v = Resource<std::vector<long> >("listlong;-listlong", def);

    EXPECT(v[0] == 88);
    EXPECT(v[1] == 99);
    EXPECT(v[2] == 11);
    EXPECT(v[3] == 22);
}

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

CASE("test_command_line") {
    int myint = Resource<int>("integer;-integer", 0);
    EXPECT(myint == 100);
}

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

CASE("test_environment_var") {
    char v[] = "TEST_ENV_INT=333";
    putenv(v);

    int intenv = Resource<int>("intEnv;$TEST_ENV_INT", 777);
    EXPECT(intenv == 333);

    char foo[] = "FOO=1Mb";
    putenv(foo);

    long l1 = Resource<long>("$FOO", 0);
    EXPECT(l1 == 1024 * 1024);

    long l2 = Resource<long>("$FOO;-foo", 0);
    EXPECT(l2 == 1024 * 1024);

    long l3 = Resource<long>("-foo;$FOO", 0);
    EXPECT(l3 == 1024 * 1024);

    long l4 = Resource<long>("$FOO;foo;-foo", 0);
    EXPECT(l4 == 1024 * 1024);
}

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

CASE("test_libresource") {
    long i = LibResource<long, LibEcKit>("foo-bar;$FOOBAR", 1024 * 1024);

    EXPECT(i == 1024 * 1024);


    char v[] = "TEST_FOOBAZ=fooBAZ";
    putenv(v);
    std::string foobaz = LibResource<std::string, LibEcKit>("foo-baz;$TEST_FOOBAZ", "foobazzzzzzz");

    EXPECT(foobaz == "fooBAZ");
}

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

}  // namespace eckit::test

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