1
0

unittest: add an API for describing LazyModule unit tests

This commit is contained in:
Wesley W. Terpstra 2017-11-07 16:02:35 -08:00
parent cc789e9063
commit fdeed7bbb3
3 changed files with 51 additions and 1 deletions

View File

@ -36,7 +36,8 @@ abstract class LazyModule()(implicit val p: Parameters)
getClass.getMethods.filter { m => getClass.getMethods.filter { m =>
m.getParameterTypes.isEmpty && m.getParameterTypes.isEmpty &&
!java.lang.reflect.Modifier.isStatic(m.getModifiers) && !java.lang.reflect.Modifier.isStatic(m.getModifiers) &&
m.getName != "children" m.getName != "children" &&
m.getName != "getChildren"
}.flatMap { m => }.flatMap { m =>
if (classOf[LazyModule].isAssignableFrom(m.getReturnType)) { if (classOf[LazyModule].isAssignableFrom(m.getReturnType)) {
val obj = m.invoke(this) val obj = m.invoke(this)
@ -126,6 +127,8 @@ abstract class LazyModule()(implicit val p: Parameters)
iterfunc(this) iterfunc(this)
children.foreach( _.nodeIterator(iterfunc) ) children.foreach( _.nodeIterator(iterfunc) )
} }
def getChildren = children
} }
object LazyModule object LazyModule

View File

@ -0,0 +1,35 @@
// See LICENSE.SiFive for license details.
package freechips.rocketchip.unittest
import Chisel._
import freechips.rocketchip.config._
import freechips.rocketchip.diplomacy._
abstract class LazyUnitTest(implicit p: Parameters) extends LazyModule
{ self =>
protected def finished: Bool
lazy val module = new LazyModuleImp(this) {
val finished = IO(Bool(OUTPUT))
finished := self.finished
}
}
// FYI, you can call .finished on a Seq[LazyUnitTest]
class TestGenerator(gen: LazyModule => Seq[LazyUnitTest])
{
def apply(lm: LazyModule) = gen(lm)
def ++ (other: TestGenerator) = new TestGenerator(gen = lm => gen(lm) ++ other(lm))
}
object TestGenerator
{
def apply(matcher: PartialFunction[LazyModule, Seq[LazyUnitTest]]): TestGenerator =
new TestGenerator(gen = matcher.lift(_).getOrElse(Nil))
def recurse(other: TestGenerator): TestGenerator = {
def helper(lm: LazyModule, tail: Seq[LazyUnitTest]): Seq[LazyUnitTest] =
lm.getChildren.foldLeft(other(lm) ++ tail) { case (tail, child) => helper(child, tail) }
new TestGenerator(gen = helper(_, Nil))
}
}

View File

@ -0,0 +1,12 @@
// See LICENSE.SiFive for license details.
package freechips.rocketchip
import Chisel._
package object unittest
{
implicit class LazyUnitTestSeq(val seq: Seq[LazyUnitTest]) {
def finished = seq.map(_.module.finished).foldLeft(Bool(true))(_ && _)
}
}