io_test.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // SExp - A S-Expression Parser for C++
  2. // Copyright (C) 2015 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 <sstream>
  18. #include "sexp/io.hpp"
  19. #include "sexp/value.hpp"
  20. #include "sexp/parser.hpp"
  21. TEST(IOTest, roundtrip)
  22. {
  23. std::vector<std::string> data{"()",
  24. "(1 2.5 foo \"TEXT\" bar bar)",
  25. "(1 0.25 foobar)",
  26. "((1) (0.25) (foobar))",
  27. "((1 ()) ((0.25)) (1 . 5) (foobar))",
  28. "(\">>\\\"str\\\\ing\\\"<<\")"
  29. };
  30. for(auto const& text : data)
  31. {
  32. EXPECT_EQ(text, sexp::Parser::from_string(text).str());
  33. }
  34. }
  35. TEST(IOLocaleTest, locale_safe_output)
  36. {
  37. auto sx = sexp::Value::real(0.015625f);
  38. std::stringstream out;
  39. try {
  40. // This will fail when the locale is not installed on the system,
  41. // just ignore the test it in that case and let the test succeed.
  42. out.imbue(std::locale("de_DE.UTF-8"));
  43. } catch (std::exception const& err) {
  44. std::cerr << "warning: failed to setup locale: " << err.what() << std::endl;
  45. }
  46. out << sx;
  47. ASSERT_EQ("0.015625", out.str());
  48. }
  49. /* EOF */