unittest: add an API for describing LazyModule unit tests
This commit is contained in:
		| @@ -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 | ||||||
|   | |||||||
							
								
								
									
										35
									
								
								src/main/scala/unittest/TestGenerator.scala
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/main/scala/unittest/TestGenerator.scala
									
									
									
									
									
										Normal 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)) | ||||||
|  |   } | ||||||
|  | } | ||||||
							
								
								
									
										12
									
								
								src/main/scala/unittest/package.scala
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/main/scala/unittest/package.scala
									
									
									
									
									
										Normal 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))(_ && _) | ||||||
|  |   } | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user