parse_double_ut.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "parse_double.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. namespace NYql {
  4. Y_UNIT_TEST_SUITE(TParseDouble) {
  5. template <typename T, typename F>
  6. void ParseAndCheck(TStringBuf buf, F f, T expected) {
  7. T result = 0;
  8. UNIT_ASSERT(f(buf, result));
  9. UNIT_ASSERT_DOUBLES_EQUAL(expected, result, 1e-6);
  10. }
  11. Y_UNIT_TEST(ExactValues) {
  12. ParseAndCheck(TStringBuf("nan"), TryFloatFromString, std::numeric_limits<float>::quiet_NaN());
  13. ParseAndCheck(TStringBuf("nAn"), TryDoubleFromString, std::numeric_limits<double>::quiet_NaN());
  14. ParseAndCheck(TStringBuf("+nan"), TryFloatFromString, std::numeric_limits<float>::quiet_NaN());
  15. ParseAndCheck(TStringBuf("+NAN"), TryDoubleFromString, std::numeric_limits<double>::quiet_NaN());
  16. ParseAndCheck(TStringBuf("-nan"), TryFloatFromString, std::numeric_limits<float>::quiet_NaN());
  17. ParseAndCheck(TStringBuf("-NaN"), TryDoubleFromString, std::numeric_limits<double>::quiet_NaN());
  18. ParseAndCheck(TStringBuf("inf"), TryFloatFromString, std::numeric_limits<float>::infinity());
  19. ParseAndCheck(TStringBuf("iNf"), TryDoubleFromString, std::numeric_limits<double>::infinity());
  20. ParseAndCheck(TStringBuf("+inf"), TryFloatFromString, std::numeric_limits<float>::infinity());
  21. ParseAndCheck(TStringBuf("+INF"), TryDoubleFromString, std::numeric_limits<double>::infinity());
  22. ParseAndCheck(TStringBuf("-inf"), TryFloatFromString, -std::numeric_limits<float>::infinity());
  23. ParseAndCheck(TStringBuf("-InF"), TryDoubleFromString, -std::numeric_limits<double>::infinity());
  24. ParseAndCheck<float>(TStringBuf("-12.3456"), TryFloatFromString, -12.3456);
  25. ParseAndCheck(TStringBuf("-12.3456"), TryDoubleFromString, -12.3456);
  26. ParseAndCheck<float>(TStringBuf("1.23e-2"), TryFloatFromString, 0.0123);
  27. ParseAndCheck(TStringBuf("1.23e-2"), TryDoubleFromString, 0.0123);
  28. UNIT_ASSERT_EQUAL(FloatFromString(TStringBuf("iNf")), std::numeric_limits<float>::infinity());
  29. UNIT_ASSERT_EQUAL(DoubleFromString(TStringBuf("iNf")), std::numeric_limits<float>::infinity());
  30. }
  31. Y_UNIT_TEST(Errors) {
  32. UNIT_ASSERT_EXCEPTION_CONTAINS(FloatFromString(TStringBuf("")), std::exception, "unable to parse float from ''");
  33. UNIT_ASSERT_EXCEPTION_CONTAINS(DoubleFromString(TStringBuf("")), std::exception, "unable to parse double from ''");
  34. UNIT_ASSERT_EXCEPTION_CONTAINS(FloatFromString(TStringBuf("info")), std::exception, "unable to parse float from 'info'");
  35. UNIT_ASSERT_EXCEPTION_CONTAINS(DoubleFromString(TStringBuf("-nana")), std::exception, "unable to parse double from '-nana'");
  36. UNIT_ASSERT_EXCEPTION_CONTAINS(FloatFromString(TStringBuf(nullptr)), std::exception, "unable to parse float from ''");
  37. UNIT_ASSERT_EXCEPTION_CONTAINS(DoubleFromString(TStringBuf(nullptr)), std::exception, "unable to parse double from ''");
  38. }
  39. }
  40. }