8000 Template-ize library... this will be interesting! · amalbansode/numeric-range@e469540 · GitHub
[go: up one dir, main page]

Skip to content

Commit e469540

Browse files
committed
Template-ize library... this will be interesting!
1 parent d2183e9 commit e469540

File tree

2 files changed

+47
-45
lines changed

2 files changed

+47
-45
lines changed

src/numeric_range.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33

44
#include <stdexcept>
55

6+
template<typename T>
67
class NumericRange
78
{
89
public:
9-
double lb = 0;
10+
T lb;
1011
bool lb_inclusive = true;
11-
double ub = 0;
12+
T ub = 0;
1213
bool ub_inclusive = true;
1314

14-
NumericRange (const double _lb, const bool _lb_inclusive,
15-
const double _ub, const bool _ub_inclusive) :
15+
NumericRange (const T _lb, const bool _lb_inclusive,
16+
const T _ub, const bool _ub_inclusive) :
1617
lb(_lb), lb_inclusive(_lb_inclusive),
1718
ub(_ub), ub_inclusive(_ub_inclusive)
1819
{
@@ -26,17 +27,18 @@ class NumericRange
2627
}
2728
}
2829

29-
explicit NumericRange (double _scalar) :
30+
explicit NumericRange (T _scalar) :
3031
lb(_scalar), lb_inclusive(true),
3132
ub(_scalar), ub_inclusive(true)
3233
{}
3334
};
3435

