File: test_integer_range.cc

package info (click to toggle)
metkit 1.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,932 kB
  • sloc: cpp: 14,098; sh: 2,532; python: 1,699; ansic: 71; makefile: 18
file content (206 lines) | stat: -rw-r--r-- 5,718 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
/*
 * (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_integer_day.cc
/// @author Metin Cakircali
/// @date   March 2025

#include <string>
#include <vector>

#include "eckit/testing/Test.h"
#include "eckit/value/Value.h"

#include "metkit/mars/MarsExpandContext.h"
#include "metkit/mars/MarsRequest.h"
#include "metkit/mars/TypeInteger.h"

namespace metkit::mars::test {

using ::eckit::ValueList;
using ::eckit::ValueMap;

const DummyContext ctx;

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

CASE("Test TypeInteger expansion range=[1,100]") {

    ValueMap settings;
    settings["range"] = ValueList{1, 100};
    TypeInteger type("day", settings);
    Type& tday = type;

    // in range

    for (int i = 1; i < 101; ++i) {
        auto num          = std::to_string(i);
        std::string value = num;
        auto ret          = tday.expand(ctx, value);
        EXPECT_EQUAL(ret, true);
        EXPECT_EQUAL(value, num);
    }

    // out of range

    {
        std::string value = "0";
        auto ret          = tday.expand(ctx, value);
        EXPECT_EQUAL(ret, false);
    }

    {
        std::string value = "101";
        auto ret          = tday.expand(ctx, value);
        EXPECT_EQUAL(ret, false);
    }
}

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

CASE("Test TypeInteger expansion range=[1,1]") {

    ValueMap settings;
    settings["range"] = ValueList{1, 1};
    TypeInteger type("day", settings);
    Type& tday = type;

    {
        std::string value = "1";
        auto ret          = tday.expand(ctx, value);
        EXPECT_EQUAL(ret, true);
    }

    {
        std::string value = "2";
        auto ret          = tday.expand(ctx, value);
        EXPECT_EQUAL(ret, false);
    }
}

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

CASE("Test TypeInteger day expansion range=[-1,1]") {

    ValueMap settings;
    settings["range"] = ValueList{-1, 1};
    TypeInteger type("day", settings);
    Type& tday = type;

    {
        std::string value = "-2";
        auto ret          = tday.expand(ctx, value);
        EXPECT_EQUAL(ret, false);
    }

    {
        std::string value = "-1";
        auto ret          = tday.expand(ctx, value);
        EXPECT_EQUAL(ret, true);
    }

    {
        std::string value = "0";
        auto ret          = tday.expand(ctx, value);
        EXPECT_EQUAL(ret, true);
    }

    {
        std::string value = "1";
        auto ret          = tday.expand(ctx, value);
        EXPECT_EQUAL(ret, true);
    }

    {
        std::string value = "2";
        auto ret          = tday.expand(ctx, value);
        EXPECT_EQUAL(ret, false);
    }
}

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

CASE("Test disseminate day expansion default by/1") {

    std::vector<std::string> expected;
    for (int i = 1; i < 32; ++i) {
        expected.push_back(std::to_string(i));
    }

    const auto* text = R"(disseminate,
  class               = od,
  expver              = 1,
  levtype             = sfc,
  time                = 0,
  stream              = eefo,
  type                = fcmean,
  param               = sd/mn2t6/mx2t6/mtsfr/tcc/stl1/msdr/tprate/msl/rsn/2d/2t/10u/10v,
  step                = 0-168/168-336/336-504/504-672,
  use                 = monday,
  day                 = 1/to/31,
  number              = 0/to/10,
  area                = 90/-180/-90/179.5,
  grid                = .5/.5,
  packing             = simple
)";

    auto request = MarsRequest::parse(text);
    auto days    = request.values("day");
    EXPECT_EQUAL(days, expected);
}

CASE("Test disseminate day expansion") {

    const auto expected = std::vector<std::string>{"1",  "3",  "5",  "7",  "9",  "11", "13", "15",
                                                   "17", "19", "21", "23", "25", "27", "29", "31"};

    const auto* text = R"(disseminate,
  class               = od,
  expver              = 1,
  levtype             = sfc,
  time                = 0,
  stream              = eefo,
  type                = fcmean,
  param               = sd/mn2t6/mx2t6/mtsfr/tcc/stl1/msdr/tprate/msl/rsn/2d/2t/10u/10v,
  step                = 0-168/168-336/336-504/504-672,
  use                 = monday,
  day                 = 1/to/31/by/2,
  number              = 0/to/10,
  area                = 90/-180/-90/179.5,
  grid                = .5/.5,
  packing             = simple
)";

    auto request = MarsRequest::parse(text);
    auto days    = request.values("day");
    EXPECT_EQUAL(days, expected);
}

CASE("Test disseminate day expansion fails outside range") {

    const auto* text = R"(disseminate,
  class               = od,
  expver              = 1,
  levtype             = sfc,
  time                = 0,
  day                 = 1/to/48,
  packing             = simple
)";

    EXPECT_THROWS_AS(MarsRequest::parse(text), eckit::UserError);
}

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

}  // namespace metkit::mars::test

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