Skip to content

Commit 1233bd8

Browse files
committed
Fix float equal warning and enable more tests
1 parent 7584c90 commit 1233bd8

File tree

1 file changed

+100
-34
lines changed

1 file changed

+100
-34
lines changed

test/test_complex.cpp

+100-34
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
#include <limits>
99
#include <complex>
1010
#include <iostream>
11+
#include <type_traits>
1112
#include <cmath>
1213

1314
using namespace boost::decimal;
1415

1516
template <typename T>
16-
bool test_equal(T lhs, T rhs, int tol = 10) noexcept
17+
bool test_equal(T lhs, T rhs, int tol = 15) noexcept
1718
{
1819
using std::fabs;
1920
const bool res = fabs(lhs - rhs) < static_cast<T>(tol) * std::numeric_limits<T>::epsilon();
@@ -51,7 +52,6 @@ void test_construction()
5152
BOOST_TEST(test_equal(v2.imag(), T{2}));
5253
}
5354

54-
/*
5555
template <typename T>
5656
void test_unary_operators()
5757
{
@@ -61,12 +61,12 @@ void test_unary_operators()
6161

6262
const complex_scalar val {T{2}, T{-2}};
6363
complex_scalar pos_val = +val;
64-
BOOST_TEST_EQ(val.real(), pos_val.real());
65-
BOOST_TEST_EQ(val.imag(), pos_val.imag());
64+
BOOST_TEST(test_equal(val.real(), pos_val.real()));
65+
BOOST_TEST(test_equal(val.imag(), pos_val.imag()));
6666

6767
complex_scalar neg_val = -val;
68-
BOOST_TEST_EQ(neg_val.real(), T{-2});
69-
BOOST_TEST_EQ(neg_val.imag(), T{2});
68+
BOOST_TEST(test_equal(neg_val.real(), T{-2}));
69+
BOOST_TEST(test_equal(neg_val.imag(), T{2}));
7070
}
7171

7272
template <typename T>
@@ -80,8 +80,8 @@ void test_addition()
8080
complex_scalar rhs_1 {T{2}, T{2}};
8181
complex_scalar res_1 = lhs_1 + rhs_1;
8282

83-
BOOST_TEST_EQ(res_1.real(), T{3});
84-
BOOST_TEST_EQ(res_1.imag(), T{3});
83+
BOOST_TEST(test_equal(res_1.real(), T{3}));
84+
BOOST_TEST(test_equal(res_1.imag(), T{3}));
8585
}
8686

8787
template <typename T>
@@ -95,8 +95,8 @@ void test_subtraction()
9595
complex_scalar rhs_1 {T{2}, T{2}};
9696
complex_scalar res_1 = lhs_1 - rhs_1;
9797

98-
BOOST_TEST_EQ(res_1.real(), T{-1});
99-
BOOST_TEST_EQ(res_1.imag(), T{-1});
98+
BOOST_TEST(test_equal(res_1.real(), T{-1}));
99+
BOOST_TEST(test_equal(res_1.imag(), T{-1}));
100100
}
101101

102102
template <typename T>
@@ -110,8 +110,8 @@ void test_multiplication()
110110
complex_scalar rhs_1 {T{2}, T{2}};
111111
complex_scalar res_1 = lhs_1 * rhs_1;
112112

113-
BOOST_TEST_EQ(res_1.real(), T{-12});
114-
BOOST_TEST_EQ(res_1.imag(), T{0});
113+
BOOST_TEST(test_equal(res_1.real(), T{-12}));
114+
BOOST_TEST(test_equal(res_1.imag(), T{0}));
115115
}
116116

117117
template <typename T>
@@ -125,8 +125,8 @@ void test_division()
125125
complex_scalar rhs_1 {T{2}, T{2}};
126126
complex_scalar res_1 = lhs_1 / rhs_1;
127127

128-
BOOST_TEST_EQ(res_1.real(), T{2});
129-
BOOST_TEST_EQ(res_1.imag(), T{-1});
128+
BOOST_TEST(test_equal(res_1.real(), T{2}));
129+
BOOST_TEST(test_equal(res_1.imag(), T{-1}));
130130
}
131131

132132
template <typename T>
@@ -162,8 +162,8 @@ void test_non_member_real_imag()
162162

163163
complex_scalar lhs {T{2}, T{-1}};
164164

165-
BOOST_TEST_EQ(real(lhs), lhs.real());
166-
BOOST_TEST_EQ(imag(lhs), lhs.imag());
165+
BOOST_TEST(test_equal(real(lhs), lhs.real()));
166+
BOOST_TEST(test_equal(imag(lhs), lhs.imag()));
167167
}
168168

169169
template <typename T>
@@ -179,7 +179,7 @@ void test_abs()
179179
BOOST_TEST(test_equal(static_cast<T>(abs(lhs)), static_cast<T>(sqrt(T{2}))));
180180
}
181181

182-
template <typename T>
182+
template <typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
183183
void test_arg()
184184
{
185185
using std::complex;
@@ -195,6 +195,21 @@ void test_arg()
195195
BOOST_TEST(test_equal(arg(complex_scalar{T{-1}, T{0}}), pi<T>()));
196196
}
197197

198+
template <typename T, std::enable_if_t<!std::is_floating_point<T>::value, bool> = true>
199+
void test_arg()
200+
{
201+
using std::complex;
202+
using std::polar;
203+
using std::arg;
204+
using complex_scalar = decltype(polar(T(), T()));
205+
206+
BOOST_TEST(test_equal(arg(complex_scalar{T{1}, T{0}}), T{0}));
207+
BOOST_TEST(test_equal(arg(complex_scalar{T{0}, T{0}}), T{0}));
208+
BOOST_TEST(test_equal(arg(complex_scalar{T{0}, T{1}}), numbers::pi_v<T> / 2));
209+
BOOST_TEST(test_equal(arg(complex_scalar{T{-1}, T{0}}), numbers::pi_v<T>));
210+
}
211+
212+
198213
template <typename T>
199214
void test_norm()
200215
{
@@ -236,13 +251,9 @@ void test_proj()
236251
lhs = complex_scalar{T{std::numeric_limits<T>::infinity()}, T{1}};
237252
complex_scalar rhs = complex_scalar{T{std::numeric_limits<T>::infinity()}, T{0}};
238253
BOOST_TEST_EQ(proj(lhs), rhs);
239-
240-
lhs = complex_scalar{T{std::numeric_limits<T>::infinity()}, T{-1}};
241-
rhs = complex_scalar{T{std::numeric_limits<T>::infinity()}, T{-0}};
242-
BOOST_TEST_EQ(proj(lhs), rhs);
243254
}
244255

245-
template <typename T>
256+
template <typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
246257
void test_exp()
247258
{
248259
using std::complex;
@@ -258,7 +269,22 @@ void test_exp()
258269
BOOST_TEST(test_equal(lhs.imag(), rhs.imag()));
259270
}
260271

261-
template <typename T>
272+
template <typename T, std::enable_if_t<!std::is_floating_point<T>::value, bool> = true>
273+
void test_exp()
274+
{
275+
using std::complex;
276+
using std::polar;
277+
using std::exp;
278+
using complex_scalar = decltype(polar(T(), T()));
279+
280+
complex_scalar lhs {T{0}, numbers::pi_v<T>};
281+
lhs = exp(lhs);
282+
complex_scalar rhs {T{-1}, T{0}};
283+
BOOST_TEST(test_equal(lhs.real(), rhs.real()));
284+
BOOST_TEST(test_equal(lhs.imag(), rhs.imag()));
285+
}
286+
287+
template <typename T, std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
262288
void test_log()
263289
{
264290
using std::complex;
@@ -291,6 +317,41 @@ void test_log()
291317
BOOST_TEST(test_equal(lhs.imag(), rhs.imag()));
292318
}
293319

320+
/*
321+
template <typename T, std::enable_if_t<!std::is_floating_point<T>::value, bool> = true>
322+
void test_log()
323+
{
324+
using std::complex;
325+
using std::polar;
326+
using std::log;
327+
using boost::math::constants::half_pi;
328+
using boost::math::constants::pi;
329+
using complex_scalar = decltype(polar(T(), T()));
330+
331+
complex_scalar lhs {T{0}, T{1}};
332+
lhs = log(lhs);
333+
complex_scalar rhs {T{0}, numbers::pi_v<T> / 2};
334+
335+
BOOST_TEST(test_equal(lhs.real(), rhs.real()));
336+
BOOST_TEST(test_equal(lhs.imag(), rhs.imag()));
337+
338+
lhs = {T{-1}, T{0}};
339+
lhs = log(lhs);
340+
rhs = {T{0}, numbers::pi_v<T>};
341+
342+
BOOST_TEST(test_equal(lhs.real(), rhs.real()));
343+
BOOST_TEST(test_equal(lhs.imag(), rhs.imag()));
344+
345+
// Other side of the cut line
346+
lhs = {T {-1}, -T {0}};
347+
lhs = log(lhs);
348+
rhs = {T {0}, -numbers::pi_v<T>};
349+
350+
BOOST_TEST(test_equal(lhs.real(), rhs.real()));
351+
BOOST_TEST(test_equal(lhs.imag(), rhs.imag()));
352+
}
353+
*/
354+
294355
template <typename T>
295356
void test_scalar_addition()
296357
{
@@ -302,8 +363,8 @@ void test_scalar_addition()
302363
T rhs_1 {T{2}};
303364
complex_scalar res_1 = lhs_1 + rhs_1;
304365

305-
BOOST_TEST_EQ(res_1.real(), T{3});
306-
BOOST_TEST_EQ(res_1.imag(), T{1});
366+
BOOST_TEST(test_equal(res_1.real(), T{3}));
367+
BOOST_TEST(test_equal(res_1.imag(), T{1}));
307368
}
308369

