1
1
package org.jetbrains.kotlinx.dataframe.statistics
2
2
3
+ import io.kotest.assertions.throwables.shouldThrow
3
4
import io.kotest.matchers.shouldBe
4
5
import org.jetbrains.kotlinx.dataframe.DataColumn
5
6
import org.jetbrains.kotlinx.dataframe.api.columnOf
@@ -8,7 +9,10 @@ import org.jetbrains.kotlinx.dataframe.api.cumSum
8
9
import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
9
10
import org.jetbrains.kotlinx.dataframe.api.groupBy
10
11
import org.jetbrains.kotlinx.dataframe.api.map
12
+ import org.jetbrains.kotlinx.dataframe.impl.nullableNothingType
13
+ import org.jetbrains.kotlinx.dataframe.math.cumSumTypeConversion
11
14
import org.junit.Test
15
+ import kotlin.reflect.typeOf
12
16
13
17
@Suppress(" ktlint:standard:argument-list-wrapping" )
14
18
class CumsumTests {
@@ -92,4 +96,39 @@ class CumsumTests {
92
96
" c" , 4 ,
93
97
)
94
98
}
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
+ }
95
134
}
0 commit comments