File: test_steprange_axis.cc

package info (click to toggle)
metkit 1.15.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,380 kB
  • sloc: cpp: 16,089; sh: 2,532; python: 1,757; ansic: 71; makefile: 18
file content (175 lines) | stat: -rw-r--r-- 4,993 bytes parent folder | download
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
/*
 * (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.
 */

/// @file   test_param_axis.cc
/// @author Baudouin Raoult
/// @date   Mai 2019

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#include "eckit/testing/Test.h"
#include "eckit/types/FloatCompare.h"

#include "metkit/mars/StepRange.h"
#include "metkit/mars/StepRangeNormalise.h"

namespace eckit {
template <>
struct VectorPrintSelector<metkit::mars::StepRange> {
    using selector = VectorPrintSimple;
};
}  // namespace eckit

namespace metkit::mars::test {

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

CASE("steprange") {

    {
        StepRange sr{0, 24};
        EXPECT_EQUAL(sr.from(), 0);
        EXPECT_EQUAL(sr.to(), 24);
    }
    {
        StepRange sr{"0-24"};
        EXPECT_EQUAL(sr.from(), 0);
        EXPECT_EQUAL(sr.to(), 24);
    }
    {
        StepRange sr{0, .5};
        EXPECT_EQUAL(sr.from(), 0);
        EXPECT(eckit::types::is_approximately_equal(sr.to(), 0.5));
    }
    {
        StepRange sr{"0-30m"};
        EXPECT_EQUAL(sr.from(), 0);
        EXPECT(eckit::types::is_approximately_equal(sr.to(), 0.5));
    }
    {
        StepRange sr{"0-24s"};
        EXPECT_EQUAL(sr.from(), 0);
        EXPECT(eckit::types::is_approximately_equal(sr.to(), 24. / 3600.));
    }
    {
        StepRange sr{"40m-260m"};
        EXPECT(eckit::types::is_approximately_equal(sr.from(), 2. / 3.));
        EXPECT(eckit::types::is_approximately_equal(sr.to(), 4 + 1. / 3.));
    }
    {
        StepRange sr{"20m"};
        EXPECT_EQUAL(sr.toString(), "20m");
        EXPECT_EQUAL(sr.toString(true), "20m");
    }
    {
        StepRange sr{"60m"};
        EXPECT_EQUAL(sr.toString(), "1");
        EXPECT_EQUAL(sr.toString(true, false), "1");
        EXPECT_EQUAL(sr.toString(true, true), "1h");
    }
    {
        StepRange sr{"1d"};
        EXPECT_EQUAL(sr.toString(), "24");
        EXPECT_EQUAL(sr.toString(true, false), "1d");
        EXPECT_EQUAL(sr.toString(true, true), "1d");
    }
    {
        StepRange sr{"25"};
        EXPECT_EQUAL(sr.toString(), "25");
        EXPECT_EQUAL(sr.toString(true, false), "1d1h");
        EXPECT_EQUAL(sr.toString(true, true), "1d1h");
    }
}


static void test_steprange_axis(const std::vector<std::string>& user, const std::vector<std::string>& axis,
                                const std::vector<std::string>& expect) {

    std::vector<StepRange> values(user.begin(), user.end());
    std::vector<StepRange> result(expect.begin(), expect.end());
    std::vector<StepRange> index(axis.begin(), axis.end());

    std::sort(index.begin(), index.end());


    std::cout << "User:" << values << std::endl;
    std::cout << "Axis:" << index << std::endl;

    StepRangeNormalise::normalise(values, index);

    std::cout << "Result:" << values << std::endl;

    EXPECT(values == result);
}


CASE("trivial") {

    std::vector<std::string> user   = {"1", "2", "3"};
    std::vector<std::string> axis   = {"1", "2", "3"};
    std::vector<std::string> expect = {"1", "2", "3"};

    test_steprange_axis(user, axis, expect);
}

CASE("subselection") {

    std::vector<std::string> user   = {"2", "3"};
    std::vector<std::string> axis   = {"1", "2", "3"};
    std::vector<std::string> expect = {"2", "3"};

    test_steprange_axis(user, axis, expect);
}

CASE("missing values") {
    std::vector<std::string> user   = {"1", "2", "3"};
    std::vector<std::string> axis   = {"1", "3"};
    std::vector<std::string> expect = {"1", "3"};

    test_steprange_axis(user, axis, expect);
}

CASE("ranges") {
    std::vector<std::string> user   = {"0-24", "24-48", "3-9"};
    std::vector<std::string> axis   = {"0-24", "6-30", "12-36", "18-42", "24-48"};
    std::vector<std::string> expect = {"0-24", "24-48"};

    test_steprange_axis(user, axis, expect);
}

CASE("default start-point") {
    std::vector<std::string> user   = {"1", "2", "24", "25"};
    std::vector<std::string> axis   = {"1", "0-1", "3", "0-3", "0-24", "25"};
    std::vector<std::string> expect = {"1", "0-1", "0-24", "25"};

    test_steprange_axis(user, axis, expect);
}

CASE("match range start") {
    // SDS: I'm not really sure why this is supported, but the original
    //      MARS code did it...
    std::vector<std::string> user   = {"2-24"};
    std::vector<std::string> axis   = {"1", "2", "3"};
    std::vector<std::string> expect = {"2"};

    test_steprange_axis(user, axis, expect);
}


}  // namespace metkit::mars::test

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

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