OOPS
test/base/PostTimer.h
Go to the documentation of this file.
1 /*
2  * (C) Copyright 2020 UCAR
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  */
7 
8 #ifndef TEST_BASE_POSTTIMER_H_
9 #define TEST_BASE_POSTTIMER_H_
10 
11 #include <string>
12 #include <vector>
13 
14 #define ECKIT_TESTING_SELF_REGISTER_CASES 0
15 
16 #include "eckit/config/LocalConfiguration.h"
17 #include "eckit/testing/Test.h"
19 #include "oops/base/PostTimer.h"
20 #include "oops/runs/Test.h"
21 #include "oops/util/Logger.h"
22 
23 namespace test {
24 
25 // -----------------------------------------------------------------------------
27  const util::DateTime winbgn("2020-01-01T00:00:00Z");
28  const util::DateTime winend("2020-01-02T00:00:00Z");
29  const util::Duration step("PT1H");
30 
31  oops::PostTimer timer;
32  timer.initialize(winbgn, winend);
33  // any time between winbgn & winend should be fine
34  EXPECT(timer.itIsTime(winbgn));
35  EXPECT(timer.itIsTime(winbgn+step/2));
36  EXPECT(timer.itIsTime(winbgn+step));
37  EXPECT(timer.itIsTime(winend));
38  EXPECT(!timer.itIsTime(winbgn-step));
39  EXPECT(!timer.itIsTime(winend+step));
40 }
41 
42 // -----------------------------------------------------------------------------
44  const util::DateTime winbgn("2020-01-01T00:00:00Z");
45  const util::DateTime winend("2020-01-02T00:00:00Z");
46  const util::Duration step("PT1H");
47 
48  // create a timer, make it run in winbgn->winend window
49  oops::PostTimer timer1(winbgn, winend, step);
50  // init with wider window (initial window should be used)
51  timer1.initialize(winbgn-step, winend+step);
52  // any time between winbgn & winend with step should be fine
53  EXPECT(timer1.itIsTime(winbgn));
54  EXPECT(!timer1.itIsTime(winbgn+step/2));
55  EXPECT(timer1.itIsTime(winbgn+step));
56  EXPECT(timer1.itIsTime(winend));
57  EXPECT(!timer1.itIsTime(winbgn-step));
58  EXPECT(!timer1.itIsTime(winend+step));
59 
60  // create a timer, make it run in winbgn-step->winend+step window
61  oops::PostTimer timer2(winbgn-step, winend+step, step);
62  // init with smaller window (initial window should be used)
63  timer2.initialize(winbgn, winend);
64  // any time between winbgn & winend with step should be fine
65  EXPECT(timer2.itIsTime(winbgn));
66  EXPECT(!timer2.itIsTime(winbgn+step/2));
67  EXPECT(timer2.itIsTime(winbgn+step));
68  EXPECT(timer2.itIsTime(winend));
69  EXPECT(timer2.itIsTime(winbgn-step));
70  EXPECT(timer2.itIsTime(winend+step));
71 }
72 
73 // -----------------------------------------------------------------------------
74 void testConfCtor() {
75  const util::DateTime winbgn("2020-01-01T00:00:00Z");
76  const util::DateTime winend("2020-01-02T00:00:00Z");
77  const util::Duration step("PT1H");
78 
79  eckit::LocalConfiguration test1;
80  // create a timer with empty config (should be equiv to default ctor)
81  oops::PostTimer timer1(test1);
82  timer1.initialize(winbgn, winend);
83  // any time between winbgn & winend should be fine
84  EXPECT(timer1.itIsTime(winbgn));
85  EXPECT(timer1.itIsTime(winbgn+step/2));
86  EXPECT(timer1.itIsTime(winbgn+step));
87  EXPECT(timer1.itIsTime(winend));
88  EXPECT(!timer1.itIsTime(winbgn-step));
89  EXPECT(!timer1.itIsTime(winend+step));
90 
91  const std::string freq = (step/2).toString();
92  test1.set("frequency", freq);
93  // create a timer, set frequency = step/2 through config
94  oops::PostTimer timer2(test1);
95  // init with winbgn->winend window
96  timer2.initialize(winbgn, winend);
97  // any time between winbgn & winend with step/2 should be fine
98  EXPECT(timer2.itIsTime(winbgn));
99  EXPECT(timer2.itIsTime(winbgn+step/2));
100  EXPECT(!timer2.itIsTime(winbgn+step/4));
101  EXPECT(timer2.itIsTime(winbgn+step));
102  EXPECT(timer2.itIsTime(winend));
103  EXPECT(!timer2.itIsTime(winbgn-step));
104  EXPECT(!timer2.itIsTime(winend+step));
105 
106  eckit::LocalConfiguration test2;
107  const std::string first = step.toString();
108  test2.set("first", first);
109  // create a timer, set first = step through config
110  oops::PostTimer timer3(test2);
111  // init with winbgn->winend window
112  timer3.initialize(winbgn, winend);
113  // any time between winbgn+step & winend should be fine
114  EXPECT(!timer3.itIsTime(winbgn));
115  EXPECT(!timer3.itIsTime(winbgn+step/2));
116  EXPECT(timer3.itIsTime(winbgn+step));
117  EXPECT(timer3.itIsTime(winbgn+step*2));
118  EXPECT(timer3.itIsTime(winend));
119  EXPECT(!timer3.itIsTime(winbgn-step));
120  EXPECT(!timer3.itIsTime(winend+step));
121 
122  eckit::LocalConfiguration test3;
123  std::vector<std::string> steps{"2020-01-01T00:00:00Z", "2020-01-01T04:00:00Z",
124  "2020-01-01T08:00:00Z"};
125  test3.set("steps", steps);
126  // create a timer, set predefined steps
127  oops::PostTimer timer4(test3);
128  // init with winbgn->winend window
129  timer4.initialize(winbgn, winend);
130  // only steps specified above should work
131  EXPECT(timer4.itIsTime(winbgn));
132  EXPECT(!timer4.itIsTime(winbgn+step));
133  EXPECT(!timer4.itIsTime(winend));
134  EXPECT(!timer4.itIsTime(winbgn-step));
135  EXPECT(!timer4.itIsTime(winend+step));
136  for (auto step : steps) {
137  EXPECT(timer4.itIsTime(util::DateTime(step)));
138  }
139 }
140 
141 class PostTimer : public oops::Test {
142  private:
143  std::string testid() const override {return "test::PostTimer";}
144 
145  void register_tests() const override {
146  std::vector<eckit::testing::Test>& ts = eckit::testing::specification();
147 
148  ts.emplace_back(CASE("util/PostTimer/defaultCtor") {
149  testDefaultCtor();
150  });
151  ts.emplace_back(CASE("util/PostTimer/bgnEndCtor") {
152  testBgnEndCtor();
153  });
154  ts.emplace_back(CASE("util/PostTimer/confCtor") {
155  testConfCtor();
156  });
157  }
158 
159  void clear() const override {}
160 };
161 
162 } // namespace test
163 
164 #endif // TEST_BASE_POSTTIMER_H_
165 
Handles timing of post-processing and similar actions.
bool itIsTime(const util::DateTime &)
void initialize(const util::DateTime &, const util::DateTime &)
std::string testid() const override
void register_tests() const override
void clear() const override
void testDefaultCtor()
void testConfCtor()
void testBgnEndCtor()
CASE("test_linearmodelparameterswrapper_valid_name")