3536
// Is LHS < RHS?
37+
template<typename T>
3638
class NumericRangeComparator
3739
{
3840
public:
39-
bool operator() (const NumericRange &lhs, const NumericRange &rhs) const
41+
bool operator() (const NumericRange<T> &lhs, const NumericRange<T> &rhs) const
4042
{
4143
const bool lhs_is_scalar = (lhs.lb == lhs.ub);
4244
const bool rhs_is_scalar = (rhs.lb == rhs.ub);

test/numeric_range_test.cpp

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,80 +8,80 @@ using namespace std;
88

99
TEST_CASE("NumericRange Constructor", "[numeric_range]" ) {
1010
// Basic Constructor should pass
11-
REQUIRE_NOTHROW(NumericRange(0, true, 1, false));
11+
REQUIRE_NOTHROW(NumericRange<int>(0, true, 1, false));
1212

1313
// LB must be <= UB
14-
REQUIRE_THROWS_AS(NumericRange(1, true, 0, false), std::runtime_error);
14+
REQUIRE_THROWS_AS(NumericRange<int>(1, true, 0, false), std::runtime_error);
1515

1616
// If LB == UB, bounds must be inclusive
17-
REQUIRE_THROWS_AS(NumericRange(0, true, 0, false), std::runtime_error);
17+
REQUIRE_THROWS_AS(NumericRange<int>(0, true, 0, false), std::runtime_error);
1818

1919
// Scalar Constructor should pass
20-
REQUIRE_NOTHROW(NumericRange(0));
20+
REQUIRE_NOTHROW(NumericRange<int>(0));
2121
}
2222

2323
/// Range-Range Comparison Tests
2424

2525
TEST_CASE("Simple Range comparisons", "[numeric_range]" ) {
26-
NumericRangeComparator comp;
26+
NumericRangeComparator<double> comp;
2727

28-
const NumericRange A = NumericRange(0, true, 1, true);
28+
const NumericRange A = NumericRange<double>(0, true, 1, true);
2929

3030
{
31-
NumericRange B = NumericRange(0, true, 1, true);
31+
NumericRange B = NumericRange<double>(0, true, 1, true);
3232
REQUIRE(comp(A, B) == false);
3333
REQUIRE(comp(B, A) == false);
3434
}
3535

3636
{
37-
NumericRange B = NumericRange(-0.5, true, -0.25, true);
37+
NumericRange B = NumericRange<double>(-0.5, true, -0.25, true);
3838
REQUIRE(comp(A, B) == false);
3939
REQUIRE(comp(B, A) == true);
4040
}
4141

4242
{
43-
NumericRange B = NumericRange(-0.5, true, 0, true);
43+
NumericRange B = NumericRange<double>(-0.5, true, 0, true);
4444
REQUIRE_THROWS_AS(comp(A, B), std::runtime_error);
4545
REQUIRE_THROWS_AS(comp(B, A), std::runtime_error);
4646
}
4747

4848
{
49-
NumericRange B = NumericRange(0, true, 0.5, true);
49+
NumericRange B = NumericRange<double>(0, true, 0.5, true);
5050
REQUIRE_THROWS_AS(comp(A, B), std::runtime_error);
5151
REQUIRE_THROWS_AS(c 9E88 omp(B, A), std::runtime_error);
5252
}
5353

5454
{
55-
NumericRange B = NumericRange(0.25, true, 0.75, true);
55+
NumericRange B = NumericRange<double>(0.25, true, 0.75, true);
5656
REQUIRE_THROWS_AS(comp(A, B), std::runtime_error);
5757
REQUIRE_THROWS_AS(comp(B, A), std::runtime_error);
5858
}
5959

6060
{
61-
NumericRange B = NumericRange(0.5, true, 1, true);
61+
NumericRange B = NumericRange<double>(0.5, true, 1, true);
6262
REQUIRE_THROWS_AS(comp(A, B), std::runtime_error);
6363
REQUIRE_THROWS_AS(comp(B, A), std::runtime_error);
6464
}
6565

6666
{
67-
NumericRange B = NumericRange(1, true, 1.25, true);
67+
NumericRange B = NumericRange<double>(1, true, 1.25, true);
6868
REQUIRE_THROWS_AS(comp(A, B), std::runtime_error);
6969
REQUIRE_THROWS_AS(comp(B, A), std::runtime_error);
7070
}
7171

7272
{
73-
NumericRange B = NumericRange(1.25, true, 1.5, true);
73+
NumericRange B = NumericRange<double>(1.25, true, 1.5, true);
7474
REQUIRE(comp(A, B) == true);
7575
REQUIRE(comp(B, A) == false);
7676
}
7777

7878
}
7979

8080
TEST_CASE("Range A UB equals Range B LB", "[numeric_range]" ) {
81-
NumericRangeComparator comp;
81+
NumericRangeComparator<int> comp;
8282

83-
NumericRange A = NumericRange(0, true, 1, true);
84-
NumericRange B = NumericRange(1, true, 2, true);
83+
NumericRange A = NumericRange<int>(0, true, 1, true);
84+
NumericRange B = NumericRange<int>(1, true, 2, true);
8585

8686
REQUIRE_THROWS_AS(comp(A, B), std::runtime_error);
8787
REQUIRE_THROWS_AS(comp(B, A), std::runtime_error);
@@ -100,10 +100,10 @@ TEST_CASE("Range A UB equals Range B LB", "[numeric_range]" ) {
100100
}
101101

102102
TEST_CASE("Range A LB equals Range B UB", "[numeric_range]" ) {
103-
NumericRangeComparator comp;
103+
NumericRangeComparator<int> comp;
104104

105-
NumericRange A = NumericRange(0, true, 1, true);
106-
NumericRange B = NumericRange(-1, true, 0, true);
105+
NumericRange A = NumericRange<int>(0, true, 1, true);
106+
NumericRange B = NumericRange<int>(-1, true, 0, true);
107107

108108
REQUIRE_THROWS_AS(comp(A, B), std::runtime_error);
109109
REQUIRE_THROWS_AS(comp(B, A), std::runtime_error);
@@ -124,64 +124,64 @@ TEST_CASE("Range A LB equals Range B UB", "[numeric_range]" ) {
124124
/// Scalar-Range and Scalar-Scalar Comparison Tests
125125

126126
TEST_CASE("Simple Scalar comparisons", "[numeric_range]" ) {
127-
NumericRangeComparator comp;
127+
NumericRangeComparator<double> comp;
128128

129-
const NumericRange A = NumericRange(0.5);
129+
const NumericRange A = NumericRange<double>(0.5);
130130

131131
{
132-
NumericRange B = NumericRange(-0.5, true, 0, true);
132+
NumericRange B = NumericRange<double>(-0.5, true, 0, true);
133133
REQUIRE(comp(A, B) == false);
134134
REQUIRE(comp(B, A) == true);
135135
}
136136

137137
{
138-
NumericRange B = NumericRange(0);
138+
NumericRange B = NumericRange<double>(0);
139139
REQUIRE(comp(A, B) == false);
140140
REQUIRE(comp(B, A) == true);
141141
}
142142

143143
{
144-
NumericRange B = NumericRange(0, true, 0.5, true);
144+
NumericRange B = NumericRange<double>(0, true, 0.5, true);
145145
REQUIRE(comp(A, B) == false);
146146
REQUIRE(comp(B, A) == false);
147147
}
148148

149149
{
150-
NumericRange B = NumericRange(0.5);
150+
NumericRange B = NumericRange<double>(0.5);
151151
REQUIRE(comp(A, B) == false);
152152
REQUIRE(comp(B, A) == false);
153153
}
154154

155155
{
156-
NumericRange B = NumericRange(0.5, true, 1, true);
156+
NumericRange B = NumericRange<double>(0.5, true, 1, true);
157157
REQUIRE(comp(A, B) == false);
158158
REQUIRE(comp(B, A) == false);
159159
}
160160

161161
{
162-
NumericRange B = NumericRange(0, true, 1, true);
162+
NumericRange B = NumericRange<double>(0, true, 1, true);
163163
REQUIRE(comp(A, B) == false);
164164
REQUIRE(comp(B, A) == false);
165165
}
166166

167167
{
168-
NumericRange B = NumericRange(1);
168+
NumericRange B = NumericRange<double>(1);
169169
REQUIRE(comp(A, B) == true);
170170
REQUIRE(comp(B, A) == false);
171171
}
172172

173173
{
174-
NumericRange B = NumericRange(1, true, 1.25, true);
174+
NumericRange B = NumericRange<double>(1, true, 1.25, true);
175175
REQUIRE(comp(A, B) == true);
176176
REQUIRE(comp(B, A) == false);
177177
}
178178
}
179179

180180
TEST_CASE("Scalar within Range", "[numeric_range]" ) {
181-
NumericRangeComparator comp;
181+
NumericRangeComparator<double> comp;
182182

183-
const NumericRange scalar = NumericRange(0.5);
184-
NumericRange range = NumericRange(0, true, 1, true);
183+
const NumericRange scalar = NumericRange<double>(0.5);
184+
NumericRange range = NumericRange<double>(0, true, 1, true);
185185

186186
REQUIRE(comp(scalar, range) == false);
187187
REQUIRE(comp(range, scalar) == false);
@@ -200,10 +200,10 @@ TEST_CASE("Scalar within Range", "[numeric_range]" ) {
200200
}
201201

202202
TEST_CASE("Scalar equals Range LB", "[numeric_range]" ) {
203-
NumericRangeComparator comp;
203+
NumericRangeComparator<int> comp;
204204

205-
const NumericRange scalar = NumericRange(0);
206-
NumericRange range = NumericRange(0, true, 1, true);
205+
const NumericRange scalar = NumericRange<int>(0);
206+
NumericRange range = NumericRange<int>(0, true, 1, true);
207207

208208
REQUIRE(comp(scalar, range) == false);
209209
REQUIRE(comp(range, scalar) == false);
@@ -222,10 +222,10 @@ TEST_CASE("Scalar equals Range LB", "[numeric_range]" ) {
222222
}
223223

224224
TEST_CASE("Scalar equals Range UB", "[numeric_range]" ) {
225-
NumericRangeComparator comp;
225+
NumericRangeComparator<int> comp;
226226

227-
const NumericRange scalar = NumericRange(1);
228-
NumericRange range = NumericRange(0, true, 1, true);
227+
const NumericRange scalar = NumericRange<int>(1);
228+
NumericRange range = NumericRange<int>(0, true, 1, true);
229229

230230
REQUIRE(comp(scalar, range) == false);
231231
REQUIRE(comp(range, scalar) == false);

0 commit comments

Comments
 (0)
0