File: TestRealCalendar.cpp

package info (click to toggle)
ecflow 5.15.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,868 kB
  • sloc: cpp: 269,341; python: 22,756; sh: 3,609; perl: 770; xml: 333; f90: 204; ansic: 141; makefile: 70
file content (349 lines) | stat: -rw-r--r-- 15,254 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
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
/*
 * Copyright 2009- 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 <string>

#include <boost/test/unit_test.hpp>

#include "ecflow/core/Calendar.hpp"
#include "ecflow/core/Chrono.hpp"
#include "ecflow/core/TimeSeries.hpp"
#include "ecflow/test/scaffold/Naming.hpp"

using namespace ecf;

BOOST_AUTO_TEST_SUITE(U_Core)

BOOST_AUTO_TEST_SUITE(T_RealCalendar)

BOOST_AUTO_TEST_CASE(test_REAL_calendar) {
    ECF_NAME_THIS_TEST();

    // init the calendar to 2009, Feb, 10th,
    auto theDate = boost::gregorian::date(2009, 2, 10);
    auto time    = boost::posix_time::ptime(theDate, boost::posix_time::hours(22) + boost::posix_time::minutes(10));

    Calendar calendar;
    calendar.init(time, Calendar::REAL);

    // record the time for the test
    auto theSuiteTime = calendar.suiteTime();
    auto theDuration  = calendar.duration();

    // Take time now and add 2 minutes, use this to update calendar by 2 minutes
    auto time_now = Calendar::second_clock_time();
    time_now += boost::posix_time::minutes(2);
    theSuiteTime += boost::posix_time::minutes(2);
    theDuration += boost::posix_time::minutes(2);

    calendar.update(time_now);

    BOOST_CHECK_MESSAGE(calendar.suiteTime() == theSuiteTime,
                        " Expected " << to_simple_string(theSuiteTime) << " but found "
                                     << to_simple_string(calendar.suiteTime()));
    BOOST_CHECK_MESSAGE(calendar.duration() == theDuration,
                        " Expected " << to_simple_string(theDuration) << " but found "
                                     << to_simple_string(calendar.duration()));

    time_now += boost::posix_time::hours(24);
    theSuiteTime += boost::posix_time::hours(24);
    theDuration += boost::posix_time::hours(24);

    calendar.update(time_now);

    BOOST_CHECK_MESSAGE(calendar.suiteTime() == theSuiteTime,
                        " Expected " << to_simple_string(theSuiteTime) << " but found "
                                     << to_simple_string(calendar.suiteTime()));
    BOOST_CHECK_MESSAGE(calendar.duration() == theDuration,
                        " Expected " << to_simple_string(theDuration) << " but found "
                                     << to_simple_string(calendar.duration()));
}

BOOST_AUTO_TEST_CASE(test_REAL_calendar_time_series_relative_complex) {
    ECF_NAME_THIS_TEST();

    // init the calendar to 2009, Feb, 10th,  0 minutes past midnight
    Calendar calendar;
    calendar.init(boost::posix_time::ptime(boost::gregorian::date(2010, 2, 10), boost::posix_time::minutes(0)),
                  Calendar::HYBRID);

    // Create a test when we can match a time series
    // Create the time series: start  10:00
    //                         finish 20:00
    //                         incr   00:15
    TimeSeries timeSeries(TimeSlot(10, 0), TimeSlot(20, 0), TimeSlot(0, 15), true /*relative*/);

    auto time_now = Calendar::second_clock_time();

    for (int hour = 0; hour < 24; hour++) {
        for (int minute = 0; minute < 60; minute++) {

            // Update calendar every hour, then see we can match time series, *RELATIVE* to suite start
            time_now += boost::posix_time::minutes(1);

            calendar.update(time_now);

            timeSeries.calendarChanged(calendar);

            tm suiteTm = to_tm(calendar.suiteTime());

            bool matches = timeSeries.isFree(calendar);

            bool intersects =
                (suiteTm.tm_hour >= timeSeries.start().hour() && suiteTm.tm_hour <= timeSeries.finish().hour() &&
                 (suiteTm.tm_min == 0 || suiteTm.tm_min % timeSeries.incr().minute() == 0));
            // Ovoid overshooting past end of series
            bool boundaryOk = true;
            if (suiteTm.tm_hour == timeSeries.finish().hour()) {
                boundaryOk = (suiteTm.tm_min <= timeSeries.finish().minute());
            }

            if (intersects && boundaryOk) {
                BOOST_CHECK_MESSAGE(matches,
                                    "Calendar should match relative time series at "
                                        << suiteTm.tm_hour << ":" << suiteTm.tm_min
                                        << " suite time = " << to_simple_string(calendar.suiteTime()));
                if (!matches) {
                    ECF_TEST_DBG(<< "suiteTm.tm_hour =" << suiteTm.tm_hour << " suiteTm.tm_min = " << suiteTm.tm_min
                                 << " timeSeries.start().hour() " << timeSeries.start().hour()
                                 << " timeSeries.start().minute() " << timeSeries.start().minute()
                                 << " timeSeries.finish().hour() " << timeSeries.finish().hour()
                                 << " timeSeries.finish().minute() " << timeSeries.finish().minute()
                                 << " suiteTm.tm_min % 15 = " << suiteTm.tm_min % 15);
                }
            }
            else {
                BOOST_CHECK_MESSAGE(!matches,
                                    "Calendar should NOT match relative time series at "
                                        << suiteTm.tm_hour << ":" << suiteTm.tm_min
                                        << " suite time = " << to_simple_string(calendar.suiteTime()));

                if (matches) {
                    ECF_TEST_DBG(<< "suiteTm.tm_hour =" << suiteTm.tm_hour << " suiteTm.tm_min = " << suiteTm.tm_min
                                 << " timeSeries.start().hour() " << timeSeries.start().hour()
                                 << " timeSeries.start().minute() " << timeSeries.start().minute()
                                 << " timeSeries.finish().hour() " << timeSeries.finish().hour()
                                 << " timeSeries.finish().minute() " << timeSeries.finish().minute()
                                 << " suiteTm.tm_min % 15 = " << suiteTm.tm_min % 15);
                }
            }
        }
    }
}

