source: trunk/test/vector_test.cc @ 782

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

Fixes #195.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.7 KB
Line 
1// $Id: vector_test.cc 782 2007-03-05 21:17:31Z 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  bool print = (argc>1 && argv[1]==std::string("-p"));
46  bool ok = true;
47
48  utility::vector vec(12);
49  for (unsigned int i=0; i<vec.size(); i++) 
50    vec(i)=i;
51
52  // checking that shuffle works
53  double sum_before = utility::sum(vec);
54  shuffle(vec);
55  double sum_after = utility::sum(vec);
56  if (sum_after != sum_before)
57    ok = false;
58
59  // checking that view works
60  sum_before=0;
61  for (unsigned int i=0; i<vec.size(); i+=2) 
62    sum_before+=vec[i];
63  utility::vector vec_view(vec,0,6,2);
64  sum_after=utility::sum(vec_view);
65  if (sum_after != sum_before)
66    ok = false;
67  vec[0]=0;
68  vec_view[0]=24;
69  if (vec[0]!=vec_view[0])
70    ok=false;
71
72  // checking that copy constructor creates an independent object
73  utility::vector vec2(vec);
74  if (vec.size()!=vec2.size())
75    ok=false;
76  if (&vec2 == &vec)
77    ok=false;
78  if (vec2.isview())
79    ok=false;
80
81  // checking that copy constructor creates an independent object when
82  // copying a view
83  vec2=vec_view;
84  if (vec_view.size()!=vec2.size())
85    ok=false;
86  if ((&vec2 == &vec_view) || (&vec2 == &vec))
87    ok=false;
88  if (vec2.isview())
89    ok=false;
90  vec2[0]=0;
91  vec_view[0]=24;
92  if (vec2[0]==vec_view[0])
93    ok=false;
94
95  // checking that reading vectors from properly formatted files works
96  try {
97    std::string data1("data/vector1.data");
98    std::string data2("data/vector2.data");
99    std::string data3("data/vector3.data");
100    std::string data4("data/vector4.data");
101    check_file_access(data1);
102    check_file_access(data2);
103    check_file_access(data3);
104    check_file_access(data4);
105    std::ifstream data_stream1(data1.c_str());
106    std::ifstream data_stream2(data2.c_str());
107    std::ifstream data_stream3(data3.c_str());
108    std::ifstream data_stream4(data4.c_str());
109    vec=utility::vector(data_stream1);
110    if (vec.size()!=9)
111      ok=false;
112    vec=utility::vector(data_stream2);
113    if (vec.size()!=9)
114      ok=false;
115    vec=utility::vector(data_stream3);
116    if (vec.size()!=12)
117      ok=false;
118    vec=utility::vector(data_stream4);
119    if (vec.size()!=12)
120      ok=false;
121  } catch (utility::IO_error& err) {
122    if (print)
123      std::cerr << err.what() << std::endl;
124    ok=false;
125  }
126
127  // Checking that vector stream input operator can read whatever
128  // vector stream output operator produces.
129  std::stringstream s;
130  s << vec;
131  vec2=utility::vector(s);
132  if (!(vec==vec2))
133    ok=false;
134
135  // Checking that badly formatted files are not accepted, or at least
136  // throws an exception for the programmer to catch.
137  bool this_ok=false;
138  // Checking that unexpected characters in a stream gives error.
139  try {
140    std::string data5("data/vector5.data");
141    check_file_access(data5);
142    std::ifstream data_stream5(data5.c_str());
143    vec=utility::vector(data_stream5); // this will give 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  try {
150    std::string data("data/vector6.data");
151    check_file_access(data);
152    std::ifstream data_stream(data.c_str());
153    vec=utility::vector(data_stream); // this will give an exception
154  } catch (utility::IO_error& err) {
155    if (print)
156      std::cerr << err.what() << std::endl;
157    this_ok=true; // good, exceoption thrown, test passed
158  }
159  if (!this_ok)
160    ok=false;
161  this_ok=false;
162  try {
163    std::string data("data/vector7.data");
164    check_file_access(data);
165    std::ifstream data_stream(data.c_str());
166    vec=utility::vector(data_stream); // this will give an exception
167  } catch (utility::IO_error& err) {
168    if (print)
169      std::cerr << err.what() << std::endl;
170    this_ok=true; // good, exceoption thrown, test passed
171  }
172  if (!this_ok)
173    ok=false;
174
175  return (ok ? 0 : -1);
176}
Note: See TracBrowser for help on using the repository browser.