source: trunk/test/vector.cc @ 1194

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

refs #475. First version using Sparse Vector class. This change is obviously quite invasive and thus the size of this commit is larger than what is preferred. I've tried to limit as much as possible and left optimization and style issues behind with a note 'FIXME'.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.5 KB
Line 
1// $Id: vector.cc 1194 2010-10-03 23:39:12Z 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 operator+\n";
68  vec3 = vec + vec2;
69  if (vec3.size()!=8) {
70    suite.add(false);
71    suite.out() << "incorrect size: " << vec3.size() << " expected: " 
72                << 8 << "\n";
73  }
74  for (size_t i=0; i<8; ++i)
75    if (!suite.add(vec3[5]==vec[5]+vec2[5]))
76      suite.out() << "operator+ failed: result: " << vec3[i] 
77                  << " expected: " << vec[i] + vec2[i] << "\n";
78}
79
80void test_accumulate(test::Suite& suite)
81{
82  SparseVector vec;
83  vec.set(5, 1);
84  vec.set(9, 12);
85 
86  SumVector result;
87  accumulate(vec, result);
88  std::vector<unsigned int> expected;
89  expected.resize(10);
90  expected[5]=1;
91  expected[6]=1;
92  expected[7]=1;
93  expected[8]=1;
94  expected[9]=13;
95  if (result.size() != expected.size()) {
96    suite.add(false);
97    suite.out() << "error: size: " << result.size() << " expected: " 
98                << expected.size() << "\n";
99  }
100  else {
101    for (size_t i=0; i<result.size(); ++i) {
102      if (result[i]!=expected[i]) {
103        suite.add(false);
104        suite.out() << "error: result[" << i << "]: " << result[i] 
105                    << " expected: " << expected[i] << "\n";
106      }
107    }
108  }
109
110}
Note: See TracBrowser for help on using the repository browser.