BOOST_AUTO_TEST_CASE(test_REAL_calendar_time_series) {
    ECF_NAME_THIS_TEST();

    // init the calendar to 2009, Feb, 10th,  0 minutes past midnight
    Calendar calendar;
    calendar.init(boost::posix_time::ptime(boost::gregorian::date(2010, 2, 10), boost::posix_time::minutes(0)),
                  Calendar::REAL);

    // Create a test when we can match a time series
    // Create the time series: start  10:00
    //                         finish 20:00
    //                         incr    1:00
    TimeSeries timeSeries(TimeSlot(10, 0), TimeSlot(20, 0), TimeSlot(1, 0));

    auto time_now = Calendar::second_clock_time();

    for (int hour = 1; hour < 24; hour++) {
        // Update calendar every hour, then see we can match time series, in REAL
        // Update will set the local time from the computers system clock, however
        // for testing this will need to be overriden below.

        time_now += boost::posix_time::hours(1);

        calendar.update(time_now);

        if (hour >= timeSeries.start().hour() && hour <= timeSeries.finish().hour()) {
            BOOST_CHECK_MESSAGE(timeSeries.isFree(calendar), "Calendar should match time series at hour " << hour);
        }
        else {
            BOOST_CHECK_MESSAGE(!timeSeries.isFree(calendar), "Calendar should NOT match time series at hour " << hour);
        }
    }
}

BOOST_AUTO_TEST_CASE(test_REAL_calendar_time_series_complex) {
    ECF_NAME_THIS_TEST();

    // init the calendar to 2009, Feb, 10th,  0 minutes past midnight
    Calendar calendar;
    calendar.init(boost::posix_time::ptime(boost::gregorian::date(2010, 2, 10), boost::posix_time::minutes(0)),
                  Calendar::REAL);

    // Create a test when we can match a time series
    // Create the time series: start  10:00
    //                         finish 20:00
    //                         incr   00:15
    TimeSeries timeSeries(TimeSlot(10, 0), TimeSlot(20, 0), TimeSlot(0, 15));

    auto time_now = Calendar::second_clock_time();

    for (int hour = 0; hour < 24; hour++) {
        for (int minute = 0; minute < 60; minute++) {

            // Update calendar every minute, then see we can match time series, *RELATIVE* to suite start
            time_now += boost::posix_time::minutes(1);
            calendar.update(time_now);

            tm suiteTm = to_tm(calendar.suiteTime());

            bool matches = timeSeries.isFree(calendar);

            bool intersects =
                (suiteTm.tm_hour >= timeSeries.start().hour() && suiteTm.tm_hour <= timeSeries.finish().hour() &&
                 (suiteTm.tm_min == 0 || suiteTm.tm_min % timeSeries.incr().minute() == 0));
            // Ovoid overshooting past end of series
            bool boundaryOk = true;
            if (suiteTm.tm_hour == timeSeries.finish().hour()) {
                boundaryOk = (suiteTm.tm_min <= timeSeries.finish().minute());
            }

            if (intersects && boundaryOk) {
                BOOST_CHECK_MESSAGE(matches,
                                    "Calendar should match relative time series at "
                                        << suiteTm.tm_hour << ":" << suiteTm.tm_min
                                        << " suite time = " << to_simple_string(calendar.suiteTime()));
                if (!matches) {
                    ECF_TEST_DBG(<< "suiteTm.tm_hour =" << suiteTm.tm_hour << " suiteTm.tm_min = " << suiteTm.tm_min
                                 << " timeSeries.start().hour() " << timeSeries.start().hour()
                                 << " timeSeries.start().minute() " << timeSeries.start().minute()
                                 << " timeSeries.finish().hour() " << timeSeries.finish().hour()
                                 << " timeSeries.finish().minute() " << timeSeries.finish().minute()
                                 << " suiteTm.tm_min % 15 = " << suiteTm.tm_min % 15);
                }
            }
            else {
                BOOST_CHECK_MESSAGE(!matches,
                                    "Calendar should NOT match relative time series at "
                                        << suiteTm.tm_hour << ":" << suiteTm.tm_min
                                        << " suite time = " << to_simple_string(calendar.suiteTime()));

                if (matches) {
                    ECF_TEST_DBG(<< "suiteTm.tm_hour =" << suiteTm.tm_hour << " suiteTm.tm_min = " << suiteTm.tm_min
                                 << " timeSeries.start().hour() " << timeSeries.start().hour()
                                 << " timeSeries.start().minute() " << timeSeries.start().minute()
                                 << " timeSeries.finish().hour() " << timeSeries.finish().hour()
                                 << " timeSeries.finish().minute() " << timeSeries.finish().minute()
                                 << " suiteTm.tm_min % 15 = " << suiteTm.tm_min % 15);
                }
            }
        }
    }
}

