source: trunk/test/stats_test.cc @ 687

Last change on this file since 687 was 687, checked in by Peter Johansson, 13 years ago

Merged patch release 0.6.6 to trunk. Delta 0.6.6 - 0.6.5

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.1 KB
Line 
1// $Id: stats_test.cc 687 2008-08-04 19:37:10Z peter $
2
3/*
4  Copyright (C) 2008 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 2 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 this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "Suite.h"
25
26#include "AddStats.h"
27#include "BlameStats.h"
28#include "ClassicStats.h"
29#include "Configuration.h"
30#include "Stats.h"
31#include "SVN.h"
32
33#include <algorithm>
34#include <cassert>
35#include <cstddef>
36#include <iterator>
37#include <iostream>
38#include <numeric>
39#include <string>
40#include <sstream>
41#include <vector>
42
43namespace theplu{
44namespace svndigest{
45
46  bool check(const Stats& stats, const std::vector<size_t>& correct,
47             int linetype, const std::string& descr, const std::string& author);
48  std::string path(void);
49  bool test_add(void);
50  bool test_blame(void);
51  bool test_classic(void);
52  bool test_base_class(const Stats&);
53  bool test_cache(const Stats&);
54 
55}} // end of namespace svndigest and theplu
56
57
58int main( int argc, char* argv[])
59{
60  using namespace theplu::svndigest;
61  bool verbose=false;
62  bool ok=true;
63  if (argc>1 && argv[1]==std::string("-v") )
64    verbose=true;
65  SVN* svn=SVN::instance(test::filename("toy_project"));
66  if (!svn){
67    std::cerr << "error: cannot create SVN instance\n";
68    return 1;
69  }
70
71  ok &= test_add();
72  ok &= test_blame();
73  ok &= test_classic();
74                                                                               
75  if (verbose)
76    if (ok)
77      std::cout << "Test is ok.\n";
78    else
79      std::cout << "Test failed.\n";
80  if (ok)
81    return 0;
82  return 1;
83}
84
85
86namespace theplu{
87namespace svndigest{
88
89  std::string path(void)
90  {
91    return test::filename("toy_project/bin/svnstat.cc");
92  }
93
94  bool test_add(void)
95  {
96    bool ok =true;
97    AddStats cs(path());
98    cs.parse(path());
99    ok &= test_base_class(cs);
100    return ok;
101  }
102 
103 
104  bool test_blame(void)
105  {
106    bool ok =true;
107    BlameStats cs(path());
108    cs.parse(path());
109    ok &= test_base_class(cs);
110    return ok;
111  }
112
113 
114  bool test_cache(const Stats& stats)
115  {
116    std::stringstream out;
117    stats.print(out);
118    std::stringstream in(out.str());
119    ClassicStats stats2(path());
120    assert(in.good());
121    if (!stats2.load_cache(in)){
122      std::cout << "load_cache() failed\n";
123      return false;
124    }
125
126    std::stringstream out2;
127    stats2.print(out2);
128   
129    if (out.str()!=out2.str()) {
130      std::cout << "test_cache() failed\n";
131      return false;
132    }
133    return true;
134  }
135
136
137  bool test_classic(void)
138  {
139    bool ok =true;
140    ClassicStats cs(path());
141    cs.parse(path());
142    ok &= test_base_class(cs);
143
144    // testing copyright lines for peter
145    std::vector<size_t> correct(48,0);
146    correct[47]=2;
147    ok &= check(cs, correct, LineTypeParser::copyright, "copyright", "peter");
148
149    // testing code lines for jari
150    correct.resize(0);
151    correct.resize(48,0);
152    correct[15]=20;
153    correct[16]=1;
154    correct[17]=1;
155    correct[28]=8;
156    correct[30]=54;
157    ok &= check(cs, correct, LineTypeParser::code, "code", "jari");
158
159    // testing comment lines for jari
160    correct.resize(0);
161    correct.resize(48,0);
162    correct[15]=1;
163    correct[28]=13;
164    correct[30]=7;
165    ok &= check(cs, correct, LineTypeParser::comment, "comment", "jari");
166
167    // testing blank lines for jari
168    correct.resize(0);
169    correct.resize(48,0);
170    correct[15]=10;
171    correct[28]=10;
172    correct[30]=2;
173    ok &= check(cs, correct, LineTypeParser::other, "other", "jari");
174
175    // testing code all lines for total
176    correct.resize(0);
177    correct.resize(48,0);
178    correct[15]=31;
179    correct[16]=1;
180    correct[17]=1;
181    correct[28]=31;
182    correct[30]=63;
183    correct[47]=2;
184    ok &= check(cs, correct, LineTypeParser::total, "total", "all");
185
186    return ok;
187  }
188 
189  bool check(const Stats& stats, const std::vector<size_t>& correct,
190             int linetype, const std::string& descr, const std::string& author)
191  {
192    bool ok=true;
193    std::vector<size_t> sum(correct.size());
194    std::partial_sum(correct.begin(), correct.end(), sum.begin());
195    for (size_t rev=0; rev<sum.size(); ++rev){
196      size_t n = stats(linetype, author, rev);
197      if (n != sum[rev]){
198        std::cerr << "error: " << descr << " " << author << " rev:" << rev
199                  << ": found " << n << " expected " << sum[rev] << "\n";
200        ok = false;
201      }
202    }
203    return true;
204  }
205
206  bool test_base_class(const Stats& s)
207  {
208    if (!test_cache(s))
209      return false;
210    if (s.code()+s.comments()+s.empty()!=s.lines()){
211      std::cerr << "Code plus comments plus empty do not add up to lines\n";
212      std::cerr << "code: " << s.code() << "\n";
213      std::cerr << "comment: " << s.comments() << "\n";
214      std::cerr << "empty: " << s.empty() << "\n";
215      std::cerr << "lines: " << s.lines() << "\n";
216      return false;
217    }
218    return true;
219  }
220
221}} // end of namespace svndigest and theplu
Note: See TracBrowser for help on using the repository browser.