Merged in rnicoll-unique-identifier-comparable (pull request #414)

Correct Comparable interface used for isOrderedAndUnique()
This commit is contained in:
Ross Nicoll 2016-10-25 13:32:15 +00:00
commit 63b1e356ad
2 changed files with 25 additions and 2 deletions

View File

@ -13,7 +13,6 @@ import rx.Observable
import rx.subjects.UnicastSubject import rx.subjects.UnicastSubject
import java.io.BufferedInputStream import java.io.BufferedInputStream
import java.io.InputStream import java.io.InputStream
import java.lang.Comparable
import java.math.BigDecimal import java.math.BigDecimal
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.LinkOption import java.nio.file.LinkOption
@ -293,7 +292,7 @@ fun <T, I: Comparable<I>> Iterable<T>.isOrderedAndUnique(extractId: T.() -> I):
if (lastLast == null) { if (lastLast == null) {
true true
} else { } else {
lastLast.compareTo(extractId(it)) < 0 lastLast < extractId(it)
} }
} }
} }

View File

@ -0,0 +1,24 @@
package com.r3corda.core
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class UtilsTest {
fun `ordered and unique basic`() {
val basic = listOf(1, 2, 3, 5, 8)
assertTrue(basic.isOrderedAndUnique { this })
val negative = listOf(-1, 2, 5)
assertTrue(negative.isOrderedAndUnique { this })
}
fun `ordered and unique duplicate`() {
val duplicated = listOf(1, 2, 2, 3, 5, 8)
assertFalse(duplicated.isOrderedAndUnique { this })
}
fun `ordered and unique out of sequence`() {
val mixed = listOf(3, 1, 2, 8, 5)
assertFalse(mixed.isOrderedAndUnique { this })
}
}