source: branches/peter-dev/test/vector_test.cc @ 1008

Last change on this file since 1008 was 1008, checked in by Peter, 15 years ago

fixing tests for new vector design

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 KB
Line 
1// $Id: vector_test.cc 1008 2008-02-01 03:49:13Z peter $
2
3/*
4  Copyright (C) 2004 Peter Johansson
5  Copyright (C) 2005 Jari Häkkinen, Markus Ringnér, Peter Johansson
6  Copyright (C) 2006 Jari Häkkinen
7  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
8
9  This file is part of the yat library, http://trac.thep.lu.se/trac/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 2 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "yat/utility/Exception.h"
28#include "yat/utility/FileUtil.h"
29#include "yat/utility/utility.h"
30#include "yat/utility/vector.h"
31#include "yat/utility/vectorView.h"
32
33#include <fstream>
34#include <sstream>
35
36using namespace theplu::yat;
37
38
39void check_file_access(std::string& str)
40{
41 
42  if (utility::FileUtil(str).permissions("r")) {
43    std::cerr << "test_nni: Cannot access file " << str << std::endl;
44    exit(-1);
45  }
46}
47
48int main(const int argc,const char* argv[])
49{ 
50  std::ostream* message;
51  if (argc>1 && argv[1]==std::string("-v"))
52    message = &std::cerr;
53  else {
54    message = new std::ofstream("/dev/null");
55    if (argc>1)
56      std::cout << "vector_test -v : for printing extra "  << "information\n";
57  }
58  *message << "testing vector" << std::endl;
59  bool ok = true;
60
61  utility::vector vec(12);
62  for (size_t i=0; i<vec.size(); i++) 
63    vec(i)=i;
64
65  // checking that shuffle works
66  *message << "shuffle" << std::endl;
67  double sum_before = utility::sum(vec);
68  shuffle(vec);
69  double sum_after = utility::sum(vec);
70  ok &= (sum_after==sum_before);
71
72  // checking that view works
73  *message << "view" << std::endl;
74  sum_before=0;
75  for (size_t i=0; i<vec.size(); i+=2) 
76    sum_before+=vec[i];
77  utility::vectorView vec_view(vec,0,6,2);
78  sum_after=utility::sum(vec_view);
79  ok &= (sum_after==sum_before);
80  vec[0]=0;
81  vec_view[0]=24;
82  ok &= (vec[0]==vec_view[0]);
83
84  // Test of const view implementation (make sure no zero pointer is
85  // used). Here we use the bad style of not making the view const!
86  // If test fails with a null pointer exception it is an uncatchable
87  // core dump!
88  {
89    *message << "const view implementation" << std::endl;
90    const utility::vector vv(10,3.0);
91    utility::vectorView vview(vv,0,5,1);
92    // const utility::vector vview(vv,0,5,1); // this is the proper line
93    utility::vector vv2(5,2.0);
94    vv2.mul(vview); // should work even without const since const arg passing
95    vv2.div(vview); // should work even without const since const arg passing
96    *message << "end view" << std::endl;
97    ok &= (vview*vview == 3.0*3.0*vview.size());
98    *message << "end view" << std::endl;
99  }
100  return 0;
101
102  *message << "end view" << std::endl;
103  // checking that copy constructor creates an independent object when
104  // a non-view vector is copied
105  {
106    *message << "copy constructor" << std::endl;
107    utility::vector vec2(vec);
108    ok &= (vec.size()==vec2.size());
109    ok &= (vec2==vec);
110    ok &= (&vec2 != &vec);
111    ok &= !vec2.isview();
112  }
113
114  // checking that copy constructor creates an independent object when
115  // a view vector is copied
116  {
117    *message << "copy contructor on view" << std::endl;
118    utility::vector vec3(vec_view);
119    ok &= (vec_view.size()==vec3.size());
120    ok &= (vec3 == vec_view);
121    ok &= !vec3.isview();
122  }
123
124  /* different sizes are allowed in new design
125  // checking that assignment operator throws an exception if vectors
126  // differ in size
127  {
128    *message << "assignment operator" << std::endl;
129    // GSL will catch the error in this test there for the GSL error
130    // handling must be disabled until after the exception is
131    // catched. The GSL error handler is reinstated after the
132    // try-catch construct.
133    gsl_error_handler_t* err_handler=gsl_set_error_handler_off();
134    bool exception_happens=false;
135    try {
136      utility::vector v(vec_view.size()+1,0.0);
137      v=vec_view;
138    } catch (utility::GSL_error& err) {
139      exception_happens=true;
140    }
141    if (!exception_happens) {
142      *message << "Vector assignment operator did not throw expected exception"
143               << std::endl;
144      ok=false;
145    }
146    gsl_set_error_handler(err_handler);
147  }
148  */
149
150  // checking that assignment operator changes the underlying object when
151  // a view is changed.
152  {
153    *message << "assignment operator on view" << std::endl;
154    vec[3]=vec[4]=vec[5]=13;
155    utility::vectorView vec_view(vec,3,3,1);
156    utility::vector vec2(3,123.0);
157    vec_view=vec2;
158    if (vec[3]!=vec_view[0] || vec[4]!=vec_view[1] || vec[5]!=vec_view[2]){
159      *message << " failed\n";
160      ok=false;
161    }
162  }
163
164  // checking clone functionality
165  {
166    *message << "clone functionality" << std::endl;
167    bool this_ok=true;
168    *message << "\tcloning normal vector" << std::endl;
169    utility::vector vec2(3,123.0);
170    vec2 = vec;
171    if (vec.size()!=vec2.size())
172      this_ok=false;
173    else
174      for (size_t i=0; i<vec.size(); ++i)
175        if (vec(i)!=vec2(i))
176          this_ok=false;
177    if (vec.gsl_vector_p()==vec2.gsl_vector_p())
178      this_ok=false;
179    *message << "\tcloning vector view" << std::endl;
180    utility::vectorView* vec_view=new utility::vectorView(vec,3,3,1);
181    utility::vectorView vec_view2(*vec_view);
182    if (!vec_view2.isview())
183      this_ok=false;
184    if (vec_view->size()!=vec_view2.size())
185      this_ok=false;
186    else
187      for (u_int i=0; i<vec_view2.size(); ++i)
188        if ((*vec_view)(i)!=vec_view2(i))
189          this_ok=false;
190    *message << "\tcloned vector view independence" << std::endl;
191    delete vec_view;
192    if (vec[3]!=vec_view2[0] || vec[4]!=vec_view2[1] || vec[5]!=vec_view2[2])
193      this_ok=false;
194
195    if (!this_ok) {
196      *message << "FAIL: clone test" << std::endl;
197      ok=false;
198    }
199  }
200
201  // checking that reading vectors from properly formatted files works
202  try {
203    *message << "stream" << std::endl;
204    std::string data1("data/vector1.data");
205    std::string data2("data/vector2.data");
206    std::string data3("data/vector3.data");
207    std::string data4("data/vector4.data");
208    check_file_access(data1);
209    check_file_access(data2);
210    check_file_access(data3);
211    check_file_access(data4);
212    std::ifstream data_stream1(data1.c_str());
213    std::ifstream data_stream2(data2.c_str());
214    std::ifstream data_stream3(data3.c_str());
215    std::ifstream data_stream4(data4.c_str());
216    utility::vector vec1(data_stream1);
217    ok &= (vec1.size()==9);
218    vec1=utility::vector(data_stream2);
219    ok &= (vec1.size()==9);
220    utility::vector vec2(data_stream3);
221    ok &= (vec2.size()==12);
222    vec2=utility::vector(data_stream4);
223    ok &= (vec2.size()==12);
224  } catch (utility::IO_error& err) {
225    *message << err.what() << std::endl;
226    ok=false;
227  }
228
229  // Checking that vector stream input operator can read whatever
230  // vector stream output operator produces.
231  {
232    *message << "checking that output stream is valid as an input stream"
233             << std::endl;
234    std::stringstream s;
235    s << vec;
236    utility::vector vec2(s);
237    ok &= (vec==vec2);
238  }
239
240  // Checking that badly formatted files are not accepted, or at least
241  // throws an exception for the programmer to catch.
242  *message << "checking that bad stream are rejected" << std::endl;
243  bool this_ok=false;
244  // Checking that unexpected characters in a stream gives error.
245  try {
246    std::string data5("data/vector5.data");
247    check_file_access(data5);
248    std::ifstream data_stream5(data5.c_str());
249    utility::vector dummy(data_stream5); // this will give an exception
250  } catch (utility::IO_error& err) {
251    *message << err.what() << std::endl;
252    this_ok=true; // good, exception thrown, test passed
253  }
254  try {
255    std::string data("data/vector6.data");
256    check_file_access(data);
257    std::ifstream data_stream(data.c_str());
258    utility::vector dummy(data_stream); // this will give an exception
259  } catch (utility::IO_error& err) {
260    *message << err.what() << std::endl;
261    this_ok=true; // good, exceoption thrown, test passed
262  }
263  ok &= this_ok;
264
265  this_ok=false;
266  try {
267    std::string data("data/vector7.data");
268    check_file_access(data);
269    std::ifstream data_stream(data.c_str());
270    vec=utility::vector(data_stream); // this will give an exception
271  } catch (utility::IO_error& err) {
272    *message << err.what() << std::endl;
273    this_ok=true; // good, exceoption thrown, test passed
274  }
275  ok &= this_ok;
276
277
278  // Test sort algorithms
279  {
280    std::string data3("data/vector3.data");
281    std::ifstream data_stream3(data3.c_str());
282    utility::vector vec3(data_stream3);
283    std::vector<size_t> dummy;
284    dummy.push_back(100); // To make sure it works starting with a non-empty vector
285
286    utility::sort_index(dummy,vec3);
287    if(dummy.size()!=vec3.size()) {
288      ok=false;
289      *message << "Vector of sorted indices has incorrect size" << std::endl;
290    }
291    if(dummy[0]!=11 || dummy[dummy.size()-1]!=8) {
292      ok=false;
293      *message << "Vector of sorted indices incorrect" << std::endl;
294    }
295    size_t k=3;
296
297    utility::sort_smallest_index(dummy,k,vec3);
298    if(dummy.size()!=k) {
299      ok=false;
300      *message << "Vector of sorted smallest indices has incorrect size" << std::endl;
301    }
302    if(dummy[0]!=11 || dummy[dummy.size()-1]!=9) {
303      ok=false;
304      *message << "Vector of sorted smallest indices incorrect" << std::endl;
305    }
306
307    k=4;
308    utility::sort_largest_index(dummy,k,vec3);
309    if(dummy.size()!=k) {
310      ok=false;
311      *message << "Vector of sorted largest indices has incorrect size" << std::endl;
312    }
313    if(dummy[0]!=8 || dummy[dummy.size()-1]!=5) {
314      ok=false;
315      *message << "Vector of sorted largest indices incorrect" << std::endl;
316    }
317  }
318
319  if (!ok)
320    *message << "vector test failed" << std::endl;
321
322  if (message!=&std::cerr)
323    delete message;
324
325  return (ok ? 0 : -1);
326}
Note: See TracBrowser for help on using the repository browser.