From 3be639c503bd677c0263c39af26c5ca42cf42635 Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Sat, 22 Aug 2009 12:43:49 +0000 Subject: The fruit of a bunch of iteration on Iterator. Flexible yet simple/clean API for grouped and sliding. --- test/files/run/unittest_iterator.scala | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 test/files/run/unittest_iterator.scala (limited to 'test') diff --git a/test/files/run/unittest_iterator.scala b/test/files/run/unittest_iterator.scala new file mode 100644 index 0000000000..93aaa4a834 --- /dev/null +++ b/test/files/run/unittest_iterator.scala @@ -0,0 +1,37 @@ +// Some iterator grouped/sliding unit tests +object Test +{ + def it = (1 to 10).iterator + def assertThat[T](expectedLength: Int, expectedLast: Seq[T])(it: Iterator[Seq[T]]) { + val xs = it.toList + def fail(msg: String) = "assertion failed on %s: %s".format(xs, msg) + assert(xs.size == expectedLength, fail("expected length " + expectedLength)) + assert(xs.last == expectedLast, fail("expected last " + expectedLast)) + } + + def main(args: Array[String]): Unit = { + val itSum = it.toStream.sum + for (i <- it) { + // sum of the groups == sum of the original + val thisSum = ((it grouped i) map (_.sum)).toStream.sum + assert(thisSum == itSum, thisSum + " != " + itSum) + } + + // grouped + assertThat(4, List(10)) { it grouped 3 } + assertThat(3, List(7, 8, 9)) { it grouped 3 withPartial false } + assertThat(4, List(10, -1, -1)) { it grouped 3 withPadding -1 } + + // testing by-name padding + val padIt = it + assertThat(4, List(10, 1, 2)) { it grouped 3 withPadding padIt.next } + + // sliding + assertThat(8, List(8, 9, 10)) { it sliding 3 } + assertThat(3, (3 to 10).toList) { it sliding 8 } + assertThat(2, List(9, 10)) { it.sliding(8, 8) } + assertThat(1, (1 to 8).toList) { it.sliding(8, 8) withPartial false } + assertThat(2, List(9, 10, -1, -1, -1)) { it.sliding(5, 8) withPadding -1 } + assertThat(1, (1 to 5).toList) { it.sliding(5, 8) withPartial false } + } +} -- cgit v1.2.3