source: trunk/test/view_test.cc @ 1137

Last change on this file since 1137 was 1137, checked in by Peter, 14 years ago

refs #327 for utility::matrix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1// $Id: view_test.cc 1137 2008-02-24 00:51:58Z peter $
2
3/*
4  Copyright (C) 2008 Peter Johansson, Markus Ringnér
5
6  This file is part of the yat library, http://trac.thep.lu.se/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/Matrix.h"
25#include "yat/utility/VectorBase.h"
26#include "yat/utility/VectorMutable.h"
27#include "yat/utility/VectorView.h"
28#include "yat/utility/VectorConstView.h"
29
30#include <fstream>
31#include <iostream>
32#include <string>
33
34int main(const int argc,const char* argv[])
35{ 
36  using namespace theplu::yat::utility;
37  std::ostream* message;
38  if (argc>1 && argv[1]==std::string("-v"))
39    message = &std::cerr;
40  else {
41    message = new std::ofstream("/dev/null");
42    if (argc>1)
43      std::cout << "vector_test -v : for printing extra "  << "information\n";
44  }
45  *message << "testing vector" << std::endl;
46  bool ok = true;
47
48  Matrix data1;
49  data1.resize(3,1);
50  for(size_t i=0;i<3;i++) {
51    data1(i,0)=7+i;
52  }
53 
54
55  Matrix data2(3,1,1.0);
56
57  VectorConstView a=data1.column_const_view(0);
58  VectorView b=data2.column_view(0);
59  VectorView bajs(data2, 0, false);
60  if (a(0)!=data1(0,0)){
61    *message << "Matrix::column_const_view failed\n";
62    ok =false;
63  }
64  if (b(0)!=data2(0,0)){
65    *message << "Matrix::column_view failed\n";
66    ok =false;
67  }
68
69  data1.column_const_view(0);
70  if (b.size()!=3){
71    ok =false;
72    *message << "ERROR: expected size to be 3 - found " << b.size() << std::endl;
73  }
74  if ( !(b(0)==1 && b(1)==(1) && b(2)==1) )
75    ok = false;
76
77  if (a.begin()!=data1.begin_column(0)){
78    ok=false;
79    *message << "VectorConstView::begin should equal " 
80             << "Matrix::begin_column(size_t)\n";
81  }
82  if (a.end()!=data1.end_column(0)) {
83    ok=false;
84    *message << "VectorConstView::end should equal " 
85             << "Matrix::end_column(size_t)\n";
86  }
87  if (b.begin()!=data2.begin_column(0)){
88    ok=false;
89    *message << "VectorView::begin " 
90             << b.begin().base() << " should equal\n"
91             << "Matrix::begin_column(size_t) "
92             << data1.begin_column(0).base() << std::endl;
93  }
94  if (b.end()!=data2.end_column(0)){
95    ok=false;
96    *message << "VectorView::end should equal " 
97             << "Matrix::end_column(size_t)\n";
98  }
99  VectorConstView c=data1.row_const_view(0);
100  VectorView d=data2.row_view(0);
101  if (c.begin()!=data1.begin_row(0)) {
102    ok=false;
103    *message << "VectorConstView::begin should equal " 
104             << "Matrix::begin_row(size_t)\n";
105  }
106  if (c.end()!=data1.end_row(0)){
107    ok=false;
108    *message << "VectorConstView::end should equal " 
109             << "Matrix::end_row(size_t)\n";
110  }
111  if (d.begin()!=data2.begin_row(0)){
112    ok=false;
113    *message << "VectorView::begin should equal " 
114             << "Matrix::begin_row(size_t)\n";
115  }
116  if (d.end()!=data2.end_row(0)){
117    ok=false;
118    *message << "VectorView::end should equal " 
119             << "Matrix::end_row(size_t)\n";
120  }
121
122
123  // test that VectorConstView created from VectorView is independent
124  // of VectorView
125  {
126    Vector vec(10,1.2);
127    VectorView* vv = new VectorView(vec);
128    VectorConstView vcv(*vv);
129    delete vv;
130    if (vcv.size()!=10)
131      ok = false;
132    if (vcv(0)!=1.2)
133      ok = false;
134  }
135 
136
137  if (ok)
138    *message << "test is ok" << std::endl;
139  else
140    *message << "view test failed" << std::endl;
141
142  if (message!=&std::cerr)
143    delete message;
144
145  return (ok ? 0 : -1);
146}
147
148
Note: See TracBrowser for help on using the repository browser.