@@ -13,7 +13,15 @@ import org.jetbrains.kotlinx.dataframe.exceptions.CellConversionException
13
13
import org.jetbrains.kotlinx.dataframe.exceptions.TypeConversionException
14
14
import org.jetbrains.kotlinx.dataframe.exceptions.TypeConverterNotFoundException
15
15
import org.jetbrains.kotlinx.dataframe.hasNulls
16
+ import org.jetbrains.kotlinx.dataframe.impl.api.toBigDecimal
17
+ import org.jetbrains.kotlinx.dataframe.impl.api.toBigInteger
16
18
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
17
25
import kotlin.reflect.typeOf
18
26
import kotlin.time.Duration.Companion.hours
19
27
import java.time.LocalTime as JavaLocalTime
@@ -183,4 +191,128 @@ class ConvertTests {
183
191
val res = col.convertTo<String >()
184
192
res.print ()
185
193
}
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
+ }
186
318
}
0 commit comments