import kotlin.test.Test import kotlin.test.assertEquals class ReturnIntTest { @Test fun return_int_test() { assertEquals(return_int(10), 10); } }
#[test]fn return_int_test() {assert_eq!(return_int(10), 10);- import kotlin.test.Test
- import kotlin.test.assertEquals
- class ReturnIntTest {
- @Test
- fun return_int_test() {
- assertEquals(return_int(10), 10);
- }
- }
fn return_int(i: i32) -> i32 { i }
public class Program{public static int GetValue(int i){return i;}}- fn return_int(i: i32) -> i32 { i }
#[test] fn return_int_test() { assert_eq!(return_int(10), 10); }
import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.assertEquals;// TODO: Replace examples and use TDD by writing your own testsclass SolutionTest {@Testvoid testSomething() {Program.GetValue(10);}}- #[test]
- fn return_int_test() {
- assert_eq!(return_int(10), 10);
- }
package solution /** * more words, but no HEAP usage on every cycle (no Triple construction) */ fun tribonacci(n: Int, signature: Triple<Int, Int, Int> = Triple(1, 1, 1)) = sequence<Int> { var a1 = signature.first var a2 = signature.second var a3 = signature.third var next = a1 + a2 + a3; while(true) { yield(a1); a1 = a2; a2 = a3; a3 = next; next = a1+a2+a3; } }.take(n).toList()
- package solution
fun tribonacci(n: Int, signature: Triple<Int, Int, Int> = Triple(1, 1, 1)) =generateSequence(signature) { Triple(it.second, it.third, it.first + it.second + it.third) }.map { it.first }.take(n).toList()- /**
- * more words, but no HEAP usage on every cycle (no Triple construction)
- */
- fun tribonacci(n: Int, signature: Triple<Int, Int, Int> = Triple(1, 1, 1)) = sequence<Int> {
- var a1 = signature.first
- var a2 = signature.second
- var a3 = signature.third
- var next = a1 + a2 + a3;
- while(true) {
- yield(a1);
- a1 = a2;
- a2 = a3;
- a3 = next;
- next = a1+a2+a3;
- }
- }.take(n).toList()
package solution import kotlin.test.assertEquals import org.junit.Test import java.lang.Runtime import java.lang.System class TestExample { @Test fun testTribonacci() { assertEquals(listOf(1), tribonacci(1)) assertEquals(listOf(1, 1, 1, 3, 5, 9, 17, 31, 57, 105), tribonacci(10)) assertEquals(listOf(2, 2, 2, 6, 10, 18, 34, 62, 114, 210), tribonacci(10, Triple(2, 2, 2))) } fun allocated_tribonacci(n: Int, signature: Triple<Int, Int, Int> = Triple(1, 1, 1)) = generateSequence(signature) { Triple(it.second, it.third, it.first + it.second + it.third) } .map { it.first } .take(n) .toList() @Test fun stackOnlyTribonacci() { val rt = Runtime.getRuntime() val stack_start_mem = rt.freeMemory() tribonacci(10000) val stack_fin_mem = rt.freeMemory() val stacked_heap_usage = stack_start_mem - stack_fin_mem System.gc() val alloc_start_mem = rt.freeMemory() allocated_tribonacci(10000) val alloc_fin_mem = rt.freeMemory() val allocated_heap_usage = alloc_start_mem - alloc_fin_mem assertEquals(0, stacked_heap_usage) assertEquals(2516640, allocated_heap_usage) } }
- package solution
- import kotlin.test.assertEquals
- import org.junit.Test
- import java.lang.Runtime
- import java.lang.System
- class TestExample {
- @Test
- fun testTribonacci() {
- assertEquals(listOf(1), tribonacci(1))
- assertEquals(listOf(1, 1, 1, 3, 5, 9, 17, 31, 57, 105), tribonacci(10))
- assertEquals(listOf(2, 2, 2, 6, 10, 18, 34, 62, 114, 210), tribonacci(10, Triple(2, 2, 2)))
- }
- fun allocated_tribonacci(n: Int, signature: Triple<Int, Int, Int> = Triple(1, 1, 1)) =
- generateSequence(signature) { Triple(it.second, it.third, it.first + it.second + it.third) }
- .map { it.first }
- .take(n)
- .toList()
- @Test
- fun stackOnlyTribonacci() {
- val rt = Runtime.getRuntime()
- val stack_start_mem = rt.freeMemory()
- tribonacci(10000)
- val stack_fin_mem = rt.freeMemory()
- val stacked_heap_usage = stack_start_mem - stack_fin_mem
- System.gc()
- val alloc_start_mem = rt.freeMemory()
- allocated_tribonacci(10000)
- val alloc_fin_mem = rt.freeMemory()
- val allocated_heap_usage = alloc_start_mem - alloc_fin_mem
- assertEquals(0, stacked_heap_usage)
- assertEquals(2516640, allocated_heap_usage)
- }
- }