diff --git a/build.gradle b/build.gradle index 0d54c35..5a0f737 100644 --- a/build.gradle +++ b/build.gradle @@ -20,4 +20,6 @@ repositories { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib" implementation 'org.junit.jupiter:junit-jupiter:5.8.1' + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2") + } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/homework/functions.kt b/src/main/kotlin/ru/otus/homework/functions.kt index 4a7fe1e..dcd50ab 100644 --- a/src/main/kotlin/ru/otus/homework/functions.kt +++ b/src/main/kotlin/ru/otus/homework/functions.kt @@ -1,6 +1,9 @@ package ru.otus.homework +import kotlinx.coroutines.delay +import kotlinx.coroutines.runBlocking import java.time.LocalDate +import kotlin.system.measureTimeMillis fun main() { println(calculate(10, 20)) @@ -45,7 +48,7 @@ internal fun LocalDate.russian(): String { fun what(): String = "Огурцов" -fun calculate(n1: Int, n2: Int): String = "$n1 + $n2 = ${ n1 + n2 } ${ what() }" +fun calculate(n1: Int, n2: Int): String = "$n1 + $n2 = ${n1 + n2} ${what()}" fun calculate(n1: Int, n2: Float): String { fun add(): String { @@ -60,7 +63,7 @@ fun calculate(n1: Int, n2: Float): String { return "$n1 + $n2 = $s" } - return "${ add() } ${ what() }" + return "${add()} ${what()}" } fun Float.formatWithDot(): String = "%.2f".format(this) @@ -78,3 +81,22 @@ fun calculate(n1: Int, n2: Int, op: (Int, Int) -> Int): String { fun add(a: Int, b: Int): Int = a + b fun subtract(a: Int, b: Int): Int = a - b + +fun sumOfInt(a: Int, b: Int, vararg c: Int): Int = a + b + c.sumOf { it } + +fun stringCombine(vararg s: String, delimiter: Char = ' '): String { + var result: String = "" + val i = s.iterator() + while (i.hasNext()) { + result = result + i.next() + (delimiter.takeIf { i.hasNext() } ?: "") + } + return result +} + +fun longFunc(delay: Long = 100) { + runBlocking { + delay(delay) + } +} + +fun getExecutionTimeMillis(block: () -> Any): Long = measureTimeMillis { block() } diff --git a/src/test/kotlin/ru/otus/homework/FunctionsTest.kt b/src/test/kotlin/ru/otus/homework/FunctionsTest.kt index 93a36cf..9dcd097 100644 --- a/src/test/kotlin/ru/otus/homework/FunctionsTest.kt +++ b/src/test/kotlin/ru/otus/homework/FunctionsTest.kt @@ -11,4 +11,26 @@ class FunctionsTest { calculate(1, 2) ) } -} \ No newline at end of file + + @Test + fun sumOfIntTest() { + Assertions.assertEquals(10, sumOfInt(3, 7)) + Assertions.assertEquals(20, sumOfInt(3, 7, 10)) + Assertions.assertEquals(40, sumOfInt(3, 7, 11, 19)) + Assertions.assertEquals(-19, sumOfInt(3, 7, -10, -19)) + } + + @Test + fun stringCombineTest() { + Assertions.assertEquals("a b c", stringCombine("a", "b", "c")) + Assertions.assertEquals("a,b,c", stringCombine("a", "b", "c", delimiter = ',')) + Assertions.assertEquals("a:b:c", stringCombine("a", "b", "c", delimiter = ':')) + } + + @Test + fun getExecutionTimeMillisTest() { + Assertions.assertTrue { 100 < getExecutionTimeMillis { longFunc() } } + Assertions.assertTrue { 200 < getExecutionTimeMillis { longFunc(200) } } + Assertions.assertTrue { 200 > getExecutionTimeMillis { longFunc(100) } } + } +}