Files
OrcaSlicer/tests/libslic3r/test_locales_utils.cpp
T
Martin Stachon 808dd7dff8 Fix OBJ import on macOS 13.3 to 14.0 when the UI language uses a decimal comma (#17851)
On macOS 13.3 to 14.0, Apple's `strtod` reads the process locale and ignores the thread locale that `CNumericLocalesSetter` sets. With a UI language that uses a decimal comma, every OBJ vertex with a decimal point was rejected and the import failed. `objparser.cpp` now reads numbers with the new `strtod_decimal_point()` in `LocalesUtils`, which parses with fast_float and doesn't depend on the locale.
2026-11-25 10:14:39 -06:00

145 lines
4.9 KiB
C++

#include <catch2/catch_all.hpp>
#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <catch2/catch_message.hpp>
#include <catch2/matchers/catch_matchers.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>
#include "libslic3r/LocalesUtils.hpp"
#include "libslic3r/Utils.hpp"
#include "test_utils.hpp"
#include <boost/lexical_cast.hpp>
#include <boost/lexical_cast/bad_lexical_cast.hpp>
#include <cstddef>
#include <string>
using namespace Slic3r;
TEST_CASE("Floats print as printf prints them in the C locale", "[LocalesUtils]")
{
const auto [value, precision, text] = GENERATE(table<double, int, std::string>({
{0.5, -1, "0.5"},
{25. / 3., -1, "8.33333"},
{1500.5, -1, "1500.5"},
{1e6, -1, "1e+06"},
{-0.000123, -1, "-0.000123"},
{25. / 3., 3, "8.333"},
{2., 0, "2"},
// Longer than the to_chars buffer.
{1e21, 2, "1000000000000000000000.00"},
}));
CHECK(float_to_string_decimal_point(value, precision) == text);
}
TEST_CASE("Floats print with a decimal point in a locale whose decimal separator is a comma", "[LocalesUtils]")
{
CommaNumericLocale comma;
{
CNumericLocalesSetter outer;
if (! comma.apply())
SKIP("no locale with a comma decimal separator is installed");
CHECK(float_to_string_decimal_point(1500.5) == "1500.5");
CHECK(float_to_string_decimal_point(25. / 3., 3) == "8.333");
}
}
TEST_CASE("a setter nested in another leaves the C locale in place for the outer one", "[LocalesUtils]")
{
CNumericLocalesSetter outer;
{
CNumericLocalesSetter inner;
CHECK(is_decimal_separator_point());
}
CHECK(is_decimal_separator_point());
}
TEST_CASE("a setter nested in another sets C again when the locale changed between them", "[LocalesUtils]")
{
CommaNumericLocale comma;
{
CNumericLocalesSetter outer;
if (! comma.apply())
SKIP("no locale with a comma decimal separator is installed");
REQUIRE_FALSE(is_decimal_separator_point());
{
CNumericLocalesSetter inner;
CHECK(is_decimal_separator_point());
}
CHECK_FALSE(is_decimal_separator_point());
}
}
TEST_CASE("atof_decimal_point parses what atof parses in the C locale", "[LocalesUtils]")
{
const auto [text, value] = GENERATE(table<const char*, double>({
{ "5", 5. },
{ " 12.5", 12.5 },
{ "\t+3", 3. },
{ "\r\n7", 7. },
{ "-1.25", -1.25 },
{ "1e2", 100. },
{ ".5", 0.5 },
{ "12.5;comment", 12.5 },
{ "+-5", 0. },
{ "", 0. },
{ "abc", 0. },
}));
INFO(text);
CHECK_THAT(atof_decimal_point(text), Catch::Matchers::WithinAbs(value, 1e-12));
}
TEST_CASE("strtod_decimal_point parses what strtod parses in the C locale", "[LocalesUtils]")
{
const auto [text, value, length] = GENERATE(table<const char*, double, size_t>({
{ "5", 5., 1 },
{ " 12.5 7", 12.5, 6 },
{ "\t+3", 3., 3 },
{ "-1.25", -1.25, 5 },
{ "1e2", 100., 3 },
{ ".5", 0.5, 2 },
{ "0,5", 0., 1 },
{ "+-5", 0., 0 },
{ "", 0., 0 },
{ "abc", 0., 0 },
}));
INFO(text);
char *end = nullptr;
CHECK_THAT(strtod_decimal_point(text, &end), Catch::Matchers::WithinAbs(value, 1e-12));
CHECK(size_t(end - text) == length);
}
TEST_CASE("strtod_decimal_point reads a decimal point when the process locale uses a comma", "[LocalesUtils]")
{
ProcessCommaNumericLocale comma;
if (! comma.apply())
SKIP("no locale with a comma decimal separator is installed");
const char *text = "0.5 1";
char *end = nullptr;
CHECK_THAT(strtod_decimal_point(text, &end), Catch::Matchers::WithinAbs(0.5, 1e-12));
CHECK(end == text + 3);
}
TEST_CASE("Nozzle diameter strings read back in a locale whose decimal separator is a comma", "[LocalesUtils]")
{
CommaNumericLocale comma;
{
CNumericLocalesSetter outer;
if (! comma.apply())
SKIP("no locale with a comma decimal separator is installed");
const std::string diameter = format_diameter_to_str(0.4);
CHECK(diameter == "0.4");
CHECK_THAT(string_to_double_decimal_point(diameter), Catch::Matchers::WithinAbs(0.4, 1e-12));
CHECK_THAT(boost::lexical_cast<double>(diameter), Catch::Matchers::WithinAbs(0.4, 1e-12));
CHECK_THAT(boost::lexical_cast<float>(diameter), Catch::Matchers::WithinAbs(0.4, 1e-6));
// A comma is not read as the decimal separator.
CHECK_THAT(string_to_double_decimal_point("0,4"), Catch::Matchers::WithinAbs(0., 1e-12));
// Callers that skip a diameter they cannot read rely on lexical_cast throwing for it.
for (const char* text : { "", "abc", "0.4x", "0,4", "1e1000" }) {
INFO(text);
CHECK_THROWS_AS(boost::lexical_cast<float>(text), boost::bad_lexical_cast);
}
}
}