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