From 2c4b48d4e042b763b0cad58a47b37e9ff357c5cb Mon Sep 17 00:00:00 2001 From: Anders Dalvander Date: Mon, 25 Nov 2024 15:25:36 +0100 Subject: [PATCH 1/2] add unicode_test --- tests/BUILD.bazel | 9 +++++++++ tests/CMakeLists.txt | 1 + tests/unicode_test.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 tests/unicode_test.cpp diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel index 5d1512f1..ea3dad32 100644 --- a/tests/BUILD.bazel +++ b/tests/BUILD.bazel @@ -97,6 +97,15 @@ cc_test( ], ) +cc_test( + name = "unicode_test", + srcs = ["unicode_test.cpp"], + deps = [ + "//:fast_float", + "@doctest//doctest", + ], +) + cc_test( name = "string_test", srcs = ["string_test.cpp"], diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 63df2e50..297fd3a9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -68,6 +68,7 @@ endfunction(fast_float_add_cpp_test) fast_float_add_cpp_test(rcppfastfloat_test) fast_float_add_cpp_test(wide_char_test) +fast_float_add_cpp_test(unicode_test) fast_float_add_cpp_test(example_test) fast_float_add_cpp_test(example_comma_test) fast_float_add_cpp_test(basictest) diff --git a/tests/unicode_test.cpp b/tests/unicode_test.cpp new file mode 100644 index 00000000..1362d492 --- /dev/null +++ b/tests/unicode_test.cpp @@ -0,0 +1,46 @@ +#include "fast_float/fast_float.h" +#include +#include +#include + +template bool test(std::string s, double expected) { + std::basic_string input(s.begin(), s.end()); + double result; + auto answer = + fast_float::from_chars(input.data(), input.data() + input.size(), result); + if (answer.ec != std::errc()) { + std::cerr << "parsing of \"" << s << "\" should succeed\n"; + return false; + } + if (result != expected && !(std::isnan(result) && std::isnan(expected))) { + std::cerr << "parsing of \"" << s << "\" succeeded, expected " << expected + << " got " << result << "\n"; + return false; + } + return true; +} + +int main() { + if (!test("4.2", 4.2)) { + std::cout << "test failure for char" << std::endl; + return EXIT_FAILURE; + } + + if (!test("4.2", 4.2)) { + std::cout << "test failure for wchar_t" << std::endl; + return EXIT_FAILURE; + } + + if (!test("4.2", 4.2)) { + std::cout << "test failure for char16_t" << std::endl; + return EXIT_FAILURE; + } + + if (!test("4.2", 4.2)) { + std::cout << "test failure for char32_t" << std::endl; + return EXIT_FAILURE; + } + + std::cout << "all ok" << std::endl; + return EXIT_SUCCESS; +} From 396feb63534da42244c361d5dc255307805fd5e4 Mon Sep 17 00:00:00 2001 From: Anders Dalvander Date: Mon, 25 Nov 2024 15:43:51 +0100 Subject: [PATCH 2/2] add char8_t test --- tests/BUILD.bazel | 4 ++-- tests/CMakeLists.txt | 2 +- tests/{unicode_test.cpp => supported_chars_test.cpp} | 7 +++++++ 3 files changed, 10 insertions(+), 3 deletions(-) rename tests/{unicode_test.cpp => supported_chars_test.cpp} (89%) diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel index ea3dad32..57d8c342 100644 --- a/tests/BUILD.bazel +++ b/tests/BUILD.bazel @@ -98,8 +98,8 @@ cc_test( ) cc_test( - name = "unicode_test", - srcs = ["unicode_test.cpp"], + name = "supported_chars_test", + srcs = ["supported_chars_test.cpp"], deps = [ "//:fast_float", "@doctest//doctest", diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 297fd3a9..c4e43b21 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -68,7 +68,7 @@ endfunction(fast_float_add_cpp_test) fast_float_add_cpp_test(rcppfastfloat_test) fast_float_add_cpp_test(wide_char_test) -fast_float_add_cpp_test(unicode_test) +fast_float_add_cpp_test(supported_chars_test) fast_float_add_cpp_test(example_test) fast_float_add_cpp_test(example_comma_test) fast_float_add_cpp_test(basictest) diff --git a/tests/unicode_test.cpp b/tests/supported_chars_test.cpp similarity index 89% rename from tests/unicode_test.cpp rename to tests/supported_chars_test.cpp index 1362d492..3660f818 100644 --- a/tests/unicode_test.cpp +++ b/tests/supported_chars_test.cpp @@ -31,6 +31,13 @@ int main() { return EXIT_FAILURE; } +#ifdef __cpp_char8_t + if (!test("4.2", 4.2)) { + std::cout << "test failure for char8_t" << std::endl; + return EXIT_FAILURE; + } +#endif + if (!test("4.2", 4.2)) { std::cout << "test failure for char16_t" << std::endl; return EXIT_FAILURE;