Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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")

}
26 changes: 24 additions & 2 deletions src/main/kotlin/ru/otus/homework/functions.kt
Original file line number Diff line number Diff line change
@@ -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))
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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() }
24 changes: 23 additions & 1 deletion src/test/kotlin/ru/otus/homework/FunctionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,26 @@ class FunctionsTest {
calculate(1, 2)
)
}
}

@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) } }
}
}