summaryrefslogtreecommitdiff
path: root/test/junit
diff options
context:
space:
mode:
Diffstat (limited to 'test/junit')
-rw-r--r--test/junit/scala/collection/ArraySortingTest.scala29
-rw-r--r--test/junit/scala/collection/PagedSeq.scala16
-rw-r--r--test/junit/scala/collection/SetMapConsistencyTest.scala479
-rw-r--r--test/junit/scala/math/NumericTest.scala18
-rw-r--r--test/junit/scala/reflect/internal/MirrorsTest.scala18
-rw-r--r--test/junit/scala/reflect/internal/PrintersTest.scala815
6 files changed, 1375 insertions, 0 deletions
diff --git a/test/junit/scala/collection/ArraySortingTest.scala b/test/junit/scala/collection/ArraySortingTest.scala
new file mode 100644
index 0000000000..4e54b39ce7
--- /dev/null
+++ b/test/junit/scala/collection/ArraySortingTest.scala
@@ -0,0 +1,29 @@
+package scala.collection.mutable
+
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+import org.junit.Test
+
+/* Tests various maps by making sure they all agree on the same answers. */
+@RunWith(classOf[JUnit4])
+class ArraySortingTest {
+
+ class CantSortMe(val i: Int) {
+ override def equals(a: Any) = throw new IllegalArgumentException("I cannot be equalled!")
+ }
+
+ object CanOrder extends Ordering[CantSortMe] {
+ def compare(a: CantSortMe, b: CantSortMe) = a.i compare b.i
+ }
+
+ // Tests SI-7837
+ @Test
+ def sortByTest() {
+ val test = Array(1,2,3,4,1,3,5,7,1,4,8,1,1,1,1)
+ val cant = test.map(i => new CantSortMe(i))
+ java.util.Arrays.sort(test)
+ scala.util.Sorting.quickSort(cant)(CanOrder)
+ assert( test(6) == 1 )
+ assert( (test,cant).zipped.forall(_ == _.i) )
+ }
+}
diff --git a/test/junit/scala/collection/PagedSeq.scala b/test/junit/scala/collection/PagedSeq.scala
new file mode 100644
index 0000000000..5f83cf6f31
--- /dev/null
+++ b/test/junit/scala/collection/PagedSeq.scala
@@ -0,0 +1,16 @@
+package scala.collection.immutable
+
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+import org.junit.Test
+import org.junit.Assert._
+
+/* Test for SI-6615 */
+@RunWith(classOf[JUnit4])
+class PagedSeqTest {
+ @Test
+ def rovingDoesNotNPE(): Unit = {
+ // should not NPE, and should equal the given Seq
+ assertEquals(Seq('a'), PagedSeq.fromStrings(List.fill(5000)("a")).slice(4096, 4097))
+ }
+}
diff --git a/test/junit/scala/collection/SetMapConsistencyTest.scala b/test/junit/scala/collection/SetMapConsistencyTest.scala
new file mode 100644
index 0000000000..c62b074483
--- /dev/null
+++ b/test/junit/scala/collection/SetMapConsistencyTest.scala
@@ -0,0 +1,479 @@
+package scala.collection
+
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+import org.junit.Test
+import scala.collection.{mutable => cm, immutable => ci}
+import scala.collection.JavaConverters._
+
+/* Tests various maps by making sure they all agree on the same answers. */
+@RunWith(classOf[JUnit4])
+class SetMapConsistencyTest {
+
+ trait MapBox[A] {
+ protected def oor(s: String, n: Int) = throw new IllegalArgumentException(s"Out of range for $s: $n")
+ def title: String
+ def adders: Int
+ def add(n: Int, a: A, v: Int): Unit
+ def subbers: Int
+ def sub(n: Int, a: A): Unit
+ def getters: Int
+ def get(n: Int, a: A): Int
+ def fiddlers: Int
+ def fiddle(n: Int): Unit
+ def keys: Iterator[A]
+ def has(a: A): Boolean
+ }
+
+
+ // Mutable map wrappers
+
+ class BoxMutableMap[A, M <: cm.Map[A, Int]](m0: M, title0: String) extends MapBox[A] {
+ var m = m0
+ def title = title0
+ def adders = 5
+ def add(n: Int, a: A, v: Int) { n match {
+ case 0 => m += ((a, v))
+ case 1 => m(a) = v
+ case 2 => m.put(a, v)
+ case 3 => m = (m + ((a, v))).asInstanceOf[M]
+ case 4 => m = (m ++ List((a, v))).asInstanceOf[M]
+ case _ => oor("add", n)
+ }}
+ def subbers: Int = 3
+ def sub(n: Int, a: A) { n match {
+ case 0 => m -= a
+ case 1 => m = (m - a).asInstanceOf[M]
+ case 2 => m = m.filter(_._1 != a).asInstanceOf[M]
+ case _ => oor("sub", n)
+ }}
+ def getters: Int = 3
+ def get(n: Int, a: A) = n match {
+ case 0 => m.get(a).getOrElse(-1)
+ case 1 => if (m contains a) m(a) else -1
+ case 2 => m.getOrElse(a, -1)
+ case _ => oor("get", n)
+ }
+ def fiddlers: Int = 0
+ def fiddle(n: Int) { oor("fiddle", n) }
+ def keys = m.keysIterator
+ def has(a: A) = m contains a
+ override def toString = m.toString
+ }
+
+ def boxMlm[A] = new BoxMutableMap[A, cm.ListMap[A, Int]](new cm.ListMap[A, Int], "mutable.ListMap")
+
+ def boxMhm[A] = new BoxMutableMap[A, cm.HashMap[A, Int]](new cm.HashMap[A, Int], "mutable.HashMap")
+
+ def boxMohm[A] = new BoxMutableMap[A, cm.OpenHashMap[A, Int]](new cm.OpenHashMap[A, Int], "mutable.OpenHashMap")
+
+ def boxMarm[A <: AnyRef] = new BoxMutableMap[A, cm.AnyRefMap[A, Int]](new cm.AnyRefMap[A, Int](_ => -1), "mutable.AnyRefMap") {
+ private def arm: cm.AnyRefMap[A, Int] = m.asInstanceOf[cm.AnyRefMap[A, Int]]
+ override def adders = 3
+ override def subbers = 1
+ override def getters: Int = 4
+ override def get(n: Int, a: A) = n match {
+ case 0 => m.get(a).getOrElse(-1)
+ case 1 => m(a)
+ case 2 => m.getOrElse(a, -1)
+ case 3 => val x = arm.getOrNull(a); if (x==0 && !(arm contains a)) -1 else x
+ case _ => oor("get", n)
+ }
+ override def fiddlers = 2
+ override def fiddle(n: Int) { n match {
+ case 0 => m = arm.clone
+ case 1 => arm.repack
+ case _ => oor("fiddle", n)
+ }}
+ }
+
+ def boxMjm = new BoxMutableMap[Long, cm.LongMap[Int]](new cm.LongMap[Int](_ => -1), "mutable.LongMap") {
+ private def lm: cm.LongMap[Int] = m.asInstanceOf[cm.LongMap[Int]]
+ override def adders = 3
+ override def subbers = 1
+ override def getters: Int = 4
+ override def get(n: Int, a: Long) = n match {
+ case 0 => m.get(a).getOrElse(-1)
+ case 1 => m(a)
+ case 2 => m.getOrElse(a, -1)
+ case 3 => val x = lm.getOrNull(a); if (x==0 && !(lm contains a)) -1 else x
+ case _ => oor("get", n)
+ }
+ override def fiddlers = 2
+ override def fiddle(n: Int) { n match {
+ case 0 => m = lm.clone
+ case 1 => lm.repack
+ case _ => oor("fiddle", n)
+ }}
+ }
+
+ def boxJavaM[A] = new BoxMutableMap[A, cm.Map[A, Int]]((new java.util.HashMap[A, Int]).asScala, "java.util.HashMap") {
+ override def adders = 3
+ override def subbers = 1
+ }
+
+
+ // Immutable map wrappers
+
+ class BoxImmutableMap[A, M <: ci.Map[A, Int]](m0: M, title0: String) extends MapBox[A] {
+ var m = m0
+ def title = title0
+ def adders = 2
+ def add(n: Int, a: A, v: Int) { n match {
+ case 0 => m = (m + ((a, v))).asInstanceOf[M]
+ case 1 => m = (m ++ List((a, v))).asInstanceOf[M]
+ case _ => oor("add", n)
+ }}
+ def subbers: Int = 2
+ def sub(n: Int, a: A) { n match {
+ case 0 => m = (m - a).asInstanceOf[M]
+ case 1 => m = m.filter(_._1 != a).asInstanceOf[M]
+ case _ => oor("sub", n)
+ }}
+ def getters: Int = 3
+ def get(n: Int, a: A) = n match {
+ case 0 => m.get(a).getOrElse(-1)
+ case 1 => if (m contains a) m(a) else -1
+ case 2 => m.getOrElse(a, -1)
+ case _ => oor("get", n)
+ }
+ def fiddlers: Int = 0
+ def fiddle(n: Int) { oor("fiddle", n) }
+ def keys = m.keysIterator
+ def has(a: A) = m contains a
+ override def toString = m.toString
+ }
+
+ def boxIhm[A] = new BoxImmutableMap[A, ci.HashMap[A,Int]](new ci.HashMap[A, Int], "immutable.HashMap")
+
+ def boxIim = new BoxImmutableMap[Int, ci.IntMap[Int]](ci.IntMap.empty[Int], "immutable.IntMap")
+
+ def boxIjm = new BoxImmutableMap[Long, ci.LongMap[Int]](ci.LongMap.empty[Int], "immutable.LongMap")
+
+ def boxIlm[A] = new BoxImmutableMap[A, ci.ListMap[A, Int]](new ci.ListMap[A, Int], "immutable.ListMap")
+
+ def boxItm[A: Ordering] = new BoxImmutableMap[A, ci.TreeMap[A, Int]](new ci.TreeMap[A, Int], "immutable.TreeMap")
+
+
+ // Mutable set wrappers placed into the same framework (everything returns 0)
+
+ class BoxMutableSet[A, M <: cm.Set[A]](s0: M, title0: String) extends MapBox[A] {
+ protected var m = s0
+ def title = title0
+ def adders = 5
+ def add(n: Int, a: A, v: Int) { n match {
+ case 0 => m += a
+ case 1 => m(a) = true
+ case 2 => m add a
+ case 3 => m = (m + a).asInstanceOf[M]
+ case 4 => m = (m ++ List(a)).asInstanceOf[M]
+ case _ => oor("add", n)
+ }}
+ def subbers: Int = 3
+ def sub(n: Int, a: A) { n match {
+ case 0 => m -= a
+ case 1 => m = (m - a).asInstanceOf[M]
+ case 2 => m = m.filter(_ != a).asInstanceOf[M]
+ case _ => oor("sub", n)
+ }}
+ def getters: Int = 1
+ def get(n: Int, a: A) = if (m(a)) 0 else -1
+ def fiddlers: Int = 0
+ def fiddle(n: Int) { oor("fiddle", n) }
+ def keys = m.iterator
+ def has(a: A) = m(a)
+ override def toString = m.toString
+ }
+
+ def boxMbs = new BoxMutableSet[Int, cm.BitSet](new cm.BitSet, "mutable.BitSet")
+
+ def boxMhs[A] = new BoxMutableSet[A, cm.HashSet[A]](new cm.HashSet[A], "mutable.HashSet")
+
+ def boxJavaS[A] = new BoxMutableSet[A, cm.Set[A]]((new java.util.HashSet[A]).asScala, "java.util.HashSet") {
+ override def adders = 3
+ override def subbers = 1
+ }
+
+
+ // Immutable set wrappers placed into the same framework (everything returns 0)
+
+ class BoxImmutableSet[A, M <: ci.Set[A]](s0: M, title0: String) extends MapBox[A] {
+ protected var m = s0
+ def title = title0
+ def adders = 2
+ def add(n: Int, a: A, v: Int) { n match {
+ case 0 => m = (m + a).asInstanceOf[M]
+ case 1 => m = (m ++ List(a)).asInstanceOf[M]
+ case _ => oor("add", n)
+ }}
+ def subbers: Int = 2
+ def sub(n: Int, a: A) { n match {
+ case 0 => m = (m - a).asInstanceOf[M]
+ case 1 => m = m.filter(_ != a).asInstanceOf[M]
+ case _ => oor("sub", n)
+ }}
+ def getters: Int = 1
+ def get(n: Int, a: A) = if (m(a)) 0 else -1
+ def fiddlers: Int = 0
+ def fiddle(n: Int) { oor("fiddle", n) }
+ def keys = m.iterator
+ def has(a: A) = m(a)
+ override def toString = m.toString
+ }
+
+ def boxIbs = new BoxImmutableSet[Int, ci.BitSet](ci.BitSet.empty, "immutable.BitSet")
+
+ def boxIhs[A] = new BoxImmutableSet[A, ci.HashSet[A]](ci.HashSet.empty[A], "mutable.HashSet")
+
+ def boxIls[A] = new BoxImmutableSet[A, ci.ListSet[A]](ci.ListSet.empty[A], "mutable.ListSet")
+
+ def boxIts[A: Ordering] = new BoxImmutableSet[A, ci.TreeSet[A]](ci.TreeSet.empty[A], "mutable.TreeSet")
+
+
+ // Random operations on maps
+ def churn[A](map1: MapBox[A], map2: MapBox[A], keys: Array[A], n: Int = 1000, seed: Int = 42, valuer: Int => Int = identity) = {
+ def check = map1.keys.forall(map2 has _) && map2.keys.forall(map1 has _)
+ val rn = new scala.util.Random(seed)
+ var what = new StringBuilder
+ what ++= "creation"
+ for (i <- 0 until n) {
+ if (!check) {
+ val temp = map2 match {
+ case b: BoxImmutableMap[_, _] => b.m match {
+ case hx: ci.HashMap.HashTrieMap[_,_] =>
+ val h = hx.asInstanceOf[ci.HashMap.HashTrieMap[A, Int]]
+ Some((h.bitmap.toHexString, h.elems.mkString, h.size))
+ case _ => None
+ }
+ case _ => None
+ }
+ throw new Exception(s"Disagreement after ${what.result} between ${map1.title} and ${map2.title} because ${map1.keys.map(map2 has _).mkString(",")} ${map2.keys.map(map1 has _).mkString(",")} at step $i:\n$map1\n$map2\n$temp")
+ }
+ what ++= " (%d) ".format(i)
+ if (rn.nextInt(10)==0) {
+
+ if (map1.fiddlers > 0) map1.fiddle({
+ val n = rn.nextInt(map1.fiddlers)
+ what ++= ("f"+n)
+ n
+ })
+ if (map2.fiddlers > 0) map2.fiddle({
+ val n = rn.nextInt(map2.fiddlers)
+ what ++= ("F"+n)
+ n
+ })
+ }
+ if (rn.nextBoolean) {
+ val idx = rn.nextInt(keys.length)
+ val key = keys(rn.nextInt(keys.length))
+ val n1 = rn.nextInt(map1.adders)
+ val n2 = rn.nextInt(map2.adders)
+ what ++= "+%s(%d,%d)".format(key,n1,n2)
+ map1.add(n1, key, valuer(idx))
+ map2.add(n2, key, valuer(idx))
+ }
+ else {
+ val n = rn.nextInt(keys.length)
+ val key = keys(n)
+ val n1 = rn.nextInt(map1.subbers)
+ val n2 = rn.nextInt(map2.subbers)
+ what ++= "-%s(%d,%d)".format(key, n1, n2)
+ //println(s"- $key")
+ map1.sub(n1, key)
+ map2.sub(n2, key)
+ }
+ val j = rn.nextInt(keys.length)
+ val gn1 = rn.nextInt(map1.getters)
+ val gn2 = rn.nextInt(map2.getters)
+ val g1 = map1.get(gn1, keys(j))
+ val g2 = map2.get(gn2, keys(j))
+ if (g1 != g2) {
+ val temp = map2 match {
+ case b: BoxImmutableMap[_, _] => b.m match {
+ case hx: ci.HashMap.HashTrieMap[_,_] =>
+ val h = hx.asInstanceOf[ci.HashMap.HashTrieMap[A, Int]]
+ val y = (ci.HashMap.empty[A, Int] ++ h).asInstanceOf[ci.HashMap.HashTrieMap[A, Int]]
+ Some(((h.bitmap.toHexString, h.elems.mkString, h.size),(y.bitmap.toHexString, y.elems.mkString, y.size)))
+ case _ => None
+ }
+ case _ => None
+ }
+ throw new Exception(s"Disagreement after ${what.result} between ${map1.title} and ${map2.title} on get of ${keys(j)} (#$j) on step $i: $g1 != $g2 using methods $gn1 and $gn2 resp.; in full\n$map1\n$map2\n$temp")
+ }
+ }
+ true
+ }
+
+
+ // Actual tests
+ val smallKeys = Array(0, 1, 42, 9127)
+ val intKeys = smallKeys ++ Array(-1, Int.MaxValue, Int.MinValue, -129385)
+ val longKeys = intKeys.map(_.toLong) ++ Array(Long.MaxValue, Long.MinValue, 1397198789151L, -41402148014L)
+ val stringKeys = intKeys.map(_.toString) ++ Array("", null)
+ val anyKeys = stringKeys.filter(_ != null) ++ Array(0L) ++ Array(true) ++ Array(math.Pi)
+
+ @Test
+ def churnIntMaps() {
+ val maps = Array[() => MapBox[Int]](
+ () => boxMlm[Int], () => boxMhm[Int], () => boxMohm[Int], () => boxJavaM[Int],
+ () => boxIim, () => boxIhm[Int], () => boxIlm[Int], () => boxItm[Int]
+ )
+ assert( maps.sliding(2).forall{ ms => churn(ms(0)(), ms(1)(), intKeys, 2000) } )
+ }
+
+ @Test
+ def churnLongMaps() {
+ val maps = Array[() => MapBox[Long]](
+ () => boxMjm, () => boxIjm, () => boxJavaM[Long],
+ () => boxMlm[Long], () => boxMhm[Long], () => boxMohm[Long], () => boxIhm[Long], () => boxIlm[Long]
+ )
+ assert( maps.sliding(2).forall{ ms => churn(ms(0)(), ms(1)(), longKeys, 10000) } )
+ }
+
+ @Test
+ def churnStringMaps() {
+ // Note: OpenHashMap and TreeMap won't store null, so skip strings
+ val maps = Array[() => MapBox[String]](
+ () => boxMlm[String], () => boxMhm[String], () => boxMarm[String], () => boxJavaM[String],
+ () => boxIhm[String], () => boxIlm[String]
+ )
+ assert( maps.sliding(2).forall{ ms => churn(ms(0)(), ms(1)(), stringKeys, 5000) } )
+ }
+
+ @Test
+ def churnAnyMaps() {
+ val maps = Array[() => MapBox[Any]](
+ () => boxMlm[Any], () => boxMhm[Any], () => boxMohm[Any], () => boxJavaM[Any], () => boxIhm[Any], () => boxIlm[Any]
+ )
+ assert( maps.sliding(2).forall{ ms => churn(ms(0)(), ms(1)(), anyKeys, 10000) } )
+ }
+
+ @Test
+ def churnIntSets() {
+ val sets = Array[() => MapBox[Int]](
+ () => boxMhm[Int], () => boxIhm[Int], () => boxJavaS[Int],
+ () => boxMbs, () => boxMhs[Int], () => boxIbs, () => boxIhs[Int], () => boxIls[Int], () => boxIts[Int]
+ )
+ assert( sets.sliding(2).forall{ ms => churn(ms(0)(), ms(1)(), smallKeys, 1000, valuer = _ => 0) } )
+ }
+
+ @Test
+ def churnAnySets() {
+ val sets = Array[() => MapBox[Any]](
+ () => boxMhm[Any], () => boxIhm[Any], () => boxJavaS[Any],
+ () => boxMhs[Any], () => boxIhs[Any], () => boxIls[Any]
+ )
+ assert( sets.sliding(2).forall{ ms => churn(ms(0)(), ms(1)(), anyKeys, 10000, valuer = _ => 0) } )
+ }
+
+ @Test
+ def extraMutableLongMapTests() {
+ import cm.{LongMap, HashMap}
+ var lm = LongMap.empty[Long]
+ longKeys.zipWithIndex.foreach{ case (k,i) => lm(k) = i }
+ assert{ lm.map{ case (k,v) => -k*k -> v.toString }.getClass == lm.getClass }
+
+ assert {
+ val lm2 = new LongMap[Unit](2000000)
+ for (i <- 0 until 1000000) lm2(i) = ()
+
+ lm2.size == 1000000 &&
+ (0 to 1100000 by 100000).forall(i => (lm2 contains i) == i < 1000000)
+ }
+
+ lm = LongMap(8L -> 22L, -5L -> 5L, Long.MinValue -> 0L)
+
+ assert{ var s = 0L; lm.foreachKey(s += _); s == Long.MinValue + 3 }
+ assert{ var s = 0L; lm.foreachValue(s += _); s == 27L }
+ assert {
+ val m2 = lm.mapValuesNow(_+2)
+ lm.transformValues(_+2)
+ m2 == lm && !(m2 eq lm) && (for ((_,v) <- lm) yield v).sum == 33L
+ }
+
+ assert {
+ val lm2 = new LongMap[String](_.toString)
+ lm2 += (5L -> "fish", 0L -> "unicorn")
+ val hm2 = (new HashMap[Long,String]) ++= lm2
+ List(Long.MinValue, 0L, 1L, 5L).forall(i =>
+ lm2.get(i) == hm2.get(i) &&
+ lm2.getOrElse(i, "") == hm2.getOrElse(i, "") &&
+ lm2(i) == hm2.get(i).getOrElse(i.toString) &&
+ lm2.getOrNull(i) == hm2.get(i).orNull
+ )
+ }
+ }
+
+ @Test
+ def extraMutableAnyRefMapTests() {
+ import cm.{AnyRefMap, HashMap}
+ var arm = AnyRefMap.empty[String, Int]
+ stringKeys.zipWithIndex.foreach{ case (k,i) => arm(k) = i }
+
+ assert{ arm.map{ case (k,v) => (if (k==null) "" else k+k) -> v.toString }.getClass == arm.getClass }
+
+ assert {
+ val arm2 = new AnyRefMap[java.lang.Integer,Unit](2000000)
+ for (i <- 0 until 1000000) arm2(java.lang.Integer.valueOf(i)) = ()
+ arm2.size == 1000000 &&
+ (0 to 1100000 by 100000).map(java.lang.Integer.valueOf).forall(i => (arm2 contains i) == i < 1000000)
+ }
+
+ arm = AnyRefMap("heron" -> 22, "dove" -> 5, "budgie" -> 0)
+
+ assert{
+ var s = ""
+ arm.foreachKey(s += _)
+ s.length == "herondovebudgie".length &&
+ s.contains("heron") &&
+ s.contains("dove") &&
+ s.contains("budgie")
+ }
+
+ assert{ var s = 0L; arm.foreachValue(s += _); s == 27L }
+
+ assert {
+ val m2 = arm.mapValuesNow(_+2)
+ arm.transformValues(_+2)
+ m2 == arm && !(m2 eq arm) && (for ((_,v) <- arm) yield v).sum == 33L
+ }
+
+ assert {
+ val arm2 = new AnyRefMap[String, String](x => if (x==null) "null" else x)
+ arm2 += ("cod" -> "fish", "Rarity" -> "unicorn")
+ val hm2 = (new HashMap[String,String]) ++= arm2
+ List(null, "cod", "sparrow", "Rarity").forall(i =>
+ arm2.get(i) == hm2.get(i) &&
+ arm2.getOrElse(i, "") == hm2.getOrElse(i, "") &&
+ arm2(i) == hm2.get(i).getOrElse(if (i==null) "null" else i.toString) &&
+ arm2.getOrNull(i) == hm2.get(i).orNull
+ )
+ }
+ }
+
+ @Test
+ def extraFilterTests() {
+ type M = scala.collection.Map[Int, Boolean]
+ val manyKVs = (0 to 1000).map(i => i*i*i).map(x => x -> ((x*x*x) < 0))
+ val rn = new scala.util.Random(42)
+ def mhm: M = { val m = new cm.HashMap[Int, Boolean]; m ++= manyKVs; m }
+ def mohm: M = { val m = new cm.OpenHashMap[Int, Boolean]; m ++= manyKVs; m }
+ def ihm: M = ci.HashMap.empty[Int, Boolean] ++ manyKVs
+ val densities = List(0, 0.05, 0.2, 0.5, 0.8, 0.95, 1)
+ def repeat = rn.nextInt(100) < 33
+ def pick(m: M, density: Double) = m.keys.filter(_ => rn.nextDouble < density).toSet
+ def test: Boolean = {
+ for (i <- 0 to 100) {
+ var ms = List(mhm, mohm, ihm)
+ do {
+ val density = densities(rn.nextInt(densities.length))
+ val keep = pick(ms.head, density)
+ ms = ms.map(_.filter(keep contains _._1))
+ if (!ms.sliding(2).forall(s => s(0) == s(1))) return false
+ } while (repeat)
+ }
+ true
+ }
+ assert(test)
+ }
+}
diff --git a/test/junit/scala/math/NumericTest.scala b/test/junit/scala/math/NumericTest.scala
new file mode 100644
index 0000000000..4f0657f471
--- /dev/null
+++ b/test/junit/scala/math/NumericTest.scala
@@ -0,0 +1,18 @@
+
+
+import org.junit.Assert._
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+
+@RunWith(classOf[JUnit4])
+class NumericTest {
+
+ /* Test for SI-8102 */
+ @Test
+ def testAbs {
+ assertTrue(-0.0.abs equals 0.0)
+ assertTrue(-0.0f.abs equals 0.0f)
+ }
+}
+
diff --git a/test/junit/scala/reflect/internal/MirrorsTest.scala b/test/junit/scala/reflect/internal/MirrorsTest.scala
new file mode 100644
index 0000000000..9108af139f
--- /dev/null
+++ b/test/junit/scala/reflect/internal/MirrorsTest.scala
@@ -0,0 +1,18 @@
+package scala.reflect.internal
+
+import org.junit.Assert._
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+
+@RunWith(classOf[JUnit4])
+class MirrorsTest {
+ @Test def rootCompanionsAreConnected(): Unit = {
+ val cm = scala.reflect.runtime.currentMirror
+ import cm._
+ assertEquals("RootPackage.moduleClass == RootClass", RootClass, RootPackage.moduleClass)
+ assertEquals("RootClass.module == RootPackage", RootPackage, RootClass.module)
+ assertEquals("EmptyPackage.moduleClass == EmptyPackageClass", EmptyPackageClass, EmptyPackage.moduleClass)
+ assertEquals("EmptyPackageClass.module == EmptyPackage", EmptyPackage, EmptyPackageClass.module)
+ }
+} \ No newline at end of file
diff --git a/test/junit/scala/reflect/internal/PrintersTest.scala b/test/junit/scala/reflect/internal/PrintersTest.scala
new file mode 100644
index 0000000000..53ea3fd8a3
--- /dev/null
+++ b/test/junit/scala/reflect/internal/PrintersTest.scala
@@ -0,0 +1,815 @@
+package scala.reflect.internal
+
+import org.junit.Test
+import org.junit.Assert._
+import scala.tools.reflect._
+import scala.reflect.runtime.universe._
+import scala.reflect.runtime.{currentMirror=>cm}
+import org.junit.runner.RunWith
+import org.junit.runners.JUnit4
+
+@RunWith(classOf[JUnit4])
+class PrintersTest extends BasePrintTests
+ with ClassPrintTests
+ with TraitPrintTests
+ with ValAndDefPrintTests
+ with QuasiTreesPrintTests
+ with PackagePrintTests
+
+object PrinterHelper {
+ val toolbox = cm.mkToolBox()
+ def assertPrintedCode(code: String, tree: Tree = EmptyTree) = {
+ val toolboxTree =
+ try{
+ toolbox.parse(code)
+ } catch {
+ case e:scala.tools.reflect.ToolBoxError => throw new Exception(e.getMessage + ": " + code)
+ }
+ if (tree ne EmptyTree) assertEquals("using quasiquote or given tree"+"\n", code.trim, showCode(tree))
+ else assertEquals("using toolbox parser", code.trim, showCode(toolboxTree))
+ }
+
+ implicit class StrContextStripMarginOps(val stringContext: StringContext) extends util.StripMarginInterpolator
+}
+
+import PrinterHelper._
+
+trait BasePrintTests {
+ @Test def testIdent = assertPrintedCode("*", Ident("*"))
+
+ @Test def testConstant1 = assertPrintedCode("\"*\"", Literal(Constant("*")))
+
+ @Test def testConstant2 = assertPrintedCode("42", Literal(Constant(42)))
+
+ @Test def testConstantFloat = assertPrintedCode("42.0F", Literal(Constant(42f)))
+
+ @Test def testConstantDouble = assertPrintedCode("42.0", Literal(Constant(42d)))
+
+ @Test def testConstantLong = assertPrintedCode("42L", Literal(Constant(42l)))
+
+ @Test def testOpExpr = assertPrintedCode("(5).+(4)")
+
+ @Test def testName1 = assertPrintedCode("class test")
+
+ @Test def testName2 = assertPrintedCode("class *")
+
+ @Test def testName4 = assertPrintedCode("class `a*`")
+
+ @Test def testName5 = assertPrintedCode("val :::: = 1")
+
+ @Test def testName6 = assertPrintedCode("val `::::t` = 1")
+
+ @Test def testName7 = assertPrintedCode("""class \/""")
+
+ @Test def testName8 = assertPrintedCode("""class \\\\""")
+
+ @Test def testName9 = assertPrintedCode("""class test_\/""")
+
+ @Test def testName10 = assertPrintedCode("""class `*_*`""")
+
+ @Test def testName11 = assertPrintedCode("""class `a_*`""")
+
+ @Test def testName12 = assertPrintedCode("""class `*_a`""")
+
+ @Test def testName13 = assertPrintedCode("""class a_a""")
+
+ @Test def testName14 = assertPrintedCode("val x$11 = 5")
+
+ @Test def testName15 = assertPrintedCode("class `[]`")
+
+ @Test def testName16 = assertPrintedCode("class `()`")
+
+ @Test def testName17 = assertPrintedCode("class `{}`")
+
+ @Test def testName18 = assertPrintedCode("class <>")
+
+ @Test def testName19 = assertPrintedCode("""class `class`""")
+
+ @Test def testName20 = assertPrintedCode("""class `test name`""")
+
+ @Test def testIfExpr1 = assertPrintedCode(sm"""
+ |if (a)
+ | ((expr1): Int)
+ |else
+ | ((expr2): Int)""")
+
+ @Test def testIfExpr2 = assertPrintedCode(sm"""
+ |(if (a)
+ | {
+ | expr1;
+ | ()
+ | }
+ |else
+ | {
+ | expr2;
+ | ()
+ | }).toString""")
+
+ @Test def testIfExpr3 = assertPrintedCode(sm"""
+ |(if (a)
+ | {
+ | expr1;
+ | ()
+ | }
+ |else
+ | {
+ | expr2;
+ | ()
+ | }).method1().method2()""")
+
+ //val x = true && true && false.!
+ @Test def testBooleanExpr1 = assertPrintedCode("val x = true.&&(true).&&(false.!)")
+
+ //val x = true && !(true && false)
+ @Test def testBooleanExpr2 = assertPrintedCode("val x = true.&&(true.&&(false).`unary_!`)")
+
+ @Test def testNewExpr1 = assertPrintedCode("new foo()")
+
+ //new foo { test }
+ @Test def testNewExpr2 = assertPrintedCode(sm"""
+ |{
+ | final class $$anon extends foo {
+ | test
+ | };
+ | new $$anon()
+ |}""")
+
+ @Test def testNewExpr3 = assertPrintedCode("new foo[t]()")
+
+ @Test def testNewExpr4 = assertPrintedCode("new foo(x)")
+
+ @Test def testNewExpr5 = assertPrintedCode("new foo[t](x)")
+
+ //new foo[t](x) { () }
+ @Test def testNewExpr6 = assertPrintedCode(sm"""
+ |{
+ | final class $$anon extends foo[t](x) {
+ | ()
+ | };
+ | new $$anon()
+ |}""")
+
+ //new foo with bar
+ @Test def testNewExpr7 = assertPrintedCode(sm"""
+ |{
+ | final class $$anon extends foo with bar;
+ | new $$anon()
+ |}""")
+
+ //new { anonymous }
+ @Test def testNewExpr8 = assertPrintedCode(sm"""
+ |{
+ | final class $$anon {
+ | anonymous
+ | };
+ | new $$anon()
+ |}""")
+
+ //new { val early = 1 } with Parent[Int] { body }
+ @Test def testNewExpr9 = assertPrintedCode(sm"""
+ |{
+ | final class $$anon extends {
+ | val early = 1
+ | } with Parent[Int] {
+ | body
+ | };
+ | new $$anon()
+ |}""")
+
+ //new Foo { self => }
+ @Test def testNewExpr10 = assertPrintedCode(sm"""
+ |{
+ | final class $$anon extends Foo { self =>
+ |
+ | };
+ | new $$anon()
+ |}""")
+
+ @Test def testReturn = assertPrintedCode("def test: Int = return 42")
+
+ @Test def testFunc1 = assertPrintedCode("List(1, 2, 3).map(((i: Int) => i.-(1)))")
+
+ //val sum: Seq[Int] => Int = _ reduceLeft (_+_)
+ @Test def testFunc2 = assertPrintedCode("val sum: _root_.scala.Function1[Seq[Int], Int] = ((x$1) => x$1.reduceLeft(((x$2, x$3) => x$2.+(x$3))))")
+
+ //List(1, 2, 3) map (_ - 1)
+ @Test def testFunc3 = assertPrintedCode("List(1, 2, 3).map(((x$1) => x$1.-(1)))")
+
+ @Test def testImport1 = assertPrintedCode("import scala.collection.mutable")
+
+ @Test def testImport2 = assertPrintedCode("import java.lang.{String=>Str}")
+
+ @Test def testImport3 = assertPrintedCode("import java.lang.{String=>Str, Object=>_, _}")
+
+ @Test def testImport4 = assertPrintedCode("import scala.collection._")
+}
+
+trait ClassPrintTests {
+ @Test def testClass = assertPrintedCode("class *")
+
+ @Test def testClassWithBody = assertPrintedCode(sm"""
+ |class X {
+ | def y = "test"
+ |}""")
+
+ @Test def testClassWithPublicParams = assertPrintedCode("class X(val x: Int, val s: String)")
+
+ @Test def testClassWithParams1 = assertPrintedCode("class X(x: Int, s: String)")
+
+ @Test def testClassWithParams2 = assertPrintedCode("class X(@test x: Int, s: String)")
+
+ @Test def testClassWithParams3 = assertPrintedCode("class X(implicit x: Int, s: String)")
+
+ @Test def testClassWithParams4 = assertPrintedCode("class X(implicit @test x: Int, s: String)")
+
+ @Test def testClassWithParams5 = assertPrintedCode("class X(override private[this] val x: Int, s: String) extends Y")
+
+ @Test def testClassWithParams6 = assertPrintedCode("class X(@test1 override private[this] val x: Int, @test2(param1 = 7) s: String) extends Y")
+
+ @Test def testClassWithParams7 = assertPrintedCode("class X protected (val x: Int, val s: String)")
+
+ @Test def testClassWithParams8 = assertPrintedCode("class X(var x: Int)")
+
+ @Test def testClassWithParams9 = assertPrintedCode("class X(var x: Int*)")
+
+ @Test def testClassWithByNameParam = assertPrintedCode("class X(x: => Int)")
+
+ @Test def testClassWithDefault = assertPrintedCode("class X(var x: Int = 5)")
+
+ @Test def testClassWithParams10 = assertPrintedCode("class X(protected[zzz] var x: Int)")
+
+ @Test def testClassWithParams11 = assertPrintedCode("class X(override var x: Int) extends F(x) with E(x)")
+
+ @Test def testClassWithParams12 = assertPrintedCode("class X(val y: Int)()(var z: Double)")
+
+ @Test def testClassWithImplicitParams = assertPrintedCode("class X(var i: Int)(implicit val d: Double, var f: Float)")
+
+ @Test def testClassWithEarly = assertPrintedCode(sm"""
+ |class X(var i: Int) extends {
+ | val a: String = i;
+ | type B
+ |} with Y""")
+
+ @Test def testClassWithThrow1 = assertPrintedCode(sm"""
+ |class Throw1 {
+ | throw new Exception("exception!")
+ |}""")
+
+ @Test def testClassWithThrow2 = assertPrintedCode(sm"""
+ |class Throw2 {
+ | var msg = " ";
+ | val e = new Exception(msg);
+ | throw e
+ |}""")
+
+ /*
+ class Test {
+ val (a, b) = (1, 2)
+ }
+ */
+ @Test def testClassWithAssignmentWithTuple1 = assertPrintedCode(sm"""
+ |class Test {
+ | private[this] val x$$1 = (scala.Tuple2(1, 2): @scala.unchecked) match {
+ | case scala.Tuple2((a @ _), (b @ _)) => scala.Tuple2(a, b)
+ | };
+ | val a = x$$1._1;
+ | val b = x$$1._2
+ |}""")
+
+ /*
+ class Test {
+ val (a, b) = (1).->(2)
+ }
+ */
+ @Test def testClassWithAssignmentWithTuple2 = assertPrintedCode(sm"""
+ |class Test {
+ | private[this] val x$$1 = ((1).->(2): @scala.unchecked) match {
+ | case scala.Tuple2((a @ _), (b @ _)) => scala.Tuple2(a, b)
+ | };
+ | val a = x$$1._1;
+ | val b = x$$1._2
+ |}""")
+
+ /*
+ class Test {
+ val List(one, three, five) = List(1,3,5)
+ }
+ */
+ @Test def testClassWithPatternMatchInAssignment = assertPrintedCode(sm"""
+ |class Test {
+ | private[this] val x$$1 = (List(1, 3, 5): @scala.unchecked) match {
+ | case List((one @ _), (three @ _), (five @ _)) => scala.Tuple3(one, three, five)
+ | };
+ | val one = x$$1._1;
+ | val three = x$$1._2;
+ | val five = x$$1._3
+ |}""")
+
+ //class A(l: List[_])
+ @Test def testClassWithExistentialParameter1 = assertPrintedCode(sm"""
+ |class Test(l: (List[_$$1] forSome {
+ | type _$$1
+ |}))""")
+
+ @Test def testClassWithExistentialParameter2 = assertPrintedCode(sm"""
+ |class B(l: (List[T] forSome {
+ | type T
+ |}))""")
+
+ @Test def testClassWithCompoundTypeTree = assertPrintedCode(sm"""
+ |{
+ | trait A;
+ | trait B;
+ | abstract class C(val a: A with B) {
+ | def method(x: A with B with C {
+ | val x: Float
+ | }): A with B
+ | };
+ | ()
+ |}""")
+
+ @Test def testClassWithSelectFromTypeTree = assertPrintedCode(sm"""
+ |{
+ | trait A {
+ | type T
+ | };
+ | class B(t: (A)#T);
+ | ()
+ |}""")
+
+ @Test def testImplicitClass = assertPrintedCode("implicit class X(protected[zzz] var x: Int)")
+
+ @Test def testAbstractClass = assertPrintedCode("abstract class X(protected[zzz] var x: Int)")
+
+ @Test def testCaseClassWithParams1 = assertPrintedCode("case class X(x: Int, s: String)")
+
+ @Test def testCaseClassWithParams2 = assertPrintedCode("case class X(protected val x: Int, s: String)")
+
+ @Test def testCaseClassWithParams3 = assertPrintedCode("case class X(implicit x: Int, s: String)")
+
+ @Test def testCaseClassWithParams4 = assertPrintedCode("case class X(override val x: Int, s: String) extends Y")
+
+ @Test def testCaseClassWithBody = assertPrintedCode(sm"""
+ |case class X() {
+ | def y = "test"
+ |}""")
+
+ @Test def testLocalClass = assertPrintedCode(sm"""
+ |def test = {
+ | class X(var a: Int) {
+ | def y = "test"
+ | };
+ | new X(5)
+ |}""")
+
+ @Test def testLocalCaseClass = assertPrintedCode(sm"""
+ |def test = {
+ | case class X(var a: Int) {
+ | def y = "test"
+ | };
+ | new X(5)
+ |}""")
+
+ @Test def testSuperInClass = assertPrintedCode(sm"""
+ |{
+ | trait Root {
+ | def r = "Root"
+ | };
+ | class X extends Root {
+ | def superX = super.r
+ | };
+ | class Y extends X with Root {
+ | class Inner {
+ | val myY = Y.super.r
+ | };
+ | def fromX = super[X].r;
+ | def fromRoot = super[Root].r
+ | };
+ | ()
+ |}""")
+
+ @Test def testThisInClass = assertPrintedCode(sm"""
+ |class Outer {
+ | class Inner {
+ | val outer = Root.this
+ | };
+ | val self = this
+ |}""")
+
+ @Test def testCaseClassWithParamsAndBody = assertPrintedCode(sm"""
+ |case class X(x: Int, s: String) {
+ | def y = "test"
+ |}""")
+
+ @Test def testObject = assertPrintedCode("object *")
+
+ @Test def testObjectWithBody = assertPrintedCode(sm"""
+ |object X {
+ | def y = "test"
+ |}""")
+
+ @Test def testObjectWithEarly1 = assertPrintedCode(sm"""
+ |object X extends {
+ | val early: T = v
+ |} with Bar""")
+
+ @Test def testObjectWithEarly2 = assertPrintedCode(sm"""
+ |object X extends {
+ | val early: T = v;
+ | type EarlyT = String
+ |} with Bar""")
+
+ @Test def testObjectWithSelf = assertPrintedCode(sm"""
+ |object Foo extends Foo { self =>
+ | body
+ |}""")
+
+ @Test def testObjectInh = assertPrintedCode("private[Y] object X extends Bar with Baz")
+
+ @Test def testObjectWithPatternMatch1 = assertPrintedCode(sm"""
+ |object PM1 {
+ | List(1, 2) match {
+ | case (i @ _) => i
+ | }
+ |}""")
+
+ @Test def testObjectWithPatternMatch2 = assertPrintedCode(sm"""
+ |object PM2 {
+ | List(1, 2).map({
+ | case (i @ _) if i.>(5) => i
+ | })
+ |}""")
+
+ //case i: Int => i
+ @Test def testObjectWithPatternMatch3 = assertPrintedCode(sm"""
+ |object PM3 {
+ | List(1, 2).map({
+ | case (i @ ((_): Int)) => i
+ | })
+ |}""")
+
+ //case a @ (i: Int) => i
+ @Test def testObjectWithPatternMatch4 = assertPrintedCode(sm"""
+ |object PM4 {
+ | List(1, 2).map({
+ | case (a @ (i @ ((_): Int))) => i
+ | })
+ |}""")
+
+ @Test def testObjectWithPatternMatch5 = assertPrintedCode(sm"""
+ |object PM5 {
+ | List(1, 2).map({
+ | case _ => 42
+ | })
+ |}""")
+
+ @Test def testObjectWithPatternMatch6 = assertPrintedCode(sm"""
+ |object PM6 {
+ | List(1, 2) match {
+ | case ::((x @ _), (xs @ _)) => x
+ | }
+ |}""")
+
+ @Test def testObjectWithPatternMatch7 = assertPrintedCode(sm"""
+ |object PM7 {
+ | List(1, 2).map({
+ | case (0| 1) => true
+ | case _ => false
+ | })
+ |}""")
+
+ @Test def testObjectWithPatternMatch8 = assertPrintedCode(sm"""
+ |object PM8 {
+ | "abcde".toList match {
+ | case Seq((car @ _), _*) => car
+ | }
+ |}""")
+
+ @Test def testObjectWithPatternMatch9 = assertPrintedCode(sm"""
+ |{
+ | object Extractor {
+ | def unapply(i: Int) = Some(i)
+ | };
+ | object PM9 {
+ | 42 match {
+ | case (a @ Extractor((i @ _))) => i
+ | }
+ | };
+ | ()
+ |}""")
+
+ @Test def testObjectWithPartialFunc = assertPrintedCode(sm"""
+ |object Test {
+ | def partFuncTest[A, B](e: Either[A, B]): scala.Unit = e match {
+ | case Right(_) => ()
+ | }
+ |}""")
+
+ @Test def testObjectWithTry = assertPrintedCode(sm"""
+ |object Test {
+ | import java.io;
+ | var file: PrintStream = null;
+ | try {
+ | val out = new FileOutputStream("myfile.txt");
+ | file = new PrintStream(out)
+ | } catch {
+ | case (ioe @ ((_): IOException)) => println("ioe")
+ | case (e @ ((_): Exception)) => println("e")
+ | } finally println("finally")
+ |}""")
+}
+
+trait TraitPrintTests {
+ @Test def testTrait = assertPrintedCode("trait *")
+
+ @Test def testTraitWithBody = assertPrintedCode(sm"""
+ |trait X {
+ | def y = "test"
+ |}""")
+
+ @Test def testTraitWithSelfTypeAndBody = assertPrintedCode(sm"""
+ |trait X { self: Order =>
+ | def y = "test"
+ |}""")
+
+ @Test def testTraitWithSelf1 = assertPrintedCode(sm"""
+ |trait X { self =>
+ | def y = "test"
+ |}""")
+
+ @Test def testTraitWithSelf2 = assertPrintedCode(sm"""
+ |trait X { self: Foo with Bar =>
+ | val x: Int = 1
+ |}""")
+
+ @Test def testTraitTypeParams = assertPrintedCode("trait X[A, B]")
+
+ @Test def testTraitWithBody2 = assertPrintedCode(sm"""
+ |trait X {
+ | def foo: scala.Unit;
+ | val bar: Baz
+ |}""")
+
+ @Test def testTraitWithInh = assertPrintedCode("trait X extends A with B")
+
+ @Test def testTraitWithEarly1 = assertPrintedCode(sm"""
+ |trait X extends {
+ | val x: Int = 1
+ |} with Any""")
+
+ @Test def testTraitWithEarly2 = assertPrintedCode(sm"""
+ |trait X extends {
+ | val x: Int = 0;
+ | type Foo = Bar
+ |} with Y""")
+
+ @Test def testTraitWithEarly3 = assertPrintedCode(sm"""
+ |trait X extends {
+ | val x: Int = 5;
+ | val y: Double = 4.0;
+ | type Foo;
+ | type XString = String
+ |} with Y""")
+
+ @Test def testTraitWithEarly4 = assertPrintedCode(sm"""
+ |trait X extends {
+ | val x: Int = 5;
+ | val y: Double = 4.0;
+ | type Foo;
+ | type XString = String
+ |} with Y {
+ | val z = 7
+ |}""")
+
+ @Test def testTraitWithEarly5 = assertPrintedCode(sm"""
+ |trait X extends {
+ | override protected[this] val x: Int = 5;
+ | val y: Double = 4.0;
+ | private type Foo;
+ | private[ee] type XString = String
+ |} with Y {
+ | val z = 7
+ |}""")
+
+ @Test def testTraitWithSingletonTypeTree = assertPrintedCode(sm"""
+ |trait Test {
+ | def testReturnSingleton(): this.type
+ |}""")
+
+ @Test def testTraitWithThis = assertPrintedCode(sm"""
+ |trait Test { _ : X with Y =>
+ |
+ |}""", q"trait Test { this: X with Y => }")
+
+ @Test def testTraitWithWhile1 = assertPrintedCode(sm"""
+ |trait Test {
+ | while (true.!=(false))
+ | println("testing...")
+ |
+ |}""")
+
+ @Test def testTraitWithWhile2 = assertPrintedCode(sm"""
+ |trait Test {
+ | while (true)
+ | {
+ | println("testing...");
+ | println("testing...")
+ | }
+ |
+ |}""")
+
+ @Test def testTraitWithDoWhile1 = assertPrintedCode(sm"""
+ |trait Test {
+ | do
+ | println("testing...")
+ | while (true)
+ |}""")
+
+ @Test def testTraitWithTypes = assertPrintedCode(sm"""
+ |trait Test {
+ | type A = Int;
+ | type B >: Nothing <: AnyRef;
+ | protected type C >: Nothing;
+ | type D <: AnyRef
+ |}""")
+}
+
+trait ValAndDefPrintTests {
+ @Test def testVal1 = assertPrintedCode("val a: Unit = null")
+
+ @Test def testVal2 = assertPrintedCode("val * : Unit = null")
+
+ @Test def testVal3 = assertPrintedCode("val a_ : Unit = null")
+
+ @Test def testDef1 = assertPrintedCode("def a: Unit = null")
+
+ @Test def testDef2 = assertPrintedCode("def * : Unit = null")
+
+ @Test def testDef3 = assertPrintedCode("def a_(x: Int): Unit = null")
+
+ @Test def testDef4 = assertPrintedCode("def a_ : Unit = null")
+
+ @Test def testDef5 = assertPrintedCode("def a_(* : Int): Unit = null")
+
+ @Test def testDef6 = assertPrintedCode("def a_(b_ : Int): Unit = null")
+
+ @Test def testDef7 = assertPrintedCode(sm"""
+ |{
+ | def test1 = ();
+ | def test2() = ()
+ |}""",
+ Block(
+ DefDef(NoMods, newTermName("test1"), Nil, Nil, EmptyTree, Literal(Constant(()))),
+ DefDef(NoMods, newTermName("test2"), Nil, Nil :: Nil, EmptyTree, Literal(Constant(())))
+ )
+ )
+
+ @Test def testDef8 = {
+ val arg = ValDef(Modifiers(Flag.IMPLICIT) , newTermName("a"),
+ AppliedTypeTree(Ident(newTypeName("R")), List(Ident(newTypeName("X")))), EmptyTree)
+
+ //def m[X](implicit a: R[X]) = ()
+ val tree = DefDef(NoMods, newTermName("test"), TypeDef(NoMods, newTypeName("X"), Nil, EmptyTree) :: Nil,
+ List(List(arg)), EmptyTree, Literal(Constant(())))
+
+ assertPrintedCode("def test[X](implicit a: R[X]) = ()", tree)
+ }
+
+ @Test def testDefWithParams1 = assertPrintedCode("def foo(x: Int*) = null")
+
+ @Test def testDefWithParams2 = assertPrintedCode("def foo(x: Int)(y: Int = 1) = null")
+
+ @Test def testDefWithTypeParams1 = assertPrintedCode("def foo[A, B, C](x: A)(y: Int = 1): C = null")
+
+ @Test def testDefWithTypeParams2 = assertPrintedCode("def foo[A, B <: Bar] = null")
+
+ @Test def testDefWithAnn1 = assertPrintedCode("@annot def foo = null")
+
+ @Test def testDefWithAnn2 = assertPrintedCode("@a(x) def foo = null")
+
+ @Test def testDefWithAnn3 = assertPrintedCode("@Foo[A, B] def foo = null")
+
+ @Test def testDefWithAnn4 = assertPrintedCode("@Foo(a)(b)(x, y) def foo = null")
+
+ @Test def testDefWithAnn5 = assertPrintedCode("@Foo[A, B](a)(b) @Bar def foo(x: Int) = null")
+
+ @Test def testDefWithAnn6 = assertPrintedCode("@test1(new test2()) def foo = 42")
+
+ @Test def testDefWithAnn7 = assertPrintedCode("@`t*` def foo = 42")
+
+ @Test def testDefWithAnn8 = assertPrintedCode("@throws(classOf[Exception]) def foo = throw new Exception()")
+
+ @Test def testAnnotated1 = assertPrintedCode("def foo = 42: @test1")
+
+ @Test def testAnnotated2 = assertPrintedCode("""def foo = 42: @test1(42, z = "5")""")
+
+ @Test def testAnnotated3 = assertPrintedCode("def foo = (42: @test1): @test2(new test1())")
+
+ @Test def testAnnotated4 = assertPrintedCode("""def foo = 42: @test1(4, "testing")(4.2)""")
+
+ @Test def testAnnotated5 = assertPrintedCode("""def foo = (42: @test1(4, "testing")(4.2)): @test2(1, "bar")(3.14)""")
+
+ @Test def testAnnotated6 = assertPrintedCode("def foo = ((42: @test1): @test2(new test1())): @test3(1)(2, 3)(4)")
+
+ @Test def testAnnotated7 = assertPrintedCode(sm"""
+ |(x: @unchecked) match {
+ | case ((_): Int) => true
+ | case _ => false
+ |}""")
+
+ @Test def testAnnotated8 = assertPrintedCode(sm"""
+ |((x: @unchecked): @test1(1, "testing")(3.14)) match {
+ | case _ => true
+ |}""")
+}
+
+trait PackagePrintTests {
+ @Test def testPackage1 = assertPrintedCode(sm"""
+ |package foo.bar {
+ |
+ |}""")
+
+ @Test def testPackage2 = assertPrintedCode(sm"""
+ |package foo {
+ | class C
+ |
+ | object D
+ |}""")
+
+ //package object foo extends a with b
+ @Test def testPackage3 = assertPrintedCode(sm"""
+ |package foo {
+ | object `package` extends a with b
+ |}""")
+
+ //package object foo { def foo; val x = 1 }
+ @Test def testPackage4 = assertPrintedCode(sm"""
+ |package foo {
+ | object `package` {
+ | def foo: scala.Unit;
+ | val x = 1
+ | }
+ |}""")
+
+ //package object foo extends { val x = 1; type I = Int } with Any
+ @Test def testPackage5 = assertPrintedCode(sm"""
+ |package foo {
+ | object `package` extends {
+ | val x = 1;
+ | type I = Int
+ | } with Any
+ |}""")
+}
+
+trait QuasiTreesPrintTests {
+ @Test def testQuasiIdent = assertPrintedCode("*", q"*")
+
+ @Test def testQuasiVal = assertPrintedCode("val * : Unit = null", q"val * : Unit = null")
+
+ @Test def testQuasiDef = assertPrintedCode("def * : Unit = null", q"def * : Unit = null")
+
+ @Test def testQuasiTrait = assertPrintedCode("trait *", q"trait *")
+
+ @Test def testQuasiClass = assertPrintedCode("class *", q"class *")
+
+ @Test def testQuasiClassWithPublicParams = assertPrintedCode( "class X(val x: Int, val s: String)", q"class X(val x: Int, val s:String)" )
+
+ @Test def testQuasiClassWithParams = assertPrintedCode("class X(x: Int, s: String)", q"class X(x: Int, s:String)")
+
+ @Test def testQuasiObject = assertPrintedCode("object *", q"object *")
+
+ @Test def testQuasiObjectWithBody = assertPrintedCode(sm"""
+ |object X {
+ | def y = "test"
+ |}""", q"""object X{ def y = "test" }""")
+
+ @Test def testQuasiClassWithBody = assertPrintedCode(sm"""
+ |class X {
+ | def y = "test"
+ |}""", q"""class X{ def y = "test" }""")
+
+ @Test def testQuasiTraitWithBody = assertPrintedCode(sm"""
+ |trait X {
+ | def y = "test"
+ |}""", q"""trait X{ def y = "test" }""")
+
+ @Test def testQuasiTraitWithSelfTypeAndBody = assertPrintedCode(sm"""
+ |trait X { self: Order =>
+ | def y = "test"
+ |}""", q"""trait X{ self: Order => def y = "test" }""")
+
+ @Test def testQuasiTraitWithSelf = assertPrintedCode(sm"""
+ |trait X { self =>
+ | def y = "test"
+ |}""", q"""trait X{ self => def y = "test" }""")
+
+ @Test def testQuasiCaseClassWithBody = assertPrintedCode(sm"""
+ |case class X() {
+ | def y = "test"
+ |}""", q"""case class X() { def y = "test" }""")
+
+ @Test def testQuasiCaseClassWithParamsAndBody = assertPrintedCode(sm"""
+ |case class X(x: Int, s: String) {
+ | def y = "test"
+ |}""", q"""case class X(x: Int, s: String){ def y = "test" }""")
+}