source: trunk/test/stats.cc @ 1164

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

improve lazycheck dependencies

To allow editing dependencies, the tests must have a suffix mentioned
in TEST_EXTENSION and it seems smoother if this suffix starts with a
period (.). Therefore tests are now named foo.test (rather than
foo_test before) and corresponding source is named foo.cc (rather than
foo_test.cc before). The change is documented in section 'Writing C++
tests' in test/README.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.3 KB
Line 
1// $Id: stats.cc 1164 2010-08-13 20:21:40Z peter $
2
3/*
4  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2009, 2010 Peter Johansson
6
7  This file is part of svndigest, http://dev.thep.lu.se/svndigest
8
9  svndigest is free software; you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 3 of the License, or
12  (at your option) any later version.
13
14  svndigest is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "Suite.h"
24
25#include "lib/AddStats.h"
26#include "lib/BlameStats.h"
27#include "lib/ClassicStats.h"
28#include "lib/Configuration.h"
29#include "lib/Stats.h"
30#include "lib/SVN.h"
31
32#include <algorithm>
33#include <cassert>
34#include <cstddef>
35#include <iterator>
36#include <iostream>
37#include <numeric>
38#include <string>
39#include <sstream>
40#include <vector>
41
42using namespace theplu::svndigest;
43
44namespace theplu{
45namespace svndigest{
46
47  bool check(const Stats& stats, const std::vector<int>& correct,
48             int linetype, const std::string& descr, const std::string& author);
49  std::string path(void);
50  bool test_add(test::Suite&);
51  bool test_blame(test::Suite&);
52  bool test_classic(test::Suite&);
53  bool test_base_class(const Stats&);
54  bool test_cache(const Stats&);
55 
56}} // end of namespace svndigest and theplu
57
58
59int main( int argc, char* argv[])
60{
61  test::Suite suite(argc, argv, true);
62
63  bool verbose=suite.verbose();
64
65  SVN* svn=SVN::instance("toy_project");
66  if (!svn){
67    std::cerr << "error: cannot create SVN instance\n";
68    return 1;
69  }
70
71  suite.add(test_add(suite));
72  suite.add(test_blame(suite));
73  suite.add(test_classic(suite));
74                                                                               
75  return suite.exit_status();
76}
77
78
79namespace theplu{
80namespace svndigest{
81
82  std::string path(void)
83  {
84    return "toy_project/bin/svnstat.cc";
85  }
86
87  bool test_add(test::Suite& suite)
88  {
89    suite.out() << "testing add\n";
90    bool ok =true;
91    AddStats cs(path());
92    cs.parse(path());
93    ok &= test_base_class(cs);
94
95    std::vector<int> correct(62,0);
96    correct[15] = 71;
97    correct[16] = 3;
98    correct[17] = 7;
99    correct[28] = 35;
100    correct[30] = 63;
101    ok &= check(cs, correct, LineTypeParser::total, "total", "jari");
102    correct[42] = 1;
103    correct[43] = 1;
104    correct[44] = 2;
105    correct[47] = 2;
106    ok &= check(cs, correct, LineTypeParser::total, "total", "all");
107    std::fill(correct.begin(), correct.end(), 0);
108    correct[42] = 1;
109    correct[43] = 1;
110    correct[44] = 2;
111    correct[47] = 2;
112    ok &= check(cs, correct, LineTypeParser::copyright, "copyright", "peter");
113    std::fill(correct.begin(), correct.end(), 0);
114    correct[15] = 49;
115    correct[16] = 3;
116    correct[17] = 7;
117    correct[28] = 11;
118    correct[30] = 54;
119    ok &= check(cs, correct, LineTypeParser::code, "code", "jari");
120    std::fill(correct.begin(), correct.end(), 0);
121    correct[15] = 5;
122    correct[28] = 13;
123    correct[30] = 7;
124    ok &= check(cs, correct, LineTypeParser::comment, "comment", "jari");
125    std::fill(correct.begin(), correct.end(), 0);
126    correct[15] = 17;
127    correct[28] = 10;
128    correct[30] = 2;
129    ok &= check(cs, correct, LineTypeParser::other, "other", "jari");
130
131    return ok;
132  }
133 
134 
135  bool test_blame(test::Suite& suite)
136  {
137    suite.out() << "testing blame\n";
138    bool ok =true;
139    BlameStats cs(path());
140    cs.parse(path());
141    ok &= test_base_class(cs);
142
143    std::vector<int> correct(62,0);
144    correct[15] = 71;
145    correct[16] = -1;
146    correct[17] = 2;
147    correct[28] = 31;
148    correct[30] = 25;
149    correct[42] = -1;
150    ok &= check(cs, correct, LineTypeParser::total, "total", "jari");
151    correct[42] = 0;
152    correct[43] = 1;
153    ok &= check(cs, correct, LineTypeParser::total, "total", "all");
154    std::fill(correct.begin(), correct.end(), 0);
155    correct[42] = 1;
156    correct[43] = 1;
157    ok &= check(cs, correct, LineTypeParser::copyright, "copyright", "peter");
158    std::fill(correct.begin(), correct.end(), 0);
159    correct[15] = 49;
160    correct[17] = 2;
161    correct[28] = 8;
162    correct[30] = 54-29;
163    ok &= check(cs, correct, LineTypeParser::code, "code", "jari");
164    std::fill(correct.begin(), correct.end(), 0);
165    correct[15] = 5;
166    correct[16] = -1;
167    correct[28] = 13;
168    correct[30] = 7-3;
169    ok &= check(cs, correct, LineTypeParser::comment, "comment", "jari");
170    std::fill(correct.begin(), correct.end(), 0);
171    correct[15] = 17;
172    correct[28] = 10-1;
173    correct[30] = 2-6;
174    ok &= check(cs, correct, LineTypeParser::other, "other", "jari");
175
176    return ok;
177  }
178
179 
180  bool test_cache(const Stats& stats)
181  {
182    std::stringstream out;
183    stats.print(out);
184    std::stringstream in(out.str());
185    ClassicStats stats2(path());
186    assert(in.good());
187    bool new_cache=true;
188    if (!stats2.load_cache(in, new_cache)){
189      std::cout << "load_cache() failed\n";
190      return false;
191    }
192
193    std::stringstream out2;
194    stats2.print(out2);
195   
196    if (out.str()!=out2.str()) {
197      std::cout << "test_cache() failed\n";
198      return false;
199    }
200    return true;
201  }
202
203
204  bool test_classic(test::Suite& suite)
205  {
206    suite.out() << "testing classic\n";
207    bool ok =true;
208    ClassicStats cs(path());
209    cs.parse(path());
210    ok &= test_base_class(cs);
211
212    // testing copyright lines for peter
213    std::vector<int> correct(48,0);
214    correct[47]=1;
215    ok &= check(cs, correct, LineTypeParser::copyright, "copyright", "peter");
216
217    // testing code lines for jari
218    correct.resize(0);
219    correct.resize(48,0);
220    correct[15]=20;
221    correct[16]=1;
222    correct[17]=1;
223    correct[28]=8;
224    correct[30]=54;
225    ok &= check(cs, correct, LineTypeParser::code, "code", "jari");
226
227    // testing comment lines for jari
228    correct.resize(0);
229    correct.resize(48,0);
230    correct[15]=1;
231    correct[28]=13;
232    correct[30]=7;
233    ok &= check(cs, correct, LineTypeParser::comment, "comment", "jari");
234
235    // testing blank lines for jari
236    correct.resize(0);
237    correct.resize(48,0);
238    correct[15]=10;
239    correct[28]=10;
240    correct[30]=2;
241    ok &= check(cs, correct, LineTypeParser::other, "other", "jari");
242
243    // testing code all lines for total
244    correct.resize(0);
245    correct.resize(48,0);
246    correct[15]=31;
247    correct[16]=1;
248    correct[17]=1;
249    correct[28]=31;
250    correct[30]=63;
251    correct[47]=1;
252    ok &= check(cs, correct, LineTypeParser::total, "total", "all");
253
254    return ok;
255  }
256 
257  bool check(const Stats& stats, const std::vector<int>& correct,
258             int linetype, const std::string& descr, const std::string& author)
259  {
260    bool ok=true;
261    std::vector<size_t> sum(correct.size());
262    std::partial_sum(correct.begin(), correct.end(), sum.begin());
263    for (size_t rev=0; rev<sum.size(); ++rev){
264      size_t n = stats(linetype, author, rev);
265      if (n != sum[rev]){
266        std::cout << "error: " << descr << " " << author << " rev:" << rev
267                  << ": found " << n << " expected " << sum[rev] << "\n";
268        ok = false;
269      }
270    }
271    return ok;
272  }
273
274  bool test_base_class(const Stats& s)
275  {
276    if (!test_cache(s))
277      return false;
278    if (s.code()+s.comments()+s.empty()!=s.lines()){
279      std::cerr << "Code plus comments plus empty do not add up to lines\n";
280      std::cerr << "code: " << s.code() << "\n";
281      std::cerr << "comment: " << s.comments() << "\n";
282      std::cerr << "empty: " << s.empty() << "\n";
283      std::cerr << "lines: " << s.lines() << "\n";
284      return false;
285    }
286    return true;
287  }
288
289}} // end of namespace svndigest and theplu
Note: See TracBrowser for help on using the repository browser.