|
| 1 | +package com.github.plokhotnyuk.jsoniter_scala.core |
| 2 | + |
| 3 | +import org.scalacheck.Arbitrary.arbitrary |
| 4 | +import org.scalatest.matchers.should.Matchers |
| 5 | +import org.scalatest.wordspec.AnyWordSpec |
| 6 | +import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks |
| 7 | +import com.epam.deltix.dfp.Decimal64Utils |
| 8 | +import com.github.plokhotnyuk.jsoniter_scala.core.GenUtils._ |
| 9 | + |
| 10 | +import java.io._ |
| 11 | +import java.nio.charset.StandardCharsets.UTF_8 |
| 12 | +import scala.util.Random |
| 13 | + |
| 14 | +class Decimal64Spec extends AnyWordSpec with Matchers with ScalaCheckPropertyChecks { |
| 15 | + "JsonWriter.writeDecimal64Val and JsonWriter.writeDecimal64ValAsString and JsonWriter.writeDecimal64Key for an underlying representation of Decimal64" should { |
| 16 | + "write finite Decimal64 values" in { |
| 17 | + def check(n: Long): Unit = { |
| 18 | + val s = withWriter(_.writeDecimal64Val(n)) |
| 19 | + print(s + " ") |
| 20 | + Decimal64Utils.compareTo(Decimal64Utils.parse(s), n) shouldBe 0 // no data loss when parsing by JVM, Native or JS Platform |
| 21 | + s.length should be <= 22 // length is 22 bytes or less |
| 22 | + withWriter(_.writeDecimal64ValAsString(n)) shouldBe s""""$s"""" |
| 23 | + withWriter(_.writeDecimal64Key(n)) shouldBe s""""$s":""" |
| 24 | + } |
| 25 | + |
| 26 | + check(Decimal64Utils.ZERO) |
| 27 | + check(Decimal64Utils.ONE) |
| 28 | + check(Decimal64Utils.TEN) |
| 29 | + check(Decimal64Utils.THOUSAND) |
| 30 | + check(Decimal64Utils.MILLION) |
| 31 | + check(Decimal64Utils.ONE_TENTH) |
| 32 | + check(Decimal64Utils.ONE_HUNDREDTH) |
| 33 | + check(Decimal64Utils.parse("1000.0")) |
| 34 | + check(Decimal64Utils.parse("1000.001")) |
| 35 | + forAll(arbitrary[Long], minSuccessful(10000)) { n => |
| 36 | + whenever(Decimal64Utils.isFinite(n)) { |
| 37 | + check(n) |
| 38 | + } |
| 39 | + } |
| 40 | + forAll(genFiniteDouble, minSuccessful(10000)) { d => |
| 41 | + check(Decimal64Utils.fromDouble(d)) |
| 42 | + } |
| 43 | + forAll(arbitrary[Long], minSuccessful(10000)) { l => |
| 44 | + check(Decimal64Utils.fromFixedPoint(l >> 8, l.toByte)) |
| 45 | + } |
| 46 | + forAll(arbitrary[Long], minSuccessful(10000)) { l => |
| 47 | + check(Decimal64Utils.fromLong(l)) |
| 48 | + } |
| 49 | + forAll(arbitrary[Int], minSuccessful(10000)) { i => |
| 50 | + check(Decimal64Utils.fromInt(i)) |
| 51 | + } |
| 52 | + } |
| 53 | + "throw i/o exception on non-finite numbers" in { |
| 54 | + forAll(arbitrary[Long], minSuccessful(100)) { n => |
| 55 | + whenever(Decimal64Utils.isNonFinite(n)) { |
| 56 | + assert(intercept[JsonWriterException](withWriter(_.writeDecimal64Val(n))).getMessage.startsWith("illegal Decimal64 number")) |
| 57 | + assert(intercept[JsonWriterException](withWriter(_.writeDecimal64ValAsString(n))).getMessage.startsWith("illegal Decimal64 number")) |
| 58 | + assert(intercept[JsonWriterException](withWriter(_.writeDecimal64Key(n))).getMessage.startsWith("illegal Decimal64 number")) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + def reader(json: String, totalRead: Long = 0): JsonReader = reader2(json.getBytes(UTF_8), totalRead) |
| 65 | + |
| 66 | + def reader2(jsonBytes: Array[Byte], totalRead: Long = 0): JsonReader = |
| 67 | + new JsonReader(new Array[Byte](Random.nextInt(20) + 12), // 12 is a minimal allowed length to test resizing of the buffer |
| 68 | + 0, 0, -1, new Array[Char](Random.nextInt(32)), null, new ByteArrayInputStream(jsonBytes), totalRead, readerConfig) |
| 69 | + |
| 70 | + def readerConfig: ReaderConfig = ReaderConfig |
| 71 | + .withPreferredBufSize(Random.nextInt(20) + 12) // 12 is a minimal allowed length to test resizing of the buffer |
| 72 | + .withPreferredCharBufSize(Random.nextInt(32)) |
| 73 | + .withThrowReaderExceptionWithStackTrace(true) |
| 74 | + |
| 75 | + def withWriter(f: JsonWriter => Unit): String = |
| 76 | + withWriter(WriterConfig.withPreferredBufSize(1).withThrowWriterExceptionWithStackTrace(true))(f) |
| 77 | + |
| 78 | + def withWriter(cfg: WriterConfig)(f: JsonWriter => Unit): String = { |
| 79 | + val writer = new JsonWriter(new Array[Byte](Random.nextInt(16)), 0, 0, 0, false, false, null, null, cfg) |
| 80 | + new String(writer.write(new JsonValueCodec[String] { |
| 81 | + override def decodeValue(in: JsonReader, default: String): String = "" |
| 82 | + |
| 83 | + override def encodeValue(x: String, out: JsonWriter): Unit = f(writer) |
| 84 | + |
| 85 | + override val nullValue: String = "" |
| 86 | + }, "", cfg), "UTF-8") |
| 87 | + } |
| 88 | +} |
0 commit comments