-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from hasselmm/improvement/better-number-parsing
Improve the number parsing
- Loading branch information
Showing
7 changed files
with
490 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* QtNetworkCrumbs - Some networking toys for Qt | ||
* Copyright (C) 2019-2024 Mathias Hasselmann | ||
*/ | ||
#include "qnctestsupport.h" | ||
|
||
// Qt headers | ||
#include <QTest> | ||
|
||
// STL headers | ||
#include <iomanip> | ||
#include <sstream> | ||
|
||
namespace qnc::tests { | ||
namespace { | ||
|
||
[[nodiscard]] std::string toStdString(long double value) | ||
{ | ||
const auto p = std::numeric_limits<long double>::digits; | ||
return (std::ostringstream{} << std::setprecision(p) << value).str(); | ||
} | ||
|
||
[[nodiscard]] QString toString(long double value) | ||
{ | ||
return QString::fromStdString(toStdString(value)); | ||
} | ||
|
||
} // namespace | ||
|
||
bool initialize() | ||
{ | ||
return QMetaType::registerConverter<long double, QString>(&toString); | ||
} | ||
|
||
} // namespace qnc::tests | ||
|
||
[[nodiscard]] char *QTest::toString(const long double &t) | ||
{ | ||
return qstrdup(qnc::tests::toStdString(t).c_str()); | ||
} | ||
|
||
[[nodiscard]] bool QTest::qCompare(long double const &t1, long double const &t2, | ||
const char *actual, const char *expected, | ||
const char *file, int line) | ||
{ | ||
auto success = false; | ||
|
||
if (std::isnan(t1)) { | ||
success = std::isnan(t2); | ||
} else if (std::isinf(t1) && t1 > 0) { | ||
success = std::isinf(t2) && t2 > 0; | ||
} else if (std::isinf(t1) && t1 < 0) { | ||
success = std::isinf(t2) && t2 < 0; | ||
} else { | ||
success = qFuzzyCompare(t1, t2); | ||
} | ||
|
||
#if QT_VERSION_MAJOR < 6 | ||
|
||
return compare_helper(success, "Compared numbers are not the same", | ||
toString(t1), toString(t2), actual, expected, file, line); | ||
|
||
#else // QT_VERSION_MAJOR >= 6 | ||
|
||
return reportResult(success, | ||
[t1] { return toString(t1); }, | ||
[t2] { return toString(t2); }, | ||
actual, expected, ComparisonOperation::Equal, | ||
file, line); | ||
|
||
#endif // QT_VERSION_MAJOR >= 6 | ||
} | ||
|
||
QDebug operator<<(QDebug debug, long double value) | ||
{ | ||
const auto _ = QDebugStateSaver{debug}; | ||
return debug.noquote() << qnc::tests::toString(value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* QtNetworkCrumbs - Some networking toys for Qt | ||
* Copyright (C) 2019-2024 Mathias Hasselmann | ||
*/ | ||
#ifndef QNC_QNCTESTSUPPORT_H | ||
#define QNC_QNCTESTSUPPORT_H | ||
|
||
// Qt headers | ||
#include <QMetaType> | ||
|
||
// STL headers | ||
#include <cmath> | ||
|
||
class QDebug; | ||
|
||
Q_DECLARE_METATYPE(long double) | ||
|
||
namespace QTest { | ||
|
||
[[nodiscard]] char *toString(const long double &t); | ||
[[nodiscard]] bool qCompare(long double const &t1, long double const &t2, | ||
const char *actual, const char *expected, | ||
const char *file, int line); | ||
|
||
[[nodiscard]] inline bool qCompare(long double const &t1, const double &t2, | ||
const char *actual, const char *expected, | ||
const char *file, int line) | ||
{ | ||
return qCompare(t1, static_cast<long double>(t2), actual, expected, file, line); | ||
} | ||
|
||
[[nodiscard]] inline bool qCompare(long double const &t1, const float &t2, | ||
const char *actual, const char *expected, | ||
const char *file, int line) | ||
{ | ||
return qCompare(t1, static_cast<long double>(t2), actual, expected, file, line); | ||
} | ||
|
||
[[nodiscard]] inline bool qCompare(const double &t1, const long double &t2, | ||
const char *actual, const char *expected, | ||
const char *file, int line) | ||
{ | ||
return qCompare(static_cast<long double>(t1), t2, actual, expected, file, line); | ||
} | ||
|
||
[[nodiscard]] inline bool qCompare(float const &t1, const long double &t2, | ||
const char *actual, const char *expected, | ||
const char *file, int line) | ||
{ | ||
return qCompare(static_cast<long double>(t1), t2, actual, expected, file, line); | ||
} | ||
|
||
} // namespace QTest | ||
|
||
[[nodiscard]] inline bool qFuzzyCompare(long double p1, long double p2) | ||
{ | ||
if constexpr (sizeof(long double) != sizeof(double)) { | ||
return (std::abs(p1 - p2) * 10000000000000000. <= std::min(std::abs(p1), std::abs(p2))); | ||
} else { | ||
return qFuzzyCompare(static_cast<double>(p1), static_cast<double>(p2)); | ||
} | ||
} | ||
|
||
QDebug operator<<(QDebug debug, long double value); | ||
|
||
namespace qnc::tests { | ||
bool initialize(); | ||
} // namespace qnc::tests | ||
|
||
#endif // QNC_QNCTESTSUPPORT_H |
Oops, something went wrong.