OSDN Git Service

Sメーター周りを修正
[trx-305dsp/dsp.git] / trx305 / kernel / cfg / base / coverage.cpp
1 /*
2  *  TOPPERS/JSP Kernel
3  *      Toyohashi Open Platform for Embedded Real-Time Systems/
4  *      Just Standard Profile Kernel
5  * 
6  *  Copyright (C) 2003 by Embedded and Real-Time Systems Laboratory
7  *                              Toyohashi Univ. of Technology, JAPAN
8  * 
9  *  上記著作権者は,以下の (1)〜(4) の条件か,Free Software Foundation 
10  *  によって公表されている GNU General Public License の Version 2 に記
11  *  述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
12  *  を改変したものを含む.以下同じ)を使用・複製・改変・再配布(以下,
13  *  利用と呼ぶ)することを無償で許諾する.
14  *  (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
15  *      権表示,この利用条件および下記の無保証規定が,そのままの形でソー
16  *      スコード中に含まれていること.
17  *  (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
18  *      用できる形で再配布する場合には,再配布に伴うドキュメント(利用
19  *      者マニュアルなど)に,上記の著作権表示,この利用条件および下記
20  *      の無保証規定を掲載すること.
21  *  (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
22  *      用できない形で再配布する場合には,次のいずれかの条件を満たすこ
23  *      と.
24  *    (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
25  *        作権表示,この利用条件および下記の無保証規定を掲載すること.
26  *    (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
27  *        報告すること.
28  *  (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
29  *      害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
30  * 
31  *  本ソフトウェアは,無保証で提供されているものである.上記著作権者お
32  *  よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
33  *  含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
34  *  接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
35  * 
36  *  @(#) $Id: coverage.cpp,v 1.1 2009/01/31 05:27:37 suikan Exp $
37  */
38
39 #include "base/coverage_defs.h"
40 #include "base/coverage_undefs.h"
41
42 #include <iostream>
43 #include <iomanip>
44
45 using namespace std;
46
47 #if defined(COVERAGE)
48 /*
49  *   簡単なカバレッジチェック
50  */
51
52     //フォーマット済みファイル位置情報の取得
53 string Coverage::Location::getDetails(void) const
54 {
55     string result;
56
57     result = string(filename) + ":" + String(lineno);
58     if(*additional != '\x0')
59         result += string("(") + additional + ")";
60
61     return result;
62 }
63
64
65
66     //要素の全削除
67 Coverage::BranchMap::~BranchMap(void) throw()
68 {
69     iterator scope;
70
71     scope = begin();
72     while(scope != end()) {
73         delete scope->second;
74         ++ scope;
75     }
76
77     clear();
78 }
79
80     //要素の登録
81 Coverage::BranchBase::BranchBase(const Location & location) throw()
82 {
83     BranchMap * bmap = Singleton<BranchMap>::getInstance();
84     NewBranchList * blist = Singleton<NewBranchList>::getInstance();
85
86     (*bmap)[location] = this;
87     blist->push_back(this);
88 }
89
90     //locationに一致する要素の取得
91 Coverage::BranchBase * Coverage::BranchBase::find(const Location & location) throw()
92 {
93     BranchMap * bmap = Singleton<BranchMap>::getInstance();
94     BranchMap::iterator scope;
95     BranchBase * result = 0;
96
97     scope = bmap->find(location);
98     if(scope != bmap->end())
99         result = scope->second;
100
101     return result;
102 }
103
104     //BranchIfコンストラクタ
105 Coverage::If::If(const Location & location) throw() : BranchBase(location), true_case(false), false_case(false)
106 {}
107
108     //ifの正当性判定 (成立/不成立の両方が起っている)
109 bool Coverage::If::checkValidity(void) const throw()
110 {   return true_case && false_case;   }
111
112     //通過情報の取得
113 string Coverage::If::getDetails(void) const throw()
114 {
115     string result;
116
117     if(true_case)
118         result += "true";
119
120     if(false_case) {
121         if(true_case)
122             result += "/";
123         result += "false";
124     }
125
126     return result;
127 }
128
129     //if分岐のチェック
130 bool Coverage::If::branch(const Location & location, bool expression) throw()
131 {
132     If * node = dynamic_cast<If *>(find(location));
133     if(node == 0)
134         node = new(nothrow) If(location);
135
136     if(node != 0) {
137         if(expression)
138             node->true_case = true;
139         else
140             node->false_case = true;
141     }
142     else
143         cerr << "[Coverage::Branch] Memory allocation error!\n";
144
145     return expression;
146 }
147
148
149     //Whileコンストラクタ
150 Coverage::While::While(const Location & location) throw() : BranchBase(location), valid(false)
151 {}
152
153     //Whileの正当性判定 (少なくとも一回はループの中をまわっている)
154 bool Coverage::While::checkValidity(void) const throw()
155 {   return valid;   }
156
157     //通過情報の取得
158 string Coverage::While::getDetails(void) const throw()
159 {
160     string result;
161
162     if(valid)
163         result = "valid";
164     else
165         result = "invalid";
166
167     return result;
168 }
169
170     //while分岐のチェック (少なくとも一回はループの中をまわっている)
171 bool Coverage::While::branch(const Location & location, bool expression) throw()
172 {
173     While * node = dynamic_cast<While *>(find(location));
174     if(node == 0)
175         node = new(nothrow) While(location);
176
177     if(node != 0) {
178         if(expression)
179             node->valid = true;
180     }
181     else
182         cerr << "[Coverage::Branch] Memory allocation error!\n";
183
184     return expression;
185 }
186
187
188     //Switchコンストラクタ
189 Coverage::Switch::Switch(const Location & location) throw() : BranchBase(location)
190 {}
191
192     //Switchの正当性判定 (通過した要素だけを覚えておく (後々判定も入れたい))
193 bool Coverage::Switch::checkValidity(void) const throw()
194 {   return true;   }
195
196     //通過情報の取得
197 string Coverage::Switch::getDetails(void) const throw()
198 {
199     stringstream buf;
200     set<int>::const_iterator scope;
201
202     scope = checkpoint.begin();
203     while(scope != checkpoint.end()) {
204         buf << *scope;
205
206         ++ scope;
207         if(scope != checkpoint.end())
208             buf << ", ";
209     }
210         
211     return buf.str();
212 }
213
214     //Switch分岐のチェック
215 void Coverage::Switch::_branch(const Location & location, int expression) throw()
216 {
217     Switch * node = dynamic_cast<Switch *>(find(location));
218     if(node == 0)
219         node = new(nothrow) Switch(location);
220
221     if(node != 0) {
222         if(expression)
223             node->checkpoint.insert(expression);
224     }
225     else
226         cerr << "[Coverage::Branch] Memory allocation error!\n";
227 }
228
229     //全ての項目を表示
230 void Coverage::printCoverage(ostream & out)
231 {
232     BranchMap * bmap = Singleton<BranchMap>::getInstance();
233     BranchMap::iterator scope;
234
235     unsigned long cases = 0;
236     unsigned long fails = 0;
237
238     scope = bmap->begin();
239     while(scope != bmap->end()) {
240         ++ cases;
241
242         if(scope->second->checkValidity())
243             out << "Success : ";
244         else {
245             out << "Failure : ";
246             ++ fails;
247         }
248         out << scope->first.getDetails() << ' ' << scope->second->getDetails() << '\n';
249
250         ++ scope;
251     }
252
253     out << fails << " fails in " << cases << " cases (" << setprecision(2) << (fails * 100.0 / cases) << "%)\n";
254 }
255
256     //全て通過したかどうかのチェック
257 bool Coverage::checkValidity(void)
258 {
259     bool result = true;
260
261     NewBranchList * blist = Singleton<NewBranchList>::getInstance();
262     NewBranchList::iterator scope;
263
264     scope = blist->begin();
265     while(scope != blist->end()) {
266         if(!(*scope)->checkValidity())
267             result = false;
268         ++ scope;
269     }
270
271     blist->clear();
272     return result;
273 }
274     //名称の取得
275 string Coverage::getBranchName(BranchBase * node)
276 {
277         //一致する要素の検索
278     BranchMap * bmap = Singleton<BranchMap>::getInstance();
279     BranchMap::iterator scope;
280
281     scope = bmap->begin();
282     while(scope != bmap->end()) {
283         if(scope->second == node)
284             break;
285         ++ scope;
286     }
287
288     //assert(scope != bmap->end());
289     
290     return scope->first.getDetails();
291 }
292
293 #endif /* COVERAGE */
294