diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/files/jvm/serialization-new.check | 2 | ||||
-rw-r--r-- | test/files/jvm/serialization.check | 2 | ||||
-rw-r--r-- | test/files/neg/t7623.check | 21 | ||||
-rw-r--r-- | test/files/neg/t7623.flags | 1 | ||||
-rw-r--r-- | test/files/neg/t7623.scala | 38 | ||||
-rw-r--r-- | test/files/neg/t9093.check | 6 | ||||
-rw-r--r-- | test/files/neg/t9093.scala | 5 | ||||
-rw-r--r-- | test/files/pos/t9050.scala | 13 | ||||
-rw-r--r-- | test/files/run/bitsets.check | 1 | ||||
-rw-r--r-- | test/junit/scala/StringContextTest.scala | 15 | ||||
-rw-r--r-- | test/junit/scala/collection/IterableViewLikeTest.scala | 1 | ||||
-rw-r--r-- | test/junit/scala/collection/immutable/VectorTest.scala | 20 | ||||
-rw-r--r-- | test/junit/scala/collection/mutable/MutableListTest.scala | 37 | ||||
-rw-r--r-- | test/junit/scala/tools/nsc/symtab/SymbolTableTest.scala | 5 | ||||
-rw-r--r-- | test/junit/scala/tools/testing/AssertThrowsTest.scala | 2 | ||||
-rw-r--r-- | test/junit/scala/tools/testing/AssertUtil.scala | 59 | ||||
-rw-r--r-- | test/junit/scala/tools/testing/AssertUtilTest.scala | 21 |
17 files changed, 238 insertions, 11 deletions
diff --git a/test/files/jvm/serialization-new.check b/test/files/jvm/serialization-new.check index 1555135926..cb26446f40 100644 --- a/test/files/jvm/serialization-new.check +++ b/test/files/jvm/serialization-new.check @@ -1,4 +1,4 @@ -warning: there were two deprecation warnings; re-run with -deprecation for details +warning: there were three deprecation warnings; re-run with -deprecation for details a1 = Array[1,2,3] _a1 = Array[1,2,3] arrayEquals(a1, _a1): true diff --git a/test/files/jvm/serialization.check b/test/files/jvm/serialization.check index 1555135926..cb26446f40 100644 --- a/test/files/jvm/serialization.check +++ b/test/files/jvm/serialization.check @@ -1,4 +1,4 @@ -warning: there were two deprecation warnings; re-run with -deprecation for details +warning: there were three deprecation warnings; re-run with -deprecation for details a1 = Array[1,2,3] _a1 = Array[1,2,3] arrayEquals(a1, _a1): true diff --git a/test/files/neg/t7623.check b/test/files/neg/t7623.check new file mode 100644 index 0000000000..db368dd369 --- /dev/null +++ b/test/files/neg/t7623.check @@ -0,0 +1,21 @@ +t7623.scala:19: warning: A repeated case parameter or extracted sequence should be matched only by a sequence wildcard (_*). + def f = "" match { case X(s) => } + ^ +t7623.scala:21: warning: A repeated case parameter or extracted sequence should be matched only by a sequence wildcard (_*). + def g = "" match { case X(s, t) => } + ^ +t7623.scala:23: warning: A repeated case parameter or extracted sequence should be matched only by a sequence wildcard (_*). + def h = "" match { case X(s, t, u @ _*) => } + ^ +t7623.scala:9: warning: A repeated case parameter or extracted sequence should be matched only by a sequence wildcard (_*). + def f = C("") match { case C(s) => } + ^ +t7623.scala:11: warning: A repeated case parameter or extracted sequence should be matched only by a sequence wildcard (_*). + def g = C("") match { case C(s, t) => } + ^ +t7623.scala:13: warning: A repeated case parameter or extracted sequence should be matched only by a sequence wildcard (_*). + def h = C("") match { case C(s, t, u @ _*) => } + ^ +error: No warnings can be incurred under -Xfatal-warnings. +6 warnings found +one error found diff --git a/test/files/neg/t7623.flags b/test/files/neg/t7623.flags new file mode 100644 index 0000000000..74c9e38323 --- /dev/null +++ b/test/files/neg/t7623.flags @@ -0,0 +1 @@ +-Xlint:stars-align -Xfatal-warnings diff --git a/test/files/neg/t7623.scala b/test/files/neg/t7623.scala new file mode 100644 index 0000000000..5c40f37bc1 --- /dev/null +++ b/test/files/neg/t7623.scala @@ -0,0 +1,38 @@ + + +case class C(s: String, xs: Int*) + +object X { def unapplySeq(a: Any): Option[(String, Seq[Int])] = Some("", List(1,2,3)) } + +// for case classes with varargs, avoid misaligned patterns +trait Ctest { + def f = C("") match { case C(s) => } + + def g = C("") match { case C(s, t) => } + + def h = C("") match { case C(s, t, u @ _*) => } + + def ok = C("") match { case C(s, u @ _*) => } +} +// for extractors that unapplySeq: Option[(Something, Seq[_])], avoid misaligned patterns +trait Xtest { + def f = "" match { case X(s) => } + + def g = "" match { case X(s, t) => } + + def h = "" match { case X(s, t, u @ _*) => } + + def ok = "" match { case X(s, u @ _*) => } +} +// for extractors that unapplySeq: Option[Seq[_]], anything goes +trait Rtest { + val r = "(a+)".r + + def f = "" match { case r(s) => } + + def g = "" match { case r(s, t) => } + + def h = "" match { case r(s, t, u @ _*) => } + + def whatever = "" match { case r(u @ _*) => } +} diff --git a/test/files/neg/t9093.check b/test/files/neg/t9093.check new file mode 100644 index 0000000000..085a433f0b --- /dev/null +++ b/test/files/neg/t9093.check @@ -0,0 +1,6 @@ +t9093.scala:3: error: polymorphic expression cannot be instantiated to expected type; + found : [C](f: C)Null + required: Unit + val x: Unit = apply2(0)/*(0)*/ + ^ +one error found diff --git a/test/files/neg/t9093.scala b/test/files/neg/t9093.scala new file mode 100644 index 0000000000..d9922ad70e --- /dev/null +++ b/test/files/neg/t9093.scala @@ -0,0 +1,5 @@ +object Main { + def apply2[C](fa: Any)(f: C) = null + val x: Unit = apply2(0)/*(0)*/ +} + diff --git a/test/files/pos/t9050.scala b/test/files/pos/t9050.scala new file mode 100644 index 0000000000..b1ab09f901 --- /dev/null +++ b/test/files/pos/t9050.scala @@ -0,0 +1,13 @@ +final class Mu[F](val value: Any) extends AnyVal { + def cata(f: F) { + // crash + ((y: Mu[F]) => y.cata(f)) + // crash + def foo(x : Mu[F]) = x.cata(f) + + // // okay + def x: Mu[F] = ??? + (() => x.cata(f)) + assert(true, cata(f)) + } +} diff --git a/test/files/run/bitsets.check b/test/files/run/bitsets.check index 41c2ccdcb8..c24fd6238f 100644 --- a/test/files/run/bitsets.check +++ b/test/files/run/bitsets.check @@ -1,3 +1,4 @@ +warning: there were three deprecation warnings; re-run with -deprecation for details ms0 = BitSet(2) ms1 = BitSet(2) ms2 = BitSet(2) diff --git a/test/junit/scala/StringContextTest.scala b/test/junit/scala/StringContextTest.scala index 608b82bd96..7e9e775d58 100644 --- a/test/junit/scala/StringContextTest.scala +++ b/test/junit/scala/StringContextTest.scala @@ -65,14 +65,23 @@ class StringContextTest { @Test def fIf() = { val res = f"${if (true) 2.5 else 2.5}%.2f" - assertEquals("2.50", res) + val expected = formatUsingCurrentLocale(2.50) + assertEquals(expected, res) } + @Test def fIfNot() = { val res = f"${if (false) 2.5 else 3.5}%.2f" - assertEquals("3.50", res) + val expected = formatUsingCurrentLocale(3.50) + assertEquals(expected, res) } + @Test def fHeteroArgs() = { val res = f"${3.14}%.2f rounds to ${3}%d" - assertEquals("3.14 rounds to 3", res) + val expected = formatUsingCurrentLocale(3.14) + " rounds to 3" + assertEquals(expected, res) } + + // Use this method to avoid problems with a locale-dependent decimal mark. + // The string interpolation is not used here intentionally as this method is used to test string interpolation. + private def formatUsingCurrentLocale(number: Double, decimalPlaces: Int = 2) = ("%." + decimalPlaces + "f").format(number) } diff --git a/test/junit/scala/collection/IterableViewLikeTest.scala b/test/junit/scala/collection/IterableViewLikeTest.scala index 55da02744b..ab09c4930b 100644 --- a/test/junit/scala/collection/IterableViewLikeTest.scala +++ b/test/junit/scala/collection/IterableViewLikeTest.scala @@ -4,6 +4,7 @@ import org.junit.Assert._ import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 +import language.postfixOps @RunWith(classOf[JUnit4]) class IterableViewLikeTest { diff --git a/test/junit/scala/collection/immutable/VectorTest.scala b/test/junit/scala/collection/immutable/VectorTest.scala new file mode 100644 index 0000000000..e7edba3e43 --- /dev/null +++ b/test/junit/scala/collection/immutable/VectorTest.scala @@ -0,0 +1,20 @@ +package scala.collection.immutable + +import org.junit.{Assert, Test} +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(classOf[JUnit4]) +class VectorTest { + /** + * Test Vector ++ with a small parallel collection concatenation (SI-9072). + * + */ + @Test + def testPlusPlus(): Unit = { + val smallVec = (0 to 1) + val smallParVec = smallVec.par + val testElementsSize = (0 to 1000).map( _ => Vector.empty ++ smallParVec ) + Assert.assertTrue(testElementsSize.forall( v => v.size == 2 )) + } +} diff --git a/test/junit/scala/collection/mutable/MutableListTest.scala b/test/junit/scala/collection/mutable/MutableListTest.scala new file mode 100644 index 0000000000..ac6d30def0 --- /dev/null +++ b/test/junit/scala/collection/mutable/MutableListTest.scala @@ -0,0 +1,37 @@ +package scala.collection.mutable + +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.junit.Test +import org.junit.Assert._ + +import scala.tools.testing.AssertUtil._ + +@RunWith(classOf[JUnit4]) +class MutableListTest { + + // Tests SI-8976 + @Test def tailIteratorMustTerminateAtLength(): Unit = { + val is = MutableList(1,2,3) + val tl = is.tail + assertEquals(tl.length, tl.iterator.length) + is += 5 + assertEquals(tl.length, tl.iterator.length) + assertSameElements(tl, tl.iterator) + } + @Test def iteratorMustFailEventually(): Unit = assertThrows[NoSuchElementException] { + MutableList[Unit]().iterator.next() + } + // was: Root empty iterator held reference + @Test def iteratorMustNotHoldOntoLast(): Unit = { + val is = MutableList(Some(1), Some(2)) + val it = is.iterator + val x = Some(3) + is += x + assertNotReachable(x, it) { + it.next() + it.next() + } + assertTrue(it.isEmpty) + } +} diff --git a/test/junit/scala/tools/nsc/symtab/SymbolTableTest.scala b/test/junit/scala/tools/nsc/symtab/SymbolTableTest.scala index 11e955a4bb..895ad9d683 100644 --- a/test/junit/scala/tools/nsc/symtab/SymbolTableTest.scala +++ b/test/junit/scala/tools/nsc/symtab/SymbolTableTest.scala @@ -44,4 +44,9 @@ class SymbolTableTest { assertFalse("Foo should be a superclass of Foo", fooSymbol.tpe <:< barSymbol.tpe) } + @Test + def noSymbolOuterClass_t9133: Unit = { + import symbolTable._ + assert(NoSymbol.outerClass == NoSymbol) + } } diff --git a/test/junit/scala/tools/testing/AssertThrowsTest.scala b/test/junit/scala/tools/testing/AssertThrowsTest.scala index d91e450bac..76758f51d2 100644 --- a/test/junit/scala/tools/testing/AssertThrowsTest.scala +++ b/test/junit/scala/tools/testing/AssertThrowsTest.scala @@ -38,6 +38,6 @@ class AssertThrowsTest { } catch { case e: AssertionError => return } - assert(false, "assertThrows should error if the tested expression does not throw anything") + fail("assertThrows should error if the tested expression does not throw anything") } } diff --git a/test/junit/scala/tools/testing/AssertUtil.scala b/test/junit/scala/tools/testing/AssertUtil.scala index 83a637783f..d29f9a473f 100644 --- a/test/junit/scala/tools/testing/AssertUtil.scala +++ b/test/junit/scala/tools/testing/AssertUtil.scala @@ -2,18 +2,42 @@ package scala.tools package testing import org.junit.Assert -import Assert.fail +import Assert._ import scala.runtime.ScalaRunTime.stringOf import scala.collection.{ GenIterable, IterableLike } +import scala.collection.JavaConverters._ +import scala.collection.mutable +import java.lang.ref._ +import java.lang.reflect._ +import java.util.IdentityHashMap /** This module contains additional higher-level assert statements * that are ultimately based on junit.Assert primitives. */ object AssertUtil { - /** - * Check if throwable T (or a subclass) was thrown during evaluation of f, and that its message - * satisfies the `checkMessage` predicate. - * If any other exception will be re-thrown. + private final val timeout = 60 * 1000L // wait a minute + + private implicit class `ref helper`[A](val r: Reference[A]) extends AnyVal { + def isEmpty: Boolean = r.get == null + def nonEmpty: Boolean = !isEmpty + } + private implicit class `class helper`(val clazz: Class[_]) extends AnyVal { + def allFields: List[Field] = { + def loop(k: Class[_]): List[Field] = + if (k == null) Nil + else k.getDeclaredFields.toList ::: loop(k.getSuperclass) + loop(clazz) + } + } + private implicit class `field helper`(val f: Field) extends AnyVal { + def follow(o: AnyRef): AnyRef = { + f setAccessible true + f get o + } + } + + /** Check if throwable T (or a subclass) was thrown during evaluation of f, and that its message + * satisfies the `checkMessage` predicate. If any other exception will be re-thrown. */ def assertThrows[T <: Throwable](f: => Any, checkMessage: String => Boolean = s => true) @@ -41,4 +65,29 @@ object AssertUtil { */ def assertSameElements[A, B >: A](expected: IterableLike[A, _], actual: Iterator[B]): Unit = assertSameElements(expected, actual.toList, "") + + /** Value is not strongly reachable from roots after body is evaluated. + */ + def assertNotReachable[A <: AnyRef](a: => A, roots: AnyRef*)(body: => Unit): Unit = { + val wkref = new WeakReference(a) + def refs(root: AnyRef): mutable.Set[AnyRef] = { + val seen = new IdentityHashMap[AnyRef, Unit] + def loop(o: AnyRef): Unit = + if (wkref.nonEmpty && o != null && !seen.containsKey(o)) { + seen.put(o, ()) + for { + f <- o.getClass.allFields + if !Modifier.isStatic(f.getModifiers) + if !f.getType.isPrimitive + if !classOf[Reference[_]].isAssignableFrom(f.getType) + } loop(f follow o) + } + loop(root) + seen.keySet.asScala + } + body + for (r <- roots if wkref.nonEmpty) { + assertFalse(s"Root $r held reference", refs(r) contains wkref.get) + } + } } diff --git a/test/junit/scala/tools/testing/AssertUtilTest.scala b/test/junit/scala/tools/testing/AssertUtilTest.scala new file mode 100644 index 0000000000..03d8815ab2 --- /dev/null +++ b/test/junit/scala/tools/testing/AssertUtilTest.scala @@ -0,0 +1,21 @@ +package scala.tools +package testing + +import org.junit.Assert._ +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import AssertUtil._ + +import java.lang.ref._ + +@RunWith(classOf[JUnit4]) +class AssertUtilTest { + + @Test def reachableIgnoresReferences(): Unit = { + class Holder[A](val ref: SoftReference[A]) + val o = new Object + val r = new SoftReference(o) + assertNotReachable(o, new Holder(r)) { } + } +} |