source: trunk/test/vector_test.cc @ 789

Last change on this file since 789 was 789, checked in by Jari Häkkinen, 17 years ago

Addresses #193. vector now works as outlined here. Added some
functionality. Added a clone function that facilitates resizing of
vectors. clone is needed since assignement operator functionality is
changed.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 KB
Line 
1// $Id: vector_test.cc 789 2007-03-10 20:07:13Z jari $
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 << argv[0] << " -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  {
82    *message << "const view implementation" << std::endl;
83    const utility::vector vv(10,3.0);
84    utility::vector vview(vv,0,5,1);
85    // const utility::vector vview(vv,0,5,1); // this is the proper line
86    utility::vector vv2(5,2.0);
87    vv2.mul(vview); // should work even without const since const arg passing
88    vv2.div(vview); // should work even without const since const arg passing
89    ok &= (vview*vview == 3.0*3.0*vview.size());
90  }
91
92  // checking that copy constructor creates an independent object when
93  // a non-view vector is copied
94  {
95    *message << "copy constructor" << std::endl;
96    utility::vector vec2(vec);
97    ok &= (vec.size()==vec2.size());
98    ok &= (vec2==vec);
99    ok &= (&vec2 != &vec);
100    ok &= !vec2.isview();
101  }
102
103  // checking that copy constructor creates an independent object when
104  // a view vector is copied
105  {
106    *message << "copy contructor on view" << std::endl;
107    utility::vector vec3(vec_view);
108    ok &= (vec_view.size()==vec3.size());
109    ok &= (vec3 == vec_view);
110    ok &= (&vec3 != &vec_view);
111    ok &= !vec3.isview();
112  }
113
114  // checking that assignment operator throws an exception if vectors
115  // differ in size
116  {
117    *message << "assignment operator" << std::endl;
118    bool exception_happens=false;
119    try {
120      utility::vector v(vec_view.size()+1,0.0);
121      v=vec_view;
122    } catch (utility::GSL_error& err) {
123      exception_happens=true;
124    }
125    if (!exception_happens) {
126      *message << "Vector assignment operator did not throw expected exception"
127               << std::endl;
128      ok=false;
129    }
130  }
131
132  // checking that assignment operator changes the underlying object when
133  // a view is changed.
134  {
135    *message << "assignment operator on view" << std::endl;
136    vec[3]=vec[4]=vec[5]=13;
137    utility::vector vec_view(vec,3,3,1);
138    utility::vector vec2(3,123.0);
139    vec_view=vec2;
140    if (vec[3]!=vec_view[0] || vec[4]!=vec_view[1] || vec[5]!=vec_view[2])
141      ok=false;
142  }
143
144  // checking clone functionality
145  {
146    *message << "clone functionality" << std::endl;
147    bool this_ok=true;
148    *message << "\tcloning normal vector" << std::endl;
149    utility::vector vec2(3,123.0);
150    vec2.clone(vec);
151    if (vec.size()!=vec2.size())
152      this_ok=false;
153    else
154      for (unsigned int i=0; i<vec.size(); ++i)
155        if (vec(i)!=vec2(i))
156          this_ok=false;
157    if (vec.gsl_vector_p()==vec2.gsl_vector_p())
158      this_ok=false;
159    *message << "\tcloning vector view" << std::endl;
160    utility::vector* vec_view=new utility::vector(vec,3,3,1);
161    utility::vector vec_view2;
162    vec_view2.clone(*vec_view);
163    if (!vec_view2.isview())
164      this_ok=false;
165    if (vec_view->size()!=vec_view2.size())
166      this_ok=false;
167    else
168      for (u_int i=0; i<vec_view2.size(); ++i)
169        if ((*vec_view)(i)!=vec_view2(i))
170          this_ok=false;
171    *message << "\tcloned vector view independence" << std::endl;
172    delete vec_view;
173    if (vec[3]!=vec_view2[0] || vec[4]!=vec_view2[1] || vec[5]!=vec_view2[2])
174      this_ok=false;
175
176    if (!this_ok) {
177      *message << "FAIL: clone test" << std::endl;
178      ok=false;
179    }
180  }
181
182  // checking that reading vectors from properly formatted files works
183  try {
184    *message << "stream" << std::endl;
185    std::string data1("data/vector1.data");
186    std::string data2("data/vector2.data");
187    std::string data3("data/vector3.data");
188    std::string data4("data/vector4.data");
189    check_file_access(data1);
190    check_file_access(data2);
191    check_file_access(data3);
192    check_file_access(data4);
193    std::ifstream data_stream1(data1.c_str());
194    std::ifstream data_stream2(data2.c_str());
195    std::ifstream data_stream3(data3.c_str());
196    std::ifstream data_stream4(data4.c_str());
197    utility::vector vec1(data_stream1);
198    ok &= (vec1.size()==9);
199    vec1=utility::vector(data_stream2);
200    ok &= (vec1.size()==9);
201    utility::vector vec2(data_stream3);
202    ok &= (vec2.size()==12);
203    vec2=utility::vector(data_stream4);
204    ok &= (vec2.size()==12);
205  } catch (utility::IO_error& err) {
206    *message << err.what() << std::endl;
207    ok=false;
208  }
209
210  // Checking that vector stream input operator can read whatever
211  // vector stream output operator produces.
212  {
213    *message << "checking that output stream is valid as an input stream"
214             << std::endl;
215    std::stringstream s;
216    s << vec;
217    utility::vector vec2(s);
218    ok &= (vec==vec2);
219  }
220
221  // Checking that badly formatted files are not accepted, or at least
222  // throws an exception for the programmer to catch.
223  *message << "checking that bad stream are rejected" << std::endl;
224  bool this_ok=false;
225  // Checking that unexpected characters in a stream gives error.
226  try {
227    std::string data5("data/vector5.data");
228    check_file_access(data5);
229    std::ifstream data_stream5(data5.c_str());
230    utility::vector dummy(data_stream5); // this will give an exception
231  } catch (utility::IO_error& err) {
232    *message << err.what() << std::endl;
233    this_ok=true; // good, exceoption thrown, test passed
234  }
235  try {
236    std::string data("data/vector6.data");
237    check_file_access(data);
238    std::ifstream data_stream(data.c_str());
239    utility::vector dummy(data_stream); // this will give an exception
240  } catch (utility::IO_error& err) {
241    *message << err.what() << std::endl;
242    this_ok=true; // good, exceoption thrown, test passed
243  }
244  ok &= this_ok;
245
246  this_ok=false;
247  try {
248    std::string data("data/vector7.data");
249    check_file_access(data);
250    std::ifstream data_stream(data.c_str());
251    vec=utility::vector(data_stream); // this will give an exception
252  } catch (utility::IO_error& err) {
253    *message << err.what() << std::endl;
254    this_ok=true; // good, exceoption thrown, test passed
255  }
256  ok &= this_ok;
257
258  if (!ok)
259    *message << "vector test failed" << std::endl;
260
261  return (ok ? 0 : -1);
262}
Note: See TracBrowser for help on using the repository browser.