Skip to content

Commit 84bae15

Browse files
committed
added tests for char/int and number conversion. Added missing conversions from Short/Byte to others
1 parent 3a3cf1f commit 84bae15

File tree

2 files changed

+208
-0
lines changed
  • core/src
    • main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api
    • test/kotlin/org/jetbrains/kotlinx/dataframe/api

2 files changed

+208
-0
lines changed

core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/impl/api/convert.kt

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,82 @@ internal fun createConverter(from: KType, to: KType, options: ParserOptions? = n
336336
else -> null
337337
}
338338

339+
Byte::class -> when (toClass) {
340+
Double::class -> convert<Byte> { it.toDouble() }
341+
342+
Float::class -> convert<Byte> { it.toFloat() }
343+
344+
Int::class -> convert<Byte> { it.toInt() }
345+
346+
Short::class -> convert<Byte> { it.toShort() }
347+
348+
Long::class -> convert<Byte> { it.toLong() }
349+
350+
BigDecimal::class -> convert<Byte> { it.toBigDecimal() }
351+
352+
BigInteger::class -> convert<Byte> { it.toBigInteger() }
353+
354+
Boolean::class -> convert<Byte> { it != 0.toByte() }
355+
356+
LocalDateTime::class -> convert<Byte> { it.toLong().toLocalDateTime(defaultTimeZone) }
357+
358+
LocalDate::class -> convert<Byte> { it.toLong().toLocalDate(defaultTimeZone) }
359+
360+
LocalTime::class -> convert<Byte> { it.toLong().toLocalTime(defaultTimeZone) }
361+
362+
Instant::class -> convert<Byte> { Instant.fromEpochMilliseconds(it.toLong()) }
363+
364+
JavaLocalDateTime::class -> convert<Byte> {
365+
it.toLong().toLocalDateTime(defaultTimeZone).toJavaLocalDateTime()
366+
}
367+
368+
JavaLocalDate::class -> convert<Byte> { it.toLong().toLocalDate(defaultTimeZone).toJavaLocalDate() }
369+
370+
JavaLocalTime::class -> convert<Byte> { it.toLong().toLocalTime(defaultTimeZone).toJavaLocalTime() }
371+
372+
JavaInstant::class -> convert<Byte> { JavaInstant.ofEpochMilli(it.toLong()) }
373+
374+
else -> null
375+
}
376+
377+
Short::class -> when (toClass) {
378+
Double::class -> convert<Short> { it.toDouble() }
379+
380+
Float::class -> convert<Short> { it.toFloat() }
381+
382+
Int::class -> convert<Short> { it.toInt() }
383+
384+
Byte::class -> convert<Short> { it.toByte() }
385+
386+
Long::class -> convert<Short> { it.toLong() }
387+
388+
BigDecimal::class -> convert<Short> { it.toBigDecimal() }
389+
390+
BigInteger::class -> convert<Short> { it.toBigInteger() }
391+
392+
Boolean::class -> convert<Short> { it != 0.toShort() }
393+
394+
LocalDateTime::class -> convert<Short> { it.toLong().toLocalDateTime(defaultTimeZone) }
395+
396+
LocalDate::class -> convert<Short> { it.toLong().toLocalDate(defaultTimeZone) }
397+
398+
LocalTime::class -> convert<Short> { it.toLong().toLocalTime(defaultTimeZone) }
399+
400+
Instant::class -> convert<Short> { Instant.fromEpochMilliseconds(it.toLong()) }
401+
402+
JavaLocalDateTime::class -> convert<Short> {
403+
it.toLong().toLocalDateTime(defaultTimeZone).toJavaLocalDateTime()
404+
}
405+
406+
JavaLocalDate::class -> convert<Short> { it.toLong().toLocalDate(defaultTimeZone).toJavaLocalDate() }
407+
408+
JavaLocalTime::class -> convert<Short> { it.toLong().toLocalTime(defaultTimeZone).toJavaLocalTime() }
409+
410+
JavaInstant::class -> convert<Short> { JavaInstant.ofEpochMilli(it.toLong()) }
411+
412+
else -> null
413+
}
414+
339415
Double::class -> when (toClass) {
340416
Int::class -> convert<Double> { it.roundToInt() }
341417
Float::class -> convert<Double> { it.toFloat() }

core/src/test/kotlin/org/jetbrains/kotlinx/dataframe/api/convert.kt

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ import org.jetbrains.kotlinx.dataframe.exceptions.CellConversionException
1313
import org.jetbrains.kotlinx.dataframe.exceptions.TypeConversionException
1414
import org.jetbrains.kotlinx.dataframe.exceptions.TypeConverterNotFoundException
1515
import org.jetbrains.kotlinx.dataframe.hasNulls
16+
import org.jetbrains.kotlinx.dataframe.impl.api.toBigDecimal
17+
import org.jetbrains.kotlinx.dataframe.impl.api.toBigInteger
1618
import org.junit.Test
19+
import java.math.BigDecimal
20+
import java.math.BigInteger
21+
import kotlin.math.roundToInt
22+
import kotlin.math.roundToLong
23+
import kotlin.random.Random
24+
import kotlin.reflect.full.starProjectedType
1725
import kotlin.reflect.typeOf
1826
import kotlin.time.Duration.Companion.hours
1927
import java.time.LocalTime as JavaLocalTime
@@ -183,4 +191,128 @@ class ConvertTests {
183191
val res = col.convertTo<String>()
184192
res.print()
185193
}
194+
195+
@Test
196+
fun `convert Int and Char by code`() {
197+
val col = columnOf(65, 66)
198+
col.convertTo<Char>() shouldBe columnOf('A', 'B')
199+
col.convertTo<Char>().convertTo<Int>() shouldBe col
200+
}
201+
202+
@Test
203+
fun `convert all number types to each other`() {
204+
val numberTypes: List<Number> = listOf(
205+
Random.nextInt().toByte(),
206+
Random.nextInt().toShort(),
207+
Random.nextInt(),
208+
Random.nextLong(),
209+
Random.nextFloat(),
210+
Random.nextDouble(),
211+
BigInteger.valueOf(Random.nextLong()),
212+
BigDecimal.valueOf(Random.nextDouble()),
213+
)
214+
for (a in numberTypes) {
215+
val aCol = columnOf(a)
216+
for (b in numberTypes) {
217+
val bCol = aCol.convertTo(b::class.starProjectedType)
218+
when (a) {
219+
is Byte ->
220+
when (b) {
221+
is Byte -> bCol.first() shouldBe a.toByte()
222+
is Short -> bCol.first() shouldBe a.toShort()
223+
is Int -> bCol.first() shouldBe a.toInt()
224+
is Long -> bCol.first() shouldBe a.toLong()
225+
is Float -> bCol.first() shouldBe a.toFloat()
226+
is Double -> bCol.first() shouldBe a.toDouble()
227+
is BigInteger -> bCol.first() shouldBe a.toBigInteger()
228+
is BigDecimal -> bCol.first() shouldBe a.toBigDecimal()
229+
}
230+
231+
is Short ->
232+
when (b) {
233+
is Byte -> bCol.first() shouldBe a.toByte()
234+
is Short -> bCol.first() shouldBe a.toShort()
235+
is Int -> bCol.first() shouldBe a.toInt()
236+
is Long -> bCol.first() shouldBe a.toLong()
237+
is Float -> bCol.first() shouldBe a.toFloat()
238+
is Double -> bCol.first() shouldBe a.toDouble()
239+
is BigInteger -> bCol.first() shouldBe a.toBigInteger()
240+
is BigDecimal -> bCol.first() shouldBe a.toBigDecimal()
241+
}
242+
243+
is Int ->
244+
when (b) {
245+
is Byte -> bCol.first() shouldBe a.toByte()
246+
is Short -> bCol.first() shouldBe a.toShort()
247+
is Int -> bCol.first() shouldBe a.toInt()
248+
is Long -> bCol.first() shouldBe a.toLong()
249+
is Float -> bCol.first() shouldBe a.toFloat()
250+
is Double -> bCol.first() shouldBe a.toDouble()
251+
is BigInteger -> bCol.first() shouldBe a.toBigInteger()
252+
is BigDecimal -> bCol.first() shouldBe a.toBigDecimal()
253+
}
254+
255+
is Long ->
256+
when (b) {
257+
is Byte -> bCol.first() shouldBe a.toByte()
258+
is Short -> bCol.first() shouldBe a.toShort()
259+
is Int -> bCol.first() shouldBe a.toInt()
260+
is Long -> bCol.first() shouldBe a.toLong()
261+
is Float -> bCol.first() shouldBe a.toFloat()
262+
is Double -> bCol.first() shouldBe a.toDouble()
263+
is BigInteger -> bCol.first() shouldBe a.toBigInteger()
264+
is BigDecimal -> bCol.first() shouldBe a.toBigDecimal()
265+
}
266+
267+
is Float ->
268+
when (b) {
269+
is Byte -> bCol.first() shouldBe a.roundToInt().toByte()
270+
is Short -> bCol.first() shouldBe a.roundToInt().toShort()
271+
is Int -> bCol.first() shouldBe a.roundToInt()
272+
is Long -> bCol.first() shouldBe a.roundToLong()
273+
is Float -> bCol.first() shouldBe a.toFloat()
274+
is Double -> bCol.first() shouldBe a.toDouble()
275+
is BigInteger -> bCol.first() shouldBe a.toBigInteger()
276+
is BigDecimal -> bCol.first() shouldBe a.toBigDecimal()
277+
}
278+
279+
is Double ->
280+
when (b) {
281+
is Byte -> bCol.first() shouldBe a.roundToInt().toByte()
282+
is Short -> bCol.first() shouldBe a.roundToInt().toShort()
283+
is Int -> bCol.first() shouldBe a.roundToInt()
284+
is Long -> bCol.first() shouldBe a.roundToLong()
285+
is Float -> bCol.first() shouldBe a.toFloat()
286+
is Double -> bCol.first() shouldBe a.toDouble()
287+
is BigInteger -> bCol.first() shouldBe a.toBigInteger()
288+
is BigDecimal -> bCol.first() shouldBe a.toBigDecimal()
289+
}
290+
291+
is BigInteger ->
292+
when (b) {
293+
is Byte -> bCol.first() shouldBe a.toByte()
294+
is Short -> bCol.first() shouldBe a.toShort()
295+
is Int -> bCol.first() shouldBe a.toInt()
296+
is Long -> bCol.first() shouldBe a.toLong()
297+
is Float -> bCol.first() shouldBe a.toFloat()
298+
is Double -> bCol.first() shouldBe a.toDouble()
299+
is BigInteger -> bCol.first() shouldBe a.toBigInteger()
300+
is BigDecimal -> bCol.first() shouldBe a.toBigDecimal()
301+
}
302+
303+
is BigDecimal ->
304+
when (b) {
305+
is Byte -> bCol.first() shouldBe a.toByte()
306+
is Short -> bCol.first() shouldBe a.toShort()
307+
is Int -> bCol.first() shouldBe a.toInt()
308+
is Long -> bCol.first() shouldBe a.toLong()
309+
is Float -> bCol.first() shouldBe a.toFloat()
310+
is Double -> bCol.first() shouldBe a.toDouble()
311+
is BigInteger -> bCol.first() shouldBe a.toBigInteger()
312+
is BigDecimal -> bCol.first() shouldBe a.toBigDecimal()
313+
}
314+
}
315+
}
316+
}
317+
}
186318
}

0 commit comments

Comments
 (0)