From 6efc9744186c3ab2289114288e80ff3bd8c175c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B0=D0=B7=D1=83=D0=BD=D0=B8=D0=BD=20=D0=90=D0=BB?= =?UTF-8?q?=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=95=D0=B2=D0=B3=D0=B5=D0=BD?= =?UTF-8?q?=D1=8C=D0=B5=D0=B2=D0=B8=D1=87?= Date: Wed, 22 Apr 2026 09:46:54 +0300 Subject: [PATCH 1/2] All tasks completed --- src/main/kotlin/ru/otus/homework/functions.kt | 88 ++++++------------- .../kotlin/ru/otus/homework/FunctionsTest.kt | 56 ++++++++++-- 2 files changed, 78 insertions(+), 66 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/functions.kt b/src/main/kotlin/ru/otus/homework/functions.kt index 4a7fe1e..0efac2e 100644 --- a/src/main/kotlin/ru/otus/homework/functions.kt +++ b/src/main/kotlin/ru/otus/homework/functions.kt @@ -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(): Unit{ + for(i in 0..100000){ + println(i) + } +} \ No newline at end of file diff --git a/src/test/kotlin/ru/otus/homework/FunctionsTest.kt b/src/test/kotlin/ru/otus/homework/FunctionsTest.kt index 93a36cf..67c4be4 100644 --- a/src/test/kotlin/ru/otus/homework/FunctionsTest.kt +++ b/src/test/kotlin/ru/otus/homework/FunctionsTest.kt @@ -1,14 +1,58 @@ 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") } } \ No newline at end of file From fe642e16b3971ad94f545faaf9a0661f787e1fbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9B=D0=B0=D0=B7=D1=83=D0=BD=D0=B8=D0=BD=20=D0=90=D0=BB?= =?UTF-8?q?=D0=B5=D0=BA=D1=81=D0=B5=D0=B9=20=D0=95=D0=B2=D0=B3=D0=B5=D0=BD?= =?UTF-8?q?=D1=8C=D0=B5=D0=B2=D0=B8=D1=87?= Date: Wed, 22 Apr 2026 09:49:00 +0300 Subject: [PATCH 2/2] Solve redundant issues --- src/main/kotlin/ru/otus/homework/functions.kt | 4 ++-- src/test/kotlin/ru/otus/homework/FunctionsTest.kt | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/functions.kt b/src/main/kotlin/ru/otus/homework/functions.kt index 0efac2e..b9ccf90 100644 --- a/src/main/kotlin/ru/otus/homework/functions.kt +++ b/src/main/kotlin/ru/otus/homework/functions.kt @@ -30,7 +30,7 @@ fun testJoinString(): Boolean{ return false } - return true; + return true } fun executionTime(op: ()->Unit): Long{ @@ -41,7 +41,7 @@ fun executionTime(op: ()->Unit): Long{ return Duration.between(start, end).toMillis() } -fun longTimeDuration(): Unit{ +fun longTimeDuration() { for(i in 0..100000){ println(i) } diff --git a/src/test/kotlin/ru/otus/homework/FunctionsTest.kt b/src/test/kotlin/ru/otus/homework/FunctionsTest.kt index 67c4be4..0292892 100644 --- a/src/test/kotlin/ru/otus/homework/FunctionsTest.kt +++ b/src/test/kotlin/ru/otus/homework/FunctionsTest.kt @@ -1,6 +1,5 @@ package ru.otus.homework -import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Test @@ -43,7 +42,7 @@ class JoinToStringTest{ @Test fun `call testJoinString function`(){ - val actual = testJoinString(); + val actual = testJoinString() val expected = true assertEquals(actual, expected)