source: trunk/test/stats_test.cc @ 604

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

lifting out more code to separate function

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1// $Id: stats_test.cc 604 2008-04-16 04:00:44Z peter $
2
3/*
4  Copyright (C) 2008 Peter Johansson
5
6  This file is part of svndigest, http://trac.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 <vector>
41
42namespace theplu{
43namespace svndigest{
44
45  bool check(const Stats& stats, const std::vector<size_t>& correct,
46             int linetype, const std::string& descr, const std::string& author);
47  std::string path(void);
48  bool test_add(void);
49  bool test_blame(void);
50  bool test_classic(void);
51  bool test_base_class(const Stats&);
52 
53}} // end of namespace svndigest and theplu
54
55
56int main( int argc, char* argv[])
57{
58  using namespace theplu::svndigest;
59  bool verbose=false;
60  bool ok=true;
61  if (argc>1 && argv[1]==std::string("-v") )
62    verbose=true;
63  SVN* svn=SVN::instance(test::filename("toy_project"));
64  if (!svn){
65    std::cerr << "error: cannot create SVN instance\n";
66    return 1;
67  }
68
69  ok &= test_add();
70  ok &= test_blame();
71  ok &= test_classic();
72                                                                               
73  if (verbose)
74    if (ok)
75      std::cout << "Test is ok.\n";
76    else
77      std::cout << "Test failed.\n";
78  if (ok)
79    return 0;
80  return 1;
81}
82
83
84namespace theplu{
85namespace svndigest{
86
87  std::string path(void)
88  {
89    return test::filename("toy_project/bin/svnstat.cc");
90  }
91
92  bool test_add(void)
93  {
94    bool ok =true;
95    AddStats cs(path());
96    cs.parse(path());
97    ok &= test_base_class(cs);
98    return ok;
99  }
100 
101 
102  bool test_blame(void)
103  {
104    bool ok =true;
105    BlameStats cs(path());
106    cs.parse(path());
107    ok &= test_base_class(cs);
108    return ok;
109  }
110
111 
112  bool test_classic(void)
113  {
114    bool ok =true;
115    ClassicStats cs(path());
116    cs.parse(path());
117    ok &= test_base_class(cs);
118
119    // testing copyright lines for peter
120    std::vector<size_t> correct(48,0);
121    correct[47]=2;
122    ok &= check(cs, correct, LineTypeParser::copyright, "copyright", "peter");
123
124    // testing code lines for jari
125    correct.resize(0);
126    correct.resize(48,0);
127    correct[15]=20;
128    correct[16]=1;
129    correct[17]=1;
130    correct[28]=8;
131    correct[30]=54;
132    ok &= check(cs, correct, LineTypeParser::code, "code", "jari");
133
134    // testing comment lines for jari
135    correct.resize(0);
136    correct.resize(48,0);
137    correct[15]=1;
138    correct[28]=13;
139    correct[30]=7;
140    ok &= check(cs, correct, LineTypeParser::comment, "comment", "jari");
141
142    // testing blank lines for jari
143    correct.resize(0);
144    correct.resize(48,0);
145    correct[15]=10;
146    correct[28]=10;
147    correct[30]=2;
148    ok &= check(cs, correct, LineTypeParser::other, "other", "jari");
149
150    // testing code all lines for total
151    correct.resize(0);
152    correct.resize(48,0);
153    correct[15]=31;
154    correct[16]=1;
155    correct[17]=1;
156    correct[28]=31;
157    correct[30]=63;
158    correct[47]=2;
159    ok &= check(cs, correct, LineTypeParser::total, "total", "all");
160
161    return ok;
162  }
163 
164  bool check(const Stats& stats, const std::vector<size_t>& correct,
165             int linetype, const std::string& descr, const std::string& author)
166  {
167    bool ok=true;
168    std::vector<size_t> sum(correct.size());
169    std::partial_sum(correct.begin(), correct.end(), sum.begin());
170    for (size_t rev=0; rev<sum.size(); ++rev){
171      size_t n = stats(linetype, author, rev);
172      if (n != sum[rev]){
173        std::cerr << "error: " << descr << " " << author << " rev:" << rev
174                  << ": found " << n << " expected " << sum[rev] << "\n";
175        ok = false;
176      }
177    }
178    return true;
179  }
180
181  bool test_base_class(const Stats& s)
182  {
183    if (s.code()+s.comments()+s.empty()!=s.lines()){
184      std::cerr << "Code plus comments plus empty do not add up to lines\n";
185      std::cerr << "code: " << s.code() << "\n";
186      std::cerr << "comment: " << s.comments() << "\n";
187      std::cerr << "empty: " << s.empty() << "\n";
188      std::cerr << "lines: " << s.lines() << "\n";
189      return false;
190    }
191    return true;
192  }
193
194}} // end of namespace svndigest and theplu
Note: See TracBrowser for help on using the repository browser.