more_expressions_tests.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*=============================================================================
  2. Phoenix V1.2.1
  3. Copyright (c) 2001-2003 Joel de Guzman
  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 <iostream>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #define PHOENIX_LIMIT 15
  11. #include <boost/spirit/include/phoenix1_primitives.hpp>
  12. #include <boost/spirit/include/phoenix1_composite.hpp>
  13. #include <boost/spirit/include/phoenix1_functions.hpp>
  14. #include <boost/spirit/include/phoenix1_operators.hpp>
  15. using namespace phoenix;
  16. using namespace std;
  17. ///////////////////////////////////////////////////////////////////////////////
  18. struct sqr_ {
  19. template <typename ArgT>
  20. struct result { typedef ArgT type; };
  21. template <typename ArgT>
  22. ArgT operator()(ArgT n) const { return n * n; }
  23. };
  24. function<sqr_> sqr;
  25. ///////////////////////////////////////////////////////////////////////////////
  26. struct adder_ {
  27. template <typename Arg1T, typename Arg2T, typename ArgT3>
  28. struct result { typedef Arg1T type; };
  29. template <typename Arg1T, typename Arg2T, typename ArgT3>
  30. Arg1T operator()(Arg1T a, Arg2T b, ArgT3 c) const { return a + b + c; }
  31. };
  32. function<adder_> adder;
  33. ///////////////////////////////////////////////////////////////////////////////
  34. int
  35. main()
  36. {
  37. int i2 = 2, i = 4, i50 = 50, i10 = 10, i20 = 20, i100 = 100;
  38. double d5 = 5, d10 = 10;
  39. string hello = "hello";
  40. ///////////////////////////////////////////////////////////////////////////////
  41. //
  42. // More complex expressions
  43. //
  44. ///////////////////////////////////////////////////////////////////////////////
  45. BOOST_TEST((10 - arg1)(i100) == (10 - i100));
  46. BOOST_TEST((20 - arg1)(i100) == (20 - i100));
  47. BOOST_TEST((arg1 - 10)(i100) == (i100 - 10));
  48. BOOST_TEST((arg1 - 20)(i100) == (i100 - 20));
  49. BOOST_TEST((arg1 - arg2)(i100, i50) == (i100 - i50));
  50. BOOST_TEST((arg1 - var(i))(i10) == (i10 - i));
  51. BOOST_TEST((arg1 + arg2 - arg3)(i100, i50, i20) == (i100 + i50 - i20));
  52. BOOST_TEST((sqr(arg1) + arg2 - arg3)(i100, i50, i20) == ((i100*i100) + i50 - i20));
  53. int ii = i;
  54. BOOST_TEST((var(i) += arg1)(i2) == (ii += i2));
  55. BOOST_TEST((sqr(sqr(arg1)))(i100) == (i100*i100*i100*i100));
  56. #if 0 /*** SHOULD NOT COMPILE ***/
  57. (val(3) += arg1)(i100);
  58. (val(3) = 3)();
  59. #endif
  60. BOOST_TEST(((adder(arg1, arg2, arg3) + arg2 - arg3)(i100, i50, i20)) == (i100 + i50 + i20) + i50 - i20);
  61. BOOST_TEST((adder(arg1, arg2, arg3)(i100, i50, i20)) == (i100 + i50 + i20));
  62. BOOST_TEST((sqr(sqr(sqr(sqr(arg1)))))(d10) == 1e16);
  63. BOOST_TEST((sqr(sqr(arg1)) / arg1 / arg1)(d5) == 25);
  64. for (int j = 0; j < 20; ++j)
  65. {
  66. cout << (10 < arg1)(j);
  67. BOOST_TEST((10 < arg1)(j) == (10 < j));
  68. }
  69. cout << endl;
  70. for (int k = 0; k < 20; ++k)
  71. {
  72. bool r = ((arg1 % 2 == 0) && (arg1 < 15))(k);
  73. cout << r;
  74. BOOST_TEST(r == ((k % 2 == 0) && (k < 15)));
  75. }
  76. cout << endl;
  77. ///////////////////////////////////////////////////////////////////////////////
  78. //
  79. // End asserts
  80. //
  81. ///////////////////////////////////////////////////////////////////////////////
  82. return boost::report_errors();
  83. }