source: trunk/test/stats_test.cc @ 981

Last change on this file since 981 was 981, checked in by Peter Johansson, 14 years ago

refs #429. convert test repo to utf-8. I stole one line in from me (@ rev 47) in bin/svnstats.cc so stats_test is updated accordingly.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.3 KB
Line 
1// $Id: stats_test.cc 981 2009-12-12 22:15:11Z peter $
2
3/*
4  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2009 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 "AddStats.h"
26#include "BlameStats.h"
27#include "ClassicStats.h"
28#include "Configuration.h"
29#include "Stats.h"
30#include "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  bool ok=true;
65
66  SVN* svn=SVN::instance(test::filename("toy_project"));
67  if (!svn){
68    std::cerr << "error: cannot create SVN instance\n";
69    return 1;
70  }
71
72  ok &= test_add(suite);
73  ok &= test_blame(suite);
74  ok &= test_classic(suite);
75                                                                               
76  if (verbose) {
77    if (ok)
78      std::cout << "Test is ok.\n";
79    else
80      std::cout << "Test failed.\n";
81  }
82  if (ok)
83    return 0;
84  return 1;
85}
86
87
88namespace theplu{
89namespace svndigest{
90
91  std::string path(void)
92  {
93    return test::filename("toy_project/bin/svnstat.cc");
94  }
95
96  bool test_add(test::Suite& suite)
97  {
98    suite.out() << "testing add\n";
99    bool ok =true;
100    AddStats cs(path());
101    cs.parse(path());
102    ok &= test_base_class(cs);
103
104    std::vector<int> correct(62,0);
105    correct[15] = 71;
106    correct[16] = 3;
107    correct[17] = 7;
108    correct[28] = 35;
109    correct[30] = 63;
110    ok &= check(cs, correct, LineTypeParser::total, "total", "jari");
111    correct[42] = 1;
112    correct[43] = 1;
113    correct[44] = 2;
114    correct[47] = 2;
115    ok &= check(cs, correct, LineTypeParser::total, "total", "all");
116    std::fill(correct.begin(), correct.end(), 0);
117    correct[42] = 1;
118    correct[43] = 1;
119    correct[44] = 2;
120    correct[47] = 2;
121    ok &= check(cs, correct, LineTypeParser::copyright, "copyright", "peter");
122    std::fill(correct.begin(), correct.end(), 0);
123    correct[15] = 49;
124    correct[16] = 3;
125    correct[17] = 7;
126    correct[28] = 11;
127    correct[30] = 54;
128    ok &= check(cs, correct, LineTypeParser::code, "code", "jari");
129    std::fill(correct.begin(), correct.end(), 0);
130    correct[15] = 5;
131    correct[28] = 13;
132    correct[30] = 7;
133    ok &= check(cs, correct, LineTypeParser::comment, "comment", "jari");
134    std::fill(correct.begin(), correct.end(), 0);
135    correct[15] = 17;
136    correct[28] = 10;
137    correct[30] = 2;
138    ok &= check(cs, correct, LineTypeParser::other, "other", "jari");
139
140    return ok;
141  }
142 
143 
144  bool test_blame(test::Suite& suite)
145  {
146    suite.out() << "testing blame\n";
147    bool ok =true;
148    BlameStats cs(path());
149    cs.parse(path());
150    ok &= test_base_class(cs);
151
152    std::vector<int> correct(62,0);
153    correct[15] = 71;
154    correct[16] = -1;
155    correct[17] = 2;
156    correct[28] = 31;
157    correct[30] = 25;
158    correct[42] = -1;
159    ok &= check(cs, correct, LineTypeParser::total, "total", "jari");
160    correct[42] = 0;
161    correct[43] = 1;
162    ok &= check(cs, correct, LineTypeParser::total, "total", "all");
163    std::fill(correct.begin(), correct.end(), 0);
164    correct[42] = 1;
165    correct[43] = 1;
166    ok &= check(cs, correct, LineTypeParser::copyright, "copyright", "peter");
167    std::fill(correct.begin(), correct.end(), 0);
168    correct[15] = 49;
169    correct[17] = 2;
170    correct[28] = 8;
171    correct[30] = 54-29;
172    ok &= check(cs, correct, LineTypeParser::code, "code", "jari");
173    std::fill(correct.begin(), correct.end(), 0);
174    correct[15] = 5;
175    correct[16] = -1;
176    correct[28] = 13;
177    correct[30] = 7-3;
178    ok &= check(cs, correct, LineTypeParser::comment, "comment", "jari");
179    std::fill(correct.begin(), correct.end(), 0);
180    correct[15] = 17;
181    correct[28] = 10-1;
182    correct[30] = 2-6;
183    ok &= check(cs, correct, LineTypeParser::other, "other", "jari");
184
185    return ok;
186  }
187
188 
189  bool test_cache(const Stats& stats)
190  {
191    std::stringstream out;
192    stats.print(out);
193    std::stringstream in(out.str());
194    ClassicStats stats2(path());
195    assert(in.good());
196    if (!stats2.load_cache(in)){
197      std::cout << "load_cache() failed\n";
198      return false;
199    }
200
201    std::stringstream out2;
202    stats2.print(out2);
203   
204    if (out.str()!=out2.str()) {
205      std::cout << "test_cache() failed\n";
206      return false;
207    }
208    return true;
209  }
210
211
212  bool test_classic(test::Suite& suite)
213  {
214    suite.out() << "testing classic\n";
215    bool ok =true;
216    ClassicStats cs(path());
217    cs.parse(path());
218    ok &= test_base_class(cs);
219
220    // testing copyright lines for peter
221    std::vector<int> correct(48,0);
222    correct[47]=1;
223    ok &= check(cs, correct, LineTypeParser::copyright, "copyright", "peter");
224
225    // testing code lines for jari
226    correct.resize(0);
227    correct.resize(48,0);
228    correct[15]=20;
229    correct[16]=1;
230    correct[17]=1;
231    correct[28]=8;
232    correct[30]=54;
233    ok &= check(cs, correct, LineTypeParser::code, "code", "jari");
234
235    // testing comment lines for jari
236    correct.resize(0);
237    correct.resize(48,0);
238    correct[15]=1;
239    correct[28]=13;
240    correct[30]=7;
241    ok &= check(cs, correct, LineTypeParser::comment, "comment", "jari");
242
243    // testing blank lines for jari
244    correct.resize(0);
245    correct.resize(48,0);
246    correct[15]=10;
247    correct[28]=10;
248    correct[30]=2;
249    ok &= check(cs, correct, LineTypeParser::other, "other", "jari");
250
251    // testing code all lines for total
252    correct.resize(0);
253    correct.resize(48,0);
254    correct[15]=31;
255    correct[16]=1;
256    correct[17]=1;
257    correct[28]=31;
258    correct[30]=63;
259    correct[47]=1;
260    ok &= check(cs, correct, LineTypeParser::total, "total", "all");
261
262    return ok;
263  }
264 
265  bool check(const Stats& stats, const std::vector<int>& correct,
266             int linetype, const std::string& descr, const std::string& author)
267  {
268    bool ok=true;
269    std::vector<size_t> sum(correct.size());
270    std::partial_sum(correct.begin(), correct.end(), sum.begin());
271    for (size_t rev=0; rev<sum.size(); ++rev){
272      size_t n = stats(linetype, author, rev);
273      if (n != sum[rev]){
274        std::cout << "error: " << descr << " " << author << " rev:" << rev
275                  << ": found " << n << " expected " << sum[rev] << "\n";
276        ok = false;
277      }
278    }
279    return ok;
280  }
281
282  bool test_base_class(const Stats& s)
283  {
284    if (!test_cache(s))
285      return false;
286    if (s.code()+s.comments()+s.empty()!=s.lines()){
287      std::cerr << "Code plus comments plus empty do not add up to lines\n";
288      std::cerr << "code: " << s.code() << "\n";
289      std::cerr << "comment: " << s.comments() << "\n";
290      std::cerr << "empty: " << s.empty() << "\n";
291      std::cerr << "lines: " << s.lines() << "\n";
292      return false;
293    }
294    return true;
295  }
296
297}} // end of namespace svndigest and theplu
Note: See TracBrowser for help on using the repository browser.