added count(predicate) helper function to String along with Char.is*() methods from java.lang.Character
This commit is contained in:
@@ -3,17 +3,17 @@ package test.string
|
||||
import kotlin.io.*
|
||||
import kotlin.test.*
|
||||
|
||||
import junit.framework.*
|
||||
import org.junit.Test as test
|
||||
|
||||
class StringTest() : TestCase() {
|
||||
fun testStringIterator() {
|
||||
class StringTest {
|
||||
test fun stringIterator() {
|
||||
var sum = 0
|
||||
for(c in "239")
|
||||
sum += (c.toInt() - '0'.toInt())
|
||||
assertTrue(sum == 14)
|
||||
}
|
||||
|
||||
fun testStringBuilderIterator() {
|
||||
test fun stringBuilderIterator() {
|
||||
var sum = 0
|
||||
val sb = StringBuilder()
|
||||
for(c in "239")
|
||||
@@ -26,7 +26,7 @@ class StringTest() : TestCase() {
|
||||
assertTrue(sum == 14)
|
||||
}
|
||||
|
||||
fun testOrEmpty() {
|
||||
test fun orEmpty() {
|
||||
val s: String? = "hey"
|
||||
val ns: String? = null
|
||||
|
||||
@@ -34,16 +34,22 @@ class StringTest() : TestCase() {
|
||||
assertEquals("", ns.orEmpty())
|
||||
}
|
||||
|
||||
fun testToShort() {
|
||||
test fun toShort() {
|
||||
assertEquals(77.toShort(), "77".toShort())
|
||||
}
|
||||
|
||||
fun testToInt() {
|
||||
test fun toInt() {
|
||||
assertEquals(77, "77".toInt())
|
||||
}
|
||||
|
||||
fun testToLong() {
|
||||
test fun toLong() {
|
||||
assertEquals(77.toLong(), "77".toLong())
|
||||
}
|
||||
|
||||
test fun count() {
|
||||
val text = "hello there\tfoo\nbar"
|
||||
val whitespaceCount = text.count { it.isWhitespace() }
|
||||
assertEquals(3, whitespaceCount)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user