diff options
| author | David Biancolin | 2020-03-17 13:26:40 -0700 |
|---|---|---|
| committer | GitHub | 2020-03-17 13:26:40 -0700 |
| commit | ba1f24345ac5ab20c669c73b871920001ac3a8ed (patch) | |
| tree | a6a55fafd5f68c35e574a34842930165af5631ad /src/test/scala/firrtl/testutils/PassTests.scala | |
| parent | d0500b33167cad060a9325d68b939d41279f6c9c (diff) | |
[RFC] Factor out common test classes; package them (#1412)
* Pull out common test utilities into a separate package
* Project a fat jar for test utilities
Co-authored-by: Albert Magyar <albert.magyar@gmail.com>
Diffstat (limited to 'src/test/scala/firrtl/testutils/PassTests.scala')
| -rw-r--r-- | src/test/scala/firrtl/testutils/PassTests.scala | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/src/test/scala/firrtl/testutils/PassTests.scala b/src/test/scala/firrtl/testutils/PassTests.scala new file mode 100644 index 00000000..c172163e --- /dev/null +++ b/src/test/scala/firrtl/testutils/PassTests.scala @@ -0,0 +1,106 @@ +// See LICENSE for license details. + +package firrtl.testutils + +import org.scalatest.FlatSpec +import firrtl.ir.Circuit +import firrtl.passes.{PassExceptions, RemoveEmpty} +import firrtl.transforms.DedupModules +import firrtl._ +import firrtl.annotations._ +import logger._ + +// An example methodology for testing Firrtl Passes +// Spec class should extend this class +abstract class SimpleTransformSpec extends FlatSpec with FirrtlMatchers with Compiler with LazyLogging { + // Utility function + def squash(c: Circuit): Circuit = RemoveEmpty.run(c) + + // Executes the test. Call in tests. + // annotations cannot have default value because scalatest trait Suite has a default value + def execute(input: String, check: String, annotations: Seq[Annotation]): CircuitState = { + val finalState = compileAndEmit(CircuitState(parse(input), ChirrtlForm, annotations)) + val actual = RemoveEmpty.run(parse(finalState.getEmittedCircuit.value)).serialize + val expected = parse(check).serialize + logger.debug(actual) + logger.debug(expected) + (actual) should be (expected) + finalState + } + + def executeWithAnnos(input: String, check: String, annotations: Seq[Annotation], + checkAnnotations: Seq[Annotation]): CircuitState = { + val finalState = compileAndEmit(CircuitState(parse(input), ChirrtlForm, annotations)) + val actual = RemoveEmpty.run(parse(finalState.getEmittedCircuit.value)).serialize + val expected = parse(check).serialize + logger.debug(actual) + logger.debug(expected) + (actual) should be (expected) + + annotations.foreach { anno => + logger.debug(anno.serialize) + } + + finalState.annotations.toSeq.foreach { anno => + logger.debug(anno.serialize) + } + checkAnnotations.foreach { check => + (finalState.annotations.toSeq) should contain (check) + } + finalState + } + // Executes the test, should throw an error + // No default to be consistent with execute + def failingexecute(input: String, annotations: Seq[Annotation]): Exception = { + intercept[PassExceptions] { + compile(CircuitState(parse(input), ChirrtlForm, annotations), Seq.empty) + } + } +} + +class CustomResolveAndCheck(form: CircuitForm) extends SeqTransform { + def inputForm = form + def outputForm = form + def transforms: Seq[Transform] = Seq[Transform](new ResolveAndCheck) +} + +trait LowTransformSpec extends SimpleTransformSpec { + def emitter = new LowFirrtlEmitter + def transform: Transform + def transforms: Seq[Transform] = Seq( + new ChirrtlToHighFirrtl(), + new IRToWorkingIR(), + new ResolveAndCheck(), + new DedupModules(), + new HighFirrtlToMiddleFirrtl(), + new MiddleFirrtlToLowFirrtl(), + new CustomResolveAndCheck(LowForm), + transform + ) +} + +trait MiddleTransformSpec extends SimpleTransformSpec { + def emitter = new MiddleFirrtlEmitter + def transform: Transform + def transforms: Seq[Transform] = Seq( + new ChirrtlToHighFirrtl(), + new IRToWorkingIR(), + new ResolveAndCheck(), + new DedupModules(), + new HighFirrtlToMiddleFirrtl(), + new CustomResolveAndCheck(MidForm), + transform + ) +} + +trait HighTransformSpec extends SimpleTransformSpec { + def emitter = new HighFirrtlEmitter + def transform: Transform + def transforms = Seq( + new ChirrtlToHighFirrtl(), + new IRToWorkingIR(), + new CustomResolveAndCheck(HighForm), + new DedupModules(), + transform + ) +} |
