-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelta_t.cpp
143 lines (129 loc) · 2.59 KB
/
delta_t.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
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
#include "delta_t.h"
#include <cassert>
#include <iostream>
#include <sstream>
delta_t::delta_t(const double dt)
: m_delta_t{dt}
{
}
void test_delta_t()
{
#ifndef NDEBUG
// get
{
const double dt{0.123};
const delta_t t{dt};
assert(t.get() == dt);
}
// operator==
{
const delta_t a(0.1);
const delta_t b(0.1);
const delta_t c(0.9876);
assert(a == b);
assert(!(a == c));
}
// operator<
{
const delta_t a(0.1);
const delta_t b(0.2);
assert(a < b);
}
// operator+
{
const double t1{0.1};
const double t2{0.2};
const delta_t d1{t1};
const delta_t d2{t2};
const delta_t d3{d1 + d2};
assert(d3.get() == t1 + t2);
}
// operator-
{
const double t1{0.5};
const double t2{0.3};
const delta_t d1{t1};
const delta_t d2{t2};
const delta_t d3{d1 - d2};
assert(d3.get() == t1 - t2);
}
// operator*
{
const double t1{0.1};
const double t2{0.2};
const delta_t d1{t1};
const delta_t d2{t2};
const delta_t d3{d1 * d2};
assert(d3.get() == t1 * t2);
}
// operator+=
{
const double t1{0.1};
const double t2{0.2};
delta_t d1{t1};
const delta_t d2{t2};
d1 += d2;
assert(d1.get() == t1 + t2);
}
// operator>
{
const delta_t low{0.0001};
const delta_t high{0.11};
assert(high > low);
}
// operator>=
{
const delta_t low{0.0001};
const delta_t high{0.11};
assert(high >= low);
}
// operator<<
{
std::stringstream s;
s << delta_t(0.123);
assert(!s.str().empty());
}
#endif // DEBUG
}
bool operator==(const delta_t& lhs, const delta_t& rhs) noexcept
{
return lhs.get() == rhs.get();
}
bool operator<(const delta_t& lhs, const delta_t& rhs) noexcept
{
return lhs.get() < rhs.get();
}
bool operator<=(const delta_t& lhs, const delta_t& rhs) noexcept
{
return lhs.get() <= rhs.get();
}
delta_t& operator+=(delta_t& lhs, const delta_t& rhs) noexcept
{
lhs = lhs + rhs;
return lhs;
}
delta_t operator+(const delta_t& lhs, const delta_t& rhs) noexcept
{
return delta_t(lhs.get() + rhs.get());
}
delta_t operator-(const delta_t& lhs, const delta_t& rhs) noexcept
{
return delta_t(lhs.get() - rhs.get());
}
delta_t operator*(const delta_t& lhs, const delta_t& rhs) noexcept
{
return delta_t(lhs.get() * rhs.get());
}
bool operator>(const delta_t& lhs, const delta_t& rhs) noexcept
{
return lhs.get() > rhs.get();
}
bool operator>=(const delta_t& lhs, const delta_t& rhs) noexcept
{
return lhs.get() >= rhs.get();
}
std::ostream& operator<<(std::ostream& os, const delta_t& dt) noexcept
{
os << dt.get();
return os;
}