real3.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*=============================================================================
  2. Copyright (c) 2001-2015 Joel de Guzman
  3. Copyright (c) 2001-2010 Hartmut Kaiser
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #include "real.hpp"
  9. int
  10. main()
  11. {
  12. using spirit_test::test;
  13. using spirit_test::test_attr;
  14. ///////////////////////////////////////////////////////////////////////////
  15. // strict real number tests
  16. ///////////////////////////////////////////////////////////////////////////
  17. {
  18. using boost::spirit::x3::real_parser;
  19. using boost::spirit::x3::parse;
  20. using boost::spirit::x3::strict_ureal_policies;
  21. using boost::spirit::x3::strict_real_policies;
  22. real_parser<double, strict_ureal_policies<double> > strict_udouble;
  23. real_parser<double, strict_real_policies<double> > strict_double;
  24. double d;
  25. BOOST_TEST(!test("1234", strict_udouble));
  26. BOOST_TEST(!test_attr("1234", strict_udouble, d));
  27. BOOST_TEST(test("1.2", strict_udouble));
  28. BOOST_TEST(test_attr("1.2", strict_udouble, d) && compare(d, 1.2));
  29. BOOST_TEST(!test("-1234", strict_double));
  30. BOOST_TEST(!test_attr("-1234", strict_double, d));
  31. BOOST_TEST(test("123.", strict_double));
  32. BOOST_TEST(test_attr("123.", strict_double, d) && compare(d, 123));
  33. BOOST_TEST(test("3.E6", strict_double));
  34. BOOST_TEST(test_attr("3.E6", strict_double, d) && compare(d, 3e6));
  35. real_parser<double, no_trailing_dot_policy<double> > notrdot_real;
  36. real_parser<double, no_leading_dot_policy<double> > nolddot_real;
  37. BOOST_TEST(!test("1234.", notrdot_real)); // Bad trailing dot
  38. BOOST_TEST(!test(".1234", nolddot_real)); // Bad leading dot
  39. }
  40. ///////////////////////////////////////////////////////////////////////////
  41. // Special thousands separated numbers
  42. ///////////////////////////////////////////////////////////////////////////
  43. {
  44. using boost::spirit::x3::real_parser;
  45. using boost::spirit::x3::parse;
  46. real_parser<double, ts_real_policies<double> > ts_real;
  47. double d;
  48. BOOST_TEST(test("123.01", ts_real));
  49. BOOST_TEST(test_attr("123.01", ts_real, d)
  50. && compare(d, 123.01));
  51. BOOST_TEST(test("123,456,789.01", ts_real));
  52. BOOST_TEST(test_attr("123,456,789.01", ts_real, d)
  53. && compare(d, 123456789.01));
  54. BOOST_TEST(test("12,345,678.90", ts_real));
  55. BOOST_TEST(test_attr("12,345,678.90", ts_real, d)
  56. && compare(d, 12345678.90));
  57. BOOST_TEST(test("1,234,567.89", ts_real));
  58. BOOST_TEST(test_attr("1,234,567.89", ts_real, d)
  59. && compare(d, 1234567.89));
  60. BOOST_TEST(!test("1234,567,890", ts_real));
  61. BOOST_TEST(!test("1,234,5678,9", ts_real));
  62. BOOST_TEST(!test("1,234,567.89e6", ts_real));
  63. BOOST_TEST(!test("1,66", ts_real));
  64. }
  65. return boost::report_errors();
  66. }