source: trunk/test/vector_test.cc @ 710

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

Addresses #1 and #80. Changed FileIO to FileUtil? and added tests on FileUtil?.

  • 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 710 2006-12-20 22:08:25Z 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  utility::FileUtil file(str);
37  if (file.access_rights(str,"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 = vec.sum();
54  shuffle(vec);
55  double sum_after = vec.sum();
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=vec_view.sum();
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
136  // Checking that badly formatted files are not accepted, or at least
137  // throws an exception for the programmer to catch.
138  bool this_ok=false;
139  // Checking that unexpected characters in a stream gives error.
140  try {
141    std::string data5("data/vector5.data");
142    check_file_access(data5);
143    std::ifstream data_stream5(data5.c_str());
144    vec=utility::vector(data_stream5); // this will give an exception
145  } catch (utility::IO_error& err) {
146    if (print)
147      std::cerr << err.what() << std::endl;
148    this_ok=true; // good, exceoption thrown, test passed
149  }
150  try {
151    std::string data("data/vector6.data");
152    check_file_access(data);
153    std::ifstream data_stream(data.c_str());
154    vec=utility::vector(data_stream); // this will give an exception
155  } catch (utility::IO_error& err) {
156    if (print)
157      std::cerr << err.what() << std::endl;
158    this_ok=true; // good, exceoption thrown, test passed
159  }
160  if (!this_ok)
161    ok=false;
162  this_ok=false;
163  try {
164    std::string data("data/vector7.data");
165    check_file_access(data);
166    std::ifstream data_stream(data.c_str());
167    vec=utility::vector(data_stream); // this will give an exception
168  } catch (utility::IO_error& err) {
169    if (print)
170      std::cerr << err.what() << std::endl;
171    this_ok=true; // good, exceoption thrown, test passed
172  }
173  if (!this_ok)
174    ok=false;
175
176  return (ok ? 0 : -1);
177}
Note: See TracBrowser for help on using the repository browser.