File: test_hour.cc

package info (click to toggle)
eckit 2.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,052 kB
  • sloc: cpp: 111,136; ansic: 2,826; yacc: 590; lex: 361; python: 302; sh: 162; makefile: 65
file content (96 lines) | stat: -rw-r--r-- 3,033 bytes parent folder | download | duplicates (3)
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
/*
 * (C) Copyright 2021- 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 <iomanip>
#include "eckit/testing/Test.h"
#include "eckit/types/FloatCompare.h"
#include "eckit/types/Hour.h"

using namespace std;
using namespace eckit;
using namespace eckit::testing;

using eckit::types::is_approximately_equal;

namespace eckit::test {

//----------------------------------------------------------------------------------------------------------------------

CASE("Basic test") {
    EXPECT(Hour(12) == Hour("12"));
    EXPECT(Hour(12.5) == Hour("12:30"));
    EXPECT(Hour(1.0 / 3.0) == Hour("0:20"));
    EXPECT(Hour(2.0 / 3.0) == Hour("0:40"));
    EXPECT(Hour(1.0 / 60.0) == Hour("0:01"));
    EXPECT(Hour(1.0 / 3600.0) == Hour("0:00:01"));
}

CASE("Test minutes") {
    for (int hour = 0; hour < 240; ++hour) {
        for (int minute = 0; minute < 60; ++minute) {
            std::ostringstream oss;

            if (minute == 0) {
                oss << hour;
            }
            else {
                oss << hour << ':' << std::setw(2) << std::setfill('0') << minute;
            }

            std::string s = oss.str();
            // std::cout << s << std::endl;
            // std::cout << Hour(s) << std::endl;
            EXPECT(Hour(s).asString() == s);

            double d = double(hour) + double(minute) / 60.0;
            EXPECT(Hour(d).asString() == s);
        }
    }
}

CASE("Test seconds") {
    for (int hour = 0; hour < 24; ++hour) {
        for (int minute = 0; minute < 60; ++minute) {
            for (int second = 0; second < 60; ++second) {
                std::ostringstream oss;

                if (second == 0) {
                    if (minute == 0) {
                        oss << hour;
                    }
                    else {
                        oss << hour << ':' << std::setw(2) << std::setfill('0') << minute;
                    }
                }
                else {
                    oss << hour << ':' << std::setw(2) << std::setfill('0') << minute << ':' << std::setw(2)
                        << std::setfill('0') << second;
                }

                std::string s = oss.str();
                // std::cout << s << std::endl;
                // std::cout << Hour(s) << std::endl;
                EXPECT(Hour(s).asString() == s);

                double d = double(hour) + double(minute) / 60.0 + double(second) / 3600.0;
                EXPECT(Hour(d).asString() == s);
            }
        }
    }
}

//----------------------------------------------------------------------------------------------------------------------

}  // namespace eckit::test

int main(int argc, char* argv[]) {
    return run_tests(argc, argv);
}