source: trunk/test/vector_test.cc @ 341

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

Added exceptions for tracing file format problems when reading vectors.
Changed implentation for reading ints and doubles into stl vectors, now
unexpected characters are skipped instead of treated as zeros.
Some other minor things.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.9 KB
Line 
1// $Id: vector_test.cc 341 2005-06-07 14:41:25Z jari $
2
3#include <c++_tools/utility/Exception.h>
4#include <c++_tools/utility/FileIO.h>
5#include <c++_tools/gslapi/vector.h>
6#include <c++_tools/gslapi/utility.h>
7
8#include <fstream>
9#include <sstream>
10
11using namespace theplu;
12
13void check_file_access(std::string& str)
14{
15  if (utility::FileIO().access_rights(str,"r")) {
16    std::cerr << "test_nni: Cannot access file " << str << std::endl;
17    exit(-1);
18  }
19}
20
21int main(const int argc,const char* argv[])
22{ 
23  bool print = (argc>1 && argv[1]==std::string("-p"));
24  bool ok = true;
25
26  gslapi::vector vec(12);
27  for (unsigned int i=0; i<vec.size(); i++) 
28    vec(i)=i;
29
30  // checking that shuffle works
31  double sum_before = vec.sum();
32  shuffle(vec);
33  double sum_after = vec.sum();
34  if (sum_after != sum_before)
35    ok = false;
36
37  // checking that view works
38  sum_before=0;
39  for (unsigned int i=0; i<vec.size(); i+=2) 
40    sum_before+=vec[i];
41  gslapi::vector vec_view(vec,0,6,2);
42  sum_after=vec_view.sum();
43  if (sum_after != sum_before)
44    ok = false;
45  vec[0]=0;
46  vec_view[0]=24;
47  if (vec[0]!=vec_view[0])
48    ok=false;
49
50  // checking that copy contrutor creates an independent object
51  gslapi::vector vec2(vec);
52  if (vec.size()!=vec2.size())
53    ok=false;
54  if (&vec2 == &vec)
55    ok=false;
56  if (vec2.isview())
57    ok=false;
58
59  // checking that copy contrutor creates an independent object when
60  // copying a view
61  vec2=vec_view;
62  if (vec_view.size()!=vec2.size())
63    ok=false;
64  if ((&vec2 == &vec_view) || (&vec2 == &vec))
65    ok=false;
66  if (vec2.isview())
67    ok=false;
68  vec2[0]=0;
69  vec_view[0]=24;
70  if (vec2[0]==vec_view[0])
71    ok=false;
72
73  // checking that reading vectors from properly formatted files works
74  try {
75    std::string data1("data/vector1.data");
76    std::string data2("data/vector2.data");
77    std::string data3("data/vector3.data");
78    std::string data4("data/vector4.data");
79    check_file_access(data1);
80    check_file_access(data2);
81    check_file_access(data3);
82    check_file_access(data4);
83    std::ifstream data_stream1(data1.c_str());
84    std::ifstream data_stream2(data2.c_str());
85    std::ifstream data_stream3(data3.c_str());
86    std::ifstream data_stream4(data4.c_str());
87    vec=gslapi::vector(data_stream1);
88    if (vec.size()!=9)
89      ok=false;
90    vec=gslapi::vector(data_stream2);
91    if (vec.size()!=9)
92      ok=false;
93    vec=gslapi::vector(data_stream3);
94    if (vec.size()!=12)
95      ok=false;
96    vec=gslapi::vector(data_stream4);
97    if (vec.size()!=12)
98      ok=false;
99  } catch (utility::IO_error& err) {
100    if (print)
101      std::cerr << err.what() << std::endl;
102    ok=false;
103  }
104
105  // Checking that vector stream input operator can read whatever
106  // vector stream output operator produces.
107  std::stringstream s;
108  s << vec;
109  vec2=gslapi::vector(s);
110  if (!(vec==vec2))
111    ok=false;
112
113  // Checking that unexpected characters in a stream are skipped. When
114  // file read is reimplemented to resemble C/C++ style this should
115  // check something else or maybe nothing at all.
116  std::string data5("data/vector5.data");
117  check_file_access(data5);
118  std::ifstream data_stream5(data5.c_str());
119  vec=gslapi::vector(data_stream5);
120  if (vec.size()!=8)
121    ok=false;
122
123  // Checking that badly formatted files are not accepted, or at least
124  // throws an exception for the programmer to catch.
125  bool this_ok=false;
126  try {
127    std::string data("data/vector6.data");
128    check_file_access(data);
129    std::ifstream data_stream(data.c_str());
130    vec=gslapi::vector(data_stream); // this will gove an exception
131  } catch (utility::IO_error& err) {
132    if (print)
133      std::cerr << err.what() << std::endl;
134    this_ok=true; // good, exceoption thrown, test passed
135  }
136  if (!this_ok)
137    ok=false;
138  this_ok=false;
139  try {
140    std::string data("data/vector7.data");
141    check_file_access(data);
142    std::ifstream data_stream(data.c_str());
143    vec=gslapi::vector(data_stream); // this will gove an exception
144  } catch (utility::IO_error& err) {
145    if (print)
146      std::cerr << err.what() << std::endl;
147    this_ok=true; // good, exceoption thrown, test passed
148  }
149  if (!this_ok)
150    ok=false;
151
152  return (ok ? 0 : -1);
153}
Note: See TracBrowser for help on using the repository browser.