diff options
Diffstat (limited to 'test/junit/scala/collection/immutable')
-rw-r--r-- | test/junit/scala/collection/immutable/ListMapTest.scala | 48 | ||||
-rw-r--r-- | test/junit/scala/collection/immutable/ListSetTest.scala | 53 | ||||
-rw-r--r-- | test/junit/scala/collection/immutable/PagedSeqTest.scala | 3 | ||||
-rw-r--r-- | test/junit/scala/collection/immutable/RangeTest.scala | 42 | ||||
-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 | 37 |
7 files changed, 309 insertions, 2 deletions
diff --git a/test/junit/scala/collection/immutable/ListMapTest.scala b/test/junit/scala/collection/immutable/ListMapTest.scala new file mode 100644 index 0000000000..320a976755 --- /dev/null +++ b/test/junit/scala/collection/immutable/ListMapTest.scala @@ -0,0 +1,48 @@ +package scala.collection.immutable + +import org.junit.Assert._ +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(classOf[JUnit4]) +class ListMapTest { + + @Test + def t7445(): Unit = { + val m = ListMap(1 -> 1, 2 -> 2, 3 -> 3, 4 -> 4, 5 -> 5) + assertEquals(ListMap(2 -> 2, 3 -> 3, 4 -> 4, 5 -> 5), m.tail) + } + + @Test + def hasCorrectBuilder(): Unit = { + val m = ListMap("a" -> "1", "b" -> "2", "c" -> "3", "b" -> "2.2", "d" -> "4") + assertEquals(List("a" -> "1", "c" -> "3", "b" -> "2.2", "d" -> "4"), m.toList) + } + + @Test + def hasCorrectHeadTailLastInit(): Unit = { + val m = ListMap(1 -> 1, 2 -> 2, 3 -> 3) + assertEquals(1 -> 1, m.head) + assertEquals(ListMap(2 -> 2, 3 -> 3), m.tail) + assertEquals(3 -> 3, m.last) + assertEquals(ListMap(1 -> 1, 2 -> 2), m.init) + } + + @Test + def hasCorrectAddRemove(): Unit = { + val m = ListMap(1 -> 1, 2 -> 2, 3 -> 3) + assertEquals(ListMap(1 -> 1, 2 -> 2, 3 -> 3, 4 -> 4), m + (4 -> 4)) + assertEquals(ListMap(1 -> 1, 3 -> 3, 2 -> 4), m + (2 -> 4)) + assertEquals(ListMap(1 -> 1, 2 -> 2, 3 -> 3), m + (2 -> 2)) + assertEquals(ListMap(2 -> 2, 3 -> 3), m - 1) + assertEquals(ListMap(1 -> 1, 3 -> 3), m - 2) + assertEquals(ListMap(1 -> 1, 2 -> 2, 3 -> 3), m - 4) + } + + @Test + def hasCorrectIterator(): Unit = { + val m = ListMap(1 -> 1, 2 -> 2, 3 -> 3, 5 -> 5, 4 -> 4) + assertEquals(List(1 -> 1, 2 -> 2, 3 -> 3, 5 -> 5, 4 -> 4), m.iterator.toList) + } +} diff --git a/test/junit/scala/collection/immutable/ListSetTest.scala b/test/junit/scala/collection/immutable/ListSetTest.scala new file mode 100644 index 0000000000..395da88c75 --- /dev/null +++ b/test/junit/scala/collection/immutable/ListSetTest.scala @@ -0,0 +1,53 @@ +package scala.collection.immutable + +import org.junit.Assert._ +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(classOf[JUnit4]) +class ListSetTest { + + @Test + def t7445(): Unit = { + val s = ListSet(1, 2, 3, 4, 5) + assertEquals(ListSet(2, 3, 4, 5), s.tail) + } + + @Test + def hasCorrectBuilder(): Unit = { + val m = ListSet("a", "b", "c", "b", "d") + assertEquals(List("a", "b", "c", "d"), m.toList) + } + + @Test + def hasTailRecursiveDelete(): Unit = { + val s = ListSet(1 to 50000: _*) + try s - 25000 catch { case e: StackOverflowError => fail("A stack overflow occurred") } + } + + @Test + def hasCorrectHeadTailLastInit(): Unit = { + val m = ListSet(1, 2, 3) + assertEquals(1, m.head) + assertEquals(ListSet(2, 3), m.tail) + assertEquals(3, m.last) + assertEquals(ListSet(1, 2), m.init) + } + + @Test + def hasCorrectAddRemove(): Unit = { + val m = ListSet(1, 2, 3) + assertEquals(ListSet(1, 2, 3, 4), m + 4) + assertEquals(ListSet(1, 2, 3), m + 2) + assertEquals(ListSet(2, 3), m - 1) + assertEquals(ListSet(1, 3), m - 2) + assertEquals(ListSet(1, 2, 3), m - 4) + } + + @Test + def hasCorrectIterator(): Unit = { + val s = ListSet(1, 2, 3, 5, 4) + assertEquals(List(1, 2, 3, 5, 4), s.iterator.toList) + } +} diff --git a/test/junit/scala/collection/immutable/PagedSeqTest.scala b/test/junit/scala/collection/immutable/PagedSeqTest.scala index 74f8825307..6c974db884 100644 --- a/test/junit/scala/collection/immutable/PagedSeqTest.scala +++ b/test/junit/scala/collection/immutable/PagedSeqTest.scala @@ -2,13 +2,14 @@ package scala.collection.immutable import org.junit.runner.RunWith import org.junit.runners.JUnit4 -import org.junit.Test +import org.junit.{Ignore, Test} import org.junit.Assert._ @RunWith(classOf[JUnit4]) class PagedSeqTest { // should not NPE, and should equal the given Seq @Test + @Ignore("This tests a non-stack safe method in a deprecated class that requires ~1.5M stack, disabling") def test_SI6615(): Unit = { assertEquals(Seq('a'), PagedSeq.fromStrings(List.fill(5000)("a")).slice(4096, 4097)) } diff --git a/test/junit/scala/collection/immutable/RangeTest.scala b/test/junit/scala/collection/immutable/RangeTest.scala new file mode 100644 index 0000000000..a0bef72bc9 --- /dev/null +++ b/test/junit/scala/collection/immutable/RangeTest.scala @@ -0,0 +1,42 @@ +package scala.collection.immutable + +import org.junit.{Assert, Test} +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import scala.tools.testing.AssertUtil + +@RunWith(classOf[JUnit4]) +class RangeTest { + import Assert._ + import AssertUtil._ + + @Test + def test_SI10060_numeric_range_min_max(): Unit = { + assertEquals(Range.Long.inclusive(1, 9, 1).min, 1) + assertEquals(Range.Long.inclusive(1, 9, 1).max, 9) + assertEquals(Range.Long.inclusive(9, 1, -1).min, 1) + assertEquals(Range.Long.inclusive(9, 1, -1).max, 9) + assertThrows[java.util.NoSuchElementException](Range.Long.inclusive(1, 9, -1).min) + assertThrows[java.util.NoSuchElementException](Range.Long.inclusive(1, 9, -1).max) + assertThrows[java.util.NoSuchElementException](Range.Long.inclusive(9, 1, 1).min) + assertThrows[java.util.NoSuchElementException](Range.Long.inclusive(9, 1, 1).max) + + assertEquals(Range.Int.inclusive(1, 9, 1).min, 1) + assertEquals(Range.Int.inclusive(1, 9, 1).max, 9) + assertEquals(Range.Int.inclusive(9, 1, -1).min, 1) + assertEquals(Range.Int.inclusive(9, 1, -1).max, 9) + assertThrows[java.util.NoSuchElementException](Range.Int.inclusive(1, 9, -1).min) + assertThrows[java.util.NoSuchElementException](Range.Int.inclusive(1, 9, -1).max) + assertThrows[java.util.NoSuchElementException](Range.Int.inclusive(9, 1, 1).min) + assertThrows[java.util.NoSuchElementException](Range.Int.inclusive(9, 1, 1).max) + + assertEquals(Range.inclusive(1, 9, 1).min, 1) + assertEquals(Range.inclusive(1, 9, 1).max, 9) + assertEquals(Range.inclusive(9, 1, -1).min, 1) + assertEquals(Range.inclusive(9, 1, -1).max, 9) + assertThrows[java.util.NoSuchElementException](Range.inclusive(1, 9, -1).min) + assertThrows[java.util.NoSuchElementException](Range.inclusive(1, 9, -1).max) + assertThrows[java.util.NoSuchElementException](Range.inclusive(9, 1, 1).min) + assertThrows[java.util.NoSuchElementException](Range.inclusive(9, 1, 1).max) + } +} 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..61f7b792e8 --- /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 // SI-6881 + def test_reference_equality: Unit = { + // Make sure we're tested with reference equality + val s = Stream.from(0) + assert(s == s, "Referentially identical streams should be equal (==)") + assert(s equals s, "Referentially identical streams should be equal (equals)") + assert((0 #:: 1 #:: s) == (0 #:: 1 #:: s), "Cons of referentially identical streams should be equal (==)") + assert((0 #:: 1 #:: s) equals (0 #:: 1 #:: s), "Cons of referentially identical streams should be equal (equals)") + } + + @Test + def t9886: Unit = { + assertEquals(Stream(None, Some(1)), None #:: Stream(Some(1))) + assertEquals(Stream(None, Some(1)), Stream(None) #::: Stream(Some(1))) + } +} diff --git a/test/junit/scala/collection/immutable/StringLikeTest.scala b/test/junit/scala/collection/immutable/StringLikeTest.scala index 3722bdfe4d..44bade860e 100644 --- a/test/junit/scala/collection/immutable/StringLikeTest.scala +++ b/test/junit/scala/collection/immutable/StringLikeTest.scala @@ -1,5 +1,6 @@ package scala.collection.immutable +import org.junit.Assert._ import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 @@ -28,10 +29,46 @@ 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 + } + + /* Test for SI-9767 */ + @Test + def testNumericConversion: Unit = { + val sOne = " \t\n 1 \n\r\t " + val sOk = "2" + val sNull:String = null + + AssertUtil.assertThrows[java.lang.NumberFormatException](sOne.toInt) + AssertUtil.assertThrows[java.lang.NumberFormatException](sOne.toLong) + AssertUtil.assertThrows[java.lang.NumberFormatException](sOne.toShort) + AssertUtil.assertThrows[java.lang.NumberFormatException](sOne.toByte) + assertTrue("trim toDouble", sOne.toDouble == 1.0d) + assertTrue("trim toFloat", sOne.toFloat == 1.0f) + + assertTrue("no trim toInt", sOk.toInt == 2) + assertTrue("no trim toLong", sOk.toLong == 2L) + assertTrue("no trim toShort", sOk.toShort == 2.toShort) + assertTrue("no trim toByte", sOk.toByte == 2.toByte) + assertTrue("no trim toDouble", sOk.toDouble == 2.0d) + assertTrue("no trim toFloat", sOk.toFloat == 2.0f) + + AssertUtil.assertThrows[java.lang.NumberFormatException](sNull.toInt, {s => s == "null"}) + AssertUtil.assertThrows[java.lang.NumberFormatException](sNull.toLong, {s => s == "null"}) + AssertUtil.assertThrows[java.lang.NumberFormatException](sNull.toShort, {s => s == "null"}) + AssertUtil.assertThrows[java.lang.NumberFormatException](sNull.toByte, {s => s == "null"}) + + AssertUtil.assertThrows[java.lang.NullPointerException](sNull.toDouble) + AssertUtil.assertThrows[java.lang.NullPointerException](sNull.toFloat) } } |