diff options
Diffstat (limited to 'test/junit/scala/collection')
-rw-r--r-- | test/junit/scala/collection/IteratorTest.scala | 22 | ||||
-rw-r--r-- | test/junit/scala/collection/ReusableBuildersTest.scala | 48 | ||||
-rw-r--r-- | test/junit/scala/collection/SeqViewTest.scala | 16 | ||||
-rw-r--r-- | test/junit/scala/collection/SetMapConsistencyTest.scala | 23 | ||||
-rw-r--r-- | test/junit/scala/collection/convert/NullSafetyTest.scala | 279 | ||||
-rw-r--r-- | test/junit/scala/collection/convert/WrapperSerializationTest.scala | 29 | ||||
-rw-r--r-- | test/junit/scala/collection/immutable/SetTest.scala (renamed from test/junit/scala/collection/immutable/SetTests.scala) | 2 | ||||
-rw-r--r-- | test/junit/scala/collection/immutable/StreamTest.scala | 126 | ||||
-rw-r--r-- | test/junit/scala/collection/immutable/StringLikeTest.scala | 6 | ||||
-rw-r--r-- | test/junit/scala/collection/mutable/BitSetTest.scala | 7 | ||||
-rw-r--r-- | test/junit/scala/collection/mutable/OpenHashMapTest.scala | 2 | ||||
-rw-r--r-- | test/junit/scala/collection/mutable/PriorityQueueTest.scala | 7 |
12 files changed, 561 insertions, 6 deletions
diff --git a/test/junit/scala/collection/IteratorTest.scala b/test/junit/scala/collection/IteratorTest.scala index 329c85127a..4df29e36c0 100644 --- a/test/junit/scala/collection/IteratorTest.scala +++ b/test/junit/scala/collection/IteratorTest.scala @@ -135,6 +135,20 @@ class IteratorTest { assertEquals(3, List(1, 2, 3, 4, 5).iterator.indexWhere { x: Int => x >= 4 }) assertEquals(-1, List(1, 2, 3, 4, 5).iterator.indexWhere { x: Int => x >= 16 }) } + @Test def indexOfFrom(): Unit = { + assertEquals(1, List(1, 2, 3, 4, 5).iterator.indexOf(2, 0)) + assertEquals(1, List(1, 2, 3, 4, 5).iterator.indexOf(2, 1)) + assertEquals(-1, List(1, 2, 3, 4, 5).iterator.indexOf(2, 2)) + assertEquals(4, List(1, 2, 3, 2, 1).iterator.indexOf(1, 1)) + assertEquals(1, List(1, 2, 3, 2, 1).iterator.indexOf(2, 1)) + } + @Test def indexWhereFrom(): Unit = { + assertEquals(1, List(1, 2, 3, 4, 5).iterator.indexWhere(_ == 2, 0)) + assertEquals(1, List(1, 2, 3, 4, 5).iterator.indexWhere(_ == 2, 1)) + assertEquals(-1, List(1, 2, 3, 4, 5).iterator.indexWhere(_ == 2, 2)) + assertEquals(4, List(1, 2, 3, 2, 1).iterator.indexWhere(_ < 2, 1)) + assertEquals(1, List(1, 2, 3, 2, 1).iterator.indexWhere(_ <= 2, 1)) + } // iterator-iterate-lazy.scala // was java.lang.UnsupportedOperationException: tail of empty list @Test def iterateIsSufficientlyLazy(): Unit = { @@ -154,6 +168,14 @@ class IteratorTest { results += (Stream from 1).toIterator.drop(10).toStream.drop(10).toIterator.next() assertSameElements(List(1,1,21), results) } + // SI-8552 + @Test def indexOfShouldWorkForTwoParams(): Unit = { + assertEquals(1, List(1, 2, 3).iterator.indexOf(2, 0)) + assertEquals(-1, List(5 -> 0).iterator.indexOf(5, 0)) + assertEquals(0, List(5 -> 0).iterator.indexOf((5, 0))) + assertEquals(-1, List(5 -> 0, 9 -> 2, 0 -> 3).iterator.indexOf(9, 2)) + assertEquals(1, List(5 -> 0, 9 -> 2, 0 -> 3).iterator.indexOf(9 -> 2)) + } // SI-9332 @Test def spanExhaustsLeadingIterator(): Unit = { def it = Iterator.iterate(0)(_ + 1).take(6) diff --git a/test/junit/scala/collection/ReusableBuildersTest.scala b/test/junit/scala/collection/ReusableBuildersTest.scala new file mode 100644 index 0000000000..8dd1a37adf --- /dev/null +++ b/test/junit/scala/collection/ReusableBuildersTest.scala @@ -0,0 +1,48 @@ +package scala.collection + +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 ReusableBuildersTest { + // GrowingBuilders are NOT reusable but can clear themselves + @Test + def test_SI8648() { + val b = collection.mutable.HashSet.newBuilder[Int] + b += 3 + b.clear + assert(!b.isInstanceOf[collection.mutable.ReusableBuilder[_,_]]) + assert(b.isInstanceOf[collection.mutable.GrowingBuilder[_,_]]) + assert(b.result == Set[Int]()) + } + + // ArrayBuilders ARE reusable, regardless of whether they returned their internal array or not + @Test + def test_SI9564() { + val b = Array.newBuilder[Float] + b += 3f + val three = b.result + b.clear + b ++= (1 to 16).map(_.toFloat) + val sixteen = b.result + b.clear + b += 0f + val zero = b.result + assert(b.isInstanceOf[collection.mutable.ReusableBuilder[_,_]]) + assert(three.toList == 3 :: Nil) + assert(sixteen.toList == (1 to 16)) + assert(zero.toList == 0 :: Nil) + } + + @Test + def test_reusability() { + val bl = List.newBuilder[String] + val bv = Vector.newBuilder[String] + val ba = collection.mutable.ArrayBuffer.newBuilder[String] + assert(bl.isInstanceOf[collection.mutable.ReusableBuilder[_, _]]) + assert(bv.isInstanceOf[collection.mutable.ReusableBuilder[_, _]]) + assert(!ba.isInstanceOf[collection.mutable.ReusableBuilder[_, _]]) + } +} diff --git a/test/junit/scala/collection/SeqViewTest.scala b/test/junit/scala/collection/SeqViewTest.scala new file mode 100644 index 0000000000..24474fc4b9 --- /dev/null +++ b/test/junit/scala/collection/SeqViewTest.scala @@ -0,0 +1,16 @@ +package scala.collection + +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.junit.Assert._ +import org.junit.Test + +@RunWith(classOf[JUnit4]) +class SeqViewTest { + + @Test + def test_SI8691() { + // Really just testing to make sure ++: doesn't throw an exception + assert( Seq(1,2) ++: Seq(3,4).view == Seq(1,2,3,4) ) + } +} diff --git a/test/junit/scala/collection/SetMapConsistencyTest.scala b/test/junit/scala/collection/SetMapConsistencyTest.scala index 261c11a98b..eb864a8449 100644 --- a/test/junit/scala/collection/SetMapConsistencyTest.scala +++ b/test/junit/scala/collection/SetMapConsistencyTest.scala @@ -66,6 +66,8 @@ class SetMapConsistencyTest { 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 boxMtm[A: Ordering] = new BoxMutableMap[A, cm.TreeMap[A, Int]](new cm.TreeMap[A, Int], "mutable.TreeMap") 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]] @@ -188,7 +190,9 @@ class SetMapConsistencyTest { 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 boxMts[A: Ordering] = new BoxMutableSet[A, cm.TreeSet[A]](new cm.TreeSet[A], "mutable.TreeSet") + 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 @@ -315,7 +319,7 @@ class SetMapConsistencyTest { @Test def churnIntMaps() { val maps = Array[() => MapBox[Int]]( - () => boxMlm[Int], () => boxMhm[Int], () => boxMohm[Int], () => boxJavaM[Int], + () => boxMlm[Int], () => boxMhm[Int], () => boxMohm[Int], () => boxMtm[Int], () => boxJavaM[Int], () => boxIim, () => boxIhm[Int], () => boxIlm[Int], () => boxItm[Int] ) assert( maps.sliding(2).forall{ ms => churn(ms(0)(), ms(1)(), intKeys, 2000) } ) @@ -325,7 +329,7 @@ class SetMapConsistencyTest { def churnLongMaps() { val maps = Array[() => MapBox[Long]]( () => boxMjm, () => boxIjm, () => boxJavaM[Long], - () => boxMlm[Long], () => boxMhm[Long], () => boxMohm[Long], () => boxIhm[Long], () => boxIlm[Long] + () => boxMlm[Long], () => boxMhm[Long], () => boxMtm[Long], () => boxMohm[Long], () => boxIhm[Long], () => boxIlm[Long] ) assert( maps.sliding(2).forall{ ms => churn(ms(0)(), ms(1)(), longKeys, 10000) } ) } @@ -352,7 +356,7 @@ class SetMapConsistencyTest { def churnIntSets() { val sets = Array[() => MapBox[Int]]( () => boxMhm[Int], () => boxIhm[Int], () => boxJavaS[Int], - () => boxMbs, () => boxMhs[Int], () => boxIbs, () => boxIhs[Int], () => boxIls[Int], () => boxIts[Int] + () => boxMbs, () => boxMhs[Int], () => boxMts[Int], () => boxIbs, () => boxIhs[Int], () => boxIls[Int], () => boxIts[Int] ) assert( sets.sliding(2).forall{ ms => churn(ms(0)(), ms(1)(), smallKeys, 1000, valuer = _ => 0) } ) } @@ -529,4 +533,15 @@ class SetMapConsistencyTest { assert(nit == 4) assert(nfe == 4) } + + @Test + def test_SI8727() { + import scala.tools.testing.AssertUtil._ + type NSEE = NoSuchElementException + val map = Map(0 -> "zero", 1 -> "one") + val m = map.filterKeys(i => if (map contains i) true else throw new NSEE) + assert{ (m contains 0) && (m get 0).nonEmpty } + assertThrows[NSEE]{ m contains 2 } + assertThrows[NSEE]{ m get 2 } + } } diff --git a/test/junit/scala/collection/convert/NullSafetyTest.scala b/test/junit/scala/collection/convert/NullSafetyTest.scala new file mode 100644 index 0000000000..173568408c --- /dev/null +++ b/test/junit/scala/collection/convert/NullSafetyTest.scala @@ -0,0 +1,279 @@ +package scala.collection.convert + +import java.{util => ju, lang => jl} +import ju.{concurrent => juc} + +import org.junit.Test +import org.junit.experimental.runners.Enclosed +import org.junit.runner.RunWith + +import collection.convert.ImplicitConversions._ +import scala.collection.JavaConverters._ +import scala.collection.{mutable, concurrent} + +@RunWith(classOf[Enclosed]) +object NullSafetyTest { + + /* + * Pertinent: SI-9113 + * Tests to insure that wrappers return null instead of wrapping it as a collection + */ + + class ToScala { + + @Test def testIteratorWrapping(): Unit = { + val nullJIterator: ju.Iterator[AnyRef] = null + val iterator: Iterator[AnyRef] = nullJIterator + + assert(iterator == null) + } + + @Test def testEnumerationWrapping(): Unit = { + val nullJEnumeration: ju.Enumeration[AnyRef] = null + val enumeration: Iterator[AnyRef] = nullJEnumeration + + assert(enumeration == null) + } + + @Test def testIterableWrapping(): Unit = { + val nullJIterable: jl.Iterable[AnyRef] = null + val iterable: Iterable[AnyRef] = nullJIterable + + assert(iterable == null) + } + + @Test def testCollectionWrapping(): Unit = { + val nullJCollection: ju.Collection[AnyRef] = null + val collection: Iterable[AnyRef] = nullJCollection + + assert(collection == null) + } + + @Test def testBufferWrapping(): Unit = { + val nullJList: ju.List[AnyRef] = null + val buffer: mutable.Buffer[AnyRef] = nullJList + + assert(buffer == null) + } + + @Test def testSetWrapping(): Unit = { + val nullJSet: ju.Set[AnyRef] = null + val set: mutable.Set[AnyRef] = nullJSet + + assert(set == null) + } + + @Test def testMapWrapping(): Unit = { + val nullJMap: ju.Map[AnyRef, AnyRef] = null + val map: mutable.Map[AnyRef, AnyRef] = nullJMap + + assert(map == null) + } + + @Test def testConcurrentMapWrapping(): Unit = { + val nullJConMap: juc.ConcurrentMap[AnyRef, AnyRef] = null + val conMap: concurrent.Map[AnyRef, AnyRef] = nullJConMap + + assert(conMap == null) + } + + @Test def testDictionaryWrapping(): Unit = { + val nullJDict: ju.Dictionary[AnyRef, AnyRef] = null + val dict: mutable.Map[AnyRef, AnyRef] = nullJDict + + assert(dict == null) + } + + + @Test def testPropertyWrapping(): Unit = { + val nullJProps: ju.Properties = null + val props: mutable.Map[String, String] = nullJProps + + assert(props == null) + } + + @Test def testIteratorDecoration(): Unit = { + val nullJIterator: ju.Iterator[AnyRef] = null + + assert(nullJIterator.asScala == null) + } + + @Test def testEnumerationDecoration(): Unit = { + val nullJEnumeration: ju.Enumeration[AnyRef] = null + + assert(nullJEnumeration.asScala == null) + } + + @Test def testIterableDecoration(): Unit = { + val nullJIterable: jl.Iterable[AnyRef] = null + + assert(nullJIterable.asScala == null) + } + + @Test def testCollectionDecoration(): Unit = { + val nullJCollection: ju.Collection[AnyRef] = null + + assert(nullJCollection.asScala == null) + } + + @Test def testBufferDecoration(): Unit = { + val nullJBuffer: ju.List[AnyRef] = null + + assert(nullJBuffer.asScala == null) + } + + @Test def testSetDecoration(): Unit = { + val nullJSet: ju.Set[AnyRef] = null + + assert(nullJSet.asScala == null) + } + + @Test def testMapDecoration(): Unit = { + val nullJMap: ju.Map[AnyRef, AnyRef] = null + + assert(nullJMap.asScala == null) + } + + @Test def testConcurrentMapDecoration(): Unit = { + val nullJConMap: juc.ConcurrentMap[AnyRef, AnyRef] = null + + assert(nullJConMap.asScala == null) + } + + @Test def testDictionaryDecoration(): Unit = { + val nullJDict: ju.Dictionary[AnyRef, AnyRef] = null + + assert(nullJDict.asScala == null) + } + + @Test def testPropertiesDecoration(): Unit = { + val nullJProperties: ju.Properties = null + + assert(nullJProperties.asScala == null) + } + } + + class ToJava { + + @Test def testIteratorWrapping(): Unit = { + val nullIterator: Iterator[AnyRef] = null + val jIterator: ju.Iterator[AnyRef] = nullIterator + + assert(jIterator == null) + } + + @Test def testEnumerationWrapping(): Unit = { + val nullEnumeration: Iterator[AnyRef] = null + val enumeration: ju.Iterator[AnyRef] = nullEnumeration + + assert(enumeration == null) + } + + @Test def testIterableWrapping(): Unit = { + val nullIterable: Iterable[AnyRef] = null + val iterable: jl.Iterable[AnyRef] = asJavaIterable(nullIterable) + + assert(iterable == null) + } + + @Test def testCollectionWrapping(): Unit = { + val nullCollection: Iterable[AnyRef] = null + val collection: ju.Collection[AnyRef] = nullCollection + + assert(collection == null) + } + + @Test def testBufferWrapping(): Unit = { + val nullList: mutable.Buffer[AnyRef] = null + val buffer: ju.List[AnyRef] = nullList + + assert(buffer == null) + } + + @Test def testSetWrapping(): Unit = { + val nullSet: mutable.Set[AnyRef] = null + val set: ju.Set[AnyRef] = nullSet + + assert(set == null) + } + + @Test def testMapWrapping(): Unit = { + val nullMap: mutable.Map[AnyRef, AnyRef] = null + val map: ju.Map[AnyRef, AnyRef] = nullMap + + assert(map == null) + } + + @Test def testConcurrentMapWrapping(): Unit = { + val nullConMap: concurrent.Map[AnyRef, AnyRef] = null + val conMap: juc.ConcurrentMap[AnyRef, AnyRef] = nullConMap + + assert(conMap == null) + } + + @Test def testDictionaryWrapping(): Unit = { + val nullDict: mutable.Map[AnyRef, AnyRef] = null + val dict: ju.Dictionary[AnyRef, AnyRef] = nullDict + + assert(dict == null) + } + + // Implicit conversion to ju.Properties is not available + + @Test def testIteratorDecoration(): Unit = { + val nullIterator: Iterator[AnyRef] = null + + assert(nullIterator.asJava == null) + } + + @Test def testEnumerationDecoration(): Unit = { + val nullEnumeration: Iterator[AnyRef] = null + + assert(nullEnumeration.asJavaEnumeration == null) + } + + @Test def testIterableDecoration(): Unit = { + val nullIterable: Iterable[AnyRef] = null + + assert(nullIterable.asJava == null) + } + + @Test def testCollectionDecoration(): Unit = { + val nullCollection: Iterable[AnyRef] = null + + assert(nullCollection.asJavaCollection == null) + } + + @Test def testBufferDecoration(): Unit = { + val nullBuffer: mutable.Buffer[AnyRef] = null + + assert(nullBuffer.asJava == null) + } + + @Test def testSetDecoration(): Unit = { + val nullSet: Set[AnyRef] = null + + assert(nullSet.asJava == null) + } + + @Test def testMapDecoration(): Unit = { + val nullMap: mutable.Map[AnyRef, AnyRef] = null + + assert(nullMap.asJava == null) + } + + @Test def testConcurrentMapDecoration(): Unit = { + val nullConMap: concurrent.Map[AnyRef, AnyRef] = null + + assert(nullConMap.asJava == null) + } + + @Test def testDictionaryDecoration(): Unit = { + val nullDict: mutable.Map[AnyRef, AnyRef] = null + + assert(nullDict.asJavaDictionary == null) + } + + // Decorator conversion to ju.Properties is not available + } +} diff --git a/test/junit/scala/collection/convert/WrapperSerializationTest.scala b/test/junit/scala/collection/convert/WrapperSerializationTest.scala new file mode 100644 index 0000000000..d398be806a --- /dev/null +++ b/test/junit/scala/collection/convert/WrapperSerializationTest.scala @@ -0,0 +1,29 @@ +package scala.collection.convert + +import org.junit.Assert._ +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(classOf[JUnit4]) +class WrapperSerializationTest { + def ser(a: AnyRef) = { + val baos = new java.io.ByteArrayOutputStream + (new java.io.ObjectOutputStream(baos)).writeObject(a) + baos + } + def des(baos: java.io.ByteArrayOutputStream): AnyRef = { + val bais = new java.io.ByteArrayInputStream(baos.toByteArray) + (new java.io.ObjectInputStream(bais)).readObject() + } + def serdes(a: AnyRef): Boolean = a == des(ser(a)) + + @Test + def test_SI8911() { + import scala.collection.JavaConverters._ + assert( serdes(scala.collection.mutable.ArrayBuffer(1,2).asJava) ) + assert( serdes(Seq(1,2).asJava) ) + assert( serdes(Set(1,2).asJava) ) + assert( serdes(Map(1 -> "one", 2 -> "two").asJava) ) + } +} diff --git a/test/junit/scala/collection/immutable/SetTests.scala b/test/junit/scala/collection/immutable/SetTest.scala index 28c7864359..4029c98009 100644 --- a/test/junit/scala/collection/immutable/SetTests.scala +++ b/test/junit/scala/collection/immutable/SetTest.scala @@ -6,7 +6,7 @@ import org.junit.runner.RunWith import org.junit.runners.JUnit4 @RunWith(classOf[JUnit4]) -class SetTests { +class SetTest { @Test def test_SI8346_toSet_soundness(): Unit = { val any2stringadd = "Disabled string conversions so as not to get confused!" diff --git a/test/junit/scala/collection/immutable/StreamTest.scala b/test/junit/scala/collection/immutable/StreamTest.scala new file mode 100644 index 0000000000..1b257aabc4 --- /dev/null +++ b/test/junit/scala/collection/immutable/StreamTest.scala @@ -0,0 +1,126 @@ +package scala.collection.immutable + +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.junit.Test +import org.junit.Assert._ + +import scala.ref.WeakReference +import scala.util.Try + +@RunWith(classOf[JUnit4]) +class StreamTest { + + @Test + def t6727_and_t6440_and_8627(): Unit = { + assertTrue(Stream.continually(()).filter(_ => true).take(2) == Seq((), ())) + assertTrue(Stream.continually(()).filterNot(_ => false).take(2) == Seq((), ())) + assertTrue(Stream(1,2,3,4,5).filter(_ < 4) == Seq(1,2,3)) + assertTrue(Stream(1,2,3,4,5).filterNot(_ > 4) == Seq(1,2,3,4)) + assertTrue(Stream.from(1).filter(_ > 4).take(3) == Seq(5,6,7)) + assertTrue(Stream.from(1).filterNot(_ <= 4).take(3) == Seq(5,6,7)) + } + + /** Test helper to verify that the given Stream operation allows + * GC of the head during processing of the tail. + */ + def assertStreamOpAllowsGC(op: (=> Stream[Int], Int => Unit) => Any, f: Int => Unit): Unit = { + val msgSuccessGC = "GC success" + val msgFailureGC = "GC failure" + + // A stream of 500 elements at most. We will test that the head can be collected + // while processing the tail. After each element we will GC and wait 10 ms, so a + // failure to collect will take roughly 5 seconds. + val ref = WeakReference( Stream.from(1).take(500) ) + + def gcAndThrowIfCollected(n: Int): Unit = { + System.gc() // try to GC + Thread.sleep(10) // give it 10 ms + if (ref.get.isEmpty) throw new RuntimeException(msgSuccessGC) // we're done if head collected + f(n) + } + + val res = Try { op(ref(), gcAndThrowIfCollected) }.failed // success is indicated by an + val msg = res.map(_.getMessage).getOrElse(msgFailureGC) // exception with expected message + // failure is indicated by no + assertTrue(msg == msgSuccessGC) // exception, or one with different message + } + + @Test + def foreach_allows_GC() { + assertStreamOpAllowsGC(_.foreach(_), _ => ()) + } + + @Test + def filter_all_foreach_allows_GC() { + assertStreamOpAllowsGC(_.filter(_ => true).foreach(_), _ => ()) + } + + @Test // SI-8990 + def withFilter_after_first_foreach_allows_GC: Unit = { + assertStreamOpAllowsGC(_.withFilter(_ > 1).foreach(_), _ => ()) + } + + @Test // SI-8990 + def withFilter_after_first_withFilter_foreach_allows_GC: Unit = { + assertStreamOpAllowsGC(_.withFilter(_ > 1).withFilter(_ < 100).foreach(_), _ => ()) + } + + @Test // SI-8990 + def withFilter_can_retry_after_exception_thrown_in_filter: Unit = { + // use mutable state to control an intermittent failure in filtering the Stream + var shouldThrow = true + + val wf = Stream.from(1).take(10).withFilter { n => + if (shouldThrow && n == 5) throw new RuntimeException("n == 5") else n > 5 + } + + assertTrue( Try { wf.map(identity) }.isFailure ) // throws on n == 5 + + shouldThrow = false // won't throw next time + + assertTrue( wf.map(identity).length == 5 ) // success instead of NPE + } + + /** Test helper to verify that the given Stream operation is properly lazy in the tail */ + def assertStreamOpLazyInTail(op: (=> Stream[Int]) => Stream[Int], expectedEvaluated: List[Int]): Unit = { + // mutable state to record every strict evaluation + var evaluated: List[Int] = Nil + + def trackEffectsOnNaturals: Stream[Int] = { + def loop(i: Int): Stream[Int] = { evaluated ++= List(i); i #:: loop(i + 1) } + loop(1) + } + + // call op on a stream which records every strict evaluation + val result = op(trackEffectsOnNaturals) + + assertTrue( evaluated == expectedEvaluated ) + } + + @Test // SI-9134 + def filter_map_properly_lazy_in_tail: Unit = { + assertStreamOpLazyInTail(_.filter(_ % 2 == 0).map(identity), List(1, 2)) + } + + @Test // SI-9134 + def withFilter_map_properly_lazy_in_tail: Unit = { + assertStreamOpLazyInTail(_.withFilter(_ % 2 == 0).map(identity), List(1, 2)) + } + + @Test + def test_si9379() { + class Boom { + private var i = -1 + def inc = { + i += 1 + if (i > 1000) throw new NoSuchElementException("Boom! Too many elements!") + i + } + } + val b = new Boom + val s = Stream.continually(b.inc) + // zipped.toString must allow s to short-circuit evaluation + assertTrue((s, s).zipped.toString contains s.toString) + } +} diff --git a/test/junit/scala/collection/immutable/StringLikeTest.scala b/test/junit/scala/collection/immutable/StringLikeTest.scala index 3722bdfe4d..50be638b89 100644 --- a/test/junit/scala/collection/immutable/StringLikeTest.scala +++ b/test/junit/scala/collection/immutable/StringLikeTest.scala @@ -28,10 +28,16 @@ class StringLikeTest { @Test def testSplitEdgeCases: Unit = { + val high = 0xD852.toChar + val low = 0xDF62.toChar + val surrogatepair = List(high, low).mkString + val twopairs = surrogatepair + "_" + surrogatepair + 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! + AssertUtil.assertSameElements(twopairs.split(high), Array(twopairs)) //don't split on characters that are half a surrogate pair } } diff --git a/test/junit/scala/collection/mutable/BitSetTest.scala b/test/junit/scala/collection/mutable/BitSetTest.scala index d56cc45601..e832194989 100644 --- a/test/junit/scala/collection/mutable/BitSetTest.scala +++ b/test/junit/scala/collection/mutable/BitSetTest.scala @@ -28,4 +28,11 @@ class BitSetTest { littleBitSet &= bigBitSet assert(littleBitSet.toBitMask.length < bigBitSet.toBitMask.length, "Needlessly extended the size of bitset on &=") } + + @Test def test_SI8647() { + val bs = BitSet() + bs.map(_ + 1) // Just needs to compile + val xs = bs: SortedSet[Int] + xs.map(_ + 1) // Also should compile (did before) + } } diff --git a/test/junit/scala/collection/mutable/OpenHashMapTest.scala b/test/junit/scala/collection/mutable/OpenHashMapTest.scala index 1459c14d78..9b5c20e01a 100644 --- a/test/junit/scala/collection/mutable/OpenHashMapTest.scala +++ b/test/junit/scala/collection/mutable/OpenHashMapTest.scala @@ -28,7 +28,7 @@ class OpenHashMapTest { val fieldMirror = mirror.reflect(m).reflectField(termSym) */ // Use Java reflection instead for now. - val field = m.getClass.getDeclaredField("scala$collection$mutable$OpenHashMap$$deleted") + val field = m.getClass.getDeclaredField("deleted") field.setAccessible(true) m.put(0, 0) diff --git a/test/junit/scala/collection/mutable/PriorityQueueTest.scala b/test/junit/scala/collection/mutable/PriorityQueueTest.scala index a14f1bf4c8..faedcf11f0 100644 --- a/test/junit/scala/collection/mutable/PriorityQueueTest.scala +++ b/test/junit/scala/collection/mutable/PriorityQueueTest.scala @@ -14,6 +14,12 @@ class PriorityQueueTest { priorityQueue.enqueue(elements :_*) @Test + def orderingReverseReverse() { + val pq = new mutable.PriorityQueue[Nothing]()((_,_)=>42) + assert(pq.ord eq pq.reverse.reverse.ord) + } + + @Test def canSerialize() { val outputStream = new ByteArrayOutputStream() new ObjectOutputStream(outputStream).writeObject(priorityQueue) @@ -27,6 +33,7 @@ class PriorityQueueTest { val objectInputStream = new ObjectInputStream(new ByteArrayInputStream(bytes)) val deserializedPriorityQueue = objectInputStream.readObject().asInstanceOf[PriorityQueue[Int]] + //correct sequencing is also tested here: assert(deserializedPriorityQueue.dequeueAll == elements.sorted.reverse) } } |