PageRenderTime 55ms CodeModel.GetById 15ms app.highlight 27ms RepoModel.GetById 8ms app.codeStats 1ms

/Src/Dependencies/Boost/libs/iterator/test/indirect_iterator_test.cpp

http://hadesmem.googlecode.com/
C++ | 221 lines | 146 code | 57 blank | 18 comment | 22 complexity | b7ad629efaa9a2e0532d499906ab3393 MD5 | raw file
  1//  (C) Copyright Jeremy Siek 1999.
  2// Distributed under the Boost Software License, Version 1.0. (See
  3// accompanying file LICENSE_1_0.txt or copy at
  4// http://www.boost.org/LICENSE_1_0.txt)
  5
  6//  Revision History
  7//  22 Nov 2002 Thomas Witt
  8//       Added interoperability check.
  9//  08 Mar 2001   Jeremy Siek
 10//       Moved test of indirect iterator into its own file. It to
 11//       to be in iterator_adaptor_test.cpp.
 12
 13#include <boost/config.hpp>
 14#include <iostream>
 15#include <algorithm>
 16
 17#include <boost/iterator/indirect_iterator.hpp>
 18#include <boost/iterator/iterator_concepts.hpp>
 19#include <boost/iterator/new_iterator_tests.hpp>
 20
 21#include <boost/detail/workaround.hpp>
 22
 23#include <boost/concept_archetype.hpp>
 24#include <boost/concept_check.hpp>
 25#include <boost/shared_ptr.hpp>
 26#include <boost/utility.hpp>
 27
 28#include <boost/mpl/has_xxx.hpp>
 29
 30#include <boost/type_traits/broken_compiler_spec.hpp>
 31
 32#include <boost/detail/lightweight_test.hpp>
 33
 34#include <vector>
 35#include <stdlib.h>
 36#include <set>
 37
 38#if !defined(__SGI_STL_PORT)                            \
 39    && (defined(BOOST_MSVC_STD_ITERATOR)                \
 40        || BOOST_WORKAROUND(_CPPLIB_VER, <= 310)        \
 41        || BOOST_WORKAROUND(__GNUC__, <= 2))
 42
 43// std container random-access iterators don't support mutable/const
 44// interoperability (but may support const/mutable interop).
 45# define NO_MUTABLE_CONST_STD_SET_ITERATOR_INTEROPERABILITY
 46
 47#endif 
 48
 49
 50template <class T> struct see_type;
 51template <int I> struct see_val;
 52
 53struct my_iterator_tag : public std::random_access_iterator_tag { };
 54
 55using boost::dummyT;
 56BOOST_TT_BROKEN_COMPILER_SPEC(boost::shared_ptr<dummyT>)
 57    
 58typedef std::vector<int> storage;
 59typedef std::vector<int*> pointer_ra_container;
 60typedef std::set<storage::iterator> iterator_set;
 61
 62template <class Container>
 63struct indirect_iterator_pair_generator
 64{
 65    typedef boost::indirect_iterator<typename Container::iterator> iterator;
 66    
 67    typedef boost::indirect_iterator<
 68        typename Container::iterator
 69      , typename iterator::value_type const
 70    > const_iterator;
 71};
 72
 73void more_indirect_iterator_tests()
 74{
 75    storage store(1000);
 76    std::generate(store.begin(), store.end(), rand);
 77    
 78    pointer_ra_container ptr_ra_container;
 79    iterator_set iter_set;
 80
 81    for (storage::iterator p = store.begin(); p != store.end(); ++p)
 82    {
 83        ptr_ra_container.push_back(&*p);
 84        iter_set.insert(p);
 85    }
 86
 87    typedef indirect_iterator_pair_generator<pointer_ra_container> indirect_ra_container;
 88
 89    indirect_ra_container::iterator db(ptr_ra_container.begin());
 90    indirect_ra_container::iterator de(ptr_ra_container.end());
 91    BOOST_TEST(static_cast<std::size_t>(de - db) == store.size());
 92    BOOST_TEST(db + store.size() == de);
 93    indirect_ra_container::const_iterator dci = db;
 94    
 95    BOOST_TEST(dci == db);
 96    
 97#ifndef NO_MUTABLE_CONST_RA_ITERATOR_INTEROPERABILITY
 98    BOOST_TEST(db == dci);
 99#endif
100    
101    BOOST_TEST(dci != de);
102    BOOST_TEST(dci < de);
103    BOOST_TEST(dci <= de);
104    
105#ifndef NO_MUTABLE_CONST_RA_ITERATOR_INTEROPERABILITY
106    BOOST_TEST(de >= dci);
107    BOOST_TEST(de > dci);
108#endif
109    
110    dci = de;
111    BOOST_TEST(dci == de);
112
113    boost::random_access_iterator_test(db + 1, store.size() - 1, boost::next(store.begin()));
114    
115    *db = 999;
116    BOOST_TEST(store.front() == 999);
117
118    // Borland C++ is getting very confused about the typedefs here
119    typedef boost::indirect_iterator<iterator_set::iterator> indirect_set_iterator;
120    typedef boost::indirect_iterator<
121        iterator_set::iterator
122      , iterator_set::iterator::value_type const
123    > const_indirect_set_iterator;
124
125    indirect_set_iterator sb(iter_set.begin());
126    indirect_set_iterator se(iter_set.end());
127    const_indirect_set_iterator sci(iter_set.begin());
128    BOOST_TEST(sci == sb);
129    
130# ifndef NO_MUTABLE_CONST_STD_SET_ITERATOR_INTEROPERABILITY
131    BOOST_TEST(se != sci);
132# endif
133    
134    BOOST_TEST(sci != se);
135    sci = se;
136    BOOST_TEST(sci == se);
137    
138    *boost::prior(se) = 888;
139    BOOST_TEST(store.back() == 888);
140    BOOST_TEST(std::equal(sb, se, store.begin()));
141
142    boost::bidirectional_iterator_test(boost::next(sb), store[1], store[2]);
143    BOOST_TEST(std::equal(db, de, store.begin()));
144}
145
146// element_type detector; defaults to true so the test passes when
147// has_xxx isn't implemented
148BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(has_element_type, element_type, true)
149    
150int
151main()
152{
153  dummyT array[] = { dummyT(0), dummyT(1), dummyT(2), 
154                     dummyT(3), dummyT(4), dummyT(5) };
155  const int N = sizeof(array)/sizeof(dummyT);
156
157# if BOOST_WORKAROUND(BOOST_MSVC, == 1200)
158  boost::shared_ptr<dummyT> zz((dummyT*)0);  // Why? I don't know, but it suppresses a bad instantiation.
159# endif
160  
161  typedef std::vector<boost::shared_ptr<dummyT> > shared_t;
162  shared_t shared;
163  
164  // Concept checks
165  {
166    typedef boost::indirect_iterator<shared_t::iterator> iter_t;
167
168    BOOST_STATIC_ASSERT(
169        has_element_type<
170            boost::detail::iterator_traits<shared_t::iterator>::value_type
171        >::value
172        );
173    
174    typedef boost::indirect_iterator<
175        shared_t::iterator
176      , boost::iterator_value<shared_t::iterator>::type const
177    > c_iter_t;
178
179# ifndef NO_MUTABLE_CONST_RA_ITERATOR_INTEROPERABILITY
180    boost::function_requires< boost_concepts::InteroperableIteratorConcept<iter_t, c_iter_t> >();
181# endif 
182  }
183
184  // Test indirect_iterator_generator
185  {
186      for (int jj = 0; jj < N; ++jj)
187          shared.push_back(boost::shared_ptr<dummyT>(new dummyT(jj)));
188      
189      dummyT* ptr[N];
190      for (int k = 0; k < N; ++k)
191          ptr[k] = array + k;
192
193      typedef boost::indirect_iterator<dummyT**> indirect_iterator;
194
195      typedef boost::indirect_iterator<dummyT**, dummyT const>
196          const_indirect_iterator;
197
198      indirect_iterator i(ptr);
199      boost::random_access_iterator_test(i, N, array);
200
201      boost::random_access_iterator_test(
202          boost::indirect_iterator<shared_t::iterator>(shared.begin())
203          , N, array);
204
205      boost::random_access_iterator_test(boost::make_indirect_iterator(ptr), N, array);
206    
207      // check operator->
208      assert((*i).m_x == i->foo());
209
210      const_indirect_iterator j(ptr);
211      boost::random_access_iterator_test(j, N, array);
212    
213      dummyT const*const* const_ptr = ptr;
214      boost::random_access_iterator_test(boost::make_indirect_iterator(const_ptr), N, array);
215      
216      boost::const_nonconst_iterator_test(i, ++j);
217
218      more_indirect_iterator_tests();
219  }
220  return boost::report_errors();
221}