Skip to content

Commit 558bec8

Browse files
committed
fix logging in basictest
1 parent 8cee025 commit 558bec8

File tree

1 file changed

+67
-24
lines changed

1 file changed

+67
-24
lines changed

tests/basictest.cpp

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
#include "doctest/doctest.h"
44

55
#include "fast_float/fast_float.h"
6+
#include <cfenv>
67
#include <cmath>
78
#include <cstdio>
89
#include <iomanip>
910
#include <ios>
11+
#include <iostream>
1012
#include <limits>
13+
#include <sstream>
1114
#include <string>
1215
#include <system_error>
1316
#include <type_traits>
14-
#include <cfenv>
1517

1618
#if FASTFLOAT_IS_CONSTEXPR
1719
#ifndef FASTFLOAT_CONSTEXPR_TESTS
@@ -54,9 +56,14 @@
5456
#define FASTFLOAT_ODDPLATFORM 1
5557
#endif
5658

57-
#define iHexAndDec(v) std::hex << "0x" << (v) << " (" << std::dec << (v) << ")"
59+
#define iHexAndDec(v) \
60+
(std::ostringstream{} << std::hex << "0x" << (v) << " (" << std::dec << (v) \
61+
<< ")") \
62+
.str()
5863
#define fHexAndDec(v) \
59-
std::hexfloat << (v) << " (" << std::defaultfloat << (v) << ")"
64+
(std::ostringstream{} << std::hexfloat << (v) << " (" << std::defaultfloat \
65+
<< (v) << ")") \
66+
.str()
6067

