summaryrefslogtreecommitdiff
path: root/test/files/run/unittest_iterator.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2009-08-22 12:43:49 +0000
committerPaul Phillips <paulp@improving.org>2009-08-22 12:43:49 +0000
commit3be639c503bd677c0263c39af26c5ca42cf42635 (patch)
tree4de6adb806060ef4e3016b13d2037e0c83617149 /test/files/run/unittest_iterator.scala
parentd4c53a90db99bfdce6a622a6d1b6195deb1b92bf (diff)
downloadscala-3be639c503bd677c0263c39af26c5ca42cf42635.tar.gz
scala-3be639c503bd677c0263c39af26c5ca42cf42635.tar.bz2
scala-3be639c503bd677c0263c39af26c5ca42cf42635.zip
The fruit of a bunch of iteration on Iterator.
Flexible yet simple/clean API for grouped and sliding.
Diffstat (limited to 'test/files/run/unittest_iterator.scala')
-rw-r--r--test/files/run/unittest_iterator.scala37
1 files changed, 37 insertions, 0 deletions
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 }
+ }
+}