summaryrefslogtreecommitdiff
path: root/src/test/scala/chiselTests/OptionBundle.scala
blob: 0f3502a42564280b57a0a656a53f91810428d0ef (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// SPDX-License-Identifier: Apache-2.0

package chiselTests

import chisel3._
import chisel3.stage.ChiselStage
import chisel3.testers.BasicTester

class OptionBundle(val hasIn: Boolean) extends Bundle {
  val in = if (hasIn) {
    Some(Input(Bool()))
  } else {
    None
  }
  val out = Output(Bool())
}

class OptionBundleModule(val hasIn: Boolean) extends Module {
  val io = IO(new OptionBundle(hasIn))
  if (hasIn) {
    io.out := io.in.get
  } else {
    io.out := false.B
  }
}

class SomeOptionBundleTester(expected: Boolean) extends BasicTester {
  val mod = Module(new OptionBundleModule(true))
  mod.io.in.get := expected.asBool
  assert(mod.io.out === expected.asBool)
  stop()
}

class NoneOptionBundleTester() extends BasicTester {
  val mod = Module(new OptionBundleModule(false))
  assert(mod.io.out === false.B)
  stop()
}

class InvalidOptionBundleTester() extends BasicTester {
  val mod = Module(new OptionBundleModule(false))
  mod.io.in.get := true.B
  assert(false.B)
  stop()
}

class OptionBundleSpec extends ChiselFlatSpec with Utils {
  "A Bundle with an Option field" should "work properly if the Option field is not None" in {
    assertTesterPasses { new SomeOptionBundleTester(true) }
    assertTesterPasses { new SomeOptionBundleTester(false) }
  }

  "A Bundle with an Option field" should "compile if the Option field is None" in {
    assertTesterPasses { new NoneOptionBundleTester() }
  }

  "A Bundle with an Option field" should "assert out accessing a None Option field" in {
    a [Exception] should be thrownBy extractCause[Exception] {
      ChiselStage.elaborate { new InvalidOptionBundleTester() }
    }
  }
}