309370
template <typename T>
@@ -317,8 +378,8 @@ void test_scalar_subtraction()
317378
T rhs_1 {T{2}};
318379
complex_scalar res_1 = lhs_1 - rhs_1;
319380

320-
BOOST_TEST_EQ(res_1.real(), T{-1});
321-
BOOST_TEST_EQ(res_1.imag(), T{1});
381+
BOOST_TEST(test_equal(res_1.real(), T{-1}));
382+
BOOST_TEST(test_equal(res_1.imag(), T{1}));
322383
}
323384

324385
template <typename T>
@@ -332,8 +393,8 @@ void test_scalar_multiplication()
332393
T rhs_1 {T{2}};
333394
complex_scalar res_1 = lhs_1 * rhs_1;
334395

335-
BOOST_TEST_EQ(res_1.real(), T{6});
336-
BOOST_TEST_EQ(res_1.imag(), T{4});
396+
BOOST_TEST(test_equal(res_1.real(), T{6}));
397+
BOOST_TEST(test_equal(res_1.imag(), T{4}));
337398
}
338399

339400
template <typename T>
@@ -347,10 +408,11 @@ void test_scalar_division()
347408
T rhs_1 {T{2}};
348409
complex_scalar res_1 = lhs_1 / rhs_1;
349410

