primitives_tests.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*=============================================================================
  2. Copyright (c) 2001-2007 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #include <iostream>
  7. #include <string>
  8. #include <boost/phoenix/core/argument.hpp>
  9. #include <boost/phoenix/core/reference.hpp>
  10. #include <boost/phoenix/core/value.hpp>
  11. #include <boost/detail/lightweight_test.hpp>
  12. using boost::phoenix::cref;
  13. using boost::phoenix::ref;
  14. using boost::phoenix::val;
  15. int
  16. main()
  17. {
  18. #ifndef BOOST_PHOENIX_NO_PREDEFINED_TERMINALS
  19. using boost::phoenix::arg_names::_1;
  20. using boost::phoenix::arg_names::arg1;
  21. using boost::phoenix::arg_names::arg2;
  22. #else
  23. boost::phoenix::arg_names::_1_type _1;
  24. boost::phoenix::arg_names::arg1_type arg1;
  25. boost::phoenix::arg_names::arg2_type arg2;
  26. #endif
  27. char c1 = '1';
  28. int i1 = 1, i2 = 2, i = 4;
  29. const char* s2 = "2";
  30. ///////////////////////////////////////////////////////////////////////////
  31. //
  32. // Values, references and arguments
  33. //
  34. ///////////////////////////////////////////////////////////////////////////
  35. // argument
  36. BOOST_TEST(arg1(c1) == c1);
  37. BOOST_TEST(arg1(i1, i2) == i1);
  38. BOOST_TEST(arg2(i1, s2) == s2);
  39. BOOST_TEST(&(arg1(c1)) == &c1); // must be an lvalue
  40. // value
  41. val(' ')();
  42. val(" ")();
  43. std::cout << val("Hello,")() << val(' ')() << val("World")() << std::endl;
  44. BOOST_TEST(val(3)() == 3);
  45. BOOST_TEST(val("Hello, world")() == std::string("Hello, world"));
  46. BOOST_TEST(val(_1)(i1) == i1);
  47. // should not compile:
  48. #ifdef PHOENIX_SHOULD_NOT_COMPILE_TEST
  49. &val(_1)(i1); // should return an rvalue
  50. #endif
  51. // reference
  52. BOOST_TEST(cref(i)() == ref(i)());
  53. BOOST_TEST(cref(i)() == 4);
  54. BOOST_TEST(i == 4);
  55. BOOST_TEST(ref(++i)() == 5);
  56. BOOST_TEST(i == 5);
  57. // should not compile:
  58. #ifdef PHOENIX_SHOULD_NOT_COMPILE_TEST
  59. ref(arg1);
  60. #endif
  61. // testing consts
  62. int const ic = 123;
  63. BOOST_TEST(arg1(ic) == 123);
  64. // should not compile:
  65. #ifdef PHOENIX_SHOULD_NOT_COMPILE_TEST
  66. arg1();
  67. #endif
  68. return boost::report_errors();
  69. }