stl_tests.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 <vector>
  10. #include <list>
  11. #include <algorithm>
  12. #include <boost/detail/lightweight_test.hpp>
  13. #define PHOENIX_LIMIT 15
  14. #include <boost/spirit/include/phoenix1_primitives.hpp>
  15. #include <boost/spirit/include/phoenix1_composite.hpp>
  16. #include <boost/spirit/include/phoenix1_functions.hpp>
  17. #include <boost/spirit/include/phoenix1_operators.hpp>
  18. #include <boost/spirit/include/phoenix1_binders.hpp>
  19. #include <boost/spirit/include/phoenix1_special_ops.hpp>
  20. using namespace phoenix;
  21. using namespace std;
  22. ///////////////////////////////////////////////////////////////////////////////
  23. struct print_ { // a typical STL style monomorphic functor
  24. typedef void result_type;
  25. void operator()(int n0) { cout << "got 1 arg " << n0 << " \n"; }
  26. };
  27. functor<print_> print = print_();
  28. ///////////////////////////////////////////////////////////////////////////////
  29. int
  30. main()
  31. {
  32. ///////////////////////////////////////////////////////////////////////////////
  33. //
  34. // STL algorithms
  35. //
  36. ///////////////////////////////////////////////////////////////////////////////
  37. vector<int> v;
  38. v.push_back(1);
  39. v.push_back(2);
  40. v.push_back(3);
  41. v.push_back(4);
  42. v.push_back(5);
  43. for_each(v.begin(), v.end(), arg1 *= 2);
  44. for (int m = 0; m < 5; ++m, (cout << ','))
  45. {
  46. cout << v[m];
  47. BOOST_TEST(v[m] == (m+1)*2);
  48. }
  49. cout << endl;
  50. for_each(v.begin(), v.end(), print(arg1));
  51. vector<int>::iterator it = find_if(v.begin(), v.end(), arg1 > 5);
  52. if (it != v.end())
  53. cout << *it << endl;
  54. ///////////////////////////////////////////////////////////////////////////////
  55. //
  56. // STL iterators and containers
  57. //
  58. ///////////////////////////////////////////////////////////////////////////////
  59. BOOST_TEST((arg1[0])(v) == v[0]);
  60. BOOST_TEST((arg1[1])(v) == v[1]);
  61. list<int> l;
  62. l.push_back(1);
  63. l.push_back(2);
  64. l.push_back(3);
  65. l.push_back(4);
  66. l.push_back(5);
  67. list<int>::iterator first = l.begin();
  68. list<int>::iterator last = l.end();
  69. BOOST_TEST((*(++arg1))(first) == 2);
  70. BOOST_TEST((*(----arg1))(last) == 4);
  71. ///////////////////////////////////////////////////////////////////////////////
  72. //
  73. // End asserts
  74. //
  75. ///////////////////////////////////////////////////////////////////////////////
  76. return boost::report_errors();
  77. }