diff --git a/kotlin/src/test/kotlin/com/baeldung/kotlin/GenericsTest.kt b/kotlin/src/test/kotlin/com/baeldung/kotlin/GenericsTest.kt new file mode 100644 index 0000000000..c2e5ca4195 --- /dev/null +++ b/kotlin/src/test/kotlin/com/baeldung/kotlin/GenericsTest.kt @@ -0,0 +1,148 @@ +package com.baeldung.kotlin + +import org.junit.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +class GenericsTest { + + @Test + fun givenParametrizeClass_whenInitializeItWithSpecificType_thenShouldBeParameterized() { + //given + val parameterizedClass = ParameterizedClass("string-value") + + //when + val res = parameterizedClass.getValue() + + //then + assertTrue(res is String) + } + + @Test + fun givenParametrizeClass_whenInitializeIt_thenShouldBeParameterizedByInferredType() { + //given + val parameterizedClass = ParameterizedClass("string-value") + + //when + val res = parameterizedClass.getValue() + + //then + assertTrue(res is String) + } + + @Test + fun givenParameterizedProducerByOutKeyword_whenGetValue_thenCanAssignItToSuperType() { + //given + val parameterizedProducer = ParameterizedProducer("string") + + //when + val ref: ParameterizedProducer = parameterizedProducer + + //then + assertTrue(ref is ParameterizedProducer) + } + + @Test + fun givenParameterizedConsumerByInKeyword_whenGetValue_thenCanAssignItToSubType() { + //given + val parameterizedConsumer = ParameterizedConsumer() + + //when + val ref: ParameterizedConsumer = parameterizedConsumer + + //then + assertTrue(ref is ParameterizedConsumer) + } + + @Test + fun givenTypeProjections_whenOperateOnTwoList_thenCanAcceptListOfSubtypes() { + //given + val ints: Array = arrayOf(1, 2, 3) + val any: Array = arrayOfNulls(3) + + //when + copy(ints, any) + + //then + assertEquals(any[0], 1) + assertEquals(any[1], 2) + assertEquals(any[2], 3) + + } + + fun copy(from: Array, to: Array) { + assert(from.size == to.size) + for (i in from.indices) + to[i] = from[i] + } + + @Test + fun givenTypeProjection_whenHaveArrayOfIn_thenShouldAddElementsOfSubtypesToIt() { + //given + val objects: Array = arrayOfNulls(1) + + //when + fill(objects, 1) + + //then + assertEquals(objects[0], 1) + } + + fun fill(dest: Array, value: Int) { + dest[0] = value + } + + @Test + fun givenStartProjection_whenPassAnyType_thenCompile() { + //given + val array = arrayOf(1,2,3) + + //then + printArray(array) + + } + + fun printArray(array: Array<*>) { + array.forEach { println(it) } + } + + @Test + fun givenFunctionWithDefinedGenericConstraints_whenCallWithProperType_thenCompile(){ + //given + val listOfInts = listOf(5,2,3,4,1) + + //when + val sorted = sort(listOfInts) + + //then + assertEquals(sorted[0], 1) + assertEquals(sorted[1], 2) + assertEquals(sorted[2], 3) + assertEquals(sorted[3], 4) + assertEquals(sorted[4], 5) + + } + + fun > sort(list: List): List{ + return list.sorted() + } + + class ParameterizedClass(private val value: A) { + + fun getValue(): A { + return value + } + } + + class ParameterizedProducer(private val value: T) { + fun get(): T { + return value + } + } + + class ParameterizedConsumer { + fun toString(value: T): String { + return value.toString() + } + } +}