summaryrefslogtreecommitdiff
path: root/src/test/scala/chisel3/internal/NamespaceSpec.scala
blob: fd808ff05f41bd857ec13e6d09a73f6d5b459603 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SPDX-License-Identifier: Apache-2.0

package chisel3.internal

import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers._

class NamespaceSpec extends AnyFlatSpec {
  behavior.of("Namespace")

  they should "support basic disambiguation" in {
    val namespace = Namespace.empty
    val name = namespace.name(_, false)
    name("x") should be("x")
    name("x") should be("x_1")
    name("x") should be("x_2")
  }

  they should "support explicit <prefix>_# names before <prefix> names" in {
    val namespace = Namespace.empty
    val name = namespace.name(_, false)
    name("x_1") should be("x_1")
    name("x_2") should be("x_2")
    name("x") should be("x")
    name("x") should be("x_3")
  }

  they should "support explicit <prefix>_# names in the middle of <prefix> names" in {
    val namespace = Namespace.empty
    val name = namespace.name(_, false)
    name("x") should be("x")
    name("x") should be("x_1")
    name("x_1") should be("x_1_1")
    name("x_2") should be("x_2")
    name("x") should be("x_3")
  }

  // For some reason, multi-character names tickled a different failure mode than single character
  they should "support explicit <prefix>_# names in the middle of longer <prefix> names" in {
    val namespace = Namespace.empty
    val name = namespace.name(_, false)
    name("foo") should be("foo")
    name("foo") should be("foo_1")
    name("foo_1") should be("foo_1_1")
    name("foo_2") should be("foo_2")
    name("foo") should be("foo_3")
  }

  they should "support collisions in recursively growing names" in {
    val namespace = Namespace.empty
    val name = namespace.name(_, false)
    name("x") should be("x")
    name("x") should be("x_1")
    name("x_1") should be("x_1_1")
    name("x_1") should be("x_1_2")
    name("x_1_1") should be("x_1_1_1")
    name("x_1_1") should be("x_1_1_2")
  }

  they should "support collisions in recursively shrinking names" in {
    val namespace = Namespace.empty
    val name = namespace.name(_, false)
    name("x_1_1") should be("x_1_1")
    name("x_1_1") should be("x_1_1_1")
    name("x_1") should be("x_1")
    name("x_1") should be("x_1_2")
    name("x") should be("x")
    name("x") should be("x_2")
  }

  // The namespace never generates names with _0 so it's actually a false collision case
  they should "properly handle false collisions with signals ending in _0" in {
    val namespace = Namespace.empty
    val name = namespace.name(_, false)
    name("x") should be("x")
    name("x") should be("x_1")
    name("x_0") should be("x_0")
    name("x") should be("x_2")
    name("x_0") should be("x_0_1")
  }
}