From c23943b41de55de9af249a3b231558ad23fc8087 Mon Sep 17 00:00:00 2001 From: Henry Cook Date: Fri, 14 Aug 2015 01:51:39 -0700 Subject: Add Vec tests. Do a better job of generating widths. --- src/test/scala/chiselTests/BitwiseOps.scala | 36 ++++++++------------------ src/test/scala/chiselTests/ChiselSpec.scala | 11 +++++++- src/test/scala/chiselTests/Vec.scala | 39 +++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 27 deletions(-) create mode 100644 src/test/scala/chiselTests/Vec.scala (limited to 'src/test') diff --git a/src/test/scala/chiselTests/BitwiseOps.scala b/src/test/scala/chiselTests/BitwiseOps.scala index 5a1f1de8..1d668ebd 100644 --- a/src/test/scala/chiselTests/BitwiseOps.scala +++ b/src/test/scala/chiselTests/BitwiseOps.scala @@ -35,38 +35,22 @@ import org.scalatest._ import org.scalatest.prop._ import Chisel.testers.BasicTester -class BitwiseOps(w: Int) extends Module { - val io = new Bundle { - val a = Bits(INPUT, w) - val b = Bits(INPUT, w) - val not = Bits(OUTPUT, w) - val and = Bits(OUTPUT, w) - val or = Bits(OUTPUT, w) - val xor = Bits(OUTPUT, w) - } - io.not := ~io.a - io.and := io.a & io.b - io.or := io.a | io.b - io.xor := io.a ^ io.b -} - class BitwiseOpsSpec extends ChiselPropSpec { - class BitwiseOpsTester(w: Int, a: Int, b: Int) extends BasicTester { - val mask = (1 << w) - 1 - val dut = Module(new BitwiseOps(w)) + class BitwiseOpsTester(w: Int, _a: Int, _b: Int) extends BasicTester { io.done := Bool(true) - dut.io.a := UInt(a) - dut.io.b := UInt(b) - when(dut.io.not != UInt(mask & ~a)) { io.error := UInt(1) } - when(dut.io.and != UInt(mask & (a & b))) { io.error := UInt(2) } - when(dut.io.or != UInt(mask & (a | b))) { io.error := UInt(3) } - when(dut.io.xor != UInt(mask & (a ^ b))) { io.error := UInt(4) } + val mask = (1 << w) - 1 + val a = UInt(_a) + val b = UInt(_b) + when(~a != UInt(mask & ~_a)) { io.error := UInt(1) } + when((a & b) != UInt(mask & (_a & _b))) { io.error := UInt(2) } + when((a | b) != UInt(mask & (_a | _b))) { io.error := UInt(3) } + when((a ^ b) != UInt(mask & (_a ^ _b))) { io.error := UInt(4) } } property("All bit-wise ops should return the correct result") { - forAll(safeUInts, safeUInts) { (a: Int, b: Int) => - assert(execute{ new BitwiseOpsTester(32, a, b) }) + forAll(safeUIntPair) { case(w: Int, a: Int, b: Int) => + assert(execute{ new BitwiseOpsTester(w, a, b) }) } } } diff --git a/src/test/scala/chiselTests/ChiselSpec.scala b/src/test/scala/chiselTests/ChiselSpec.scala index 3485b398..3d98d0a2 100644 --- a/src/test/scala/chiselTests/ChiselSpec.scala +++ b/src/test/scala/chiselTests/ChiselSpec.scala @@ -41,7 +41,7 @@ class ChiselPropSpec extends PropSpec with PropertyChecks { def popCount(n: Long) = n.toBinaryString.count(_=='1') val smallPosInts = Gen.choose(1, 7) - val safeUIntWidth = Gen.choose(1, 31) + val safeUIntWidth = Gen.choose(1, 30) val safeUInts = Gen.choose(0, (1 << 30)) val vecSizes = Gen.choose(0, 4) val binaryString = for(i <- Arbitrary.arbitrary[Int]) yield "b" + i.toBinaryString @@ -51,10 +51,19 @@ class ChiselPropSpec extends PropSpec with PropertyChecks { w <- smallPosInts i <- Gen.containerOfN[List,Int](n, Gen.choose(0, (1 << w) - 1)) } yield (w, i) + val safeUInt = for { + w <- smallPosInts + i <- Gen.choose(0, (1 << w) - 1) + } yield (w, i) def safeUIntPairN(n: Int) = for { w <- smallPosInts i <- Gen.containerOfN[List,Int](n, Gen.choose(0, (1 << w) - 1)) j <- Gen.containerOfN[List,Int](n, Gen.choose(0, (1 << w) - 1)) } yield (w, i zip j) + val safeUIntPair= for { + w <- smallPosInts + i <- Gen.choose(0, (1 << w) - 1) + j <- Gen.choose(0, (1 << w) - 1) + } yield (w, i, j) } diff --git a/src/test/scala/chiselTests/Vec.scala b/src/test/scala/chiselTests/Vec.scala new file mode 100644 index 00000000..47e4af2c --- /dev/null +++ b/src/test/scala/chiselTests/Vec.scala @@ -0,0 +1,39 @@ +package chiselTests + +import Chisel._ +import org.scalatest._ +import org.scalatest.prop._ +import Chisel.testers.BasicTester + +class VecSpec extends ChiselPropSpec { + + class ValueTester(w: Int, values: List[Int]) extends BasicTester { + io.done := Bool(true) + val v = Vec(values.map(UInt(_, width = w))) + io.error := v.zip(values).map { case(a,b) => + a != UInt(b) + }.foldLeft(UInt(0))(_##_) + } + + property("Vecs should be assignable") { + forAll(safeUIntN(8)) { case(w: Int, v: List[Int]) => + assert(execute{ new ValueTester(w, v) }) + } + } + + class TabulateTester(w: Int, n: Int) extends BasicTester { + io.done := Bool(true) + val v = Vec(Range(0, n).map(i => UInt(i * 2))) + val x = Vec(Array.tabulate(n){ i => UInt(i * 2) }) + val u = Vec.tabulate(n)(i => UInt(i*2)) + when(v.toBits != x.toBits) { io.error := UInt(1) } + when(v.toBits != u.toBits) { io.error := UInt(2) } + when(x.toBits != u.toBits) { io.error := UInt(3) } + } + + property("Vecs should tabulate correctly") { + forAll(smallPosInts) { (n: Int) => + assert(execute{ new TabulateTester(n) }) + } + } +} -- cgit v1.2.3