forked from svunit/svunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svunit_testcase.sv
227 lines (177 loc) · 4.73 KB
/
svunit_testcase.sv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//###########################################################################
//
// Copyright 2011 The SVUnit Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//###########################################################################
/*
Class: svunit_testcase
Base class for the unit test case
*/
class svunit_testcase extends svunit_base;
/*
uint: test_count
Counter for number of tests
*/
local int unsigned test_count = 0;
/*
uint: error_count
Counter for number of errors
*/
local int unsigned error_count = 0;
/*
Variable: running
1 is somewhere between setup and teardown, 0 otherwise
*/
local bit running = 0;
local junit_xml::TestCase current_junit_test_case;
local junit_xml::TestCase junit_test_cases[$];
/*
Interface
*/
extern function new(string name);
extern task wait_for_error();
extern function integer get_error_count();
extern task give_up();
extern function bit fail(string c, logic b, string s, string f, int l, string d = "");
extern function void start();
extern function void stop();
extern function bit is_running();
extern function void update_exit_status();
extern function void report();
extern virtual task setup();
extern virtual task teardown();
function void add_junit_test_case(string name);
current_junit_test_case = new(name, get_name());
junit_test_cases.push_back(current_junit_test_case);
endfunction
/* local */ typedef junit_xml::TestCase array_of_junit_test_cases[];
function array_of_junit_test_cases as_junit_test_cases();
return junit_test_cases;
endfunction
endclass
/*
Constructor: new
Initializes the test case
Parameters:
name - instance name of the test case
*/
function svunit_testcase::new(string name);
super.new(name);
endfunction
/*
Method: wait_for_error
Blocks until the error_count changes
*/
task svunit_testcase::wait_for_error();
@(error_count);
endtask
/*
Method: get_error_count
Returns the error count
*/
function integer svunit_testcase::get_error_count();
return error_count;
endfunction
/*
Method: give_up
Blocks indefinitely (Should only be called by `FAIL_IF)
*/
task svunit_testcase::give_up();
event never;
@(never);
endtask
/*
Method: fail
If expression fails, increments error count, displays a message
and returns the results
Parameters:
c - calling function
b - evaluation of expression (0 - false, 1 - true)
s - string to pass to pass or fail task
f - file name of the failure
l - line number of the failure
d - user specified debug info
return 1 if fail else 0
*/
function bit svunit_testcase::fail(string c, logic b, string s, string f, int l, string d = "");
string _d;
if (b !== 0) begin
error_count++;
if (d != "") begin
$sformat(_d, "[ %s ] ", d);
end
current_junit_test_case.add_failure($sformatf("%s: %s %s(at %s line:%0d)",c,s,_d,f,l));
`ERROR($sformatf("%s: %s %s(at %s line:%0d)",c,s,_d,f,l));
return 1;
end
else begin
return 0;
end
endfunction
/*
Method: start
Changes the execution status of the test to running and increment the test count
*/
function void svunit_testcase::start();
running = 1;
test_count++;
endfunction
/*
Method: stop
Changes the execution status of the test to stopped
*/
function void svunit_testcase::stop();
running = 0;
endfunction
/*
Method: is_running
Returns the execution status of the test
*/
function bit svunit_testcase::is_running();
return running;
endfunction
/*
Methos: update_exit_status
Updates the results of this testcase
*/
function void svunit_testcase::update_exit_status();
if (error_count == 0)
success = PASS;
else
success = FAIL;
endfunction
/*
Method: report
This task reports the results for the unit tests
*/
function void svunit_testcase::report();
string success_str = (success)? "PASSED":"FAILED";
`INFO($sformatf("%0s (%0d of %0d tests passing)",
success_str,
test_count-error_count,
test_count));
endfunction
/*
Method: setup
Only required if using VCS since pure virtual functions are not implemented
*/
task svunit_testcase::setup();
endtask
/*
Method: teardown
House cleaning after each test
*/
task svunit_testcase::teardown();
endtask