From 5744ce6df80ba9ec153b07744eb8da7a01876f2c Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Thu, 14 Mar 2019 02:04:05 -0700 Subject: Add PopCount test --- src/test/scala/chiselTests/PopCount.scala | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/test/scala/chiselTests/PopCount.scala (limited to 'src/test/scala/chiselTests') diff --git a/src/test/scala/chiselTests/PopCount.scala b/src/test/scala/chiselTests/PopCount.scala new file mode 100644 index 00000000..2c144382 --- /dev/null +++ b/src/test/scala/chiselTests/PopCount.scala @@ -0,0 +1,25 @@ +// See LICENSE for license details. + +package chiselTests + +import chisel3._ +import chisel3.util.PopCount +import org.scalatest._ +import org.scalatest.prop._ +import chisel3.testers.BasicTester + +class PopCountTester(n: Int) extends BasicTester { + val x = RegInit(0.U(n.W)) + x := x + 1.U + when (RegNext(x === ~0.U(n.W))) { stop() } + + val result = PopCount(x.asBools) + val expected = x.asBools.foldLeft(0.U)(_ +& _) + assert(result === expected) +} + +class PopCountSpec extends ChiselPropSpec { + property("Mul lookup table should return the correct result") { + forAll(smallPosInts) { (n: Int) => assertTesterPasses { new PopCountTester(n) } } + } +} -- cgit v1.2.3 From 6c39009771d3c9169f66e8a65b5d8f996d5c6e1f Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Thu, 14 Mar 2019 02:04:23 -0700 Subject: Add width constraint to PopCount test (which currently fails) --- src/test/scala/chiselTests/PopCount.scala | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/test/scala/chiselTests') diff --git a/src/test/scala/chiselTests/PopCount.scala b/src/test/scala/chiselTests/PopCount.scala index 2c144382..d9b3b837 100644 --- a/src/test/scala/chiselTests/PopCount.scala +++ b/src/test/scala/chiselTests/PopCount.scala @@ -16,6 +16,8 @@ class PopCountTester(n: Int) extends BasicTester { val result = PopCount(x.asBools) val expected = x.asBools.foldLeft(0.U)(_ +& _) assert(result === expected) + + require(result.getWidth == BigInt(n).bitLength) } class PopCountSpec extends ChiselPropSpec { -- cgit v1.2.3