31
|
1 |
// Copyright 2002 The Trustees of Indiana University.
|
|
2 |
|
|
3 |
// Use, modification and distribution is subject to the Boost Software
|
|
4 |
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
|
5 |
// http://www.boost.org/LICENSE_1_0.txt)
|
|
6 |
|
|
7 |
// Boost.MultiArray Library
|
|
8 |
// Authors: Ronald Garcia
|
|
9 |
// Jeremy Siek
|
|
10 |
// Andrew Lumsdaine
|
|
11 |
// See http://www.boost.org/libs/multi_array for documentation.
|
|
12 |
|
|
13 |
//
|
|
14 |
// iterators.cpp - checking out iterator stuffs.
|
|
15 |
// The tests assume that the array has shape 2x3x4
|
|
16 |
//
|
|
17 |
/*
|
|
18 |
* © Portions copyright (c) 2006-2007 Nokia Corporation. All rights reserved.
|
|
19 |
*/
|
|
20 |
|
|
21 |
#define MULTIARRAY_TEST_ASSIGN
|
|
22 |
#include "generative_tests.hpp"
|
|
23 |
#include "boost/concept_check.hpp" // for ignore_unused_variable_warning
|
|
24 |
#include "boost/mpl/if.hpp"
|
|
25 |
#include "boost/type_traits/is_same.hpp"
|
|
26 |
|
|
27 |
// iterator-test-specific code
|
|
28 |
|
|
29 |
template <typename Array>
|
|
30 |
void assign_if_not_const(Array& A, const mutable_array_tag&) {
|
|
31 |
|
|
32 |
typedef typename Array::iterator iterator3;
|
|
33 |
typedef typename Array::template subarray<2>::type::iterator iterator2;
|
|
34 |
typedef typename Array::template subarray<1>::type::iterator iterator1;
|
|
35 |
|
|
36 |
int num = 0;
|
|
37 |
for (iterator3 i = A.begin(); i != A.end(); ++i)
|
|
38 |
for(iterator2 ii = (*i).begin(); ii != (*i).end(); ++ii)
|
|
39 |
for(iterator1 iii = (*ii).begin(); iii != (*ii).end(); ++iii)
|
|
40 |
*iii = num++;
|
|
41 |
}
|
|
42 |
|
|
43 |
|
|
44 |
template <typename Array>
|
|
45 |
struct ittraits_const {
|
|
46 |
typedef typename Array::const_iterator iterator3;
|
|
47 |
typedef typename boost::subarray_gen<Array,2>::type::const_iterator
|
|
48 |
iterator2;
|
|
49 |
typedef typename boost::subarray_gen<Array,1>::type::const_iterator
|
|
50 |
iterator1;
|
|
51 |
|
|
52 |
typedef typename Array::const_reverse_iterator riterator3;
|
|
53 |
typedef typename boost::subarray_gen<Array,2>::type::const_reverse_iterator
|
|
54 |
riterator2;
|
|
55 |
typedef typename boost::subarray_gen<Array,1>::type::const_reverse_iterator
|
|
56 |
riterator1;
|
|
57 |
};
|
|
58 |
|
|
59 |
template <typename Array>
|
|
60 |
struct ittraits_mutable {
|
|
61 |
typedef typename Array::iterator iterator3;
|
|
62 |
typedef typename boost::subarray_gen<Array,2>::type::iterator iterator2;
|
|
63 |
typedef typename boost::subarray_gen<Array,1>::type::iterator iterator1;
|
|
64 |
|
|
65 |
typedef typename Array::reverse_iterator riterator3;
|
|
66 |
typedef typename boost::subarray_gen<Array,2>::type::reverse_iterator
|
|
67 |
riterator2;
|
|
68 |
typedef typename boost::subarray_gen<Array,1>::type::reverse_iterator
|
|
69 |
riterator1;
|
|
70 |
};
|
|
71 |
|
|
72 |
|
|
73 |
// Meta-program chooses ittraits implementation.
|
|
74 |
template <typename Array, typename ConstTag>
|
|
75 |
struct ittraits_generator :
|
|
76 |
boost::mpl::if_< boost::is_same<ConstTag,const_array_tag>,
|
|
77 |
ittraits_const<Array>,
|
|
78 |
ittraits_mutable<Array> >
|
|
79 |
{};
|
|
80 |
|
|
81 |
|
|
82 |
template <typename Array>
|
|
83 |
void construct_iterators(Array&) {
|
|
84 |
|
|
85 |
// Default constructed iterators and
|
|
86 |
// const iterators constructed from iterators.
|
|
87 |
{
|
|
88 |
typename Array::iterator i1;
|
|
89 |
// typename Array::const_iterator ci1;
|
|
90 |
typename Array::reverse_iterator r1;
|
|
91 |
typename Array::const_reverse_iterator cr1;
|
|
92 |
|
|
93 |
#if 0 // RG - MSVC fails to compile these
|
|
94 |
typename Array::const_iterator ci2 =
|
|
95 |
typename Array::iterator();
|
|
96 |
typename Array::const_reverse_iterator cr2 =
|
|
97 |
typename Array::reverse_iterator();
|
|
98 |
#endif
|
|
99 |
typename Array::const_iterator ci2 = i1;
|
|
100 |
typename Array::const_reverse_iterator cr2 = cr1;
|
|
101 |
boost::ignore_unused_variable_warning(cr2);
|
|
102 |
}
|
|
103 |
}
|
|
104 |
|
|
105 |
template <typename Array, typename IterTraits>
|
|
106 |
void test_iterators(Array& A, const IterTraits&) {
|
|
107 |
|
|
108 |
// Iterator comparison and arithmetic
|
|
109 |
{
|
|
110 |
typedef typename IterTraits::iterator3 iterator;
|
|
111 |
iterator i1 = A.begin();
|
|
112 |
iterator i2 = A.end();
|
|
113 |
BOOST_CHECK(i1 < i2);
|
|
114 |
BOOST_CHECK((i2 - i1) == typename iterator::difference_type(2));
|
|
115 |
}
|
|
116 |
|
|
117 |
// Standard Array Iteration
|
|
118 |
{
|
|
119 |
typedef typename IterTraits::iterator3 iterator3;
|
|
120 |
typedef typename IterTraits::iterator2 iterator2;
|
|
121 |
typedef typename IterTraits::iterator1 iterator1;
|
|
122 |
|
|
123 |
int vals = 0;
|
|
124 |
for (iterator3 i = A.begin(); i != A.end(); ++i)
|
|
125 |
for(iterator2 ii = (*i).begin(); ii != (*i).end(); ++ii)
|
|
126 |
for(iterator1 iii = (*ii).begin(); iii != (*ii).end(); ++iii)
|
|
127 |
BOOST_CHECK(*iii == vals++);
|
|
128 |
}
|
|
129 |
|
|
130 |
// Using operator->() on iterators
|
|
131 |
{
|
|
132 |
typedef typename IterTraits::iterator3 iterator3;
|
|
133 |
typedef typename IterTraits::iterator2 iterator2;
|
|
134 |
typedef typename IterTraits::iterator1 iterator1;
|
|
135 |
|
|
136 |
int vals = 0;
|
|
137 |
for (iterator3 i = A.begin(); i != A.end(); ++i)
|
|
138 |
for(iterator2 ii = i->begin(); ii != i->end(); ++ii)
|
|
139 |
for(iterator1 iii = ii->begin(); iii != ii->end(); ++iii)
|
|
140 |
BOOST_CHECK(*iii == vals++);
|
|
141 |
}
|
|
142 |
|
|
143 |
// Reverse Iterator Hierarchy Test
|
|
144 |
{
|
|
145 |
typedef typename IterTraits::riterator3 riterator3;
|
|
146 |
typedef typename IterTraits::riterator2 riterator2;
|
|
147 |
typedef typename IterTraits::riterator1 riterator1;
|
|
148 |
|
|
149 |
int check_iter_val = A.num_elements()-1;
|
|
150 |
for (riterator3 i = A.rbegin(); i != (riterator3)A.rend(); ++i)
|
|
151 |
for(riterator2 ii = (*i).rbegin(); ii != (riterator2)(*i).rend(); ++ii)
|
|
152 |
for(riterator1 iii = (*ii).rbegin(); iii != (riterator1)(*ii).rend();
|
|
153 |
++iii)
|
|
154 |
BOOST_CHECK(*iii == check_iter_val--);
|
|
155 |
}
|
|
156 |
++tests_run;
|
|
157 |
}
|
|
158 |
|
|
159 |
|
|
160 |
template <typename Array>
|
|
161 |
void access(Array& A, const mutable_array_tag&) {
|
|
162 |
assign(A);
|
|
163 |
|
|
164 |
construct_iterators(A);
|
|
165 |
|
|
166 |
typedef typename ittraits_generator<Array,mutable_array_tag>::type
|
|
167 |
m_iter_traits;
|
|
168 |
|
|
169 |
typedef typename ittraits_generator<Array,const_array_tag>::type
|
|
170 |
c_iter_traits;
|
|
171 |
test_iterators(A,m_iter_traits());
|
|
172 |
test_iterators(A,c_iter_traits());
|
|
173 |
|
|
174 |
const Array& CA = A;
|
|
175 |
test_iterators(CA,c_iter_traits());
|
|
176 |
}
|
|
177 |
|
|
178 |
template <typename Array>
|
|
179 |
void access(Array& A, const const_array_tag&) {
|
|
180 |
construct_iterators(A);
|
|
181 |
typedef typename ittraits_generator<Array,const_array_tag>::type
|
|
182 |
c_iter_traits;
|
|
183 |
test_iterators(A,c_iter_traits());
|
|
184 |
}
|
|
185 |
|
|
186 |
|
|
187 |
int
|
|
188 |
test_main(int, char*[])
|
|
189 |
{
|
|
190 |
#ifdef __SYMBIAN32__
|
|
191 |
if(boost::minimal_test::errors_counter() != 0)
|
|
192 |
assert_failed = true;
|
|
193 |
testResultXml("iterators");
|
|
194 |
close_log_file();
|
|
195 |
#endif
|
|
196 |
return run_generative_tests();
|
|
197 |
}
|