aboutsummaryrefslogtreecommitdiff
path: root/tests/run/t2417.scala
blob: 80105f72b53cb8cf82791a6c1031a379f9eab70d (plain) (blame)
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
// #2417
object Test {

  def parallel(numThreads: Int)(block: => Unit): Unit = {
    var failure: Throwable = null
    val threads = Array.tabulate(numThreads)(i => new Thread {
      override def run: Unit = {
        try {
          block
          } catch {
            case x: Throwable => failure = x
          }
        }
      })
      for (t <- threads) t.start
      for (t <- threads) t.join
      if (failure != null) println("FAILURE: " + failure)
    }

    def testSet(initialSize: Int, numThreads: Int, passes: Int): Unit = {
      val orig = Set.empty ++ (1 to initialSize)
      parallel(numThreads) {
        for (pass <- 0 until passes) {
          var s = orig
          for (e <- (initialSize to 1 by -1)) {
            s -= e
            val obs = s.size
            if (obs != e - 1) {
              throw new Exception("removed e=" + e + ", size was " + obs + ", s=" + s)
            }
          }
        }
      }
    }

    def testMap(initialSize: Int, numThreads: Int, passes: Int): Unit = {
      val orig = Map.empty ++ ((1 to initialSize) map ((_,"v")))
      parallel(numThreads) {
        for (pass <- 0 until passes) {
          var m = orig
          for (e <- (initialSize to 1 by -1)) {
            m -= e
            val obs = m.size
            if (obs != e - 1) {
              throw new Exception("removed e=" + e + ", size was " + obs + ", m=" + m)
            }
          }
        }
      }
    }

    def main(args: Array[String]): Unit = {
      println("testing small Map that doesn't promote to HashMap...")
      testMap(4, 2, 1000000)
      println()

      println("testing single-threaded HashMap use...")
      testMap(5, 1, 1000000)
      println()

      println("testing HashMap.size from multiple threads...")
      testMap(5, 2, 1000000)
      println()

      println("testing small Set that doesn't promote to HashSet...")
      testSet(4, 2, 1000000)
      println()

      println("testing single-threaded HashSet use...")
      testSet(5, 1, 1000000)
      println()

      println("testing HashSet.size from multiple threads...")
      testSet(5, 2, 1000000)
      println()
    }
}