source: trunk/test/vector.cc @ 1203

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

refs #475. prefer operator+= and remove operator+ for Vector class to ensure we use +=

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.5 KB
Line 
1// $Id: vector.cc 1203 2010-10-05 12:33:57Z peter $
2
3/*
4  Copyright (C) 2010 Peter Johansson
5
6  This file is part of svndigest, http://dev.thep.lu.se/svndigest
7
8  svndigest is free software; you can redistribute it and/or modify it
9  under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12
13  svndigest is distributed in the hope that it will be useful, but
14  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 svndigest. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "Suite.h"
23
24#include "lib/Vector.h"
25
26#include <iostream>
27#include <vector>
28
29using namespace theplu::svndigest;
30
31template<class T>
32void run_test(T, test::Suite&);
33void test_accumulate(test::Suite&);
34
35int main(int argc, char* argv[])
36{
37  test::Suite suite(argc, argv);
38
39  run_test(SparseVector(), suite);
40  run_test(SumVector(), suite);
41  test_accumulate(suite);
42
43  return suite.exit_status();
44}
45
46template<class T>
47void run_test(T vec, test::Suite& suite)
48{
49  vec.begin();
50  vec.end();
51  vec.size();
52  vec[0];
53  vec.set(0, 0);
54
55  vec.set(5,10);
56  T vec2(vec);
57  vec2.set(7,11);
58  T vec3;
59  vec3 = vec2;
60  suite.out() << "testing assignment\n";
61  if (vec3.size()!=vec2.size()) {
62    suite.add(false);
63    suite.out() << "incorrect size: " << vec3.size() << " expected: " 
64                << vec2.size() << "\n";
65  }
66
67  suite.out() << "testing back()\n";
68  vec.resize(0);
69  vec.resize(10);
70  vec.set(9,71);
71  if (!suite.add(vec.back()==vec[9])) {
72    suite.out() << "vec.back(): " << vec.back() 
73                << "\nexpected: " << vec[9] << "\n";
74  }
75  vec.resize(20);
76  if (!suite.add(vec.back()==vec[19])) {
77    suite.out() << "vec.back(): " << vec.back() 
78                << "\nexpected: " << vec[19] << "\n";
79  }
80}
81
82void test_accumulate(test::Suite& suite)
83{
84  SparseVector vec;
85  vec.set(5, 1);
86  vec.set(9, 12);
87 
88  SumVector result;
89  accumulate(vec, result);
90  std::vector<unsigned int> expected;
91  expected.resize(10);
92  expected[5]=1;
93  expected[6]=1;
94  expected[7]=1;
95  expected[8]=1;
96  expected[9]=13;
97  if (result.size() != expected.size()) {
98    suite.add(false);
99    suite.out() << "error: size: " << result.size() << " expected: " 
100                << expected.size() << "\n";
101  }
102  else {
103    for (size_t i=0; i<result.size(); ++i) {
104      if (result[i]!=expected[i]) {
105        suite.add(false);
106        suite.out() << "error: result[" << i << "]: " << result[i] 
107                    << " expected: " << expected[i] << "\n";
108      }
109    }
110  }
111
112}
Note: See TracBrowser for help on using the repository browser.