source: trunk/test/stats_test.cc @ 603

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

avoid copy and paste code

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