Skip to content

Commit 63bbefa

Browse files
committed
templates and types
1 parent ac453a0 commit 63bbefa

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

tests/basictest.cpp

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,18 @@
5656
#define FASTFLOAT_ODDPLATFORM 1
5757
#endif
5858

59-
#define iHexAndDec(v) \
60-
(std::ostringstream{} << std::hex << "0x" << (v) << " (" << std::dec << (v) \
61-
<< ")") \
62-
.str()
63-
#define fHexAndDec(v) \
64-
(std::ostringstream{} << std::hexfloat << (v) << " (" << std::defaultfloat \
65-
<< std::setprecision(DBL_MAX_10_EXP + 1) << (v) \
66-
<< ")") \
67-
.str()
59+
template <typename T> std::string iHexAndDec(T v) {
60+
std::ostringstream ss;
61+
ss << std::hex << "0x" << (v) << " (" << std::dec << (v) << ")";
62+
return ss.str();
63+
}
64+
65+
template <typename T> std::string fHexAndDec(T v) {
66+
std::ostringstream ss;
67+
ss << std::hexfloat << (v) << " (" << std::defaultfloat
68+
<< std::setprecision(DBL_MAX_10_EXP + 1) << (v) << ")";
69+
return ss.str();
70+
}
6871

6972
char const *round_name(int d) {
7073
switch (d) {
@@ -174,7 +177,7 @@ TEST_CASE("double.parse_zero") {
174177
auto r1 = fast_float::from_chars(zero, zero + 1, f);
175178
CHECK(r1.ec == std::errc());
176179
std::cout << "FE_UPWARD parsed zero as " << fHexAndDec(f) << std::endl;
177-
CHECK(f == 0);
180+
CHECK(f == 0.);
178181
::memcpy(&float64_parsed, &f, sizeof(f));
179182
std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
180183
<< std::endl;
@@ -184,7 +187,7 @@ TEST_CASE("double.parse_zero") {
184187
auto r2 = fast_float::from_chars(zero, zero + 1, f);
185188
CHECK(r2.ec == std::errc());
186189
std::cout << "FE_TOWARDZERO parsed zero as " << fHexAndDec(f) << std::endl;
187-
CHECK(f == 0);
190+
CHECK(f == 0.);
188191
::memcpy(&float64_parsed, &f, sizeof(f));
189192
std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
190193
<< std::endl;
@@ -194,7 +197,7 @@ TEST_CASE("double.parse_zero") {
194197
auto r3 = fast_float::from_chars(zero, zero + 1, f);
195198
CHECK(r3.ec == std::errc());
196199
std::cout << "FE_DOWNWARD parsed zero as " << fHexAndDec(f) << std::endl;
197-
CHECK(f == 0);
200+
CHECK(f == 0.);
198201
::memcpy(&float64_parsed, &f, sizeof(f));
199202
std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
200203
<< std::endl;
@@ -204,7 +207,7 @@ TEST_CASE("double.parse_zero") {
204207
auto r4 = fast_float::from_chars(zero, zero + 1, f);
205208
CHECK(r4.ec == std::errc());
206209
std::cout << "FE_TONEAREST parsed zero as " << fHexAndDec(f) << std::endl;
207-
CHECK(f == 0);
210+
CHECK(f == 0.);
208211
::memcpy(&float64_parsed, &f, sizeof(f));
209212
std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
210213
<< std::endl;
@@ -226,7 +229,7 @@ TEST_CASE("double.parse_negative_zero") {
226229
CHECK(r1.ec == std::errc());
227230
std::cout << "FE_UPWARD parsed negative zero as " << fHexAndDec(f)
228231
<< std::endl;
229-
CHECK(f == 0);
232+
CHECK(f == 0.);
230233
::memcpy(&float64_parsed, &f, sizeof(f));
231234
std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
232235
<< std::endl;
@@ -237,7 +240,7 @@ TEST_CASE("double.parse_negative_zero") {
237240
CHECK(r2.ec == std::errc());
238241
std::cout << "FE_TOWARDZERO parsed negative zero as " << fHexAndDec(f)
239242
<< std::endl;
240-
CHECK(f == 0);
243+
CHECK(f == 0.);
241244
::memcpy(&float64_parsed, &f, sizeof(f));
242245
std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
243246
<< std::endl;
@@ -248,7 +251,7 @@ TEST_CASE("double.parse_negative_zero") {
248251
CHECK(r3.ec == std::errc());
249252
std::cout << "FE_DOWNWARD parsed negative zero as " << fHexAndDec(f)
250253
<< std::endl;
251-
CHECK(f == 0);
254+
CHECK(f == 0.);
252255
::memcpy(&float64_parsed, &f, sizeof(f));
253256
std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
254257
<< std::endl;
@@ -259,7 +262,7 @@ TEST_CASE("double.parse_negative_zero") {
259262
CHECK(r4.ec == std::errc());
260263
std::cout << "FE_TONEAREST parsed negative zero as " << fHexAndDec(f)
261264
<< std::endl;
262-
CHECK(f == 0);
265+
CHECK(f == 0.);
263266
::memcpy(&float64_parsed, &f, sizeof(f));
264267
std::cout << "double as uint64_t is " << iHexAndDec(float64_parsed)
265268
<< std::endl;
@@ -312,7 +315,7 @@ TEST_CASE("float.parse_zero") {
312315
auto r1 = fast_float::from_chars(zero, zero + 1, f);
313316
CHECK(r1.ec == std::errc());
314317
std::cout << "FE_UPWARD parsed zero as " << fHexAndDec(f) << std::endl;
315-
CHECK(f == 0);
318+
CHECK(f == 0.f);
316319
::memcpy(&float32_parsed, &f, sizeof(f));
317320
std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
318321
<< std::endl;
@@ -322,7 +325,7 @@ TEST_CASE("float.parse_zero") {
322325
auto r2 = fast_float::from_chars(zero, zero + 1, f);
323326
CHECK(r2.ec == std::errc());
324327
std::cout << "FE_TOWARDZERO parsed zero as " << fHexAndDec(f) << std::endl;
325-
CHECK(f == 0);
328+
CHECK(f == 0.f);
326329
::memcpy(&float32_parsed, &f, sizeof(f));
327330
std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
328331
<< std::endl;
@@ -332,7 +335,7 @@ TEST_CASE("float.parse_zero") {
332335
auto r3 = fast_float::from_chars(zero, zero + 1, f);
333336
CHECK(r3.ec == std::errc());
334337
std::cout << "FE_DOWNWARD parsed zero as " << fHexAndDec(f) << std::endl;
335-
CHECK(f == 0);
338+
CHECK(f == 0.f);
336339
::memcpy(&float32_parsed, &f, sizeof(f));
337340
std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
338341
<< std::endl;
@@ -342,7 +345,7 @@ TEST_CASE("float.parse_zero") {
342345
auto r4 = fast_float::from_chars(zero, zero + 1, f);
343346
CHECK(r4.ec == std::errc());
344347
std::cout << "FE_TONEAREST parsed zero as " << fHexAndDec(f) << std::endl;
345-
CHECK(f == 0);
348+
CHECK(f == 0.f);
346349
::memcpy(&float32_parsed, &f, sizeof(f));
347350
std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
348351
<< std::endl;
@@ -364,7 +367,7 @@ TEST_CASE("float.parse_negative_zero") {
364367
CHECK(r1.ec == std::errc());
365368
std::cout << "FE_UPWARD parsed negative zero as " << fHexAndDec(f)
366369
<< std::endl;
367-
CHECK(f == 0);
370+
CHECK(f == 0.f);
368371
::memcpy(&float32_parsed, &f, sizeof(f));
369372
std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
370373
<< std::endl;
@@ -375,7 +378,7 @@ TEST_CASE("float.parse_negative_zero") {
375378
CHECK(r2.ec == std::errc());
376379
std::cout << "FE_TOWARDZERO parsed negative zero as " << fHexAndDec(f)
377380
<< std::endl;
378-
CHECK(f == 0);
381+
CHECK(f == 0.f);
379382
::memcpy(&float32_parsed, &f, sizeof(f));
380383
std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
381384
<< std::endl;
@@ -386,7 +389,7 @@ TEST_CASE("float.parse_negative_zero") {
386389
CHECK(r3.ec == std::errc());
387390
std::cout << "FE_DOWNWARD parsed negative zero as " << fHexAndDec(f)
388391
<< std::endl;
389-
CHECK(f == 0);
392+
CHECK(f == 0.f);
390393
::memcpy(&float32_parsed, &f, sizeof(f));
391394
std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
392395
<< std::endl;
@@ -397,7 +400,7 @@ TEST_CASE("float.parse_negative_zero") {
397400
CHECK(r4.ec == std::errc());
398401
std::cout << "FE_TONEAREST parsed negative zero as " << fHexAndDec(f)
399402
<< std::endl;
400-
CHECK(f == 0);
403+
CHECK(f == 0.f);
401404
::memcpy(&float32_parsed, &f, sizeof(f));
402405
std::cout << "float as uint32_t is " << iHexAndDec(float32_parsed)
403406
<< std::endl;

0 commit comments

Comments
 (0)