-
Notifications
You must be signed in to change notification settings - Fork 4
/
tpdates4.cpp
70 lines (61 loc) · 2.22 KB
/
tpdates4.cpp
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
#include "dtdatetime.hpp"
#include "tpdate.hpp"
#include <cassert>
#include <random>
/*
* Check the precision of a TwoPartDate when constructed from a
* datetime<T> instance, with T being seconds
*/
using namespace dso;
constexpr const long num_tests = 1'000'000;
using nsec = dso::seconds;
typedef nsec::underlying_type SecIntType;
constexpr const double PRECISION_NSEC = 2e-12;
int main() {
/* Generators for random numbers ... */
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> ydstr(1972, 2050); /* range for years */
std::uniform_int_distribution<> mdstr(1, 12); /* range for months */
std::uniform_int_distribution<> ddstr(1, 31); /* range for day of month */
std::uniform_int_distribution<SecIntType> nsdstr(
0, nsec::max_in_day); /* range for day of month */
int testnr = 0, ok;
datetime<nsec> d1, d2, d3;
while (testnr < num_tests) {
/* do we have a valid date ? */
try {
d1 = datetime<nsec>{year(ydstr(gen)), month(mdstr(gen)),
day_of_month(ddstr(gen)), nsec(nsdstr(gen))};
d2 = datetime<nsec>{year(ydstr(gen)), month(mdstr(gen)),
day_of_month(ddstr(gen)), nsec(nsdstr(gen))};
/* d3 on same day as d2 */
d3 = datetime<nsec>{d2.imjd(), nsec(nsdstr(gen))};
ok = 1;
} catch (std::exception &) {
ok = 0;
}
if (ok) {
/* construct a TwoPartDate from a datetime */
TwoPartDate tpd1(d1);
/* the difference between the two dates, in nsec should be below
* PRECISION_NSEC
*/
assert(tpd1.imjd() == d1.imjd().as_underlying_type());
assert(std::abs(tpd1.sec_of_day<nsec>() - d1.sec().as_underlying_type()) <
PRECISION_NSEC);
TwoPartDate tpd2(d2);
assert(tpd2.imjd() == d2.imjd().as_underlying_type());
assert(std::abs(tpd2.sec_of_day<nsec>() - d2.sec().as_underlying_type()) <
PRECISION_NSEC);
TwoPartDate tpd3(d3);
assert(tpd3.imjd() == d3.imjd().as_underlying_type());
assert(std::abs(tpd3.sec_of_day<nsec>() - d3.sec().as_underlying_type()) <
PRECISION_NSEC);
++testnr;
if (testnr % 10)
printf("%d/%ld\r", testnr, num_tests);
}
}
return 0;
}