diff options
| author | Albert Chen | 2020-07-16 16:59:28 -0700 |
|---|---|---|
| committer | GitHub | 2020-07-16 16:59:28 -0700 |
| commit | c4cc6bc5b614bd7f5383f8a85c7fc81facdc4b20 (patch) | |
| tree | f178900374cf7e1bc44404569210070b4a0dba0a /fuzzer/src/test | |
| parent | da221ea21f6e5e4022156df9337e3054c333e62f (diff) | |
Add Expression Fuzzer (#1741)
Includes:
* Random generator of FIRRTL Expressions (UInt and SInt types)
* JQF SBT plugin and CLI
* Documentation in README.md
Co-authored-by: Jack Koenig <koenig@sifive.com>
Diffstat (limited to 'fuzzer/src/test')
| -rw-r--r-- | fuzzer/src/test/scala/Example.scala | 63 | ||||
| -rw-r--r-- | fuzzer/src/test/scala/GenMonad.scala | 21 |
2 files changed, 84 insertions, 0 deletions
diff --git a/fuzzer/src/test/scala/Example.scala b/fuzzer/src/test/scala/Example.scala new file mode 100644 index 00000000..9779f7b0 --- /dev/null +++ b/fuzzer/src/test/scala/Example.scala @@ -0,0 +1,63 @@ +package firrtl.fuzzer + +import org.scalacheck.{Gen, Prop, Properties} +import firrtl.{ChirrtlForm, CircuitState, CustomTransformException, LowFirrtlCompiler, Namespace} +import firrtl.passes.CheckWidths +import ExprGen._ +import ScalaCheckGenMonad._ + +object FirrtlCompileProperties extends Properties("FirrtlCompile") { + property("compile") = { + val gen = Gen.sized { size => + val params = ExprGenParams( + maxDepth = size / 3, + maxWidth = math.min(size + 1, CheckWidths.MaxWidth), + generators = Seq( + 1 -> AddDoPrimGen, + 1 -> SubDoPrimGen, + 1 -> MulDoPrimGen, + 1 -> DivDoPrimGen, + 1 -> LtDoPrimGen, + 1 -> LeqDoPrimGen, + 1 -> GtDoPrimGen, + 1 -> GeqDoPrimGen, + 1 -> EqDoPrimGen, + 1 -> NeqDoPrimGen, + 1 -> PadDoPrimGen, + 1 -> ShlDoPrimGen, + 1 -> ShrDoPrimGen, + 1 -> DshlDoPrimGen, + 1 -> CvtDoPrimGen, + 1 -> NegDoPrimGen, + 1 -> NotDoPrimGen, + 1 -> AndDoPrimGen, + 1 -> OrDoPrimGen, + 1 -> XorDoPrimGen, + 1 -> AndrDoPrimGen, + 1 -> OrrDoPrimGen, + 1 -> XorrDoPrimGen, + 1 -> CatDoPrimGen, + 1 -> BitsDoPrimGen, + 1 -> HeadDoPrimGen, + 1 -> TailDoPrimGen, + 1 -> AsUIntDoPrimGen, + 1 -> AsSIntDoPrimGen, + ) + ) + params.generateSingleExprCircuit[Gen]() + } + val lowFirrtlCompiler = new firrtl.LowFirrtlCompiler() + Prop.forAll(gen) { circuit => + val state = CircuitState(circuit, ChirrtlForm, Seq()) + //val compiler = new LowFirrtlCompiler() + val compiler = lowFirrtlCompiler + try { + val res = compiler.compile(state, Seq()) + true + } catch { + case e: CustomTransformException => false + case any : Throwable => false + } + } + } +} diff --git a/fuzzer/src/test/scala/GenMonad.scala b/fuzzer/src/test/scala/GenMonad.scala new file mode 100644 index 00000000..0eaadd13 --- /dev/null +++ b/fuzzer/src/test/scala/GenMonad.scala @@ -0,0 +1,21 @@ +package firrtl.fuzzer + +import org.scalacheck.{Gen, Properties} + +object ScalaCheckGenMonad { + implicit def scalaCheckGenMonadInstance: GenMonad[Gen] = new GenMonad[Gen] { + def flatMap[A, B](a: Gen[A])(f: A => Gen[B]): Gen[B] = a.flatMap(f) + + def map[A, B](a: Gen[A])(f: A => B): Gen[B] = a.map(f) + + def choose(min: Int, max: Int): Gen[Int] = Gen.choose(min, max) + + def oneOf[A](items: A*): Gen[A] = Gen.oneOf(items) + + def const[A](c: A): Gen[A] = Gen.const(c) + + def widen[A, B >: A](ga: Gen[A]): Gen[B] = ga + + def generate[A](ga: Gen[A]): A = ga.sample.get + } +} |
