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
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ plugins {
id 'org.jetbrains.kotlin.jvm' version '2.2.10'
}

dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.9.2")
}

test {
useJUnitPlatform()
}
Expand Down
34 changes: 34 additions & 0 deletions src/main/kotlin/ru/otus/homework/Homework.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ru.otus.homework

//region Задание 1
fun calc(n1: Int, n2: Int, vararg other: Int): Int {
val sum = n1 + n2 + other.sum()
return sum
}
//endregion

//region Задание 2
fun join(vararg string: String, separator: Char = ' '): String =
string.joinToString(separator = separator.toString())
//endregion

//region Задание 4
fun main() {
val ms = runTime { longFunction() }
println("Время выполнения — $ms милисекунд")
}

fun runTime(block: () -> Unit): Long {
val start = System.currentTimeMillis()
block()
val end = System.currentTimeMillis()
val time = end - start
return time
}

fun longFunction() {
repeat(1000000000) { i ->
if (i % 500000000 == 0) println("$i")
}
}
//endregion
39 changes: 39 additions & 0 deletions src/test/kotlin/ru/otus/homework/HomeworkTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ru.otus.homework

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


class HomeworkTest {
//region Тест для задания 1
@Test
fun calcTest1() {
val actual = calc(1, 2)
val expected = 3
Assertions.assertEquals(expected, actual)
}

@Test
fun calcTest2() {
val actual = calc(1, 2, 3, 4)
val expected = 10
Assertions.assertEquals(expected, actual)
}
//endregion

//region Задание 3
@Test
fun `no separator - use default`() {
val actual = join("Я – инженер","и моя голова сразу забывает бесполезные слова. Хуяк-хуяк", "и в продакшн.")
val expected = "Я – инженер и моя голова сразу забывает бесполезные слова. Хуяк-хуяк и в продакшн."
Assertions.assertEquals(expected, actual)
}

@Test
fun `has separator - use the separator`() {
val actual = join("Я – инженер","и моя голова сразу забывает бесполезные слова. Хуяк-хуяк", "и в продакшн.", separator =',')
val expected = "Я – инженер,и моя голова сразу забывает бесполезные слова. Хуяк-хуяк,и в продакшн."
Assertions.assertEquals(expected, actual)
}
//endregion
}