11package org.jetbrains.kotlinx.dataframe.statistics
22
3+ import io.kotest.assertions.throwables.shouldThrow
34import io.kotest.matchers.shouldBe
45import org.jetbrains.kotlinx.dataframe.DataColumn
56import org.jetbrains.kotlinx.dataframe.api.columnOf
@@ -8,7 +9,10 @@ import org.jetbrains.kotlinx.dataframe.api.cumSum
89import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
910import org.jetbrains.kotlinx.dataframe.api.groupBy
1011import org.jetbrains.kotlinx.dataframe.api.map
12+ import org.jetbrains.kotlinx.dataframe.impl.nullableNothingType
13+ import org.jetbrains.kotlinx.dataframe.math.cumSumTypeConversion
1114import org.junit.Test
15+ import kotlin.reflect.typeOf
1216
1317@Suppress(" ktlint:standard:argument-list-wrapping" )
1418class CumsumTests {
@@ -92,4 +96,39 @@ class CumsumTests {
9296 " c" , 4 ,
9397 )
9498 }
99+
100+ @Test
101+ fun `df cumSum default` () {
102+ val df = dataFrameOf(
103+ " doubles" to columnOf(1.0 , 2.0 , null ),
104+ " shorts" to columnOf(1 .toShort(), 2 .toShort(), null ),
105+ " bigInts" to columnOf(1 .toBigInteger(), 2 .toBigInteger(), null ),
106+ " mixed" to columnOf<Number ?>(1.0 , 2 , null ),
107+ )
108+
109+ val res = df.cumSum()
110+
111+ // works for Doubles, turns nulls into NaNs
112+ res[" doubles" ].values() shouldBe columnOf(1.0 , 3.0 , Double .NaN ).values()
113+ // works for Shorts, turns into Ints, skips nulls
114+ res[" shorts" ].values() shouldBe columnOf(1 , 3 , null ).values()
115+ // does not work for big numbers, keeps them as is
116+ res[" bigInts" ].values() shouldBe columnOf(1 .toBigInteger(), 2 .toBigInteger(), null ).values()
117+ // works for mixed columns of primitives, number-unifies them; in this case to Doubles
118+ res[" mixed" ].values() shouldBe columnOf(1.0 , 3.0 , Double .NaN ).values()
119+ }
120+
121+ @Test
122+ fun `cumSumTypeConversion tests` () {
123+ cumSumTypeConversion(typeOf<Int >(), false ) shouldBe typeOf<Int >()
124+ cumSumTypeConversion(typeOf<Long ?>(), false ) shouldBe typeOf<Long ?>()
125+ cumSumTypeConversion(typeOf<Short ?>(), false ) shouldBe typeOf<Int ?>()
126+ cumSumTypeConversion(typeOf<Byte >(), false ) shouldBe typeOf<Int >()
127+ cumSumTypeConversion(typeOf<Float ?>(), false ) shouldBe typeOf<Float >()
128+ cumSumTypeConversion(typeOf<Double ?>(), false ) shouldBe typeOf<Double >()
129+ cumSumTypeConversion(typeOf<Double >(), false ) shouldBe typeOf<Double >()
130+ cumSumTypeConversion(nullableNothingType, false ) shouldBe nullableNothingType
131+
132+ shouldThrow<IllegalStateException > { cumSumTypeConversion(typeOf<String >(), false ) }
133+ }
95134}
0 commit comments