File: TestChrono.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 (161 lines) | stat: -rw-r--r-- 5,380 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
/*
 * 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 <boost/test/unit_test.hpp>

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

using namespace ecf;

BOOST_AUTO_TEST_SUITE(U_Core)

BOOST_AUTO_TEST_SUITE(T_Chrono)

BOOST_AUTO_TEST_CASE(test_chrono_is_able_to_create_reference_instant) {
    ECF_NAME_THIS_TEST();

    Instant instant;
    BOOST_CHECK_EQUAL(Instant::format(instant), "19700101T000000");
}

BOOST_AUTO_TEST_CASE(test_chrono__is_able_to_create_instant_based_on_std_chrono_time_point) {
    ECF_NAME_THIS_TEST();

    Instant original{std::chrono::system_clock::now()};

    auto timestamp        = Instant::format(original);
    Instant reconstructed = Instant::parse(timestamp);
    auto timestamp2       = Instant::format(reconstructed);

    BOOST_CHECK_EQUAL(original, reconstructed);
}

BOOST_AUTO_TEST_CASE(test_chrono_is_able_to_parse_and_format_instant) {
    ECF_NAME_THIS_TEST();

    {
        std::string value = "19700101T000000";
        Instant instant   = Instant::parse(value);
        BOOST_CHECK_EQUAL(Instant::format(instant), value);
    }
    {
        std::string value = "20000101T235959";
        Instant instant   = Instant::parse(value);
        BOOST_CHECK_EQUAL(Instant::format(instant), value);
    }
}

BOOST_AUTO_TEST_CASE(test_chrono_is_able_to_compare_instants_for_equality) {
    ECF_NAME_THIS_TEST();

    Instant instant0 = Instant::parse("20000101T235959");

    Instant instant1 = instant0;
    Instant instant2 = Instant::parse("20000102T000000");
    Instant instant3 = Instant::parse("20000102T000001");

    BOOST_CHECK(instant0 == instant1);

    BOOST_CHECK(instant1 == instant1);
    BOOST_CHECK(instant1 != instant2);
    BOOST_CHECK(instant1 != instant3);

    BOOST_CHECK(instant2 != instant1);
    BOOST_CHECK(instant2 == instant2);
    BOOST_CHECK(instant2 != instant3);

    BOOST_CHECK(instant3 != instant1);
    BOOST_CHECK(instant3 != instant2);
    BOOST_CHECK(instant3 == instant3);
}

BOOST_AUTO_TEST_CASE(test_chrono_is_able_to_compare_instants_for_inequality) {
    ECF_NAME_THIS_TEST();

    Instant instant1 = Instant::parse("20000101T235959");
    Instant instant2 = Instant::parse("20000102T000000");
    Instant instant3 = Instant::parse("20000102T000001");

    BOOST_CHECK(instant1 < instant2);
    BOOST_CHECK(instant1 < instant3);

    BOOST_CHECK(instant2 > instant1);
    BOOST_CHECK(instant2 < instant3);

    BOOST_CHECK(instant3 > instant1);
    BOOST_CHECK(instant3 > instant2);
}

BOOST_AUTO_TEST_CASE(test_chrono_is_able_to_add_duration_to_instant) {
    ECF_NAME_THIS_TEST();

    {
        Instant instant = Instant::parse("20000101T235959");
        Instant next    = instant + Duration{std::chrono::seconds{1}};
        BOOST_CHECK_EQUAL(Instant::format(next), "20000102T000000");
    }
    {
        Instant instant = Instant::parse("20000101T235959");
        Instant next    = instant + Duration{-std::chrono::seconds{1}};
        BOOST_CHECK_EQUAL(Instant::format(next), "20000101T235958");
    }
}

BOOST_AUTO_TEST_CASE(test_chrono_is_able_to_subtract_duration_from_instant) {
    ECF_NAME_THIS_TEST();

    {
        Instant instant = Instant::parse("20000101T235959");
        Instant next    = instant - Duration{std::chrono::seconds{1}};
        BOOST_CHECK_EQUAL(Instant::format(next), "20000101T235958");
    }
    {
        Instant instant = Instant::parse("20000101T235959");
        Instant next    = instant - Duration{-std::chrono::seconds{1}};
        BOOST_CHECK_EQUAL(Instant::format(next), "20000102T000000");
    }
}

BOOST_AUTO_TEST_CASE(test_chrono_parsing_invalid_value_throws) {
    ECF_NAME_THIS_TEST();

    using expected = std::runtime_error;
    BOOST_CHECK_THROW(Instant::parse("20000101T235961"), expected);
    BOOST_CHECK_THROW(Instant::parse("20000101T236059"), expected);
    BOOST_CHECK_THROW(Instant::parse("20000101T240000"), expected);
    BOOST_CHECK_THROW(Instant::parse("20000101T555555"), expected);
    BOOST_CHECK_THROW(Instant::parse("20000132T000000"), expected);
    BOOST_CHECK_THROW(Instant::parse("20000230T000000"), expected);
    BOOST_CHECK_THROW(Instant::parse("20000431T000000"), expected);
    BOOST_CHECK_THROW(Instant::parse("20000631T000000"), expected);
}

BOOST_AUTO_TEST_CASE(test_chrono_experimental) {
    ECF_NAME_THIS_TEST();

    {
        ecf::Instant instant;
        BOOST_CHECK_EQUAL(ecf::Instant::format(instant), "19700101T000000");
        BOOST_CHECK_EQUAL(ecf::coerce_from_instant_into_seconds(instant), 0);
        BOOST_CHECK_EQUAL(ecf::coerce_from_instant_into<std::chrono::nanoseconds>(instant), 0);
    }

    {
        ecf::Instant instant = ecf::Instant::parse("20230601T000000");
        BOOST_CHECK_EQUAL(ecf::Instant::format(instant), "20230601T000000");
        BOOST_CHECK_EQUAL(ecf::coerce_from_instant_into_seconds(instant), 1685577600);
        BOOST_CHECK_EQUAL(ecf::coerce_from_instant_into<std::chrono::nanoseconds>(instant), 1685577600000000000);
    }
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()