source: trunk/test/vector_test.cc @ 328

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

Changed vector(istream&) implementation, i.e. removed matrix dependency.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.2 KB
Line 
1// $Id: vector_test.cc 328 2005-05-30 23:04:20Z jari $
2
3#include <c++_tools/utility/FileIO.h>
4#include <c++_tools/gslapi/vector.h>
5#include <c++_tools/gslapi/utility.h>
6
7#include <fstream>
8
9using namespace theplu;
10
11void check_file_access(std::string& str)
12{
13  if (utility::FileIO().access_rights(str,"r")) {
14    std::cerr << "test_nni: Cannot access file " << str << std::endl;
15    exit(-1);
16  }
17}
18
19int main(const int argc,const char* argv[])
20{ 
21  bool ok = true;
22
23  gslapi::vector vec(12);
24  for (unsigned int i=0; i<vec.size(); i++) 
25    vec(i)=i;
26
27  // checking that shuffle works
28  double sum_before = vec.sum();
29  shuffle(vec);
30  double sum_after = vec.sum();
31  if (sum_after != sum_before)
32    ok = false;
33
34  // checking that view works
35  sum_before=0;
36  for (unsigned int i=0; i<vec.size(); i+=2) 
37    sum_before+=vec[i];
38  gslapi::vector vec_view(vec,0,6,2);
39  sum_after=vec_view.sum();
40  if (sum_after != sum_before)
41    ok = false;
42  vec[0]=0;
43  vec_view[0]=24;
44  if (vec[0]!=vec_view[0])
45    ok=false;
46
47  // checking that copy contrutor creates an independent object
48  gslapi::vector vec2(vec);
49  if (vec.size()!=vec2.size())
50    ok=false;
51  if (&vec2 == &vec)
52    ok=false;
53  if (vec2.isview())
54    ok=false;
55
56  // checking that copy contrutor creates an independent object when
57  // copying a view
58  vec2=vec_view;
59  if (vec_view.size()!=vec2.size())
60    ok=false;
61  if ((&vec2 == &vec_view) || (&vec2 == &vec))
62    ok=false;
63  if (vec2.isview())
64    ok=false;
65  vec2[0]=0;
66  vec_view[0]=24;
67  if (vec2[0]==vec_view[0])
68    ok=false;
69
70  // checking that reading vectors from file works
71  std::string data1("data/vector1.data");
72  std::string data2("data/vector2.data");
73  std::string data3("data/vector3.data");
74  std::string data4("data/vector4.data");
75  check_file_access(data1);
76  check_file_access(data2);
77  check_file_access(data3);
78  check_file_access(data4);
79  std::ifstream data_stream1(data1.c_str());
80  std::ifstream data_stream2(data2.c_str());
81  std::ifstream data_stream3(data3.c_str());
82  std::ifstream data_stream4(data4.c_str());
83  vec=gslapi::vector(data_stream1);
84  if (vec.size()!=9)
85    ok=false;
86  vec=gslapi::vector(data_stream2);
87  if (vec.size()!=9)
88    ok=false;
89  vec=gslapi::vector(data_stream3);
90  if (vec.size()!=12)
91    ok=false;
92  vec=gslapi::vector(data_stream4);
93  if (vec.size()!=12)
94    ok=false;
95
96  std::cout << "ok: " << ok << std::endl;
97
98  if (ok) 
99    return 0;
100  return -1;
101}
Note: See TracBrowser for help on using the repository browser.