source: trunk/test/view_test.cc @ 1720

Last change on this file since 1720 was 1720, checked in by Peter, 12 years ago

adding some tests that refs #474. The commented code should compile before closing this ticket

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1// $Id: view_test.cc 1720 2009-01-15 05:51:07Z peter $
2
3/*
4  Copyright (C) 2008 Peter Johansson
5
6  This file is part of the yat library, http://dev.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 3 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 yat. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "Suite.h"
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
34using namespace theplu::yat;
35void test_rvalue(test::Suite&);
36void test_rvalue_func(test::Suite& suite, const utility::VectorBase& vec);
37
38int main(int argc, char* argv[])
39{ 
40  using namespace theplu::yat::utility;
41  theplu::yat::test::Suite suite(argc, argv);
42  suite.err() << "testing vector" << std::endl;
43
44  Matrix data1;
45  data1.resize(3,1);
46  for(size_t i=0;i<3;i++) {
47    data1(i,0)=7+i;
48  }
49 
50
51  Matrix data2(3,1,1.0);
52
53  VectorConstView a=data1.column_const_view(0);
54  VectorView b=data2.column_view(0);
55  VectorView bajs(data2, 0, false);
56  if (a(0)!=data1(0,0)){
57    suite.err() << "Matrix::column_const_view failed\n";
58    suite.add(false);
59  }
60  if (b(0)!=data2(0,0)){
61    suite.err() << "Matrix::column_view failed\n";
62    suite.add(false);
63  }
64
65  data1.column_const_view(0);
66  if (b.size()!=3){
67    suite.add(false);
68    suite.err() << "ERROR: expected size to be 3 - found " << b.size() << std::endl;
69  }
70  if ( !(b(0)==1 && b(1)==(1) && b(2)==1) )
71    suite.add(false);
72
73  if (a.begin()!=data1.begin_column(0)){
74    suite.add(false);
75    suite.err() << "VectorConstView::begin should equal " 
76             << "Matrix::begin_column(size_t)\n";
77  }
78  if (a.end()!=data1.end_column(0)) {
79    suite.add(false);
80    suite.err() << "VectorConstView::end should equal " 
81             << "Matrix::end_column(size_t)\n";
82  }
83  if (b.begin()!=data2.begin_column(0)){
84    suite.add(false);
85    suite.err() << "VectorView::begin " 
86             << b.begin().base() << " should equal\n"
87             << "Matrix::begin_column(size_t) "
88             << data1.begin_column(0).base() << std::endl;
89  }
90  if (b.end()!=data2.end_column(0)){
91    suite.add(false);
92    suite.err() << "VectorView::end should equal " 
93             << "Matrix::end_column(size_t)\n";
94  }
95  VectorConstView c=data1.row_const_view(0);
96  VectorView d=data2.row_view(0);
97  if (c.begin()!=data1.begin_row(0)) {
98    suite.add(false);
99    suite.err() << "VectorConstView::begin should equal " 
100             << "Matrix::begin_row(size_t)\n";
101  }
102  if (c.end()!=data1.end_row(0)){
103    suite.add(false);
104    suite.err() << "VectorConstView::end should equal " 
105             << "Matrix::end_row(size_t)\n";
106  }
107  if (d.begin()!=data2.begin_row(0)){
108    suite.add(false);
109    suite.err() << "VectorView::begin should equal " 
110             << "Matrix::begin_row(size_t)\n";
111  }
112  if (d.end()!=data2.end_row(0)){
113    suite.add(false);
114    suite.err() << "VectorView::end should equal " 
115             << "Matrix::end_row(size_t)\n";
116  }
117
118
119  // test that VectorConstView created from VectorView is independent
120  // of VectorView
121  {
122    Vector vec(10,1.2);
123    VectorView* vv = new VectorView(vec);
124    VectorConstView vcv(*vv);
125    delete vv;
126    if (vcv.size()!=10)
127      suite.add(false);
128    if (vcv(0)!=1.2)
129      suite.add(false);
130  }
131 
132  test_rvalue(suite);
133
134  return suite.return_value();
135}
136
137void test_rvalue(test::Suite& suite)
138{
139  using namespace utility;
140  suite.err() << "test rvalue" << std::endl;
141  Matrix m(10,10);
142  VectorView vv(m.row_view(0));
143  // this block doesn't compile, se ticket:474
144  // vv = m.row_view(1);
145
146  //Vector vec(m.row_view(0));
147  //vec = m.row_view(1);
148
149  //VectorConstView vcv(m.row_view(0));
150  //test_rvalue_func(suite, m.row_view(0));
151
152  test_rvalue_func(suite, m.row_const_view(0));
153
154}
155
156void test_rvalue_func(test::Suite& suite, const utility::VectorBase& vec)
157{
158  suite.err() << "test_rvalue_func: size: " << vec.size() << std::endl;
159}
Note: See TracBrowser for help on using the repository browser.