diff options
author | Grzegorz Kossakowski <grzegorz.kossakowski@gmail.com> | 2014-11-17 19:20:18 +0100 |
---|---|---|
committer | Grzegorz Kossakowski <grzegorz.kossakowski@gmail.com> | 2014-11-17 19:20:18 +0100 |
commit | bddb3afbcc26a037764ea83b67f3d1cc57dcc8e0 (patch) | |
tree | 6de021c206b91659956ecd2b6a50e5a6edc1d7b2 /test/junit | |
parent | 9a99913dfe1fbf06eff9deae2d2c7ee4e096f621 (diff) | |
parent | 1f0d3474115968e94327c5484ce402b4d83f0443 (diff) | |
download | scala-bddb3afbcc26a037764ea83b67f3d1cc57dcc8e0.tar.gz scala-bddb3afbcc26a037764ea83b67f3d1cc57dcc8e0.tar.bz2 scala-bddb3afbcc26a037764ea83b67f3d1cc57dcc8e0.zip |
Merge pull request #4075 from som-snytt/issue/8835-junit
SI-8835 Iterator tests can be junit
Diffstat (limited to 'test/junit')
-rw-r--r-- | test/junit/scala/collection/IteratorTest.scala | 133 | ||||
-rw-r--r-- | test/junit/scala/tools/testing/AssertUtil.scala | 20 |
2 files changed, 150 insertions, 3 deletions
diff --git a/test/junit/scala/collection/IteratorTest.scala b/test/junit/scala/collection/IteratorTest.scala index b7a9805c9f..d5389afd0c 100644 --- a/test/junit/scala/collection/IteratorTest.scala +++ b/test/junit/scala/collection/IteratorTest.scala @@ -6,11 +6,14 @@ import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 +import scala.tools.testing.AssertUtil._ + +import Seq.empty + @RunWith(classOf[JUnit4]) class IteratorTest { - @Test - def groupedIteratorShouldNotAskForUnneededElement(): Unit = { + @Test def groupedIteratorShouldNotAskForUnneededElement(): Unit = { var counter = 0 val it = new Iterator[Int] { var i = 0 ; def hasNext = { counter = i; true } ; def next = { i += 1; i } } val slidingIt = it sliding 2 @@ -25,4 +28,130 @@ class IteratorTest { slidingIt.next assertEquals("Counter should be one, that means we didn't look further than needed", 1, counter) } + + @Test def dropDoesNotGrowStack(): Unit = { + def it = new Iterator[Throwable] { def hasNext = true ; def next = new Throwable } + + assertEquals(it.drop(1).next.getStackTrace.length, it.drop(1).drop(1).next.getStackTrace.length) + } + + @Test def dropIsChainable(): Unit = { + assertSameElements(1 to 4, Iterator from 0 take 5 drop 1) + assertSameElements(3 to 4, Iterator from 0 take 5 drop 3) + assertSameElements(empty, Iterator from 0 take 5 drop 5) + assertSameElements(empty, Iterator from 0 take 5 drop 10) + assertSameElements(0 to 4, Iterator from 0 take 5 drop 0) + assertSameElements(0 to 4, Iterator from 0 take 5 drop -1) + assertSameElements(2 to 8 by 2, Iterator from 0 take 5 drop 1 map (2 * _)) + assertSameElements(2 to 8 by 2, Iterator from 0 take 5 map (2 * _) drop 1) + assertSameElements(3 to 4, Iterator from 0 take 5 drop 1 drop 2) + assertSameElements(3 to 4, Iterator from 0 take 5 drop 2 drop 1) + } + + @Test def sliceIsChainable(): Unit = { + assertSameElements(3 to 6, Iterator from 0 slice (3, 7)) + assertSameElements(empty, Iterator from 0 slice (3, 3)) + assertSameElements(0 to 2, Iterator from 0 slice (-1, 3)) + assertSameElements(empty, Iterator from 0 slice (3, -1)) + assertSameElements(6 to 12 by 2, Iterator from 0 slice (3, 7) map (2 * _)) + assertSameElements(6 to 12 by 2, Iterator from 0 map (2 * _) slice (3, 7)) + assertSameElements(4 to 6, Iterator from 0 slice (3, 7) drop 1) + assertSameElements(4 to 7, Iterator from 0 drop 1 slice (3, 7)) + assertSameElements(4 to 5, Iterator from 0 slice (3, 7) slice (1, 3)) + assertSameElements(4 to 6, Iterator from 0 slice (3, 7) slice (1, 10)) + } + + // test/files/run/iterator-concat.scala + @Test def concatIsStackFriendly(): Unit = { + // Create `size` Function0s, each of which evaluates to an Iterator + // which produces 1. Then fold them over ++ to get a single iterator, + // which should sum to "size". + def mk(size: Int): Iterator[Int] = { + //val closures = (1 to size).toList.map(x => (() => Iterator(1))) + //closures.foldLeft(Iterator.empty: Iterator[Int])((res, f) => res ++ f()) + List.fill(size)(() => Iterator(1)).foldLeft(Iterator.empty: Iterator[Int])((res, f) => res ++ f()) + } + assertEquals(100, mk(100).sum) + assertEquals(1000, mk(1000).sum) + assertEquals(10000, mk(10000).sum) + assertEquals(100000, mk(100000).sum) + } + + @Test def from(): Unit = { + val it1 = Iterator.from(-1) + val it2 = Iterator.from(0, -1) + assertEquals(-1, it1.next()) + assertEquals(0, it2.next()) + } + @Test def range(): Unit = { + assertEquals(5, Iterator.range(0, 10, 2).size) + assertEquals(0, Iterator.range(0, 10, -2).size) + assertEquals(5, Iterator.range(10, 0, -2).size) + assertEquals(0, Iterator.range(10, 0, 2).size) + assertEquals(1, Iterator.range(0, 10, 11).size) + assertEquals(10, Iterator.range(0, 10, 1).size) + assertEquals(10, Iterator.range(10, 0, -1).size) + } + @Test def range3(): Unit = { + val r1 = Iterator.range(0, 10) + assertTrue(r1 contains 5) + assertTrue(r1 contains 6) + assertFalse(r1 contains 4) + val r2a = Iterator.range(0, 10, 2) + assertFalse(r2a contains 5) + val r2b = Iterator.range(0, 10, 2) + assertTrue(r2b contains 6) + val r3 = Iterator.range(0, 10, 11) + assertFalse(r3 contains 5) + assertTrue(r3.isEmpty) + } + @Test def take(): Unit = { + assertEquals(10, (Iterator from 0 take 10).size) + } + @Test def foreach(): Unit = { + val it1 = Iterator.from(0) take 20 + var n = 0 + it1 foreach { n += _ } + assertEquals(190, n) + } + // ticket #429 + @Test def fromArray(): Unit = { + val a = List(1, 2, 3, 4).toArray + var xs0 = a.iterator.toList; + var xs1 = a.slice(0, 1).iterator + var xs2 = a.slice(0, 2).iterator + var xs3 = a.slice(0, 3).iterator + var xs4 = a.slice(0, 4).iterator + assertEquals(14, xs0.size + xs1.size + xs2.size + xs3.size + xs4.size) + } + @Test def toSeq(): Unit = { + assertEquals("1x2x3x4x5", List(1, 2, 3, 4, 5).iterator.mkString("x")) + } + @Test def indexOf(): Unit = { + assertEquals(3, List(1, 2, 3, 4, 5).iterator.indexOf(4)) + assertEquals(-1, List(1, 2, 3, 4, 5).iterator.indexOf(16)) + } + @Test def indexWhere(): Unit = { + 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 }) + } + // iterator-iterate-lazy.scala + // was java.lang.UnsupportedOperationException: tail of empty list + @Test def iterateIsSufficientlyLazy(): Unit = { + //Iterator.iterate((1 to 5).toList)(_.tail).takeWhile(_.nonEmpty).toList // suffices + Iterator.iterate((1 to 5).toList)(_.tail).takeWhile(_.nonEmpty).map(_.head).toList + } + // SI-3516 + @Test def toStreamIsSufficientlyLazy(): Unit = { + val results = collection.mutable.ListBuffer.empty[Int] + def mkIterator = (1 to 5).iterator map (x => { results += x ; x }) + def mkInfinite = Iterator continually { results += 1 ; 1 } + + // Stream is strict in its head so we should see 1 from each of them. + val s1 = mkIterator.toStream + val s2 = mkInfinite.toStream + // back and forth without slipping into nontermination. + results += (Stream from 1).toIterator.drop(10).toStream.drop(10).toIterator.next() + assertSameElements(List(1,1,21), results) + } } diff --git a/test/junit/scala/tools/testing/AssertUtil.scala b/test/junit/scala/tools/testing/AssertUtil.scala index 9b4833d46b..83a637783f 100644 --- a/test/junit/scala/tools/testing/AssertUtil.scala +++ b/test/junit/scala/tools/testing/AssertUtil.scala @@ -1,6 +1,11 @@ package scala.tools package testing +import org.junit.Assert +import Assert.fail +import scala.runtime.ScalaRunTime.stringOf +import scala.collection.{ GenIterable, IterableLike } + /** This module contains additional higher-level assert statements * that are ultimately based on junit.Assert primitives. */ @@ -21,6 +26,19 @@ object AssertUtil { throw e else return } - throw new AssertionError("Expression did not throw!") + fail("Expression did not throw!") } + + /** JUnit-style assertion for `IterableLike.sameElements`. + */ + def assertSameElements[A, B >: A](expected: IterableLike[A, _], actual: GenIterable[B], message: String = ""): Unit = + if (!(expected sameElements actual)) + fail( + f"${ if (message.nonEmpty) s"$message " else "" }expected:<${ stringOf(expected) }> but was:<${ stringOf(actual) }>" + ) + + /** Convenient for testing iterators. + */ + def assertSameElements[A, B >: A](expected: IterableLike[A, _], actual: Iterator[B]): Unit = + assertSameElements(expected, actual.toList, "") } |