#KT-1391 Fixed
This commit is contained in:
@@ -28,7 +28,7 @@ public inline fun <T> Array<T>.any(predicate: (T) -> Boolean) : Boolean {
|
|||||||
/**
|
/**
|
||||||
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||||
*
|
*
|
||||||
* @includeFunctionBody ../../test/CollectionTest.kt makeString
|
* @includeFunctionBody ../../test/CollectionTest.kt appendString
|
||||||
*/
|
*/
|
||||||
public inline fun <T> Array<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit {
|
public inline fun <T> Array<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit {
|
||||||
buffer.append(prefix)
|
buffer.append(prefix)
|
||||||
@@ -180,22 +180,25 @@ public inline fun <T> Array<T>.reverse() : List<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Copies all elements into the given collection */
|
/** Copies all elements into the given collection */
|
||||||
public inline fun <T, C: Collection<T>> Array<T>.to(result: C) : C {
|
public inline fun <in T, C: Collection<in T>> Array<T>.to(result: C) : C {
|
||||||
for (element in this) result.add(element)
|
for (element in this) result.add(element)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Copies all elements into a [[LinkedList]] */
|
/** Copies all elements into a [[LinkedList]] */
|
||||||
public inline fun <T> Array<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
|
public inline fun <in T> Array<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
|
||||||
|
|
||||||
/** Copies all elements into a [[List]] */
|
/** Copies all elements into a [[List]] */
|
||||||
public inline fun <T> Array<T>.toList() : List<T> = to(ArrayList<T>())
|
public inline fun <in T> Array<T>.toList() : List<T> = to(ArrayList<T>())
|
||||||
|
|
||||||
|
/** Copies all elements into a [[List] */
|
||||||
|
public inline fun <in T> Array<T>.toCollection() : Collection<T> = to(ArrayList<T>())
|
||||||
|
|
||||||
/** Copies all elements into a [[Set]] */
|
/** Copies all elements into a [[Set]] */
|
||||||
public inline fun <T> Array<T>.toSet() : Set<T> = to(HashSet<T>())
|
public inline fun <in T> Array<T>.toSet() : Set<T> = to(HashSet<T>())
|
||||||
|
|
||||||
/** Copies all elements into a [[SortedSet]] */
|
/** Copies all elements into a [[SortedSet]] */
|
||||||
public inline fun <T> Array<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
|
public inline fun <in T> Array<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
|
||||||
|
|
||||||
/**
|
/**
|
||||||
TODO figure out necessary variance/generics ninja stuff... :)
|
TODO figure out necessary variance/generics ninja stuff... :)
|
||||||
|
|||||||
@@ -42,6 +42,23 @@ public inline fun <T> Array<T?>?.filterNotNull() : List<T> = filterNotNullTo<T,
|
|||||||
*/
|
*/
|
||||||
public inline fun <T, R> Array<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
|
public inline fun <T, R> Array<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
|
||||||
|
*
|
||||||
|
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
|
||||||
|
*/
|
||||||
|
public inline fun <in T> Array<T?>?.requireNoNulls() : List<T> {
|
||||||
|
val list = ArrayList<T>()
|
||||||
|
for (element in this) {
|
||||||
|
if (element == null) {
|
||||||
|
throw IllegalArgumentException("null element found in $this")
|
||||||
|
} else {
|
||||||
|
list.add(element)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list containing the first *n* elements
|
* Returns a list containing the first *n* elements
|
||||||
*
|
*
|
||||||
|
|||||||
+10
-7
@@ -26,7 +26,7 @@ public inline fun <T> java.util.Iterator<T>.any(predicate: (T) -> Boolean) : Boo
|
|||||||
/**
|
/**
|
||||||
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||||
*
|
*
|
||||||
* @includeFunctionBody ../../test/CollectionTest.kt makeString
|
* @includeFunctionBody ../../test/CollectionTest.kt appendString
|
||||||
*/
|
*/
|
||||||
public inline fun <T> java.util.Iterator<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit {
|
public inline fun <T> java.util.Iterator<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit {
|
||||||
buffer.append(prefix)
|
buffer.append(prefix)
|
||||||
@@ -178,22 +178,25 @@ public inline fun <T> java.util.Iterator<T>.reverse() : List<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Copies all elements into the given collection */
|
/** Copies all elements into the given collection */
|
||||||
public inline fun <T, C: Collection<T>> java.util.Iterator<T>.to(result: C) : C {
|
public inline fun <in T, C: Collection<in T>> java.util.Iterator<T>.to(result: C) : C {
|
||||||
for (element in this) result.add(element)
|
for (element in this) result.add(element)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Copies all elements into a [[LinkedList]] */
|
/** Copies all elements into a [[LinkedList]] */
|
||||||
public inline fun <T> java.util.Iterator<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
|
public inline fun <in T> java.util.Iterator<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
|
||||||
|
|
||||||
/** Copies all elements into a [[List]] */
|
/** Copies all elements into a [[List]] */
|
||||||
public inline fun <T> java.util.Iterator<T>.toList() : List<T> = to(ArrayList<T>())
|
public inline fun <in T> java.util.Iterator<T>.toList() : List<T> = to(ArrayList<T>())
|
||||||
|
|
||||||
|
/** Copies all elements into a [[List] */
|
||||||
|
public inline fun <in T> java.util.Iterator<T>.toCollection() : Collection<T> = to(ArrayList<T>())
|
||||||
|
|
||||||
/** Copies all elements into a [[Set]] */
|
/** Copies all elements into a [[Set]] */
|
||||||
public inline fun <T> java.util.Iterator<T>.toSet() : Set<T> = to(HashSet<T>())
|
public inline fun <in T> java.util.Iterator<T>.toSet() : Set<T> = to(HashSet<T>())
|
||||||
|
|
||||||
/** Copies all elements into a [[SortedSet]] */
|
/** Copies all elements into a [[SortedSet]] */
|
||||||
public inline fun <T> java.util.Iterator<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
|
public inline fun <in T> java.util.Iterator<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
|
||||||
|
|
||||||
/**
|
/**
|
||||||
TODO figure out necessary variance/generics ninja stuff... :)
|
TODO figure out necessary variance/generics ninja stuff... :)
|
||||||
@@ -28,7 +28,7 @@ public inline fun <T> Iterable<T>.any(predicate: (T) -> Boolean) : Boolean {
|
|||||||
/**
|
/**
|
||||||
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||||
*
|
*
|
||||||
* @includeFunctionBody ../../test/CollectionTest.kt makeString
|
* @includeFunctionBody ../../test/CollectionTest.kt appendString
|
||||||
*/
|
*/
|
||||||
public inline fun <T> Iterable<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit {
|
public inline fun <T> Iterable<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit {
|
||||||
buffer.append(prefix)
|
buffer.append(prefix)
|
||||||
@@ -180,22 +180,25 @@ public inline fun <T> Iterable<T>.reverse() : List<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Copies all elements into the given collection */
|
/** Copies all elements into the given collection */
|
||||||
public inline fun <T, C: Collection<T>> Iterable<T>.to(result: C) : C {
|
public inline fun <in T, C: Collection<in T>> Iterable<T>.to(result: C) : C {
|
||||||
for (element in this) result.add(element)
|
for (element in this) result.add(element)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Copies all elements into a [[LinkedList]] */
|
/** Copies all elements into a [[LinkedList]] */
|
||||||
public inline fun <T> Iterable<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
|
public inline fun <in T> Iterable<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
|
||||||
|
|
||||||
/** Copies all elements into a [[List]] */
|
/** Copies all elements into a [[List]] */
|
||||||
public inline fun <T> Iterable<T>.toList() : List<T> = to(ArrayList<T>())
|
public inline fun <in T> Iterable<T>.toList() : List<T> = to(ArrayList<T>())
|
||||||
|
|
||||||
|
/** Copies all elements into a [[List] */
|
||||||
|
public inline fun <in T> Iterable<T>.toCollection() : Collection<T> = to(ArrayList<T>())
|
||||||
|
|
||||||
/** Copies all elements into a [[Set]] */
|
/** Copies all elements into a [[Set]] */
|
||||||
public inline fun <T> Iterable<T>.toSet() : Set<T> = to(HashSet<T>())
|
public inline fun <in T> Iterable<T>.toSet() : Set<T> = to(HashSet<T>())
|
||||||
|
|
||||||
/** Copies all elements into a [[SortedSet]] */
|
/** Copies all elements into a [[SortedSet]] */
|
||||||
public inline fun <T> Iterable<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
|
public inline fun <in T> Iterable<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
|
||||||
|
|
||||||
/**
|
/**
|
||||||
TODO figure out necessary variance/generics ninja stuff... :)
|
TODO figure out necessary variance/generics ninja stuff... :)
|
||||||
|
|||||||
@@ -42,6 +42,23 @@ public inline fun <T> Iterable<T?>?.filterNotNull() : List<T> = filterNotNullTo<
|
|||||||
*/
|
*/
|
||||||
public inline fun <T, R> Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
|
public inline fun <T, R> Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
|
||||||
|
*
|
||||||
|
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
|
||||||
|
*/
|
||||||
|
public inline fun <in T> Iterable<T?>?.requireNoNulls() : List<T> {
|
||||||
|
val list = ArrayList<T>()
|
||||||
|
for (element in this) {
|
||||||
|
if (element == null) {
|
||||||
|
throw IllegalArgumentException("null element found in $this")
|
||||||
|
} else {
|
||||||
|
list.add(element)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list containing the first *n* elements
|
* Returns a list containing the first *n* elements
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -110,6 +110,19 @@ private class FlatMapIterator<T, R>(val iterator : java.util.Iterator<T>, val tr
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an iterator containing all the non-*null* elements, lazily throwing an [[IllegalArgumentException]]
|
||||||
|
if there are any null elements
|
||||||
|
*
|
||||||
|
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt requireNoNulls
|
||||||
|
*/
|
||||||
|
public inline fun <in T> java.util.Iterator<T?>.requireNoNulls(): java.util.Iterator<T> {
|
||||||
|
return map<T?, T>{
|
||||||
|
if (it == null) throw IllegalArgumentException("null element in iterator $this") else it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an iterator restricted to the first *n* elements
|
* Returns an iterator restricted to the first *n* elements
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -182,20 +182,20 @@ public inline fun <in T, C: Collection<in T>> java.lang.Iterable<T>.to(result: C
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Copies all elements into a [[LinkedList]] if its not already a [[LinkedList]] */
|
/** Copies all elements into a [[LinkedList]] */
|
||||||
public inline fun <in T> java.lang.Iterable<T>.toLinkedList() : LinkedList<T> = if (this is LinkedList<T>) this else to(LinkedList<T>())
|
public inline fun <in T> java.lang.Iterable<T>.toLinkedList() : LinkedList<T> = to(LinkedList<T>())
|
||||||
|
|
||||||
/** Copies all elements into a [[List]] if its not already a [[List]] */
|
/** Copies all elements into a [[List]] */
|
||||||
public inline fun <in T> java.lang.Iterable<T>.toList() : List<T> = if (this is List<T>) this else to(ArrayList<T>())
|
public inline fun <in T> java.lang.Iterable<T>.toList() : List<T> = to(ArrayList<T>())
|
||||||
|
|
||||||
/** Copies all elements into a [[List]] if it is not already a [[Collection]] */
|
/** Copies all elements into a [[List] */
|
||||||
public inline fun <in T> java.lang.Iterable<T>.toCollection() : Collection<T> = if (this is Collection<T>) this else to(ArrayList<T>())
|
public inline fun <in T> java.lang.Iterable<T>.toCollection() : Collection<T> = to(ArrayList<T>())
|
||||||
|
|
||||||
/** Copies all elements into a [[Set]] if its not already a [[Set]] */
|
/** Copies all elements into a [[Set]] */
|
||||||
public inline fun <in T> java.lang.Iterable<T>.toSet() : Set<T> = if (this is Set<T>) this else to(HashSet<T>())
|
public inline fun <in T> java.lang.Iterable<T>.toSet() : Set<T> = to(HashSet<T>())
|
||||||
|
|
||||||
/** Copies all elements into a [[SortedSet]] if its not already a [[SortedSet]] */
|
/** Copies all elements into a [[SortedSet]] */
|
||||||
public inline fun <in T> java.lang.Iterable<T>.toSortedSet() : SortedSet<T> = if (this is SortedSet<T>) this else to(TreeSet<T>())
|
public inline fun <in T> java.lang.Iterable<T>.toSortedSet() : SortedSet<T> = to(TreeSet<T>())
|
||||||
|
|
||||||
/**
|
/**
|
||||||
TODO figure out necessary variance/generics ninja stuff... :)
|
TODO figure out necessary variance/generics ninja stuff... :)
|
||||||
|
|||||||
@@ -39,6 +39,23 @@ public inline fun <T> java.lang.Iterable<T?>?.filterNotNull() : List<T> = filter
|
|||||||
*/
|
*/
|
||||||
public inline fun <T, R> java.lang.Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
|
public inline fun <T, R> java.lang.Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), transform)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
|
||||||
|
*
|
||||||
|
* @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls
|
||||||
|
*/
|
||||||
|
public inline fun <in T> java.lang.Iterable<T?>?.requireNoNulls() : List<T> {
|
||||||
|
val list = ArrayList<T>()
|
||||||
|
for (element in this) {
|
||||||
|
if (element == null) {
|
||||||
|
throw IllegalArgumentException("null element found in $this")
|
||||||
|
} else {
|
||||||
|
list.add(element)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list containing the first *n* elements
|
* Returns a list containing the first *n* elements
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -101,13 +101,15 @@ public fun fails(block: ()-> Unit): Throwable? {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Asserts that a block fails with a specific exception being thrown */
|
/** Asserts that a block fails with a specific exception being thrown */
|
||||||
public fun <T: Throwable> failsWith(block: ()-> Unit) {
|
public fun <T: Throwable> failsWith(block: ()-> Any): T {
|
||||||
try {
|
try {
|
||||||
block()
|
block()
|
||||||
asserter.fail("Expected an exception to be thrown")
|
asserter.fail("Expected an exception to be thrown")
|
||||||
|
throw IllegalStateException("Should have failed")
|
||||||
} catch (e: T) {
|
} catch (e: T) {
|
||||||
//println("Caught expected exception: $e")
|
//println("Caught expected exception: $e")
|
||||||
// OK
|
// OK
|
||||||
|
return e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -271,6 +271,18 @@ class CollectionTest {
|
|||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test fun requireNoNulls() {
|
||||||
|
val data = arrayList<String?>("foo", "bar")
|
||||||
|
val notNull = data.requireNoNulls()
|
||||||
|
assertEquals(arrayList("foo", "bar"), notNull)
|
||||||
|
|
||||||
|
val hasNulls = arrayList("foo", null, "bar")
|
||||||
|
failsWith<IllegalArgumentException> {
|
||||||
|
// should throw an exception as we have a null
|
||||||
|
hasNulls.requireNoNulls()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
test fun reverse() {
|
test fun reverse() {
|
||||||
val data = arrayList("foo", "bar")
|
val data = arrayList("foo", "bar")
|
||||||
val rev = data.reverse()
|
val rev = data.reverse()
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ fun main(args: Array<String>) {
|
|||||||
it.replaceAll("java.lang.Iterable<T", "Iterable<T")
|
it.replaceAll("java.lang.Iterable<T", "Iterable<T")
|
||||||
}
|
}
|
||||||
|
|
||||||
generateFile(File(outDir, "JUnilIteratorsFromJLangIterables.kt"), "package kotlin", File(srcDir, "JLangIterables.kt")) {
|
generateFile(File(outDir, "JUtilIteratorsFromJLangIterables.kt"), "package kotlin", File(srcDir, "JLangIterables.kt")) {
|
||||||
it.replaceAll("java.lang.Iterable<T", "java.util.Iterator<T")
|
it.replaceAll("java.lang.Iterable<T", "java.util.Iterator<T")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +74,7 @@ fun main(args: Array<String>) {
|
|||||||
it.replaceAll("java.util.Collection<T", "Array<T")
|
it.replaceAll("java.util.Collection<T", "Array<T")
|
||||||
}
|
}
|
||||||
|
|
||||||
generateFile(File(outDir, "JUnilIterablesFromJUtilCollections.kt"), "package kotlin", File(srcDir, "JUtilCollections.kt")) {
|
generateFile(File(outDir, "JUtilIterablesFromJUtilCollections.kt"), "package kotlin", File(srcDir, "JUtilCollections.kt")) {
|
||||||
it.replaceAll("java.util.Collection<T", "java.lang.Iterable<T").replaceAll("(this.size)", "")
|
it.replaceAll("java.util.Collection<T", "java.lang.Iterable<T").replaceAll("(this.size)", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
package iterators
|
package iterators
|
||||||
|
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
import org.junit.Test
|
import org.junit.Test as test
|
||||||
|
import kotlin.test.failsWith
|
||||||
|
|
||||||
class IteratorsTest {
|
class IteratorsTest {
|
||||||
|
|
||||||
@@ -11,24 +12,24 @@ class IteratorsTest {
|
|||||||
return iterate<Int> { when (index++) { 0 -> a; 1 -> b; else -> { val result = a + b; a = b; b = result; result } } }
|
return iterate<Int> { when (index++) { 0 -> a; 1 -> b; else -> { val result = a + b; a = b; b = result; result } } }
|
||||||
}
|
}
|
||||||
|
|
||||||
Test fun filterAndTakeWhileExtractTheElementsWithinRange() {
|
test fun filterAndTakeWhileExtractTheElementsWithinRange() {
|
||||||
assertEquals(arrayList(144, 233, 377, 610, 987), fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.toList())
|
assertEquals(arrayList(144, 233, 377, 610, 987), fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.toList())
|
||||||
}
|
}
|
||||||
|
|
||||||
Test fun foldReducesTheFirstNElements() {
|
test fun foldReducesTheFirstNElements() {
|
||||||
val sum = { (a: Int, b: Int) -> a + b }
|
val sum = { (a: Int, b: Int) -> a + b }
|
||||||
assertEquals(arrayList(13, 21, 34, 55, 89).fold(0, sum), fibonacci().filter { it > 10 }.take(5).fold(0, sum))
|
assertEquals(arrayList(13, 21, 34, 55, 89).fold(0, sum), fibonacci().filter { it > 10 }.take(5).fold(0, sum))
|
||||||
}
|
}
|
||||||
|
|
||||||
Test fun takeExtractsTheFirstNElements() {
|
test fun takeExtractsTheFirstNElements() {
|
||||||
assertEquals(arrayList(0, 1, 1, 2, 3, 5, 8, 13, 21, 34), fibonacci().take(10).toList())
|
assertEquals(arrayList(0, 1, 1, 2, 3, 5, 8, 13, 21, 34), fibonacci().take(10).toList())
|
||||||
}
|
}
|
||||||
|
|
||||||
Test fun mapAndTakeWhileExtractTheTransformedElements() {
|
test fun mapAndTakeWhileExtractTheTransformedElements() {
|
||||||
assertEquals(arrayList(0, 3, 3, 6, 9, 15), fibonacci().map { it * 3 }.takeWhile { (i: Int) -> i < 20 }.toList())
|
assertEquals(arrayList(0, 3, 3, 6, 9, 15), fibonacci().map { it * 3 }.takeWhile { (i: Int) -> i < 20 }.toList())
|
||||||
}
|
}
|
||||||
|
|
||||||
Test fun flatMapAndTakeExtractTheTransformedElements() {
|
test fun flatMapAndTakeExtractTheTransformedElements() {
|
||||||
fun intToBinaryDigits() = { (i: Int) ->
|
fun intToBinaryDigits() = { (i: Int) ->
|
||||||
val binary = Integer.toBinaryString(i).sure()
|
val binary = Integer.toBinaryString(i).sure()
|
||||||
var index = 0
|
var index = 0
|
||||||
@@ -47,11 +48,24 @@ class IteratorsTest {
|
|||||||
assertEquals(expected, fibonacci().flatMap<Int, Char>(intToBinaryDigits()).take(10).toList())
|
assertEquals(expected, fibonacci().flatMap<Int, Char>(intToBinaryDigits()).take(10).toList())
|
||||||
}
|
}
|
||||||
|
|
||||||
Test fun joinConcatenatesTheFirstNElementsAboveAThreshold() {
|
test fun joinConcatenatesTheFirstNElementsAboveAThreshold() {
|
||||||
assertEquals("13, 21, 34, 55, 89, ...", fibonacci().filter { it > 10 }.makeString(separator = ", ", limit = 5))
|
assertEquals("13, 21, 34, 55, 89, ...", fibonacci().filter { it > 10 }.makeString(separator = ", ", limit = 5))
|
||||||
}
|
}
|
||||||
|
|
||||||
Test fun toStringJoinsNoMoreThanTheFirstTenElements() {
|
test fun requireNoNulls() {
|
||||||
|
val iter = arrayList<String?>("foo", "bar").iterator()
|
||||||
|
val notNull = iter.requireNoNulls()
|
||||||
|
assertEquals(arrayList("foo", "bar"), notNull.toList())
|
||||||
|
|
||||||
|
val iterWithNulls = arrayList("foo", null, "bar").iterator()
|
||||||
|
val notNull2 = iterWithNulls.requireNoNulls()
|
||||||
|
failsWith<IllegalArgumentException> {
|
||||||
|
// should throw an exception as we have a null
|
||||||
|
notNull2.toList()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test fun toStringJoinsNoMoreThanTheFirstTenElements() {
|
||||||
assertEquals("0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...", fibonacci().makeString(limit = 10))
|
assertEquals("0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...", fibonacci().makeString(limit = 10))
|
||||||
assertEquals("13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...", fibonacci().filter { it > 10 }.makeString(limit = 10))
|
assertEquals("13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...", fibonacci().filter { it > 10 }.makeString(limit = 10))
|
||||||
assertEquals("144, 233, 377, 610, 987", fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.makeString())
|
assertEquals("144, 233, 377, 610, 987", fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.makeString())
|
||||||
|
|||||||
Reference in New Issue
Block a user