|
| 1 | +package com.fasterxml.jackson.module.kotlin.test |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.exc.InputCoercionException |
| 4 | +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper |
| 5 | +import com.fasterxml.jackson.module.kotlin.readValue |
| 6 | +import org.junit.Assert.assertThrows |
| 7 | +import org.junit.Test |
| 8 | +import java.math.BigInteger |
| 9 | +import kotlin.test.assertEquals |
| 10 | + |
| 11 | +internal class UnsignedNumbersOnKeyTest { |
| 12 | + companion object { |
| 13 | + val MAPPER = jacksonObjectMapper() |
| 14 | + } |
| 15 | + |
| 16 | + class ForUByte { |
| 17 | + private fun makeSrc(v: Int): String = MAPPER.writeValueAsString(mapOf(v to 0)) |
| 18 | + |
| 19 | + @Test |
| 20 | + fun test() { |
| 21 | + val actual = MAPPER.readValue<Map<UByte, UByte>>(makeSrc(UByte.MAX_VALUE.toInt())) |
| 22 | + assertEquals(mapOf(UByte.MAX_VALUE to 0.toUByte()), actual) |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + fun overflow() { |
| 27 | + assertThrows(InputCoercionException::class.java) { |
| 28 | + MAPPER.readValue<Map<UByte, UByte>>(makeSrc(UByte.MAX_VALUE.toInt() + 1)) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + fun underflow() { |
| 34 | + assertThrows(InputCoercionException::class.java) { |
| 35 | + MAPPER.readValue<Map<UByte, UByte>>(makeSrc(-1)) |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + class ForUShort { |
| 41 | + private fun makeSrc(v: Int): String = MAPPER.writeValueAsString(mapOf(v to 0)) |
| 42 | + |
| 43 | + @Test |
| 44 | + fun test() { |
| 45 | + val actual = MAPPER.readValue<Map<UShort, UShort>>(makeSrc(UShort.MAX_VALUE.toInt())) |
| 46 | + assertEquals(mapOf(UShort.MAX_VALUE to 0.toUShort()), actual) |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun overflow() { |
| 51 | + assertThrows(InputCoercionException::class.java) { |
| 52 | + MAPPER.readValue<Map<UShort, UShort>>(makeSrc(UShort.MAX_VALUE.toInt() + 1)) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun underflow() { |
| 58 | + assertThrows(InputCoercionException::class.java) { |
| 59 | + MAPPER.readValue<Map<UShort, UShort>>(makeSrc(-1)) |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + class ForUInt { |
| 65 | + private fun makeSrc(v: Long): String = MAPPER.writeValueAsString(mapOf(v to 0)) |
| 66 | + |
| 67 | + @Test |
| 68 | + fun test() { |
| 69 | + val actual = MAPPER.readValue<Map<UInt, UInt>>(makeSrc(UInt.MAX_VALUE.toLong())) |
| 70 | + assertEquals(mapOf(UInt.MAX_VALUE to 0.toUInt()), actual) |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + fun overflow() { |
| 75 | + assertThrows(InputCoercionException::class.java) { |
| 76 | + MAPPER.readValue<Map<UInt, UInt>>(makeSrc(UInt.MAX_VALUE.toLong() + 1L)) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun underflow() { |
| 82 | + assertThrows(InputCoercionException::class.java) { |
| 83 | + MAPPER.readValue<Map<UInt, UInt>>(makeSrc(-1L)) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + class ForULong { |
| 89 | + private fun makeSrc(v: BigInteger): String = MAPPER.writeValueAsString(mapOf(v to 0)) |
| 90 | + |
| 91 | + @Test |
| 92 | + fun test() { |
| 93 | + val actual = MAPPER.readValue<Map<ULong, ULong>>(makeSrc(BigInteger(ULong.MAX_VALUE.toString()))) |
| 94 | + assertEquals(mapOf(ULong.MAX_VALUE to 0.toULong()), actual) |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + fun overflow() { |
| 99 | + assertThrows(InputCoercionException::class.java) { |
| 100 | + MAPPER.readValue<Map<ULong, ULong>>(makeSrc(BigInteger(ULong.MAX_VALUE.toString()) + BigInteger.ONE)) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + fun underflow() { |
| 106 | + assertThrows(InputCoercionException::class.java) { |
| 107 | + MAPPER.readValue<Map<ULong, ULong>>(makeSrc(BigInteger.valueOf(-1L))) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments