~ubuntu-branches/ubuntu/saucy/dune-common/saucy-proposed

« back to all changes in this revision

Viewing changes to dune/common/test/arraylisttest.cc

  • Committer: Package Import Robot
  • Author(s): Ansgar Burchardt
  • Date: 2012-03-17 17:15:13 UTC
  • Revision ID: package-import@ubuntu.com-20120317171513-l2eqm95mddmu2dj3
Tags: upstream-2.2~svn6573
ImportĀ upstreamĀ versionĀ 2.2~svn6573

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// $Id$
 
2
#ifdef HAVE_CONFIG_H
 
3
#include "config.h"
 
4
#endif
 
5
 
 
6
#include<dune/common/arraylist.hh>
 
7
#include<dune/common/test/iteratortest.hh>
 
8
#include<iostream>
 
9
#include<cstdlib>
 
10
#include<algorithm>
 
11
 
 
12
class Double{
 
13
public:
 
14
    double val;
 
15
    Double(): val(0){}
 
16
    Double(double d): val(d){}
 
17
    Double& operator=(double d){
 
18
        val=d;
 
19
        return *this;
 
20
    }
 
21
};
 
22
 
 
23
bool operator<(Double a, Double b){
 
24
    return a.val<b.val;
 
25
}
 
26
 
 
27
template<class T, int size>
 
28
void randomizeList(Dune::ArrayList<T,size>& alist){
 
29
    using namespace Dune;
 
30
    
 
31
    srand(300);
 
32
 
 
33
    int lowest=0, highest=1000, range=(highest-lowest)+1;
 
34
    
 
35
    for(int i=0; i < 250; i++)
 
36
        alist.push_back(T(static_cast<int>(range*(rand()/(RAND_MAX+1.0)))));
 
37
}
 
38
 
 
39
int testSorting(){
 
40
    using namespace Dune;
 
41
    ArrayList<double,10> alist;
 
42
 
 
43
    randomizeList(alist);
 
44
    std::sort(alist.begin(), alist.end());
 
45
    double last=-1;
 
46
    
 
47
    for(ArrayList<double,10>::iterator iter=alist.begin(), end=alist.end();
 
48
        iter != end; ++iter){
 
49
        if((*iter)>=last){
 
50
            last=*iter;
 
51
        }else{
 
52
            std::cerr << last<<">"<<(*iter)<<" List is not sorted! "<<__FILE__ <<":"<<__LINE__<<std::endl;
 
53
            return 1;
 
54
        }
 
55
    }
 
56
 
 
57
    return 0;
 
58
}
 
59
 
 
60
template<int size>
 
61
void initConsecutive(Dune::ArrayList<double,size>& alist){
 
62
    using namespace Dune;
 
63
 
 
64
    for(int i=0; i < 100; i++)
 
65
        alist.push_back(i);
 
66
}
 
67
 
 
68
int testIteratorRemove(){
 
69
    using namespace Dune;
 
70
    ArrayList<double,10> alist;
 
71
    initConsecutive(alist);
 
72
    ArrayList<double,10>::iterator iter=alist.begin();
 
73
 
 
74
    iter+=8;
 
75
 
 
76
    iter.eraseToHere();
 
77
    ++iter;
 
78
 
 
79
    if((*iter)!=10){
 
80
        std::cerr<<"Removing from iterator failed! "<<__FILE__<<":"<<__LINE__<<std::endl;
 
81
        return 1;
 
82
    }
 
83
 
 
84
    iter = alist.begin();
 
85
 
 
86
    if((*iter)!=9){
 
87
        std::cerr<<"Removing from iterator failed! "<<__FILE__<<":"<<__LINE__<<std::endl;
 
88
        return 1;
 
89
    }
 
90
 
 
91
    iter +=3;
 
92
    iter.eraseToHere();
 
93
    iter +=4;
 
94
 
 
95
    if((*iter)!=17){
 
96
        std::cerr<<"Removing from iterator failed! "<<__FILE__<<":"<<__LINE__<<std::endl;
 
97
        return 1;
 
98
    }
 
99
 
 
100
    alist.purge();
 
101
    if(*(alist.begin())!=13){
 
102
      std::cerr<<"Purging iterator failed! "<<__FILE__<<":"<<__LINE__<<std::endl;
 
103
      return 1;
 
104
    }
 
105
    return 0;
 
106
}
 
107
int testRandomAccess(){
 
108
    using namespace Dune;
 
109
    ArrayList<double,10> alist;
 
110
    initConsecutive(alist);
 
111
 
 
112
    ArrayList<double,10>::iterator iter=alist.begin();
 
113
 
 
114
 
 
115
    for(int i=0; i < 100; i++){
 
116
        if(iter[i]!=i){
 
117
            std::cerr << "Random Access failed: "<<iter[i]<<"!="<<i<<" "<< __FILE__ <<":"<<__LINE__<<std::endl;
 
118
            return 1;
 
119
        }
 
120
        
 
121
        if(*(iter+i)!=i){
 
122
            std::cerr << "Random Access failed "<< __FILE__ <<":"<<__LINE__<<std::endl;
 
123
            return 1;
 
124
        }
 
125
    }
 
126
    return 0;
 
127
}
 
128
 
 
129
int testComparison(){
 
130
    using namespace Dune;
 
131
    ArrayList<double,10> alist;
 
132
    initConsecutive(alist);
 
133
 
 
134
    ArrayList<double,10>::iterator iter=alist.begin(), iter1=alist.begin();
 
135
    iter1=iter+5;
 
136
    iter1=iter-5;
 
137
    iter1=iter+5;
 
138
    
 
139
 
 
140
    if(!(iter<iter1)){
 
141
        std::cerr<<*iter<<">="<<*iter1<<" Operator< seems to be wrong! "<< __FILE__ <<__LINE__<<std::endl;
 
142
        return 1;
 
143
    }
 
144
 
 
145
    if(!(iter1>iter)){
 
146
        std::cerr<<"operator> seems to be wrong! "<< __FILE__ <<__LINE__<<std::endl;
 
147
        return 1;
 
148
    }
 
149
    
 
150
    if(!(iter<=iter1)){
 
151
        std::cerr<<"operator<= seems to be wrong! "<< __FILE__ <<__LINE__<<std::endl;
 
152
        return 1;
 
153
    }
 
154
 
 
155
    if(!(iter1>=iter)){
 
156
        std::cerr<<"operator>= seems to be wrong! "<< __FILE__ <<__LINE__<<std::endl;
 
157
        return 1;
 
158
    }
 
159
 
 
160
    if(!(iter1 != iter)){
 
161
        std::cerr<<"operator!= seems to be wrong! "<< __FILE__ <<__LINE__<<std::endl;
 
162
        return 1;
 
163
    }
 
164
    
 
165
    if(!(iter1 == iter+5)){
 
166
        std::cerr<<"operator== seems to be wrong! "<< __FILE__ <<__LINE__<<std::endl;
 
167
        return 1;
 
168
    }
 
169
    return 0;
 
170
}
 
171
 
 
172
           
 
173
int main(){
 
174
    using namespace Dune;
 
175
    using namespace std;
 
176
    ArrayList<double,100> alist;
 
177
 
 
178
    randomizeList(alist);
 
179
    
 
180
    int ret=testIterator(alist);
 
181
 
 
182
    if(0!=testComparison()){
 
183
        ret++;
 
184
        cerr<< "Comparison failed!"<<endl;
 
185
    }
 
186
 
 
187
    if(0!=testRandomAccess()){
 
188
        ret++;
 
189
        cerr<< "Ransom Access failed!"<<endl;
 
190
    }
 
191
 
 
192
    if(0!=testSorting()){
 
193
        ret++;
 
194
        cerr<< "Sorting failed!"<<endl;
 
195
    }
 
196
 
 
197
    if(0!=testIteratorRemove()){
 
198
        ret++;
 
199
        cerr<< "Erasing failed!"<<endl;
 
200
    }
 
201
    return ret;
 
202
 
 
203
}