generate.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Boost.Range library
  2. //
  3. // Copyright Neil Groves 2009. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. //
  9. // For more information, see http://www.boost.org/libs/range/
  10. //
  11. #include <boost/range/algorithm/generate.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/assign.hpp>
  15. #include <boost/bind.hpp>
  16. #include <algorithm>
  17. #include <functional>
  18. #include <list>
  19. #include <numeric>
  20. #include <deque>
  21. #include <vector>
  22. namespace boost
  23. {
  24. namespace
  25. {
  26. class generator_fn
  27. {
  28. public:
  29. typedef int result_type;
  30. generator_fn() : m_count(0) {}
  31. int operator()() { return ++m_count; }
  32. private:
  33. int m_count;
  34. };
  35. template< class Container >
  36. void test_generate_impl(Container& cont)
  37. {
  38. Container reference(cont);
  39. std::generate(reference.begin(), reference.end(), generator_fn());
  40. Container test(cont);
  41. boost::generate(test, generator_fn());
  42. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  43. test.begin(), test.end() );
  44. Container test2(cont);
  45. boost::generate(boost::make_iterator_range(test2), generator_fn());
  46. BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(),
  47. test2.begin(), test2.end() );
  48. }
  49. template< class Container >
  50. void test_generate_impl()
  51. {
  52. using namespace boost::assign;
  53. Container cont;
  54. test_generate_impl(cont);
  55. cont.clear();
  56. cont += 9;
  57. test_generate_impl(cont);
  58. cont.clear();
  59. cont += 9,8,7,6,5,4,3,2,1;
  60. test_generate_impl(cont);
  61. }
  62. void test_generate()
  63. {
  64. test_generate_impl< std::vector<int> >();
  65. test_generate_impl< std::list<int> >();
  66. test_generate_impl< std::deque<int> >();
  67. }
  68. }
  69. }
  70. boost::unit_test::test_suite*
  71. init_unit_test_suite(int argc, char* argv[])
  72. {
  73. boost::unit_test::test_suite* test
  74. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.generate" );
  75. test->add( BOOST_TEST_CASE( &boost::test_generate ) );
  76. return test;
  77. }