diff --git a/src/main/kotlin/ru/otus/homework/main.kt b/src/main/kotlin/ru/otus/homework/main.kt new file mode 100644 index 0000000..0e9ff82 --- /dev/null +++ b/src/main/kotlin/ru/otus/homework/main.kt @@ -0,0 +1,39 @@ +package ru.otus.homework + +import kotlin.system.measureTimeMillis + +fun fun1MandatoryAndOptionalParams(numberOne: Int, numberTwo: Int, vararg additionalNumber: Int): Int { + var sum = 0 + for(i in additionalNumber) + sum += i + return numberOne + numberTwo + sum +} + +fun fun2OptionalAndPositionalParams(vararg additionalStr: String, ch: Char=' '): String { + var result: String= "" + for(str in additionalStr) + result += str + ch + return result.dropLast(1) +} + +fun sum(a: Int, b: Int){ + var sum : Long = 0 + repeat(a) { + repeat(b) { + sum += a+b + } + } +} + +fun fun4Time(numberOne: Int, numberTwo: Int, myFunction: (Int, Int)->Unit): Long { + val time = measureTimeMillis { + myFunction(numberOne, numberTwo) + } + return time +} + +fun main() { + println(fun1MandatoryAndOptionalParams(8, 45, 10,20)) + println(fun2OptionalAndPositionalParams("one", "two", "free", "four", "five", ch= '*')) + println(fun4Time(100000,200000, :: sum)) +} \ No newline at end of file diff --git a/src/test/kotlin/ru/otus/homework/fun2OptionalAndPositionalParamsTest.kt b/src/test/kotlin/ru/otus/homework/fun2OptionalAndPositionalParamsTest.kt new file mode 100644 index 0000000..e946ba9 --- /dev/null +++ b/src/test/kotlin/ru/otus/homework/fun2OptionalAndPositionalParamsTest.kt @@ -0,0 +1,20 @@ +package ru.otus.homework + +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test + +class fun2OptionalAndPositionalParamsTest { + @Test + fun `testcase 1`() { + val expected : String = "str1 str2 str3" + val actual = fun2OptionalAndPositionalParams("str1","str2","str3") + assertEquals(expected, actual) + } + + @Test + fun `testcase 2`() { + val expected : String = "str1,str2,str3" + val actual = fun2OptionalAndPositionalParams("str1","str2","str3",ch=',') + assertEquals(expected, actual) + } +}