Coverage Report

Created: 2021-08-28 18:14

D:\git\skunkworks\herald-for-cpp\herald-tests\datetime-tests.cpp
Line
Count
Source
1
//  Copyright 2020-2021 Herald Project Contributors
2
//  SPDX-License-Identifier: Apache-2.0
3
//
4
5
#include "catch.hpp"
6
7
#include "herald/herald.h"
8
9
10
1
TEST_CASE("datatypes-date-basics", "[datatypes][date][basics]") {
11
1
  SECTION("datatypes-date-basics") {
12
1
    herald::datatype::Date d(1608483600); // long ctor
13
1
14
1
    REQUIRE(d.secondsSinceUnixEpoch() == 1608483600);
15
1
    // Testing without date functions
16
1
    REQUIRE(d.iso8601DateTime() == std::string("1608483600"));
17
1
    REQUIRE(((std::string)d) == std::string("1608483600"));
18
1
    // REQUIRE(d.iso8601DateTime() == std::string("2020-12-20T17:00:00Z"));
19
1
    // REQUIRE(((std::string)d) == std::string("2020-12-20T17:00:00Z"));
20
1
21
1
    herald::datatype::Date d2(d); // copy ctor
22
1
    REQUIRE(d2.secondsSinceUnixEpoch() == 1608483600);
23
1
    REQUIRE(d2.iso8601DateTime() == std::string("1608483600"));
24
1
    REQUIRE(((std::string)d2) == std::string("1608483600"));
25
1
    // REQUIRE(d2.iso8601DateTime() == std::string("2020-12-20T17:00:00Z"));
26
1
    // REQUIRE(((std::string)d2) == std::string("2020-12-20T17:00:00Z"));
27
1
28
1
    // TODO Default constructor producing 'now'
29
1
  }
30
1
}
31
32
33
34
1
TEST_CASE("datatypes-timeinterval-basics", "[datatypes][timeinterval][ctor][basics]") {
35
1
  SECTION("datatypes-timeinterval-basics") {
36
1
    herald::datatype::TimeInterval ti{1200};
37
1
38
1
    REQUIRE(ti.millis() == 1'200'000);
39
1
40
1
    auto t2 = herald::datatype::TimeInterval::never();
41
1
    REQUIRE(t2.millis() == LONG_MAX);
42
1
    REQUIRE(((std::string)t2) == std::string("never"));
43
1
44
1
    auto t3 = herald::datatype::TimeInterval::minutes(20);
45
1
    REQUIRE(t3.millis() == 20 * 60 * 1000);
46
1
47
1
    auto t4 = herald::datatype::TimeInterval::seconds(20);
48
1
    REQUIRE(t4.millis() == 20 * 1000);
49
1
50
1
    auto t5 = herald::datatype::TimeInterval::zero();
51
1
    REQUIRE(t5.millis() == 0);
52
1
53
1
    herald::datatype::Date d1{1000};
54
1
    herald::datatype::Date d2{1200};
55
1
    herald::datatype::TimeInterval t6(d1,d2);
56
1
57
1
    REQUIRE(t6.millis() == 200 * 1000);
58
1
    REQUIRE(((std::string)t6) == std::string("200"));
59
1
60
1
    REQUIRE(t5 < ti);
61
1
    REQUIRE(t5 < t2);
62
1
    REQUIRE(t5 < t3);
63
1
    REQUIRE(!(t5 > t3));
64
1
    REQUIRE(t5 < t4);
65
1
  }
66
1
}
67
68
69
1
TEST_CASE("datatypes-timeinterval-date", "[datatypes][timeinterval][ctor][date]") {
70
1
  SECTION("datatypes-timeinterval-date") {
71
1
    herald::datatype::Date earlier{1200};
72
1
    herald::datatype::Date now{1500};
73
1
74
1
    herald::datatype::TimeInterval difference{earlier,now};
75
1
    REQUIRE(difference.seconds() == 300);
76
1
77
1
    herald::datatype::TimeInterval reverseDifference{now,earlier};
78
1
    REQUIRE(reverseDifference.seconds() == -300);
79
1
80
1
    herald::datatype::Date advanced = earlier + difference;
81
1
    REQUIRE(advanced == now);
82
1
  }
83
1
}
84
85
86
1
TEST_CASE("datatypes-timeinterval-daterelative", "[datatypes][timeinterval][ctor][daterelative]") {
87
1
  SECTION("datatypes-timeinterval-daterelative") {
88
1
    herald::datatype::Date now;
89
1
    herald::datatype::TimeInterval threeHundred(300);
90
1
    herald::datatype::Date earlier = now - threeHundred;
91
1
92
1
    herald::datatype::TimeInterval difference{earlier,now};
93
1
    REQUIRE(difference.seconds() == 300);
94
1
95
1
    herald::datatype::TimeInterval reverseDifference{now,earlier};
96
1
    REQUIRE(reverseDifference.seconds() == -300);
97
1
98
1
    herald::datatype::Date advanced = earlier + difference;
99
1
    REQUIRE(advanced == now);
100
1
  }
101
1
}