From 815b1c3cb311b7f4dfb7a2f00e0e2d62795bdc6b Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 11 Nov 2016 14:37:03 -0800 Subject: Add checks for misuse or omission of Module() Implemented by adding a Boolean to check for alternating invocations of object Module.apply and the constructor of abstract class Module. Fixes #192 --- src/test/scala/chiselTests/AnnotatingExample.scala | 4 +- src/test/scala/chiselTests/ChiselSpec.scala | 2 +- src/test/scala/chiselTests/DeqIOSpec.scala | 62 ---------------------- src/test/scala/chiselTests/Module.scala | 34 ++++++++++++ 4 files changed, 37 insertions(+), 65 deletions(-) delete mode 100644 src/test/scala/chiselTests/DeqIOSpec.scala (limited to 'src/test') diff --git a/src/test/scala/chiselTests/AnnotatingExample.scala b/src/test/scala/chiselTests/AnnotatingExample.scala index c84edf86..04228d6b 100644 --- a/src/test/scala/chiselTests/AnnotatingExample.scala +++ b/src/test/scala/chiselTests/AnnotatingExample.scala @@ -141,5 +141,5 @@ object MyDriver extends BackendCompilationUtilities { /** * illustrates a chisel3 style driver that, annotations can only processed within this structure */ - def buildAnnotatedCircuit[T <: Module](gen: () => T): Map[String, String] = MyBuilder.build(Module(gen())) -} \ No newline at end of file + def buildAnnotatedCircuit[T <: Module](gen: () => T): Map[String, String] = MyBuilder.build(gen()) +} diff --git a/src/test/scala/chiselTests/ChiselSpec.scala b/src/test/scala/chiselTests/ChiselSpec.scala index d335bdf6..7b915c09 100644 --- a/src/test/scala/chiselTests/ChiselSpec.scala +++ b/src/test/scala/chiselTests/ChiselSpec.scala @@ -28,7 +28,7 @@ trait ChiselRunners extends Assertions { class ChiselFlatSpec extends FlatSpec with ChiselRunners with Matchers /** Spec base class for property-based testers. */ -class ChiselPropSpec extends PropSpec with ChiselRunners with PropertyChecks { +class ChiselPropSpec extends PropSpec with ChiselRunners with PropertyChecks with Matchers { // Constrain the default number of instances generated for every use of forAll. implicit override val generatorDrivenConfig = diff --git a/src/test/scala/chiselTests/DeqIOSpec.scala b/src/test/scala/chiselTests/DeqIOSpec.scala deleted file mode 100644 index d41c50e5..00000000 --- a/src/test/scala/chiselTests/DeqIOSpec.scala +++ /dev/null @@ -1,62 +0,0 @@ -// See LICENSE for license details. - -package chiselTests - -import chisel3._ -import chisel3.testers.BasicTester -import chisel3.util._ - -/** - * Created by chick on 2/8/16. - */ -class UsesDeqIOInfo extends Bundle { - val test_width = 32 - - val info_data = UInt.width(test_width) -} - -class UsesDeqIO extends Module { - val io = IO(new Bundle { - val in = chisel3.util.DeqIO(new UsesDeqIOInfo) - val out = chisel3.util.EnqIO(new UsesDeqIOInfo) - }) -} - -class DeqIOSpec extends ChiselFlatSpec { - runTester { - new BasicTester { - val dut = new UsesDeqIO -/* - "DeqIO" should "set the direction of it's parameter to INPUT" in { - assert(dut.io.in.bits.info_data.dir === INPUT) - } - "DeqIO" should "create a valid input and ready output" in { - assert(dut.io.in.valid.dir === INPUT) - assert(dut.io.in.ready.dir === OUTPUT) - } - "EnqIO" should "set the direction of it's parameter OUTPUT" in { - assert(dut.io.out.bits.info_data.dir === OUTPUT) - } - "EnqIO" should "create a valid input and ready output" in { - assert(dut.io.out.valid.dir === OUTPUT) - assert(dut.io.out.ready.dir === INPUT) - } - - val in_clone = dut.io.in.cloneType - val out_clone = dut.io.out.cloneType - - "A deqIO device" should "clone itself with it's directions intact" in { - assert(dut.io.in.bits.info_data.dir == in_clone.bits.info_data.dir) - assert(dut.io.in.ready.dir == in_clone.ready.dir) - assert(dut.io.in.valid.dir == in_clone.valid.dir) - } - - "A enqIO device" should "clone itself with it's directions intact" in { - assert(dut.io.out.bits.info_data.dir == out_clone.bits.info_data.dir) - assert(dut.io.out.ready.dir == out_clone.ready.dir) - assert(dut.io.out.valid.dir == out_clone.valid.dir) - } - */ - } - } -} diff --git a/src/test/scala/chiselTests/Module.scala b/src/test/scala/chiselTests/Module.scala index 7a4050db..c902d073 100644 --- a/src/test/scala/chiselTests/Module.scala +++ b/src/test/scala/chiselTests/Module.scala @@ -69,6 +69,22 @@ class ModuleWhen extends Module { } otherwise { io.s.out := io.s.in } } +class ModuleForgetWrapper extends Module { + val io = IO(new SimpleIO) + val inst = new PlusOne +} + +class ModuleDoubleWrap extends Module { + val io = IO(new SimpleIO) + val inst = Module(Module(new PlusOne)) +} + +class ModuleRewrap extends Module { + val io = IO(new SimpleIO) + val inst = Module(new PlusOne) + val inst2 = Module(inst) +} + class ModuleSpec extends ChiselPropSpec { property("ModuleVec should elaborate") { @@ -88,4 +104,22 @@ class ModuleSpec extends ChiselPropSpec { } ignore("ModuleWhenTester should return the correct result") { } + + property("Forgetting a Module() wrapper should result in an error") { + (the [ChiselException] thrownBy { + elaborate { new ModuleForgetWrapper } + }).getMessage should include("attempted to instantiate a Module without wrapping it") + } + + property("Double wrapping a Module should result in an error") { + (the [ChiselException] thrownBy { + elaborate { new ModuleDoubleWrap } + }).getMessage should include("Called Module() twice without instantiating a Module") + } + + property("Rewrapping an already instantiated Module should result in an error") { + (the [ChiselException] thrownBy { + elaborate { new ModuleRewrap } + }).getMessage should include("This is probably due to rewrapping a Module instance") + } } -- cgit v1.2.3