-
Notifications
You must be signed in to change notification settings - Fork 70
/
BasicTest.scala
206 lines (185 loc) · 4.74 KB
/
BasicTest.scala
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
// SPDX-License-Identifier: Apache-2.0
package chiseltest.tests
import org.scalatest._
import chisel3._
import chisel3.experimental.BundleLiterals._
import chiseltest._
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
class BasicTest extends AnyFlatSpec with ChiselScalatestTester with Matchers {
behavior.of("Testers2")
it should "test static circuits" in {
test(new StaticModule(42.U)) { c =>
c.out.expect(42.U)
}
}
it should "fail on poking outputs" in {
assertThrows[UnpokeableException] {
test(new StaticModule(42.U)) { c =>
c.out.poke(0.U)
}
}
}
it should "fail on peeking internal signals" in {
val e = intercept[UnpeekableException] {
test(new Module {
val hidden = Reg(Bool())
}) { c =>
c.hidden.peek()
}
}
assert(e.getMessage.contains("hidden"))
}
it should "fail on peeking internal signals with single threaded backend" in {
val e = intercept[UnpeekableException] {
test(new Module {
val hidden = Reg(Bool())
}) { c =>
c.hidden.peek()
}
}
assert(e.getMessage.contains("hidden"))
}
it should "fail on expect mismatch" in {
assertThrows[exceptions.TestFailedException] {
test(new StaticModule(42.U)) { c =>
c.out.expect(0.U)
}
}
}
it should "test record partial poke" in {
val typ = new CustomBundle("foo" -> UInt(32.W), "bar" -> UInt(32.W))
test(new PassthroughModule(typ)) { c =>
c.in.pokePartial(
typ.Lit(
_.elements("foo") -> 4.U
)
)
c.out.expectPartial(
typ.Lit(
_.elements("foo") -> 4.U
)
)
c.in.pokePartial(
typ.Lit(
_.elements("bar") -> 5.U
)
)
c.out.expect(
typ.Lit(
_.elements("foo") -> 4.U,
_.elements("bar") -> 5.U
)
)
}
}
it should "fail on record expect mismatch" in {
val typ = new CustomBundle("foo" -> UInt(32.W), "bar" -> UInt(32.W))
assertThrows[exceptions.TestFailedException] {
test(new PassthroughModule(typ)) { c =>
c.in.pokePartial(
typ.Lit(
_.elements("foo") -> 4.U
)
)
c.out.expect(
typ.Lit(
_.elements("foo") -> 4.U,
_.elements("bar") -> 5.U
)
)
}
}
}
it should "fail on partial expect mismatch" in {
val typ = new CustomBundle("foo" -> UInt(32.W), "bar" -> UInt(32.W))
assertThrows[exceptions.TestFailedException] {
test(new PassthroughModule(typ)) { c =>
c.in.poke(
typ.Lit(
_.elements("foo") -> 4.U,
_.elements("bar") -> 5.U
)
)
c.out.expectPartial(
typ.Lit(
_.elements("foo") -> 5.U
)
)
}
}
}
it should "fail with user-defined message" in {
intercept[exceptions.TestFailedException] {
test(new StaticModule(42.U)) { c =>
c.out.expect(0.U, "user-defined failure message =(")
}
}.getMessage should include("user-defined failure message =(")
}
it should "test inputless sequential circuits" in {
test(new Module {
val io = IO(new Bundle {
val out = Output(UInt(8.W))
})
val counter = RegInit(UInt(8.W), 0.U)
counter := counter + 1.U
io.out := counter
}) { c =>
c.io.out.expect(0.U)
c.clock.step()
c.io.out.expect(1.U)
c.clock.step()
c.io.out.expect(2.U)
c.clock.step()
c.io.out.expect(3.U)
}
}
it should "test combinational circuits" in {
test(new PassthroughModule(UInt(8.W))) { c =>
c.in.poke(0.U)
c.out.expect(0.U)
c.in.poke(42.U)
c.out.expect(42.U)
}
}
it should "test sequential circuits" in {
test(new Module {
val io = IO(new Bundle {
val in = Input(UInt(8.W))
val out = Output(UInt(8.W))
})
io.out := RegNext(io.in, 0.U)
}) { c =>
c.io.in.poke(0.U)
c.clock.step()
c.io.out.expect(0.U)
c.io.in.poke(42.U)
c.clock.step()
c.io.out.expect(42.U)
}
}
it should "test reset" in {
test(new Module {
val io = IO(new Bundle {
val in = Input(UInt(8.W))
val out = Output(UInt(8.W))
})
io.out := RegNext(io.in, 0.U)
}) { c =>
c.io.out.expect(0.U)
c.io.in.poke(42.U)
c.clock.step()
c.io.out.expect(42.U)
c.reset.poke(true.B)
c.io.out.expect(42.U) // sync reset not effective until next clk
c.clock.step()
c.io.out.expect(0.U)
c.clock.step()
c.io.out.expect(0.U)
c.reset.poke(false.B)
c.io.in.poke(43.U)
c.clock.step()
c.io.out.expect(43.U)
}
}
}