fixed_size_queue_tests.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*=============================================================================
  2. Copyright (c) 2001-2003 Daniel Nuffer
  3. Copyright (c) 2003 Hartmut Kaiser
  4. http://spirit.sourceforge.net/
  5. Use, modification and distribution is subject to the Boost Software
  6. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. http://www.boost.org/LICENSE_1_0.txt)
  8. =============================================================================*/
  9. #include <boost/spirit/include/classic_fixed_size_queue.hpp>
  10. #include <boost/mpl/assert.hpp>
  11. #include <boost/type_traits/is_same.hpp>
  12. #include <boost/concept_check.hpp>
  13. #include <boost/detail/lightweight_test.hpp>
  14. #include <iostream>
  15. typedef BOOST_SPIRIT_CLASSIC_NS::fixed_size_queue<int, 5> queue_t;
  16. typedef queue_t::iterator iter_t;
  17. typedef queue_t::const_iterator const_iter_t;
  18. BOOST_CLASS_REQUIRE(const_iter_t, boost, RandomAccessIteratorConcept);
  19. // Right now, the iterator is not a full compliant MutableRandomAccessIterator
  20. // because operator[] does not return a reference. This seems a problem in
  21. // boost::iterator_adaptors. Anyway, this feature is not used in multi_pass
  22. // iterator, and this class is not really meant for public use yet.
  23. BOOST_CLASS_REQUIRE(iter_t, boost, RandomAccessIteratorConcept);
  24. int main(int, char**)
  25. {
  26. queue_t q;
  27. const queue_t& cq = q;
  28. q.push_back(1);
  29. q.push_back(2);
  30. q.push_back(3);
  31. q.push_back(4);
  32. BOOST_TEST(q.front() == 1);
  33. q.pop_front();
  34. BOOST_TEST(q.front() == 2);
  35. q.pop_front();
  36. BOOST_TEST(q.front() == 3);
  37. q.pop_front();
  38. BOOST_TEST(q.front() == 4);
  39. q.pop_front();
  40. q.push_back(5);
  41. q.push_back(6);
  42. q.push_back(7);
  43. q.push_back(8);
  44. BOOST_TEST(q.front() == 5);
  45. q.pop_front();
  46. BOOST_TEST(q.front() == 6);
  47. q.pop_front();
  48. BOOST_TEST(q.front() == 7);
  49. q.pop_front();
  50. BOOST_TEST(q.front() == 8);
  51. q.pop_front();
  52. q.push_front(5);
  53. q.push_front(4);
  54. q.push_front(3);
  55. q.push_front(2);
  56. q.push_front(1);
  57. // NOTE: Iterator tests are not exhaustive and they are not meant to be so.
  58. // Check iterator
  59. iter_t b = q.begin();
  60. BOOST_TEST(*b++ == 1);
  61. BOOST_TEST(*b++ == 2);
  62. BOOST_TEST(*b++ == 3);
  63. BOOST_TEST(*b++ == 4);
  64. BOOST_TEST(*b++ == 5);
  65. BOOST_TEST(b == q.end());
  66. BOOST_TEST(*--b == 5);
  67. BOOST_TEST(*--b == 4);
  68. BOOST_TEST(*--b == 3);
  69. BOOST_TEST(*--b == 2);
  70. BOOST_TEST(*--b == 1);
  71. BOOST_TEST(b == q.begin());
  72. // Check const_iterator
  73. const_iter_t c = cq.begin();
  74. BOOST_TEST(*c++ == 1);
  75. BOOST_TEST(*c++ == 2);
  76. BOOST_TEST(*c++ == 3);
  77. BOOST_TEST(*c++ == 4);
  78. BOOST_TEST(*c++ == 5);
  79. BOOST_TEST(c == cq.end());
  80. BOOST_TEST(*--c == 5);
  81. BOOST_TEST(*--c == 4);
  82. BOOST_TEST(*--c == 3);
  83. BOOST_TEST(*--c == 2);
  84. BOOST_TEST(*--c == 1);
  85. BOOST_TEST(c == cq.begin());
  86. #if 0
  87. // Conforming compilers aren't able to compile this code for the new iterator
  88. // adaptors.
  89. // The problem here is, that the old fixed_size_queue code wasn't a complete
  90. // and 'clean' iterator implementation, some of the required iterator concepts
  91. // were missing. It was never meant to be exposed outside the multi_pass. So I
  92. // haven't added any features while porting. The #ifdef'ed tests expose the
  93. // code weaknesses ((un-)fortunately only on conformant compilers, with a quite
  94. // good STL implementation). The simplest way to solve this issue was to switch
  95. // of the tests for these compilers then.
  96. // $$$ This is isolated in fixed_size_queue_fail_tests.cpp [JDG 11-5-2003] $$$
  97. // Iterators are random access.
  98. BOOST_MPL_ASSERT(( boost::is_same<
  99. iter_t::iterator_category,
  100. std::random_access_iterator_tag > ));
  101. BOOST_MPL_ASSERT(( boost::is_same<
  102. const_iter_t::iterator_category,
  103. std::random_access_iterator_tag > ));
  104. // Check comparisons and interoperations (we are comparing
  105. // const and non-const iterators)
  106. BOOST_TEST(c == b);
  107. BOOST_TEST(c+4 > b);
  108. BOOST_TEST(c < b+4);
  109. #endif
  110. // Check that you can actually modify the queue with an iterator
  111. *b = 123;
  112. BOOST_TEST(*c == 123);
  113. // Check random access
  114. BOOST_TEST(*((c+4)-4) == 123);
  115. BOOST_TEST(*((c-4)+4) == 123);
  116. return boost::report_errors();
  117. }