summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/PopCount.scala
blob: d9b3b837195bc63c9ed4a3f8e9350ab345ac21a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 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)

  require(result.getWidth == BigInt(n).bitLength)
}

class PopCountSpec extends ChiselPropSpec {
  property("Mul lookup table should return the correct result") {
    forAll(smallPosInts) { (n: Int) =>  assertTesterPasses { new PopCountTester(n) } }
  }
}