1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-22 17:18:39 +00:00

MetaDataRef: Make set_float preserve numbers exactly (#16090)

This commit is contained in:
Lars Müller 2025-05-02 21:27:00 +02:00 committed by GitHub
parent 6f3735281f
commit d96f5e1c76
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 92 additions and 12 deletions

View file

@ -5,6 +5,7 @@
#include "test.h"
#include <cmath>
#include <limits>
#include "util/enriched_string.h"
#include "util/numeric.h"
#include "util/string.h"
@ -48,6 +49,7 @@ public:
void testColorizeURL();
void testSanitizeUntrusted();
void testReadSeed();
void testMyDoubleStringConversions();
};
static TestUtilities g_test_instance;
@ -84,6 +86,7 @@ void TestUtilities::runTests(IGameDef *gamedef)
TEST(testColorizeURL);
TEST(testSanitizeUntrusted);
TEST(testReadSeed);
TEST(testMyDoubleStringConversions);
}
////////////////////////////////////////////////////////////////////////////////
@ -763,3 +766,37 @@ void TestUtilities::testReadSeed()
// hashing should produce some non-zero number
UASSERT(read_seed("hello") != 0);
}
void TestUtilities::testMyDoubleStringConversions()
{
const auto expect_parse_failure = [](const std::string &s) {
UASSERT(!my_string_to_double(s).has_value());
};
expect_parse_failure("");
expect_parse_failure("helloworld");
expect_parse_failure("42x");
const auto expect_double = [](const std::string &s, double expected) {
auto got = my_string_to_double(s);
UASSERT(got.has_value());
UASSERTEQ(double, *got, expected);
};
expect_double("1", 1.0);
expect_double("42", 42.0);
expect_double("42.25", 42.25);
expect_double("3e3", 3000.0);
expect_double("0xff", 255.0);
expect_double("0x1.0p+1", 2.0);
UASSERT(std::isnan(my_string_to_double(my_double_to_string(
std::numeric_limits<double>::quiet_NaN())).value()));
const auto test_round_trip = [](double number) {
auto got = my_string_to_double(my_double_to_string(number));
UASSERT(got.has_value());
UASSERTEQ(double, *got, number);
};
test_round_trip(std::numeric_limits<double>::infinity());
test_round_trip(-std::numeric_limits<double>::infinity());
test_round_trip(0.3);
test_round_trip(0.1 + 0.2);
}