-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtest_covergroup_sampling.py
269 lines (222 loc) · 8.08 KB
/
test_covergroup_sampling.py
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
'''
Created on Aug 8, 2020
@author: ballance
'''
from enum import IntEnum, auto, Enum
import vsc
from vsc_test_case import VscTestCase
from vsc.model.covergroup_model import CovergroupModel
from vsc.model.coverpoint_model import CoverpointModel
class TestCovergroupSampling(VscTestCase):
def test_sample_nonvsc_object_local_lambda(self):
class my_e(IntEnum):
A = auto()
B = auto()
class my_c:
def __init__(self):
self.a = 5
self.b = my_e.A
@vsc.covergroup
class my_cg(object):
def __init__(self, input):
super().__init__()
# self.cp1 = vsc.coverpoint(lambda : input.a,
# bins=dict(
# a=vsc.bin_array([], [1, 15])
# ))
self.cp2 = vsc.coverpoint(lambda : input.b,
cp_t=vsc.enum_t(my_e))
inst = my_c()
cg = my_cg(inst)
inst.b = my_e.A
cg.sample()
inst.b = my_e.B
cg.sample()
vsc.report_coverage(details=True)
self.assertEquals(cg.cp2.get_coverage(), 100)
# def test_sample_nonvsc_object_local_lambda_enum(self):
# class my_e(Enum):
# A = auto()
# B = auto()
#
#
# class my_c:
# def __init__(self):
# self.a = 5
# self.b = my_e.A
#
#
# @vsc.covergroup
# class my_cg(object):
# def __init__(self, input):
# super().__init__()
# self.cp1 = vsc.coverpoint(lambda : input.a,
# bins=dict(
# a=vsc.bin_array([], [1, 15])
# ))
# self.cp2 = vsc.coverpoint(lambda : input.b,
# cp_t=vsc.enum_t(my_e))
#
# inst = my_c()
# cg = my_cg(inst)
# inst.b = my_e.A
# print("inst.b=" + str(inst.b))
# cg.sample()
# inst.b = my_e.B
# print("inst.b=" + str(inst.b))
# cg.sample()
# vsc.report_coverage(details=True)
# def test_sample_vsc_object(self):
# class my_e(IntEnum):
# A = auto()
# B = auto()
#
#
# @vsc.randobj
# class my_c:
# def __init__(self):
# self.a = 5
# self.b = my_e.A
#
#
# @vsc.covergroup
# class my_cg(object):
# def __init__(self, input):
# super().__init__()
# self.cp1 = vsc.coverpoint(input.a,
# bins=dict(
# a=vsc.bin_array([], [1, 15])
# ))
# self.cp2 = vsc.coverpoint(input.b, cp_t=vsc.enum_t(my_e))
#
# inst = my_c()
# cg = my_cg(inst)
# inst.b = my_e.A
# cg.sample()
# inst.b = my_e.B
# cg.sample()
# vsc.report_coverage(details=True)
def test_sample_vsc_object_with_sample(self):
@vsc.randobj
class my_c:
def __init__(self):
self.a = vsc.uint32_t(i=0)
self.b = vsc.uint32_t(i=0)
@vsc.covergroup
class my_cg(object):
def __init__(self):
super().__init__()
self.with_sample(dict(
input=my_c())
)
self.cp1 = vsc.coverpoint(self.input.a,
bins=dict(
a=vsc.bin_array([], [1, 15])
))
self.cp2 = vsc.coverpoint(self.input.b,
bins=dict(
b=vsc.bin_array([], [1,15])
))
inst = my_c()
cg = my_cg()
inst.a = 1
inst.b = 1
cg.sample(inst)
inst.a = 2
inst.b = 2
cg.sample(inst)
vsc.report_coverage(details=True)
report = vsc.get_coverage_report_model()
self.assertEqual(len(report.covergroups), 1)
self.assertEqual(len(report.covergroups[0].coverpoints), 2)
self.assertEqual(round(report.covergroups[0].coverpoints[0].coverage,2), 13.33)
self.assertEqual(round(report.covergroups[0].coverpoints[1].coverage,2), 13.33)
def test_cross_sampling(self):
@vsc.covergroup
class my_cg(object):
def __init__(self, a, b):
super().__init__()
self.cp1 = vsc.coverpoint(a, bins={
"a": vsc.bin_array([], [1, 10])
})
self.cp2 = vsc.coverpoint(b, bins={
"b": vsc.bin_array([], [1, 10])
})
self.cp1X2 = vsc.cross([self.cp1, self.cp2])
a = 6
b = 8
cg = my_cg(lambda: a, lambda: b)
for i in range(50):
cg.sample()
vsc.report_coverage(details=True)
# vsc.report_coverage(details=False)
print("Coverage: " + str(cg.get_coverage()))
def test_object_sample_2(self):
import vsc
@vsc.randobj
class BranchInstr:
def __init__(self):
self.type = vsc.rand_bit_t(1)
self.disp = vsc.rand_bit_t(19)
@vsc.constraint
def short_offset_cnstr(self):
self.disp <= 4096
def __str__(self):
return(f"type = {self.type}, displacement = {self.disp}")
@vsc.covergroup
class BranchInstr_cg:
def __init__(self):
self.with_sample(
item = BranchInstr()
)
self.type = vsc.coverpoint(self.item.type)
self.disp_cp = vsc.coverpoint(self.item.disp, bins = {
"small_pos" : vsc.bin_array([4], [0, 4096])
})
branchInstr = BranchInstr()
branchInstr_cg = BranchInstr_cg()
for i in range(32):
branchInstr.randomize()
branchInstr_cg.sample(branchInstr)
print(branchInstr)
rpt = vsc.get_coverage_report_model()
self.assertEqual(len(rpt.covergroups[0].coverpoints), 2)
self.assertEqual(len(rpt.covergroups[0].coverpoints[0].bins), 2)
self.assertEqual(len(rpt.covergroups[0].coverpoints[1].bins), 4)
vsc.report_coverage(details=True)
def test_object_sample_cb_1(self):
import vsc
@vsc.randobj
class BranchInstr:
def __init__(self):
self.type = vsc.rand_bit_t(1)
self.disp = vsc.rand_bit_t(19)
@vsc.constraint
def short_offset_cnstr(self):
self.disp <= 4096
def __str__(self):
return(f"type = {self.type}, displacement = {self.disp}")
# Note: Covergroup must inherit from CovergroupCallbackBase
@vsc.covergroup
class BranchInstr_cg(vsc.util.CovergroupCallbackBase):
def __init__(self):
super().__init__()
self.with_sample(
item = BranchInstr()
)
self.type = vsc.coverpoint(self.item.type)
self.disp_cp = vsc.coverpoint(self.item.disp, bins = {
"small_pos" : vsc.bin_array([4], [0, 4096])
})
branchInstr = BranchInstr()
branchInstr_cg = BranchInstr_cg()
# Note: Callback function accepts bin name and new hit count
def callback(bin, hit):
print("Hit: %s %d" % (bin, hit))
# Note: Register the callback with the covergroup
branchInstr_cg.set_cb(callback)
for i in range(32):
branchInstr.randomize()
# Note: Call 'sample_cb' instead of 'sample'
branchInstr_cg.sample_cb(branchInstr)
print(branchInstr)