float_test.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // SExp - A S-Expression Parser for C++
  2. // Copyright (C) 2018 Ingo Ruhnke <grumbel@gmail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include <gtest/gtest.h>
  17. #include <string>
  18. #include <sstream>
  19. #include <vector>
  20. #include "float.hpp"
  21. TEST(FloatTest, float2string)
  22. {
  23. std::vector<std::tuple<float, std::string> > datas =
  24. {
  25. { 1.0F, "1" },
  26. { 1234.567F, "1234.567" },
  27. { 1.0e3F, "1000" },
  28. { 1234567e-4F, "123.4567" },
  29. { 1.234e13F, "1.234e+13" },
  30. { 1234.0e13F, "1.234e+16" },
  31. { -1.0F, "-1" },
  32. { -1234.567F, "-1234.567" },
  33. { -1.0e3F, "-1000" },
  34. { -1234567e-4F, "-123.4567" },
  35. { -1.234e13F, "-1.234e+13" },
  36. { -1234.0e13F, "-1.234e+16" },
  37. // FIXME: std::ostream and std::to_chars() have different ideas
  38. // here, "1234.123" vs "1234.1234", using 1234.1230 as workaround
  39. { 1234.123056789012345678901234567890123456789, "1234.123" },
  40. { -1234.123056789012345678901234567890123456789, "-1234.123" },
  41. };
  42. for(const auto& data: datas)
  43. {
  44. std::ostringstream os;
  45. sexp::float2string(os, std::get<0>(data));
  46. ASSERT_EQ(os.str(), std::get<1>(data));
  47. }
  48. }
  49. TEST(FloatTest, string2float)
  50. {
  51. ASSERT_EQ(1234.5678F, sexp::string2float("1234.5678"));
  52. ASSERT_EQ(1234.5678F, sexp::string2float("+1234.5678"));
  53. ASSERT_EQ(-1234.5678F, sexp::string2float("-1234.5678"));
  54. ASSERT_EQ(5.0F, sexp::string2float("5.0ef"));
  55. ASSERT_EQ(1234.5F, sexp::string2float("1.2345e3f"));
  56. ASSERT_EQ(12.345F, sexp::string2float("12345e-3f"));
  57. }
  58. /* EOF */