1 | // $Id: Suite.cc 801 2009-07-06 02:07:42Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2008, 2009 Peter Johansson |
---|
5 | |
---|
6 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
7 | |
---|
8 | The yat library is free software; you can redistribute it and/or |
---|
9 | modify it under the terms of the GNU General Public License as |
---|
10 | published by the Free Software Foundation; either version 3 of the |
---|
11 | License, or (at your option) any later version. |
---|
12 | |
---|
13 | The yat library is distributed in the hope that it will be useful, |
---|
14 | but 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 svndigest. If not, see <http://www.gnu.org/licenses/>. |
---|
20 | */ |
---|
21 | |
---|
22 | #include <config.h> |
---|
23 | |
---|
24 | #include "Suite.h" |
---|
25 | #include "environment.h" |
---|
26 | |
---|
27 | #include "Stats.h" |
---|
28 | #include "StatsCollection.h" |
---|
29 | #include "../lib/utility.h" |
---|
30 | |
---|
31 | #include <cassert> |
---|
32 | #include <fstream> |
---|
33 | #include <iostream> |
---|
34 | #include <string> |
---|
35 | |
---|
36 | namespace theplu { |
---|
37 | namespace svndigest { |
---|
38 | namespace test { |
---|
39 | |
---|
40 | Suite::Suite(int argc, char* argv[], bool need_test_repo) |
---|
41 | : dev_null_(NULL), ok_(true), verbose_(false) |
---|
42 | { |
---|
43 | char* buffer=std::getenv("VERBOSE"); |
---|
44 | if ( (argc>1 && (argv[1]==std::string("-v") |
---|
45 | || argv[1]==std::string("--verbose")) |
---|
46 | || (buffer && buffer == std::string("1"))) ) { |
---|
47 | verbose_=true; |
---|
48 | } |
---|
49 | else |
---|
50 | dev_null_ = new std::ofstream("/dev/null"); |
---|
51 | |
---|
52 | if (need_test_repo) { |
---|
53 | bool have_test_repo=false; |
---|
54 | #ifdef HAVE_TEST_REPO |
---|
55 | have_test_repo=true; |
---|
56 | #endif |
---|
57 | if (!have_test_repo) { |
---|
58 | out() << "Skipping test because test repository is not available\n"; |
---|
59 | exit (77); |
---|
60 | } |
---|
61 | update_test_wc(); |
---|
62 | } |
---|
63 | |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | Suite::~Suite(void) |
---|
68 | { |
---|
69 | delete dev_null_; |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | bool Suite::add(bool b) |
---|
74 | { |
---|
75 | ok_ = ok_ && b; |
---|
76 | return b; |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | bool Suite::ok(void) const |
---|
81 | { |
---|
82 | return ok_; |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | std::ostream& Suite::out(void) const |
---|
87 | { |
---|
88 | if (verbose()) |
---|
89 | return std::cout; |
---|
90 | return *dev_null_; |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | bool equal(const StatsCollection& a, const StatsCollection& b, |
---|
95 | Suite& suite) |
---|
96 | { |
---|
97 | if (a.stats().size() != b.stats().size()) { |
---|
98 | suite.out() << "size mismatch\n"; |
---|
99 | return false; |
---|
100 | } |
---|
101 | std::map<std::string, Stats*>::const_iterator iter1 = a.stats().begin(); |
---|
102 | std::map<std::string, Stats*>::const_iterator iter2 = b.stats().begin(); |
---|
103 | while (iter1 != a.stats().end()) { |
---|
104 | if (iter1->first != iter2->first) { |
---|
105 | suite.out() << "key mismatch\n"; |
---|
106 | suite.out() << iter1->first << " vs " << iter2->first << "\n"; |
---|
107 | return false; |
---|
108 | } |
---|
109 | if (!equal(*iter1->second, *iter2->second, suite)) { |
---|
110 | suite.out() << "error in " << iter1->first << "\n"; |
---|
111 | return false; |
---|
112 | } |
---|
113 | ++iter1; |
---|
114 | ++iter2; |
---|
115 | } |
---|
116 | return true; |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | bool equal(const Stats& a, const Stats& b, test::Suite& suite) |
---|
121 | { |
---|
122 | if (a.authors() != b.authors()) { |
---|
123 | suite.out() << "authors are not equal\n"; |
---|
124 | return false; |
---|
125 | } |
---|
126 | if (a.revision() != b.revision()) { |
---|
127 | suite.out() << "revision mismatch\n"; |
---|
128 | return false; |
---|
129 | } |
---|
130 | std::vector<std::string> authors; |
---|
131 | authors.reserve(a.authors().size()+1); |
---|
132 | std::copy(a.authors().begin(), a.authors().end(), |
---|
133 | std::back_inserter(authors)); |
---|
134 | authors.push_back("all"); |
---|
135 | for (int linetype=0; linetype <= LineTypeParser::total; ++linetype) { |
---|
136 | for (std::vector<std::string>::const_iterator author=authors.begin(); |
---|
137 | author!=authors.end(); ++author) { |
---|
138 | for (svn_revnum_t rev=0; rev<a.revision(); ++rev) { |
---|
139 | size_t ax = a(linetype, *author, rev); |
---|
140 | size_t bx = b(linetype, *author, rev); |
---|
141 | if (ax != bx) { |
---|
142 | suite.out() << "error: linetype: " << linetype |
---|
143 | << " author " << *author |
---|
144 | << " rev " << rev << "\n" |
---|
145 | << " a: " << ax << "\n" |
---|
146 | << " b: " << bx << "\n"; |
---|
147 | return false; |
---|
148 | } |
---|
149 | } |
---|
150 | } |
---|
151 | } |
---|
152 | return true; |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | void Suite::update_test_wc(void) const |
---|
157 | { |
---|
158 | std::string cmd = abs_builddir()+"/svn_update.sh"; |
---|
159 | out() << cmd << std::endl; |
---|
160 | int status = system(cmd.c_str()); |
---|
161 | if (status) { |
---|
162 | out() << "failed with status: " << status << std::endl; |
---|
163 | exit (1); |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | bool Suite::verbose(void) const |
---|
169 | { |
---|
170 | return verbose_; |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | std::string filename(const std::string& path) |
---|
175 | { |
---|
176 | return abs_builddir()+"/"+path; |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | std::string src_filename(const std::string& path) |
---|
181 | { |
---|
182 | return abs_srcdir()+"/"+path; |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | }}} |
---|