summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests
diff options
context:
space:
mode:
authorducky2015-10-23 11:49:07 -0700
committerPalmer Dabbelt2015-10-23 13:21:22 -0700
commit32a6322fc7e280e03f78a25fe3af92da516c5c7a (patch)
treef93aa64998d4d3c11ac1babbd8abaeee25e86726 /src/test/scala/chiselTests
parentda7162921544ca6265ee837b811f1eafb9b47208 (diff)
Add Scalaland unit tests for Reg
Diffstat (limited to 'src/test/scala/chiselTests')
-rw-r--r--src/test/scala/chiselTests/ChiselSpec.scala9
-rw-r--r--src/test/scala/chiselTests/Reg.scala43
2 files changed, 51 insertions, 1 deletions
diff --git a/src/test/scala/chiselTests/ChiselSpec.scala b/src/test/scala/chiselTests/ChiselSpec.scala
index 03f057d4..2d546d00 100644
--- a/src/test/scala/chiselTests/ChiselSpec.scala
+++ b/src/test/scala/chiselTests/ChiselSpec.scala
@@ -8,10 +8,17 @@ import org.scalacheck._
import Chisel._
import Chisel.testers._
-class ChiselPropSpec extends PropSpec with PropertyChecks {
+/** Common utility functions for Chisel unit tests. */
+trait ChiselRunners {
def execute(t: => BasicTester): Boolean = TesterDriver.execute(t)
def elaborate(t: => Module): Circuit = TesterDriver.elaborate(t)
+}
+
+/** Spec base class for BDD-style testers. */
+class ChiselFlatSpec extends FlatSpec with ChiselRunners with Matchers
+/** Spec base class for property-based testers. */
+class ChiselPropSpec extends PropSpec with ChiselRunners with PropertyChecks {
def popCount(n: Long) = n.toBinaryString.count(_=='1')
val smallPosInts = Gen.choose(1, 7)
diff --git a/src/test/scala/chiselTests/Reg.scala b/src/test/scala/chiselTests/Reg.scala
new file mode 100644
index 00000000..77d10d98
--- /dev/null
+++ b/src/test/scala/chiselTests/Reg.scala
@@ -0,0 +1,43 @@
+// See LICENSE for license details.
+
+package chiselTests
+
+import org.scalatest._
+import Chisel._
+import Chisel.testers.BasicTester
+
+class RegSpec extends ChiselFlatSpec {
+ "A Reg" should "throw an exception if not given any parameters" in {
+ a [ChiselException] should be thrownBy {
+ val reg = Reg()
+ }
+ }
+
+ "A Reg" should "be of the same type and width as outType, if specified" in {
+ class RegOutTypeWidthTester extends BasicTester {
+ val reg = Reg(t=UInt(width=2), next=UInt(width=3), init=UInt(20))
+ reg.width.get should be (2)
+ }
+ assert(execute{new RegOutTypeWidthTester})
+ }
+
+ "A Reg" should "be of unknown width if outType is not specified and width is not forced" in {
+ class RegUnknownWidthTester extends BasicTester {
+ val reg1 = Reg(next=UInt(width=3), init=UInt(20))
+ reg1.width.known should be (false)
+ val reg2 = Reg(init=UInt(20))
+ reg2.width.known should be (false)
+ val reg3 = Reg(next=UInt(width=3), init=UInt(width=5))
+ reg3.width.known should be (false)
+ }
+ assert(execute{new RegUnknownWidthTester})
+ }
+
+ "A Reg" should "be of width of init if outType and next are missing and init is a literal of forced width" in {
+ class RegForcedWidthTester extends BasicTester {
+ val reg2 = Reg(init=UInt(20, width=7))
+ reg2.width.get should be (7)
+ }
+ assert(execute{new RegForcedWidthTester})
+ }
+}