added drop(n) and dropWhile(predicates) for KT-2067 - also tail() now returns the usual idea of tail() - namely everything but the head - rather than just the last element. Finally added more test sample code to the kdoc

This commit is contained in:
James Strachan
2012-05-23 09:35:16 +01:00
parent 53a9fff0bc
commit 939f0e9085
7 changed files with 133 additions and 34 deletions
+23
View File
@@ -302,6 +302,29 @@ class CollectionTest {
}
}
test fun drop() {
val coll = arrayList("foo", "bar", "abc")
assertEquals(arrayList("bar", "abc"), coll.drop(1))
assertEquals(arrayList("abc"), coll.drop(2))
}
test fun dropWhile() {
val coll = arrayList("foo", "bar", "abc")
assertEquals(arrayList("bar", "abc"), coll.dropWhile{ it.startsWith("f") })
}
test fun take() {
val coll = arrayList("foo", "bar", "abc")
assertEquals(arrayList("foo"), coll.take(1))
assertEquals(arrayList("foo", "bar"), coll.take(2))
}
test fun takeWhile() {
val coll = arrayList("foo", "bar", "abc")
assertEquals(arrayList("foo"), coll.takeWhile{ it.startsWith("f") })
assertEquals(arrayList("foo", "bar", "abc"), coll.takeWhile{ it.size == 3 })
}
test fun toArray() {
val data = arrayList("foo", "bar")
val arr = data.toArray()
+31 -28
View File
@@ -1,36 +1,39 @@
package test.collections
import kotlin.test.*
import org.junit.Test
import org.junit.Test as test
class ListTest {
val data = arrayList("foo", "bar")
Test fun headAndTail() {
val h = data.head
assertEquals("foo", h)
val t = data.tail
assertEquals("bar", t)
}
Test fun firstAndLast() {
val h = data.first
assertEquals("foo", h)
val t = data.last
assertEquals("bar", t)
}
Test fun withIndices() {
val withIndices = data.withIndices()
var index = 0
for (withIndex in withIndices) {
assertEquals(withIndex._1, index)
assertEquals(withIndex._2, data[index])
index++
test fun head() {
val data = arrayList("foo", "bar")
assertEquals("foo", data.head)
}
test fun tail() {
val data = arrayList("foo", "bar", "whatnot")
assertEquals(arrayList("bar", "whatnot"), data.tail)
}
test fun first() {
val data = arrayList("foo", "bar")
assertEquals("foo", data.first)
}
test fun last() {
val data = arrayList("foo", "bar")
assertEquals("bar", data.last)
}
test fun withIndices() {
val data = arrayList("foo", "bar")
val withIndices = data.withIndices()
var index = 0
for (withIndex in withIndices) {
assertEquals(withIndex._1, index)
assertEquals(withIndex._2, data[index])
index++
}
assertEquals(data.size(), index)
}
assertEquals(data.size(), index)
}
}