source: trunk/test/vector.cc @ 1196

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

speeding up Vector::back(). refs #475

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1// $Id: vector.cc 1196 2010-10-04 02:40:13Z 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  suite.out() << "testing back()\n";
80  vec.resize(0);
81  vec.resize(10);
82  vec.set(9,71);
83  if (!suite.add(vec.back()==vec[9])) {
84    suite.out() << "vec.back(): " << vec.back() 
85                << "\nexpected: " << vec[9] << "\n";
86  }
87  vec.resize(20);
88  if (!suite.add(vec.back()==vec[19])) {
89    suite.out() << "vec.back(): " << vec.back() 
90                << "\nexpected: " << vec[19] << "\n";
91  }
92}
93
94void test_accumulate(test::Suite& suite)
95{
96  SparseVector vec;
97  vec.set(5, 1);
98  vec.set(9, 12);
99 
100  SumVector result;
101  accumulate(vec, result);
102  std::vector<unsigned int> expected;
103  expected.resize(10);
104  expected[5]=1;
105  expected[6]=1;
106  expected[7]=1;
107  expected[8]=1;
108  expected[9]=13;
109  if (result.size() != expected.size()) {
110    suite.add(false);
111    suite.out() << "error: size: " << result.size() << " expected: " 
112                << expected.size() << "\n";
113  }
114  else {
115    for (size_t i=0; i<result.size(); ++i) {
116      if (result[i]!=expected[i]) {
117        suite.add(false);
118        suite.out() << "error: result[" << i << "]: " << result[i] 
119                    << " expected: " << expected[i] << "\n";
120      }
121    }
122  }
123
124}
Note: See TracBrowser for help on using the repository browser.