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
|
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#include <dune/common/arraylist.hh>
#include <dune/common/test/iteratortest.hh>
#include <iostream>
#include <cstdlib>
#include <algorithm>
class Double {
public:
double val;
Double() : val(0){}
Double(double d) : val(d){}
Double& operator=(double d){
val=d;
return *this;
}
};
bool operator<(Double a, Double b){
return a.val<b.val;
}
template<class T, int size>
void randomizeList(Dune::ArrayList<T,size>& alist){
using namespace Dune;
srand(300);
int lowest=0, highest=1000, range=(highest-lowest)+1;
for(int i=0; i < 250; i++)
alist.push_back(T(static_cast<int>(range*(rand()/(RAND_MAX+1.0)))));
}
int testSorting(){
using namespace Dune;
ArrayList<double,10> alist;
randomizeList(alist);
std::sort(alist.begin(), alist.end());
double last=-1;
for(ArrayList<double,10>::iterator iter=alist.begin(), end=alist.end();
iter != end; ++iter) {
if((*iter)>=last) {
last=*iter;
}else{
std::cerr << last<<">"<<(*iter)<<" List is not sorted! "<<__FILE__ <<":"<<__LINE__<<std::endl;
return 1;
}
}
return 0;
}
template<int size>
void initConsecutive(Dune::ArrayList<double,size>& alist){
using namespace Dune;
for(int i=0; i < 100; i++)
alist.push_back(i);
}
int testIteratorRemove(){
using namespace Dune;
ArrayList<double,10> alist;
initConsecutive(alist);
ArrayList<double,10>::iterator iter=alist.begin();
iter+=8;
iter.eraseToHere();
++iter;
if((*iter)!=10) {
std::cerr<<"Removing from iterator failed! "<<__FILE__<<":"<<__LINE__<<std::endl;
return 1;
}
iter = alist.begin();
if((*iter)!=9) {
std::cerr<<"Removing from iterator failed! "<<__FILE__<<":"<<__LINE__<<std::endl;
return 1;
}
iter +=3;
iter.eraseToHere();
iter +=4;
if((*iter)!=17) {
std::cerr<<"Removing from iterator failed! "<<__FILE__<<":"<<__LINE__<<std::endl;
return 1;
}
alist.purge();
if(*(alist.begin())!=13) {
std::cerr<<"Purging iterator failed! "<<__FILE__<<":"<<__LINE__<<std::endl;
return 1;
}
return 0;
}
int testRandomAccess(){
using namespace Dune;
ArrayList<double,10> alist;
initConsecutive(alist);
ArrayList<double,10>::iterator iter=alist.begin();
for(int i=0; i < 100; i++) {
if(iter[i]!=i) {
std::cerr << "Random Access failed: "<<iter[i]<<"!="<<i<<" "<< __FILE__ <<":"<<__LINE__<<std::endl;
return 1;
}
if(*(iter+i)!=i) {
std::cerr << "Random Access failed "<< __FILE__ <<":"<<__LINE__<<std::endl;
return 1;
}
}
return 0;
}
int testComparison(){
using namespace Dune;
ArrayList<double,10> alist;
initConsecutive(alist);
ArrayList<double,10>::iterator iter=alist.begin(), iter1=alist.begin();
iter1=iter+5;
iter1=iter-5;
iter1=iter+5;
if(!(iter<iter1)) {
std::cerr<<*iter<<">="<<*iter1<<" Operator< seems to be wrong! "<< __FILE__ <<__LINE__<<std::endl;
return 1;
}
if(!(iter1>iter)) {
std::cerr<<"operator> seems to be wrong! "<< __FILE__ <<__LINE__<<std::endl;
return 1;
}
if(!(iter<=iter1)) {
std::cerr<<"operator<= seems to be wrong! "<< __FILE__ <<__LINE__<<std::endl;
return 1;
}
if(!(iter1>=iter)) {
std::cerr<<"operator>= seems to be wrong! "<< __FILE__ <<__LINE__<<std::endl;
return 1;
}
if(!(iter1 != iter)) {
std::cerr<<"operator!= seems to be wrong! "<< __FILE__ <<__LINE__<<std::endl;
return 1;
}
if(!(iter1 == iter+5)) {
std::cerr<<"operator== seems to be wrong! "<< __FILE__ <<__LINE__<<std::endl;
return 1;
}
return 0;
}
int main(){
using namespace Dune;
using namespace std;
ArrayList<double,100> alist;
randomizeList(alist);
int ret=testIterator(alist);
if(0!=testComparison()) {
ret++;
cerr<< "Comparison failed!"<<endl;
}
if(0!=testRandomAccess()) {
ret++;
cerr<< "Random Access failed!"<<endl;
}
if(0!=testSorting()) {
ret++;
cerr<< "Sorting failed!"<<endl;
}
if(0!=testIteratorRemove()) {
ret++;
cerr<< "Erasing failed!"<<endl;
}
return ret;
}
|