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
88 changes: 28 additions & 60 deletions src/main/kotlin/ru/otus/homework/functions.kt
Original file line number Diff line number Diff line change
@@ -1,80 +1,48 @@
package ru.otus.homework

import java.time.LocalDate
import java.time.LocalDateTime
import java.time.Duration

fun main() {
println(calculate(10, 20))
println(calculate(10, 20.5F))
println(calculate(30.1F, 40.2F, 50.3F, 60.4F))

println(calculate(3, 2, ::add))
println(calculate(3, 2, ::subtract))
println(calculate(3, 2) { n1, n2 -> n1 * n2 })

sign(
lastName = "Иванов",
firstName = "Вася"
)

translate(calculate(1.1F, 2.2F, 3.3F)) {
"In english: ${it.replace("+", "plus").replace("=", "equals")}"
}
println(
calculate(1.1F, 2.2F, 3.3F) {
"%.4f (с точностью до четырех знаков)".format(this)
}
)

val product = 2 by 2
println("Произведение: $product")
}

infix fun Int.by(other: Int): Int = this * other

fun translate(what: String, translator: (String) -> String) {
println(translator(what))
}

fun sign(firstName: String, lastName: String, date: LocalDate = LocalDate.now()) {
println("Работу выполнил: $firstName $lastName, ${date.russian()}")
fun sum(n1: Int, n2: Int, vararg params: Int): Int{
return n1 + n2 + params.sum()
}

internal fun LocalDate.russian(): String {
return "${this.dayOfMonth}.${monthValue}.${year}"
fun joinStrings(vararg strings: String, separator: Char = ' '): String{
return strings.joinToString(separator = separator.toString())
}

fun what(): String = "Огурцов"
fun testJoinString(): Boolean{
var actual = joinStrings("str1", "str2", "str3")
var expected = "str1 str2 str3"

fun calculate(n1: Int, n2: Int): String = "$n1 + $n2 = ${ n1 + n2 } ${ what() }"

fun calculate(n1: Int, n2: Float): String {
fun add(): String {
val s: Float
if(actual != expected){
return false
}

while (true) {
// Пример блока. Вычисляем, и сразу выходим
val s1 = n1 + n2
s = s1
break
}
actual = joinStrings("str1", "str2", "str3", separator = ',')
expected = "str1,str2,str3"

return "$n1 + $n2 = $s"
if(actual != expected){
return false
}
return "${ add() } ${ what() }"
}

fun Float.formatWithDot(): String = "%.2f".format(this)

fun calculate(vararg n: Float, format: Float.() -> String = Float::formatWithDot): String {
var sum = 0F
n.forEach { sum += it }
return "${n.joinToString(" + ")} = ${sum.format()}"
return true
}

fun calculate(n1: Int, n2: Int, op: (Int, Int) -> Int): String {
val result = op(n1, n2)
return "Результат операции $n1 и $n2 равен: $result"
fun executionTime(op: ()->Unit): Long{
val start = LocalDateTime.now()
op()
val end = LocalDateTime.now()

return Duration.between(start, end).toMillis()
}

fun add(a: Int, b: Int): Int = a + b
fun subtract(a: Int, b: Int): Int = a - b
fun longTimeDuration() {
for(i in 0..100000){
println(i)
}
}
57 changes: 50 additions & 7 deletions src/test/kotlin/ru/otus/homework/FunctionsTest.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,57 @@
package ru.otus.homework

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class FunctionsTest {
class SumTest {

@Test
fun `2 obligatory parameters only`() {
val actual = sum(5, 3)
val expected = 8


assertEquals(actual, expected)
}

@Test
fun `more then 2 arguments`(){
val actual = sum(5, 3, 2, 4)
val expected = 14

assertEquals(actual, expected)
}
}

class JoinToStringTest{
@Test
fun `default separator`(){
val actual = joinStrings("str1", "str2", "str3")
val expected = "str1 str2 str3"

assertEquals(actual, expected)
}

@Test
fun `comma separator`(){
val actual = joinStrings("str1", "str2", "str3", separator = ',')
val expected = "str1,str2,str3"

assertEquals(actual, expected)
}

@Test
fun `call testJoinString function`(){
val actual = testJoinString()
val expected = true

assertEquals(actual, expected)
}
}

class ExecutionTimeTest{
@Test
fun calculationTest() {
Assertions.assertEquals(
"1 + 2 = 3 Огурцов",
calculate(1, 2)
)
fun `100_000 println`(){
println("Duration time: ${executionTime(op = ::longTimeDuration)} ms")
}
}