350-
BOOST_TEST_EQ(res_1.real(), T{2});
351-
BOOST_TEST_EQ(res_1.imag(), T{1});
411+
BOOST_TEST(test_equal(res_1.real(), T{2}));
412+
BOOST_TEST(test_equal(res_1.imag(), T{1}));
352413
}
353414

415+
/*
354416
template <typename T>
355417
void test_log10()
356418
{
@@ -364,7 +426,9 @@ void test_log10()
364426
complex_scalar rhs {T{2}, static_cast<T>(1.36438)};
365427
BOOST_TEST(test_equal(lhs.real(), rhs.real()));
366428
}
429+
*/
367430

431+
/*
368432
template <typename T>
369433
void test_pow()
370434
{
@@ -395,6 +459,7 @@ void test_pow()
395459
BOOST_TEST(test_equal(lhs.imag(), rhs.imag()));
396460
#endif
397461
}
462+
*/
398463

399464
template <typename T>
400465
void test_sqrt()
@@ -480,7 +545,6 @@ void test_tanh()
480545
BOOST_TEST(test_equal(lhs.real(), rhs.real()));
481546
BOOST_TEST(test_equal(lhs.imag(), rhs.imag()));
482547
}
483-
*/
484548

485549
int main()
486550
{
@@ -489,7 +553,6 @@ int main()
489553
test_construction<decimal32>();
490554
test_construction<decimal64>();
491555

492-
/*
493556
test_unary_operators<float>();
494557
test_unary_operators<double>();
495558
test_unary_operators<decimal32>();
@@ -555,10 +618,12 @@ int main()
555618
test_exp<decimal32>();
556619
test_exp<decimal64>();
557620

621+
/*
558622
test_log<float>();
559623
test_log<double>();
560624
test_log<decimal32>();
561625
test_log<decimal64>();
626+
*/
562627

563628
test_scalar_addition<float>();
564629
test_scalar_addition<double>();
@@ -580,6 +645,7 @@ int main()
580645
test_scalar_division<decimal32>();
581646
test_scalar_division<decimal64>();
582647

648+
/*
583649
test_log10<float>();
584650
test_log10<double>();
585651
test_log10<decimal32>();
@@ -589,6 +655,7 @@ int main()
589655
test_pow<double>();
590656
test_pow<decimal32>();
591657
test_pow<decimal64>();
658+
*/
592659

593660
test_sqrt<float>();
594661
test_sqrt<double>();
@@ -609,7 +676,6 @@ int main()
609676
test_tanh<double>();
610677
test_tanh<decimal32>();
611678
test_tanh<decimal64>();
612-
*/
613679

614680
return boost::report_errors();
615681
}

0 commit comments

Comments
 (0)