Skip to content

Commit

Permalink
Merge pull request #292 from dalle/dalle/unicode-tests
Browse files Browse the repository at this point in the history
Add basic tests for all supported char types (char, wchar_t, char8_t, char16_t, char32_t)
  • Loading branch information
dalle authored Nov 25, 2024
2 parents 321d3a7 + 396feb6 commit ac33f96
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ cc_test(
],
)

cc_test(
name = "supported_chars_test",
srcs = ["supported_chars_test.cpp"],
deps = [
"//:fast_float",
"@doctest//doctest",
],
)

cc_test(
name = "string_test",
srcs = ["string_test.cpp"],
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(supported_chars_test)
fast_float_add_cpp_test(example_test)
fast_float_add_cpp_test(example_comma_test)
fast_float_add_cpp_test(basictest)
Expand Down
53 changes: 53 additions & 0 deletions tests/supported_chars_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "fast_float/fast_float.h"
#include <iostream>
#include <string>
#include <system_error>

template <typename UC> bool test(std::string s, double expected) {
std::basic_string<UC> 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<char>("4.2", 4.2)) {
std::cout << "test failure for char" << std::endl;
return EXIT_FAILURE;
}

if (!test<wchar_t>("4.2", 4.2)) {
std::cout << "test failure for wchar_t" << std::endl;
return EXIT_FAILURE;
}

#ifdef __cpp_char8_t
if (!test<char8_t>("4.2", 4.2)) {
std::cout << "test failure for char8_t" << std::endl;
return EXIT_FAILURE;
}
#endif

if (!test<char16_t>("4.2", 4.2)) {
std::cout << "test failure for char16_t" << std::endl;
return EXIT_FAILURE;
}

if (!test<char32_t>("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;
}

0 comments on commit ac33f96

Please sign in to comment.