aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala
diff options
context:
space:
mode:
authorJack Koenig2023-03-20 17:28:59 -0700
committerGitHub2023-03-20 17:28:59 -0700
commit57b8a395ee8d5fdabb2deed3db7d0c644f0a7eed (patch)
tree85020fee61ef2a61a9e93326f3d6cbcefb34d3ee /src/test/scala
parentd5dbf71310f9534a108c59e605da1f215b86393b (diff)
Make return value of Serializer.lazily lazy (#2627)HEADmaster1.6.x
Directly subclassing Iterable is lazy-ish, but if you call any operation on the resulting value (eg. map or ++) it will evaluate the Iterable and return a List.
Diffstat (limited to 'src/test/scala')
-rw-r--r--src/test/scala/firrtlTests/SerializerSpec.scala44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/test/scala/firrtlTests/SerializerSpec.scala b/src/test/scala/firrtlTests/SerializerSpec.scala
index 0e72b97f..0af05e60 100644
--- a/src/test/scala/firrtlTests/SerializerSpec.scala
+++ b/src/test/scala/firrtlTests/SerializerSpec.scala
@@ -136,4 +136,48 @@ class SerializerSpec extends AnyFlatSpec with Matchers {
val readNew = parseSerializeParse(SMemTestCircuit.src(" new"))
assert(SMemTestCircuit.findRuw(readNew) == ReadUnderWrite.New)
}
+
+ it should "support lazy serialization" in {
+ var stmtSerialized = false
+ case class HackStmt(stmt: Statement) extends Statement {
+ def serialize: String = {
+ stmtSerialized = true
+ stmt.serialize
+ }
+ def foreachExpr(f: Expression => Unit): Unit = stmt.foreachExpr(f)
+ def foreachInfo(f: Info => Unit): Unit = stmt.foreachInfo(f)
+ def foreachStmt(f: Statement => Unit): Unit = stmt.foreachStmt(f)
+ def foreachString(f: String => Unit): Unit = stmt.foreachString(f)
+ def foreachType(f: Type => Unit): Unit = stmt.foreachType(f)
+ def mapExpr(f: Expression => Expression): Statement = this.copy(stmt.mapExpr(f))
+ def mapInfo(f: Info => Info): Statement = this.copy(stmt.mapInfo(f))
+ def mapStmt(f: Statement => Statement): Statement = this.copy(stmt.mapStmt(f))
+ def mapString(f: String => String): Statement = this.copy(stmt.mapString(f))
+ def mapType(f: Type => Type): Statement = this.copy(stmt.mapType(f))
+ }
+
+ val stmt = HackStmt(DefNode(NoInfo, "foo", Reference("bar")))
+ val it: Iterable[String] = Serializer.lazily(stmt)
+ assert(!stmtSerialized, "We should be able to construct the serializer lazily")
+
+ var mapExecuted = false
+ val it2: Iterable[String] = it.map { x =>
+ mapExecuted = true
+ x + ","
+ }
+ assert(!stmtSerialized && !mapExecuted, "We should be able to map the serializer lazily")
+
+ var appendExecuted = false
+ val it3: Iterable[String] = it2 ++ Seq("hi").view.map { x =>
+ appendExecuted = true
+ x
+ }
+ assert(!stmtSerialized && !mapExecuted && !appendExecuted, "We should be able to append to the serializer lazily")
+
+ val result = it3.mkString
+ assert(
+ stmtSerialized && mapExecuted && appendExecuted,
+ "Once we traverse the serializer, everything should execute"
+ )
+ }
}