/usr/include/dune/common/test/iteratortest.hh is in libdune-common-dev 2.3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// $Id$
#ifndef DUNE_ITERATORTEST_HH
#define DUNE_ITERATORTEST_HH
#include <iostream>
#include <algorithm>
#include <dune/common/typetraits.hh>
#include <dune/common/unused.hh>
/**
* @brief Tests the capabilities of a forward iterator.
* @param begin Iterator positioned at the start.
* @param end Iterator positioned at the end.
* @param opt Functor for doing whatever one wants.
*/
template<class Iter, class Opt>
int testForwardIterator(Iter begin, Iter end, Opt& opt)
{
//std::cout<< "forward: ";
Iter tmp=begin, tmp1(begin);
int ret=0;
if(tmp!=begin || tmp1!=begin || tmp!=tmp1) {
std::cerr<<" Copying iterator failed "<<__FILE__<<":"<<__LINE__<<std::endl;
ret=1;
}
for(; begin!=end; ++begin)
opt(*begin);
//std::cout<< " OK "<<std::endl;
return ret;
}
/**
* @brief Tests the capabilities of a bidirectional iterator.
*
* Namely it test wether random positions can be reached from
* each directions.
*
* @param begin Iterator positioned at the stsrt.
* @param end Iterator positioned at the end.
* @param opt Functor for doing whatever one wants.
*/
template<class Iter, class Opt>
int testBidirectionalIterator(Iter begin, Iter end, Opt opt)
{
testForwardIterator(begin, end, opt);
Iter tbegin=--begin;
Iter tend=--end;
for(; tbegin!=tend; --tend)
opt(*tend);
typename Iter::difference_type size = std::distance(begin, end);
srand(300);
int no= (size>10) ? 10 : size;
for(int i=0; i < no; i++)
{
int index = static_cast<int>(size*(rand()/(RAND_MAX+1.0)));
int backwards=size-index;
tbegin=begin;
tend=end;
for(int j=0; j < index; j++) ++tbegin;
for(int j=0; j < backwards; j++) --tend;
if(tbegin != tend)
{
std::cerr<<"Did not reach same index by starting forward from "
<<"begin and backwards from end."<<std::endl;
return 1;
}
}
return 0;
}
template<class Iter, class Opt>
int testRandomAccessIterator(Iter begin, Iter end, Opt opt){
int ret=testBidirectionalIterator(begin, end, opt);
typename Iter::difference_type size = end-begin;
srand(300);
int no= (size>10) ? 10 : size;
for(int i=0; i < no; i++)
{
int index = static_cast<int>(size*(rand()/(RAND_MAX+1.0)));
opt(begin[index]);
}
// Test the less than operator
if(begin != end &&!( begin<end))
{
std::cerr<<"! (begin()<end())"<<std::endl;
ret++;
}
if(begin != end) {
if(begin-end >= 0) {
std::cerr<<"begin!=end, but begin-end >= 0!"<<std::endl;
ret++;
}
if(end-begin <= 0) {
std::cerr<<"begin!=end, but end-begin <= 0!"<<std::endl;
ret++;
}
}
for(int i=0; i < no; i++)
{
int index = static_cast<int>(size*(rand()/(RAND_MAX+1.0)));
Iter rand(begin), test(begin), res;
rand+=index;
if((res=begin+index) != rand)
{
std::cerr << " i+n should have the result i+=n, where i is the "
<<"iterator and n is the difference type!" <<std::endl;
ret++;
}
for(int i=0; i< index; i++) ++test;
if(test != rand)
{
std::cerr << "i+=n should have the same result as applying the"
<< "increment ooperator n times!"<< std::cerr;
ret++;
}
rand=end, test=end;
rand-=index;
if((end-index) != rand)
{
std::cerr << " i-n should have the result i-=n, where i is the "
<<"iterator and n is the difference type!" <<std::endl;
ret++;
}
for(int i=0; i< index; i++) --test;
if(test != rand)
{
std::cerr << "i+=n should have the same result as applying the"
<< "increment ooperator n times!"<< std::cerr;
ret++;
}
}
for(int i=0; i < no; i++)
{
Iter iter1 = begin+static_cast<int>(size*(rand()/(RAND_MAX+1.0)));
Iter iter2 = begin+static_cast<int>(size*(rand()/(RAND_MAX+1.0)));
typename Iter::difference_type diff = iter2 -iter1;
if((iter1+diff)!=iter2) {
std::cerr<< "i+(j-i) = j should hold, where i,j are iterators!"<<std::endl;
ret++;
}
}
return ret;
}
template<class Iter, class Opt, typename iterator_category>
int testIterator(Iter& begin, Iter& end, Opt& opt, iterator_category cat);
template<class Iter, class Opt>
int testIterator(Iter& begin, Iter& end, Opt& opt, std::forward_iterator_tag)
{
return testForwardIterator(begin, end, opt);
}
template<class Iter, class Opt>
int testIterator(Iter& begin, Iter& end, Opt& opt, std::bidirectional_iterator_tag)
{
return testBidirectionalIterator(begin, end, opt);
}
template<class Iter, class Opt>
int testIterator(Iter& begin, Iter& end, Opt& opt, std::random_access_iterator_tag)
{
// std::cout << "Testing iterator ";
int ret = testRandomAccessIterator(begin, end, opt);
//std::cout<<std::endl;
return ret;
}
template<class Iter, class Opt>
int testConstIterator(Iter& begin, Iter& end, Opt& opt)
{
//std::cout << "Testing constant iterator: ";
int ret=testIterator(begin, end, opt, typename std::iterator_traits<Iter>::iterator_category());
//std::cout<<std::endl;
return ret;
}
template<bool>
struct TestSorting
{
template<class Container, typename IteratorTag>
static void testSorting(Container&, IteratorTag)
{}
template<class Container>
static void testSorting(Container& c, std::random_access_iterator_tag)
{
std::sort(c.begin(), c.end());
}
}
;
template<>
struct TestSorting<false>
{
template<class Container>
static void testSorting(Container& c, std::random_access_iterator_tag)
{}
template<class Container, typename IteratorTag>
static void testSorting(Container& c, IteratorTag tag)
{}
};
template<class Container, class Opt, bool testSort>
int testIterator(Container& c, Opt& opt)
{
typename Container::iterator begin=c.begin(), end=c.end();
typename Container::const_iterator cbegin(begin);
typename Container::const_iterator cbegin1 DUNE_UNUSED = begin;
typename Container::const_iterator cend=c.end();
int ret = 0;
TestSorting<testSort>::testSorting(c, typename std::iterator_traits<typename Container::iterator>::iterator_category());
if(end!=cend || cend!=end)
{
std::cerr<<"constant and mutable iterators should be equal!"<<std::endl;
ret=1;
}
ret += testConstIterator(cbegin, cend, opt);
if(testSort)
ret += testIterator(begin,end,opt);
return ret;
}
template<class Container, class Opt>
int testIterator(Container& c, Opt& opt)
{
return testIterator<Container,Opt,true>(c,opt);
}
template<class Iter, class Opt>
void testAssignment(Iter begin, Iter end, Opt&)
{
//std::cout << "Assignment: ";
for(; begin!=end; begin++)
*begin=typename std::iterator_traits<Iter>::value_type();
//std::cout<<" Done."<< std::endl;
}
template<class Iter, class Opt>
int testIterator(Iter& begin, Iter& end, Opt& opt)
{
testAssignment(begin, end, opt);
return testConstIterator(begin, end, opt);
}
template<class T>
class Printer {
typename Dune::remove_const<T>::type res;
public:
Printer() : res(0){}
void operator()(const T& t){
res+=t;
// std::cout << t <<" ";
}
};
template<class Container, class Opt>
int testIterator(const Container& c, Opt& opt)
{
typename Container::const_iterator begin=c.begin(), end=c.end();
return testConstIterator(begin,end, opt);
}
template<class Container>
int testIterator(Container& c)
{
Printer<typename std::iterator_traits<typename Container::iterator>::value_type> print;
return testIterator(c,print);
}
#endif
|