diff --git a/build.gradle b/build.gradle index 0d54c35..00016e7 100644 --- a/build.gradle +++ b/build.gradle @@ -1,13 +1,21 @@ plugins { - id 'org.jetbrains.kotlin.jvm' version '2.2.10' + id 'org.jetbrains.kotlin.jvm' version '2.0.21' } test { useJUnitPlatform() } +java { + sourceCompatibility = JavaVersion.VERSION_21 + targetCompatibility = JavaVersion.VERSION_21 + toolchain { + languageVersion = JavaLanguageVersion.of(21) + } +} + kotlin { - jvmToolchain(17) + jvmToolchain(21) } group 'ru.otus' @@ -18,6 +26,7 @@ repositories { } dependencies { - implementation "org.jetbrains.kotlin:kotlin-stdlib" + + implementation("org.jetbrains.kotlin:kotlin-stdlib:2.0.21") implementation 'org.junit.jupiter:junit-jupiter:5.8.1' } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/homework/home_work.kt b/src/main/kotlin/ru/otus/homework/home_work.kt new file mode 100644 index 0000000..81f2487 --- /dev/null +++ b/src/main/kotlin/ru/otus/homework/home_work.kt @@ -0,0 +1,34 @@ +package ru.otus.homework + +fun main() { + println(sumAll(null, null)) + println(concatAll("arg1", "arg2", "arg3", separator = '+')) + println("duration is ${duration { Thread.sleep(1100) }}") +} + +// ## 1. Функция с обязательными и необязательными позиционными параметрами +internal fun sumAll(field1: Int?, field2: Int?, vararg fields: Int?): Int { + var sum: Int = (field1 ?: 0) + (field2 ?: 0) + for(field in fields){ + sum += (field ?: 0) + } + + return sum +} + +//## 2. Функция с необязательным параметром и позиционными параметрами +internal fun concatAll(vararg args: String, separator: Char = ' ') : String { + var concatenation = "" + args.forEach { v -> concatenation += v + separator.toString() } + + return concatenation.substring(0, concatenation.length - 1) +} + +//## 4. Функция, измеряющая время выполнения другой функции +fun duration( callback: () -> Unit): Long { + val before = System.currentTimeMillis() + callback() + val after = System.currentTimeMillis() + + return after - before +} \ 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..137849d 100644 --- a/src/test/kotlin/ru/otus/homework/FunctionsTest.kt +++ b/src/test/kotlin/ru/otus/homework/FunctionsTest.kt @@ -1,6 +1,7 @@ package ru.otus.homework import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test class FunctionsTest { @@ -11,4 +12,11 @@ class FunctionsTest { calculate(1, 2) ) } + + @Test + @DisplayName("## 3. Тестовая функция для пункта №2") + fun concatenationVarargsBySeparatorTest(){ + Assertions.assertEquals("str1 str2 str3", concatAll("str1", "str2", "str3")) + Assertions.assertEquals("str1,str2,str3", concatAll("str1", "str2", "str3", separator = ',')) + } } \ No newline at end of file