diff options
Diffstat (limited to 'test/junit/scala/collection')
7 files changed, 179 insertions, 0 deletions
diff --git a/test/junit/scala/collection/IterableViewLikeTest.scala b/test/junit/scala/collection/IterableViewLikeTest.scala index 55da02744b..435a43c215 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 { @@ -12,6 +13,7 @@ class IterableViewLikeTest { def hasCorrectDropAndTakeMethods() { val iter = Iterable(1, 2, 3) + import scala.language.postfixOps assertEquals(Iterable.empty[Int], iter.view take Int.MinValue force) assertEquals(Iterable.empty[Int], iter.view takeRight Int.MinValue force) assertEquals(iter, iter.view drop Int.MinValue force) diff --git a/test/junit/scala/collection/ParallelConsistencyTest.scala b/test/junit/scala/collection/ParallelConsistencyTest.scala new file mode 100644 index 0000000000..da96362413 --- /dev/null +++ b/test/junit/scala/collection/ParallelConsistencyTest.scala @@ -0,0 +1,44 @@ +package scala.collection.immutable + +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(classOf[JUnit4]) +class ParallelConsistencyTest { + + private val theSeq = Seq(1,2,3) + + // This collection will throw an exception if you do anything but call .length or .seq + private val mustCallSeq: collection.GenSeq[Int] = new collection.parallel.ParSeq[Int] { + def length = 3 + + // This method is surely sequential & safe -- want all access to go through here + def seq = theSeq + + def notSeq = throw new Exception("Access to parallel collection not via .seq") + + // These methods could possibly be used dangerously explicitly or internally + // (apply could also be used safely; if it is, do test with mustCallSeq) + def apply(i: Int) = notSeq + def splitter = notSeq + } + + // Test Vector ++ with a small parallel collection concatenation (SI-9072). + @Test + def testPlusPlus(): Unit = { + assert((Vector.empty ++ mustCallSeq) == theSeq, "Vector ++ unsafe with parallel vectors") + } + + // SI-9126, 1 of 2 + @Test + def testTranspose(): Unit = { + assert(List(mustCallSeq).transpose.flatten == theSeq, "Transposing inner parallel collection unsafe") + } + + // SI-9126, 2 of 2 + @Test + def testList_flatMap(): Unit = { + assert(List(1).flatMap(_ => mustCallSeq) == theSeq, "List#flatMap on inner parallel collection unsafe") + } +} diff --git a/test/junit/scala/collection/immutable/StringLikeTest.scala b/test/junit/scala/collection/immutable/StringLikeTest.scala new file mode 100644 index 0000000000..3722bdfe4d --- /dev/null +++ b/test/junit/scala/collection/immutable/StringLikeTest.scala @@ -0,0 +1,37 @@ +package scala.collection.immutable + +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +import scala.tools.testing.AssertUtil +import scala.util.Random + +/* Test for SI-8988 */ +@RunWith(classOf[JUnit4]) +class StringLikeTest { + @Test + def testStringSplitWithChar: Unit = { + val chars = (0 to 255).map(_.toChar) + def randString = Random.nextString(30) + + for (c <- chars) { + val s = randString + val jString = new java.lang.String(s) + + // make sure we can match a literal character done by Java's split + val jSplit = jString.split("\\Q" + c.toString + "\\E") + val sSplit = s.split(c) + AssertUtil.assertSameElements(jSplit, sSplit, s"Not same result as Java split for char $c in string $s") + } + } + + @Test + def testSplitEdgeCases: Unit = { + AssertUtil.assertSameElements("abcd".split('d'), Array("abc")) // not Array("abc", "") + AssertUtil.assertSameElements("abccc".split('c'), Array("ab")) // not Array("ab", "", "", "") + AssertUtil.assertSameElements("xxx".split('x'), Array[String]()) // not Array("", "", "", "") + AssertUtil.assertSameElements("".split('x'), Array("")) // not Array() + AssertUtil.assertSameElements("--ch--omp--".split("-"), Array("", "", "ch", "", "omp")) // All the cases! + } +} diff --git a/test/junit/scala/collection/mutable/BitSetTest.scala b/test/junit/scala/collection/mutable/BitSetTest.scala index 8d164b50d4..d56cc45601 100644 --- a/test/junit/scala/collection/mutable/BitSetTest.scala +++ b/test/junit/scala/collection/mutable/BitSetTest.scala @@ -19,4 +19,13 @@ class BitSetTest { bitSet &~= bitSet assert(bitSet.toBitMask.length == size, "Capacity of bitset changed after &~=") } + + @Test def test_SI8917() { + val bigBitSet = BitSet(1, 100, 10000) + val littleBitSet = BitSet(100) + bigBitSet &= littleBitSet + assert(!(bigBitSet contains 10000), "&= not applied to the full bitset") + littleBitSet &= bigBitSet + assert(littleBitSet.toBitMask.length < bigBitSet.toBitMask.length, "Needlessly extended the size of bitset on &=") + } } diff --git a/test/junit/scala/collection/mutable/LinkedHashMapTest.scala b/test/junit/scala/collection/mutable/LinkedHashMapTest.scala new file mode 100644 index 0000000000..37dcd028a5 --- /dev/null +++ b/test/junit/scala/collection/mutable/LinkedHashMapTest.scala @@ -0,0 +1,25 @@ +package scala.collection.mutable + +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.junit.{ Assert, Test } + +import scala.collection.mutable + +/* Test for SI-9095 */ +@RunWith(classOf[JUnit4]) +class LinkedHashMapTest { + class TestClass extends mutable.LinkedHashMap[String, Int] { + def lastItemRef = lastEntry + } + + @Test + def testClear: Unit = { + val lhm = new TestClass + Seq("a" -> 8, "b" -> 9).foreach(kv => lhm.put(kv._1, kv._2)) + + Assert.assertNotNull(lhm.lastItemRef) + lhm.clear() + Assert.assertNull(lhm.lastItemRef) + } +} diff --git a/test/junit/scala/collection/mutable/LinkedHashSetTest.scala b/test/junit/scala/collection/mutable/LinkedHashSetTest.scala new file mode 100644 index 0000000000..b419ad37ec --- /dev/null +++ b/test/junit/scala/collection/mutable/LinkedHashSetTest.scala @@ -0,0 +1,25 @@ +package scala.collection.mutable + +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.junit.{ Assert, Test } + +import scala.collection.mutable + +/* Test for SI-9095 */ +@RunWith(classOf[JUnit4]) +class LinkedHashSetTest { + class TestClass extends mutable.LinkedHashSet[String] { + def lastItemRef = lastEntry + } + + @Test + def testClear: Unit = { + val lhs = new TestClass + Seq("a", "b").foreach(k => lhs.add(k)) + + Assert.assertNotNull(lhs.lastItemRef) + lhs.clear() + Assert.assertNull(lhs.lastItemRef) + } +} 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) + } +} |