6168
char const *round_name(int d) {
6269
switch (d) {
@@ -256,7 +263,6 @@ TEST_CASE("parse_negative_zero") {
256263
#if (FASTFLOAT_CPLUSPLUS >= 201703L) && (FASTFLOAT_IS_BIG_ENDIAN == 0) && \
257264
!defined(FASTFLOAT_ODDPLATFORM)
258265

259-
#include <iostream>
260266
#include <filesystem>
261267
#include <charconv>
262268

@@ -722,6 +728,38 @@ uint64_t get_mantissa(double f) {
722728
return (m & ((uint64_t(1) << 57) - 1));
723729
}
724730

731+
#ifdef __STDCPP_FLOAT64_T__
732+
uint64_t get_mantissa(std::float64_t f) {
733+
uint64_t m;
734+
memcpy(&m, &f, sizeof(f));
735+
return (m & ((uint64_t(1) << 10) - 1));
736+
}
737+
#endif
738+
739+
#ifdef __STDCPP_FLOAT32_T__
740+
uint32_t get_mantissa(std::float32_t f) {
741+
uint32_t m;
742+
memcpy(&m, &f, sizeof(f));
743+
return (m & ((uint32_t(1) << 10) - 1));
744+
}
745+
#endif
746+
747+
#ifdef __STDCPP_FLOAT16_T__
748+
uint16_t get_mantissa(std::float16_t f) {
749+
uint16_t m;
750+
memcpy(&m, &f, sizeof(f));
751+
return (m & ((uint16_t(1) << 10) - 1));
752+
}
753+
#endif
754+
755+
#ifdef __STDCPP_BFLOAT16_T__
756+
uint16_t get_mantissa(std::bfloat16_t f) {
757+
uint16_t m;
758+
memcpy(&m, &f, sizeof(f));
759+
return (m & ((uint16_t(1) << 7) - 1));
760+
}
761+
#endif
762+
725763
std::string append_zeros(std::string str, size_t number_of_zeros) {
726764
std::string answer(str);
727765
for (size_t i = 0; i < number_of_zeros; i++) {
@@ -736,26 +774,31 @@ enum class Diag { runtime, comptime };
736774

737775
} // anonymous namespace
738776

777+
constexpr size_t global_string_capacity = 2048;
778+
739779
template <Diag diag, class T, typename result_type, typename stringtype>
740780
constexpr void check_basic_test_result(stringtype str, result_type result,
741781
T actual, T expected,
742782
std::errc expected_ec) {
743-
if constexpr (diag == Diag::runtime) {
744-
INFO("str=" << str << "\n"
745-
<< " expected=" << fHexAndDec(expected) << "\n"
746-
<< " ..actual=" << fHexAndDec(actual) << "\n"
747-
<< " expected mantissa=" << iHexAndDec(get_mantissa(expected))
748-
<< "\n"
749-
<< " ..actual mantissa=" << iHexAndDec(get_mantissa(actual)));
750-
}
751-
752783
struct ComptimeDiag {
753784
// Purposely not constexpr
754785
static void error_not_equal() {}
755786
};
756787

757788
#define FASTFLOAT_CHECK_EQ(...) \
758789
if constexpr (diag == Diag::runtime) { \
790+
char narrow[global_string_capacity]{}; \
791+
for (size_t i = 0; i < str.size(); i++) { \
792+
narrow[i] = char(str[i]); \
793+
} \
794+
INFO("str(char" << 8 * sizeof(typename stringtype::value_type) \
795+
<< ")=" << narrow << "\n" \
796+
<< " expected=" << fHexAndDec(expected) << "\n" \
797+
<< " ..actual=" << fHexAndDec(actual) << "\n" \
798+
<< " expected mantissa=" \
799+
<< iHexAndDec(get_mantissa(expected)) << "\n" \
800+
<< " ..actual mantissa=" \
801+
<< iHexAndDec(get_mantissa(actual))); \
759802
CHECK_EQ(__VA_ARGS__); \
760803
} else { \
761804
if ([](auto const &lhs, auto const &rhs) { \
@@ -774,9 +817,9 @@ constexpr void check_basic_test_result(stringtype str, result_type result,
774817
return -x;
775818
}
776819
return x;
777-
}
820+
} else
778821
#endif
779-
return std::copysign(x, y);
822+
return std::copysign(x, y);
780823
};
781824

782825
auto isnan = [](double x) -> bool { return x != x; };
@@ -797,26 +840,26 @@ constexpr void basic_test(std::string_view str, T expected,
797840
auto result =
798841
fast_float::from_chars(str.data(), str.data() + str.size(), actual);
799842
check_basic_test_result<diag>(str, result, actual, expected, expected_ec);
800-
constexpr size_t global_string_capacity = 2048;
801843

802844
if (str.size() > global_string_capacity) {
803845
return;
804846
}
847+
805848
// We give plenty of memory: 2048 characters.
806849
char16_t u16[global_string_capacity]{};
807-
808850
for (size_t i = 0; i < str.size(); i++) {
809851
u16[i] = char16_t(str[i]);
810852
}
853+
811854
auto result16 = fast_float::from_chars(u16, u16 + str.size(), actual);
812855
check_basic_test_result<diag>(std::u16string_view(u16, str.size()), result16,
813856
actual, expected, expected_ec);
814857

815858
char32_t u32[global_string_capacity]{};
816-
817859
for (size_t i = 0; i < str.size(); i++) {
818860
u32[i] = char32_t(str[i]);
819861
}
862+
820863
auto result32 = fast_float::from_chars(u32, u32 + str.size(), actual);
821864
check_basic_test_result<diag>(std::u32string_view(u32, str.size()), result32,
822865
actual, expected, expected_ec);
@@ -906,7 +949,7 @@ void basic_test(float val) {
906949
basic_test(val); \
907950
}
908951

909-
TEST_CASE("64bit.inf") {
952+
TEST_CASE("double.inf") {
910953
verify("INF", std::numeric_limits<double>::infinity());
911954
verify("-INF", -std::numeric_limits<double>::infinity());
912955
verify("INFINITY", std::numeric_limits<double>::infinity());
@@ -934,7 +977,7 @@ TEST_CASE("64bit.inf") {
934977
std::errc::result_out_of_range);
935978
}
936979

937-
TEST_CASE("64bit.general") {
980+
TEST_CASE("double.general") {
938981
verify("0.95000000000000000000", 0.95);
939982
verify("22250738585072012e-324",
940983
0x1p-1022); /* limit between normal and subnormal*/
@@ -1118,7 +1161,7 @@ TEST_CASE("64bit.general") {
11181161
-0x1.f1660a65b00bfp+60);
11191162
}
11201163

1121-
TEST_CASE("64bit.decimal_point") {
1164+
TEST_CASE("double.decimal_point") {
11221165
constexpr auto options = [] {
11231166
fast_float::parse_options ret{};
11241167
ret.decimal_point = ',';
@@ -1274,7 +1317,7 @@ TEST_CASE("64bit.decimal_point") {
12741317
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000022250738585072008890245868760858598876504231122409594654935248025624400092282356951787758888037591552642309780950434312085877387158357291821993020294379224223559819827501242041788969571311791082261043971979604000454897391938079198936081525613113376149842043271751033627391549782731594143828136275113838604094249464942286316695429105080201815926642134996606517803095075913058719846423906068637102005108723282784678843631944515866135041223479014792369585208321597621066375401613736583044193603714778355306682834535634005074073040135602968046375918583163124224521599262546494300836851861719422417646455137135420132217031370496583210154654068035397417906022589503023501937519773030945763173210852507299305089761582519159720757232455434770912461317493580281734466552734375);
12751318
}
12761319

1277-
TEST_CASE("32bit.inf") {
1320+
TEST_CASE("float.inf") {
12781321
verify("INF", std::numeric_limits<float>::infinity());
12791322
verify("-INF", -std::numeric_limits<float>::infinity());
12801323
verify("INFINITY", std::numeric_limits<float>::infinity());
@@ -1292,7 +1335,7 @@ TEST_CASE("32bit.inf") {
12921335
std::errc::result_out_of_range);
12931336
}
12941337

1295-
TEST_CASE("32bit.general") {
1338+
TEST_CASE("float.general") {
12961339
verify("-1e-999", -0.0f, std::errc::result_out_of_range);
12971340
verify("1."
12981341
"175494140627517859246175898662808184331245864732796240031385942718174"
@@ -1432,7 +1475,7 @@ TEST_CASE("32bit.general") {
14321475
0.00000000000000000000000000000000000001175494210692441075487029444849287348827052428745893333857174530571588870475618904265502351336181163787841796875f);
14331476
}
14341477

1435-
TEST_CASE("32bit.decimal_point") {
1478+
TEST_CASE("float.decimal_point") {
14361479
constexpr auto options = [] {
14371480
fast_float::parse_options ret{};
14381481
ret.decimal_point = ',';

0 commit comments

Comments
 (0)