summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/benchmarks/src/main/scala/scala/BitManipulationBenchmark.scala170
-rw-r--r--test/files/neg/sabin2.check2
-rw-r--r--test/files/neg/t0764.check2
-rw-r--r--test/files/neg/t1010.check2
-rw-r--r--test/files/neg/t5120.check2
-rw-r--r--test/files/neg/t6829.check14
-rw-r--r--test/files/pos/sd268.scala17
-rw-r--r--test/files/pos/t10009.scala6
-rw-r--r--test/files/run/t10009.scala28
-rw-r--r--test/junit/scala/tools/nsc/backend/jvm/opt/InlinerTest.scala15
-rw-r--r--test/junit/scala/tools/nsc/interpreter/CompletionTest.scala8
11 files changed, 255 insertions, 11 deletions
diff --git a/test/benchmarks/src/main/scala/scala/BitManipulationBenchmark.scala b/test/benchmarks/src/main/scala/scala/BitManipulationBenchmark.scala
new file mode 100644
index 0000000000..23e303ede0
--- /dev/null
+++ b/test/benchmarks/src/main/scala/scala/BitManipulationBenchmark.scala
@@ -0,0 +1,170 @@
+package scala.collection
+
+import org.openjdk.jmh.annotations._
+import org.openjdk.jmh.infra._
+import org.openjdk.jmh.runner.IterationType
+import benchmark._
+import java.util.concurrent.TimeUnit
+
+@BenchmarkMode(Array(Mode.AverageTime))
+@Fork(2)
+@Threads(1)
+@Warmup(iterations = 10)
+@Measurement(iterations = 10)
+@OutputTimeUnit(TimeUnit.NANOSECONDS)
+@State(Scope.Benchmark)
+class BitManipulationBenchmark {
+ val powersOfTwo = Array(1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824)
+
+ //////////////////////////////////////////////
+
+ @Benchmark def withIntegerBitCount(bh: Blackhole) {
+ for (v <- powersOfTwo) {
+ val leadingZeros = withIntegerBitCount(v)
+ // assert (leadingZeros == withLoop(v), s"$leadingZeros != ${withLoop(v)} ($v)")
+ bh.consume(leadingZeros)
+ }
+ }
+
+ private def withIntegerBitCount(v: Int) = Integer.SIZE - Integer.bitCount(v - 1)
+
+ //////////////////////////////////////////////
+
+ @Benchmark def withIntegerNumberOfLeadingZeros(bh: Blackhole) {
+ for (v <- powersOfTwo) {
+ val leadingZeros = withIntegerNumberOfLeadingZeros(v)
+ // assert (leadingZeros == withLoop(v), s"$leadingZeros != ${withLoop(v)} ($v)")
+ bh.consume(leadingZeros)
+ }
+ }
+
+ private def withIntegerNumberOfLeadingZeros(v: Int) = Integer.numberOfLeadingZeros(v - 1)
+
+ //////////////////////////////////////////////
+
+ @Benchmark def withLoop(bh: Blackhole) {
+ for (v <- powersOfTwo) {
+ val leadingZeros = withLoop(v)
+ bh.consume(leadingZeros)
+ }
+ }
+
+ private def withLoop(v: Int): Int = {
+ var r = Integer.SIZE
+ var copy = v >> 1
+ while (copy != 0) {
+ r -= 1
+ copy = copy >> 1
+ }
+ r
+ }
+
+ //////////////////////////////////////////////
+
+ @Benchmark def withMatch(bh: Blackhole) {
+ for (v <- powersOfTwo) {
+ val leadingZeros = withMatch(v)
+ // assert (leadingZeros == withLoop(v), s"$leadingZeros != ${withLoop(v)} ($v)")
+ bh.consume(leadingZeros)
+ }
+ }
+
+ private def withMatch(i: Int) = i match {
+ case 1 => 32
+ case 2 => 31
+ case 4 => 30
+ case 8 => 29
+ case 16 => 28
+ case 32 => 27
+ case 64 => 26
+ case 128 => 25
+ case 256 => 24
+ case 512 => 23
+ case 1024 => 22
+ case 2048 => 21
+ case 4096 => 20
+ case 8192 => 19
+ case 16384 => 18
+ case 32768 => 17
+ case 65536 => 16
+ case 131072 => 15
+ case 262144 => 14
+ case 524288 => 13
+ case 1048576 => 12
+ case 2097152 => 11
+ case 4194304 => 10
+ case 8388608 => 9
+ case 16777216 => 8
+ case 33554432 => 7
+ case 67108864 => 6
+ case 134217728 => 5
+ case 268435456 => 4
+ case 536870912 => 3
+ case 1073741824 => 2
+ }
+
+
+ //////////////////////////////////////////////
+
+ @Benchmark def with2DeBruijn(bh: Blackhole) {
+ for (v <- powersOfTwo) {
+ val leadingZeros = with2DeBruijn(v)
+ // assert (leadingZeros == withLoop(v), s"$leadingZeros != ${withLoop(v)} ($v)")
+ bh.consume(leadingZeros)
+ }
+ }
+
+ // https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn
+ private val multiplyDeBruijnBitPosition2 = Array(32, 31, 4, 30, 3, 18, 8, 29, 2, 10, 12, 17, 7, 15, 28, 24, 1, 5, 19, 9, 11, 13, 16, 25, 6, 20, 14, 26, 21, 27, 22, 23)
+
+ private def with2DeBruijn(v: Int) = multiplyDeBruijnBitPosition2((v * 0x077CB531) >>> 27)
+
+
+ //////////////////////////////////////////////
+
+ @Benchmark def withBinSearch(bh: Blackhole) {
+ for (v <- powersOfTwo) {
+ val leadingZeros = withBinSearch(v)
+ // assert (leadingZeros == withLoop(v), s"$leadingZeros != ${withLoop(v)} ($v)")
+ bh.consume(leadingZeros)
+ }
+ }
+
+ private def withBinSearch(v: Int) =
+ if (v < 65536) if (v < 256) if (v < 16) if (v < 4) if (v == 1) 32 else 31
+ else if (v == 4) 30 else 29
+ else if (v < 64) if (v == 16) 28 else 27
+ else if (v == 64) 26 else 25
+ else if (v < 4096) if (v < 1024) if (v == 256) 24 else 23
+ else if (v == 1024) 22 else 21
+ else if (v < 16384) if (v == 4096) 20 else 19
+ else if (v == 16384) 18 else 17
+ else if (v < 16777216) if (v < 1048576) if (v < 262144) if (v == 65536) 16 else 15
+ else if (v == 262144) 14 else 13
+ else if (v < 4194304) if (v == 1048576) 12 else 11
+ else if (v == 4194304) 10 else 9
+ else if (v < 268435456) if (v < 67108864) if (v == 16777216) 8 else 7
+ else if (v == 67108864) 6 else 5
+ else if (v < 1073741824) if (v == 268435456) 4 else 3
+ else if (v == 1073741824) 2 else 1
+
+ //////////////////////////////////////////////
+
+ @Benchmark def withSumBinSearch(bh: Blackhole) {
+ for (v <- powersOfTwo) {
+ val leadingZeros = withSumBinSearch(v)
+ // assert(leadingZeros == withLoop(v), s"$leadingZeros != ${withLoop(v)} ($v)")
+ bh.consume(leadingZeros)
+ }
+ }
+
+ private def withSumBinSearch(v: Int): Int = {
+ var exponent = Integer.SIZE
+ var remaining = v
+ if (remaining >= 65536) { remaining >>>= 16; exponent = 16 }
+ if (remaining >= 256) { remaining >>>= 8; exponent -= 8 }
+ if (remaining >= 16) { remaining >>>= 4; exponent -= 4 }
+ if (remaining >= 4) { remaining >>>= 2; exponent -= 2 }
+ if (remaining >= 2) exponent - 1 else exponent
+ }
+} \ No newline at end of file
diff --git a/test/files/neg/sabin2.check b/test/files/neg/sabin2.check
index aa0e8f734c..cd6fde4608 100644
--- a/test/files/neg/sabin2.check
+++ b/test/files/neg/sabin2.check
@@ -1,6 +1,6 @@
sabin2.scala:22: error: type mismatch;
found : Test.Base#T
- required: _5.T where val _5: Test.Base
+ required: _1.T where val _1: Test.Base
a.set(b.get()) // Error
^
one error found
diff --git a/test/files/neg/t0764.check b/test/files/neg/t0764.check
index 830278e715..0c7cff1e1e 100644
--- a/test/files/neg/t0764.check
+++ b/test/files/neg/t0764.check
@@ -1,5 +1,5 @@
t0764.scala:13: error: type mismatch;
- found : Node{type T = _2.type} where val _2: Node{type T = NextType}
+ found : Node{type T = _1.type} where val _1: Node{type T = NextType}
required: Node{type T = Main.this.AType}
(which expands to) Node{type T = Node{type T = NextType}}
new Main[AType]( (value: AType).prepend )
diff --git a/test/files/neg/t1010.check b/test/files/neg/t1010.check
index 2cc8f9d986..d412d8ac1e 100644
--- a/test/files/neg/t1010.check
+++ b/test/files/neg/t1010.check
@@ -1,6 +1,6 @@
t1010.scala:14: error: type mismatch;
found : MailBox#Message
- required: _3.in.Message where val _3: Actor
+ required: _1.in.Message where val _1: Actor
unstable.send(msg) // in.Message becomes unstable.Message, but that's ok since Message is a concrete type member
^
one error found
diff --git a/test/files/neg/t5120.check b/test/files/neg/t5120.check
index 34d4ebde31..b6a3cb96aa 100644
--- a/test/files/neg/t5120.check
+++ b/test/files/neg/t5120.check
@@ -6,7 +6,7 @@ t5120.scala:11: error: type mismatch;
t5120.scala:25: error: type mismatch;
found : Thread
required: h.T
- (which expands to) _2
+ (which expands to) _1
List(str, num).foreach(h => h.f1 = new Thread())
^
two errors found
diff --git a/test/files/neg/t6829.check b/test/files/neg/t6829.check
index 274094f791..5ccd531be1 100644
--- a/test/files/neg/t6829.check
+++ b/test/files/neg/t6829.check
@@ -1,6 +1,6 @@
t6829.scala:35: error: type mismatch;
found : AgentSimulation.this.state.type (with underlying type G#State)
- required: _9.State
+ required: _1.State
lazy val actions: Map[G#Agent,G#Action] = agents.map(a => a -> a.chooseAction(state)).toMap
^
t6829.scala:45: error: trait AgentSimulation takes type parameters
@@ -17,32 +17,32 @@ t6829.scala:49: error: not found: value nextState
^
t6829.scala:50: error: type mismatch;
found : s.type (with underlying type Any)
- required: _30.State where val _30: G
+ required: _1.State where val _1: G
val r = rewards(agent).r(s,a,s2)
^
t6829.scala:50: error: type mismatch;
found : a.type (with underlying type Any)
- required: _30.Action where val _30: G
+ required: _1.Action where val _1: G
val r = rewards(agent).r(s,a,s2)
^
t6829.scala:50: error: type mismatch;
found : s2.type (with underlying type Any)
- required: _30.State where val _30: G
+ required: _1.State where val _1: G
val r = rewards(agent).r(s,a,s2)
^
t6829.scala:51: error: type mismatch;
found : s.type (with underlying type Any)
- required: _25.State
+ required: _1.State
agent.learn(s,a,s2,r): G#Agent
^
t6829.scala:51: error: type mismatch;
found : a.type (with underlying type Any)
- required: _25.Action
+ required: _1.Action
agent.learn(s,a,s2,r): G#Agent
^
t6829.scala:51: error: type mismatch;
found : s2.type (with underlying type Any)
- required: _25.State
+ required: _1.State
agent.learn(s,a,s2,r): G#Agent
^
t6829.scala:53: error: not found: value nextState
diff --git a/test/files/pos/sd268.scala b/test/files/pos/sd268.scala
new file mode 100644
index 0000000000..8839651501
--- /dev/null
+++ b/test/files/pos/sd268.scala
@@ -0,0 +1,17 @@
+class Context(val v : AnyRef)
+
+trait AbidePlugin {
+ val someVal = ""
+
+ val x = null.asInstanceOf[Context { val v : someVal.type }] // CRASH
+ lazy val y = null.asInstanceOf[Context { val v : someVal.type }] // CRASH
+ var z = null.asInstanceOf[Context { val v : someVal.type }] // CRASH
+}
+
+class C {
+ val someVal = ""
+
+ val x = null.asInstanceOf[Context { val v : someVal.type }]
+ lazy val y = null.asInstanceOf[Context { val v : someVal.type }] // CRASH
+ var z = null.asInstanceOf[Context { val v : someVal.type }]
+}
diff --git a/test/files/pos/t10009.scala b/test/files/pos/t10009.scala
new file mode 100644
index 0000000000..7cd96f0f3d
--- /dev/null
+++ b/test/files/pos/t10009.scala
@@ -0,0 +1,6 @@
+class C {
+ def c(a: Any, b: Any*) = a
+}
+object Test {
+ new C().c(b = new { val x = 42 }, a = 0)
+}
diff --git a/test/files/run/t10009.scala b/test/files/run/t10009.scala
new file mode 100644
index 0000000000..2a318752f1
--- /dev/null
+++ b/test/files/run/t10009.scala
@@ -0,0 +1,28 @@
+import scala.reflect.runtime.currentMirror
+import scala.reflect.runtime.universe._
+import scala.tools.reflect.ToolBox
+
+object Test {
+ def test(code: String, log: Boolean = false) {
+ val tb = currentMirror.mkToolBox()
+ val tree = tb.parse(code)
+ val typed = tb.typecheck(tree)
+ if (log) {
+ println("=" * 80)
+ println(typed)
+ }
+ val untyped = tb.untypecheck(typed)
+ if (log) println(untyped)
+ val retyped = tb.typecheck(untyped)
+ if (log) println(retyped)
+ }
+ def main(args: Array[String]): Unit = {
+ test("{ class a { val x = 42 }; new a }") // failed
+ test("{ trait a { val x = 42 }; new a {} }") // worked
+ test("{ abstract class a { val x: Int } }") // worked
+ test("{ abstract class a { val x: Int }; new a { val x = 42 } }") // failed
+ test("{ class a { private val x = 42 }; new a }") // failed
+ test("{ class a { protected val x = 42 }; new a { x } }") // failed
+ test("{ class a { protected[a] val x = 42 }; new a }") // failed
+ }
+} \ No newline at end of file
diff --git a/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerTest.scala b/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerTest.scala
index 90a938be35..7be88816d5 100644
--- a/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerTest.scala
+++ b/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerTest.scala
@@ -1738,4 +1738,19 @@ class InlinerTest extends BytecodeTesting {
val List(a, c, t) = compile(code, allowMessage = _.msg contains warn)
assertInvoke(getMethod(c, "t"), "T", "m$")
}
+
+ @Test
+ def sd259d(): Unit = {
+ val code =
+ """trait T {
+ | @inline final def m = 1
+ |}
+ |class C extends T {
+ | def t = super.m // inline call to T.m$ here, we're not in the mixin forwarder C.m
+ |}
+ """.stripMargin
+ val List(c, t) = compileClasses(code)
+ assertNoInvoke(getMethod(c, "t"))
+ assertInvoke(getMethod(c, "m"), "T", "m$")
+ }
}
diff --git a/test/junit/scala/tools/nsc/interpreter/CompletionTest.scala b/test/junit/scala/tools/nsc/interpreter/CompletionTest.scala
index 1233e8b1cc..a216b319a8 100644
--- a/test/junit/scala/tools/nsc/interpreter/CompletionTest.scala
+++ b/test/junit/scala/tools/nsc/interpreter/CompletionTest.scala
@@ -185,6 +185,14 @@ class CompletionTest {
checkExact(completer, "p1.p2.p3.Ping.Po")("Pong")
}
+ @Test
+ def performanceOfLenientMatch(): Unit = {
+ val intp = newIMain()
+ val completer = new PresentationCompilerCompleter(intp)
+ val ident: String = "thisIsAReallyLongMethodNameWithManyManyManyManyChunks"
+ checkExact(completer, s"($ident: Int) => tia")(ident)
+ }
+
def checkExact(completer: PresentationCompilerCompleter, before: String, after: String = "")(expected: String*): Unit = {
assertEquals(expected.toSet, completer.complete(before, after).candidates.toSet)
}