source: trunk/test/vector_test.cc @ 795

Last change on this file since 795 was 795, checked in by Peter, 16 years ago

confusing to use argv[0] since it is nout test/vector_test but rather test/.libs/vector_test. Some magic automake stuff :)

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