File: TestDayAttr.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 (171 lines) | stat: -rw-r--r-- 6,283 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
/*
 * 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 <iostream>
#include <string>

#include <boost/test/unit_test.hpp>

#include "ecflow/attribute/DayAttr.hpp"
#include "ecflow/core/Calendar.hpp"
#include "ecflow/core/PrintStyle.hpp"
#include "ecflow/core/Str.hpp"
#include "ecflow/node/formatter/DefsWriter.hpp"
#include "ecflow/test/scaffold/Naming.hpp"

using namespace ecf;

BOOST_AUTO_TEST_SUITE(U_Attributes)

BOOST_AUTO_TEST_SUITE(T_DayAttr)

BOOST_AUTO_TEST_CASE(test_day_attr) {
    ECF_NAME_THIS_TEST();

    // See TimeAttr.hpp for rules concerning isFree() and checkForReque()
    // test time attr isFree(), and checkForRequeue
    Calendar calendar;
    calendar.init(boost::posix_time::ptime(boost::gregorian::date(2013, 7, 9), boost::posix_time::minutes(0)),
                  Calendar::REAL); // tuesday

    // Represent a day within a week (range 0==Sun to 6==Sat)
    BOOST_CHECK_MESSAGE(calendar.day_of_week() == 2, " Expected tuesday(2) but found " << calendar.day_of_week());

    DayAttr day(DayAttr::WEDNESDAY);
    day.reset(calendar);

    // int day_changed = 0; // after midnight make sure we keep day_changed
    // day_changed = 0;  tuesday
    // day_changed = 1;  wednesday
    // day_changed = 3;  thursday
    for (int m = 1; m < 96; m++) {
        calendar.update(boost::posix_time::time_duration(boost::posix_time::hours(1)));
        // if (calendar.dayChanged())
        //     day_changed++;

        day.calendarChanged(calendar);

        if (day.date() < calendar.date()) {
            BOOST_CHECK_MESSAGE(!day.isFree(calendar),
                                day.toString() << " is free should fail at day " << calendar.day_of_week());
            BOOST_CHECK_MESSAGE(!day.checkForRequeue(calendar),
                                day.toString() << " checkForRequeue should return false for a single day "
                                               << calendar.day_of_week());
        }
        else if (day.date() == calendar.date()) {
            BOOST_CHECK_MESSAGE(day.isFree(calendar),
                                day.toString() << " is free should pass at day " << calendar.day_of_week());
            BOOST_CHECK_MESSAGE(!day.checkForRequeue(calendar),
                                day.toString() << " checkForRequeue should fail at " << calendar.day_of_week());
        }
        else {
            BOOST_CHECK_MESSAGE(day.date() > calendar.date(), "");
            BOOST_CHECK_MESSAGE(!day.isFree(calendar),
                                day.toString() << " is free should pass at day " << calendar.day_of_week());
            BOOST_CHECK_MESSAGE(day.checkForRequeue(calendar),
                                day.toString() << " checkForRequeue should fail at " << calendar.day_of_week());
        }
    }
}

BOOST_AUTO_TEST_CASE(test_day_attr_constructor) {
    ECF_NAME_THIS_TEST();

    {
        DayAttr day;
        BOOST_CHECK_MESSAGE(day.day() == DayAttr::SUNDAY, "");
        BOOST_CHECK_MESSAGE(day.state_change_no() == 0, "");
        BOOST_CHECK_MESSAGE(day.isSetFree() == false, "");
    }
    {
        DayAttr day(DayAttr::WEDNESDAY);
        BOOST_CHECK_MESSAGE(day.day() == DayAttr::WEDNESDAY, "");
        BOOST_CHECK_MESSAGE(day.state_change_no() == 0, "");
        BOOST_CHECK_MESSAGE(day.isSetFree() == false, "");
    }
    {
        DayAttr day("monday");
        BOOST_CHECK_MESSAGE(day.day() == DayAttr::MONDAY, "");
        BOOST_CHECK_MESSAGE(day.state_change_no() == 0, "");
        BOOST_CHECK_MESSAGE(day.isSetFree() == false, "");
    }
}

static DayAttr print_and_parse_attr(DayAttr& day) {

    std::string output;
    ecf::write_t(output, day, PrintStyle::MIGRATE);
    output.erase(output.begin() + output.size() - 1); // remove trailing newline

    std::vector<std::string> tokens;
    Str::split_orig(output, tokens);

    return DayAttr::create(tokens, true /*read state*/);
}

BOOST_AUTO_TEST_CASE(test_day_parsing) {
    ECF_NAME_THIS_TEST();

    {
        DayAttr day(DayAttr::WEDNESDAY);
        day.setFree();
        DayAttr parsed_day = print_and_parse_attr(day);

        BOOST_CHECK_MESSAGE(day == parsed_day,
                            "Parse failed expected " << day.dump() << " but found " << parsed_day.dump());
    }
    {
        DayAttr day(DayAttr::WEDNESDAY);
        day.setFree();
        DayAttr parsed_day = print_and_parse_attr(day);

        BOOST_CHECK_MESSAGE(day == parsed_day,
                            "Parse failed expected " << day.dump() << " but found " << parsed_day.dump());
    }
    {
        DayAttr day(DayAttr::WEDNESDAY);
        DayAttr parsed_day = print_and_parse_attr(day);

        BOOST_CHECK_MESSAGE(day == parsed_day,
                            "Parse failed expected " << day.dump() << " but found " << parsed_day.dump());
    }
    {
        Calendar calendar;
        calendar.init(boost::posix_time::ptime(boost::gregorian::date(2020, 6, 26), boost::posix_time::minutes(0)),
                      Calendar::REAL); // friday

        DayAttr day(calendar.date());

        DayAttr parsed_day = print_and_parse_attr(day);
        BOOST_CHECK_MESSAGE(day == parsed_day,
                            "Parse failed expected " << day.dump() << " but found " << parsed_day.dump());
    }
    {
        Calendar c;
        c.init(boost::posix_time::ptime(boost::gregorian::date(2020, 6, 26), boost::posix_time::minutes(0)),
               Calendar::REAL); // friday

        auto one_day  = boost::gregorian::date_duration(1);
        auto next_day = c.date(); // todays date

        for (int i = 0; i < 7; i++) {
            next_day += one_day;

            DayAttr day(next_day);
            DayAttr parsed_day = print_and_parse_attr(day);
            BOOST_CHECK_MESSAGE(day == parsed_day,
                                "Parse failed expected " << day.dump() << " but found " << parsed_day.dump());
        }
    }
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()