BOOST_AUTO_TEST_CASE(test_REAL_calendar_hybrid_date) {
    ECF_NAME_THIS_TEST();

    // The hybrid calendar should not change the suite date.
    // Test by updateing calendar by more than 24 hours

    // init the calendar to 2009, Feb, 10th,  0 minutes past midnight
    Calendar calendar;
    calendar.init(boost::posix_time::ptime(boost::gregorian::date(2010, 2, 10), boost::posix_time::minutes(0)),
                  Calendar::HYBRID);

    std::string expectedDate = "2010-Feb-10";
    auto time_now            = Calendar::second_clock_time();

    for (int hour = 1; hour < 60; hour++) {
        // Update calendar every hour, for 60 hours
        // the date should be the same, i.e 2009, Feb, 10th

        auto timeBeforeUpdate = calendar.suiteTime();

        time_now += boost::posix_time::hours(1);

        calendar.update(time_now);

        auto timeAfterUpdate = calendar.suiteTime();

        if (hour != 24 && hour != 48) {
            auto diff = boost::posix_time::time_period(timeBeforeUpdate, timeAfterUpdate);
            auto gap  = diff.length();
            BOOST_CHECK_MESSAGE(gap.hours() == 1,
                                "Expected one hour difference but found " << gap.hours() << " at hour " << hour);
        }

        std::string actualDate = to_simple_string(calendar.suiteTime().date());
        BOOST_CHECK_MESSAGE(actualDate == expectedDate,
                            "Expected '" << expectedDate << "' but found " << actualDate << " at hour " << hour);
    }
}

BOOST_AUTO_TEST_CASE(test_REAL_day_changed) {
    ECF_NAME_THIS_TEST();

    // init the calendar to 2009, Feb, 10th,  0 minutes past midnight
    Calendar calendar;
    calendar.init(boost::posix_time::ptime(boost::gregorian::date(2010, 2, 10), boost::posix_time::minutes(0)),
                  Calendar::REAL);
    BOOST_CHECK_MESSAGE(!calendar.hybrid(), "calendar type should be real");

    auto time_now = Calendar::second_clock_time();

    for (int hour = 1; hour < 73; hour++) {
        // Update calendar every hour, for 72 hours

        time_now += boost::posix_time::hours(1);

        calendar.update(time_now);

        if (hour == 24 || hour == 48 || hour == 72) {
            BOOST_CHECK_MESSAGE(calendar.dayChanged(),
                                "Expected day change at hour " << hour << " calendar " << calendar.toString());
        }
        else {
            BOOST_CHECK_MESSAGE(!calendar.dayChanged(),
                                "Un-Expected day change at hour " << hour << " calendar " << calendar.toString());
        }
    }
}

BOOST_AUTO_TEST_CASE(test_REAL_day_changed_for_hybrid) {
    ECF_NAME_THIS_TEST();

    // init the calendar to 2009, Feb, 10th,  0 minutes past midnight
    Calendar calendar;
    calendar.init(boost::posix_time::ptime(boost::gregorian::date(2010, 2, 10), boost::posix_time::minutes(0)),
                  Calendar::HYBRID);
    BOOST_CHECK_MESSAGE(calendar.hybrid(), "calendar type should be real");

    // HYBRID calendars allow for day change but not date.
    std::string expected_date = to_simple_string(calendar.date());

    auto time_now = Calendar::second_clock_time();

    for (int hour = 1; hour < 73; hour++) {
        // Update calendar every hour, for 72 hours

        time_now += boost::posix_time::hours(1);
        calendar.update(time_now);

        BOOST_CHECK_MESSAGE(expected_date == to_simple_string(calendar.date()),
                            "Unexpected date change for hybrid calendar at hour " << hour);

        // Day should change even for hybrid calendar,
        if (hour == 24 || hour == 48 || hour == 72) {
            BOOST_CHECK_MESSAGE(calendar.dayChanged(),
                                "Expected day change at hour " << hour << " calendar " << calendar.toString());
        }
        else {
            BOOST_CHECK_MESSAGE(!calendar.dayChanged(),
                                "Un-Expected day change at hour " << hour << " calendar " << calendar.toString());
        }
    }
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()