| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent fc791f1 commit b8368dd
11 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,6 @@ import org.msgpack.core.MessagePackSpec.createMessagePackData | |||
| 4 | 4 | import wvlet.airspec.AirSpec | |
| 5 | 5 | ||
| 6 | 6 | /** | |
| 7 | - * | ||
| 8 | 7 | */ | |
| 9 | 8 | class InvalidDataReadTest extends AirSpec { | |
| 10 | 9 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,7 +28,7 @@ import scala.util.Random | |||
| 28 | 28 | class MessageFormatTest extends AirSpec with Benchmark { | |
| 29 | 29 | test("MessageFormat") { | |
| 30 | 30 | test("cover all byte codes") { | |
| 31 | - def checkV(b: Byte, tpe: ValueType) { | ||
| 31 | + def checkV(b: Byte, tpe: ValueType): Unit = { | ||
| 32 | 32 | try MessageFormat.valueOf(b).getValueType shouldBe tpe | |
| 33 | 33 | catch { | |
| 34 | 34 | case e: AirSpecException => | |
@@ -37,11 +37,11 @@ class MessageFormatTest extends AirSpec with Benchmark { | |||
| 37 | 37 | } | |
| 38 | 38 | } | |
| 39 | 39 | ||
| 40 | - def checkF(b: Byte, f: MessageFormat) { | ||
| 40 | + def checkF(b: Byte, f: MessageFormat): Unit = { | ||
| 41 | 41 | MessageFormat.valueOf(b) shouldBe f | |
| 42 | 42 | } | |
| 43 | 43 | ||
| 44 | - def check(b: Byte, tpe: ValueType, f: MessageFormat) { | ||
| 44 | + def check(b: Byte, tpe: ValueType, f: MessageFormat): Unit = { | ||
| 45 | 45 | checkV(b, tpe) | |
| 46 | 46 | checkF(b, f) | |
| 47 | 47 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -82,7 +82,7 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark { | |||
| 82 | 82 | Code.isPosFixInt(i.toByte) shouldBe true | |
| 83 | 83 | } | |
| 84 | 84 | ||
| 85 | - for (i <- 0x80 until 0xFF) { | ||
| 85 | + for (i <- 0x80 until 0xff) { | ||
| 86 | 86 | Code.isPosFixInt(i.toByte) shouldBe false | |
| 87 | 87 | } | |
| 88 | 88 | } | |
@@ -166,7 +166,7 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark { | |||
| 166 | 166 | Code.isNegFixInt(i.toByte) shouldBe false | |
| 167 | 167 | } | |
| 168 | 168 | ||
| 169 | - for (i <- 0xe0 until 0xFF) { | ||
| 169 | + for (i <- 0xe0 until 0xff) { | ||
| 170 | 170 | Code.isNegFixInt(i.toByte) shouldBe true | |
| 171 | 171 | } | |
| 172 | 172 | ||
@@ -223,7 +223,7 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark { | |||
| 223 | 223 | fail("cannot not reach here") | |
| 224 | 224 | } | |
| 225 | 225 | ||
| 226 | - private def checkOverflow[A](v: A, pack: MessagePacker => Unit, unpack: MessageUnpacker => A) { | ||
| 226 | + private def checkOverflow[A](v: A, pack: MessagePacker => Unit, unpack: MessageUnpacker => A): Unit = { | ||
| 227 | 227 | try { | |
| 228 | 228 | checkException[A](v, pack, unpack) | |
| 229 | 229 | } catch { | |
@@ -253,63 +253,80 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark { | |||
| 253 | 253 | forAll { (v: Double) => | |
| 254 | 254 | check(v, _.packDouble(v), _.unpackDouble) | |
| 255 | 255 | } | |
| 256 | - check(null, _.packNil, { unpacker => | ||
| 257 | - unpacker.unpackNil(); null | ||
| 258 | - }) | ||
| 256 | + check( | ||
| 257 | + null, | ||
| 258 | + _.packNil, | ||
| 259 | + { unpacker => | ||
| 260 | + unpacker.unpackNil(); null | ||
| 261 | + } | ||
| 262 | + ) | ||
| 259 | 263 | } | |
| 260 | 264 | ||
| 261 | 265 | test("skipping a nil value") { | |
| 262 | 266 | check(true, _.packNil, _.tryUnpackNil) | |
| 263 | - check(false, { packer => | ||
| 264 | - packer.packString("val") | ||
| 265 | - }, { unpacker => | ||
| 266 | - unpacker.tryUnpackNil() | ||
| 267 | - }) | ||
| 268 | - check("val", { packer => | ||
| 269 | - packer.packString("val") | ||
| 270 | - }, { unpacker => | ||
| 271 | - unpacker.tryUnpackNil(); unpacker.unpackString() | ||
| 272 | - }) | ||
| 273 | - check("val", { packer => | ||
| 274 | - packer.packNil(); packer.packString("val") | ||
| 275 | - }, { unpacker => | ||
| 276 | - unpacker.tryUnpackNil(); unpacker.unpackString() | ||
| 277 | - }) | ||
| 267 | + check( | ||
| 268 | + false, | ||
| 269 | + { packer => | ||
| 270 | + packer.packString("val") | ||
| 271 | + }, | ||
| 272 | + { unpacker => | ||
| 273 | + unpacker.tryUnpackNil() | ||
| 274 | + } | ||
| 275 | + ) | ||
| 276 | + check( | ||
| 277 | + "val", | ||
| 278 | + { packer => | ||
| 279 | + packer.packString("val") | ||
| 280 | + }, | ||
| 281 | + { unpacker => | ||
| 282 | + unpacker.tryUnpackNil(); unpacker.unpackString() | ||
| 283 | + } | ||
| 284 | + ) | ||
| 285 | + check( | ||
| 286 | + "val", | ||
| 287 | + { packer => | ||
| 288 | + packer.packNil(); packer.packString("val") | ||
| 289 | + }, | ||
| 290 | + { unpacker => | ||
| 291 | + unpacker.tryUnpackNil(); unpacker.unpackString() | ||
| 292 | + } | ||
| 293 | + ) | ||
| 278 | 294 | try { | |
| 279 | - checkException(null, { _ => | ||
| 280 | - }, _.tryUnpackNil) | ||
| 295 | + checkException(null, { _ => }, _.tryUnpackNil) | ||
| 281 | 296 | } catch { | |
| 282 | 297 | case e: MessageInsufficientBufferException => // OK | |
| 283 | 298 | } | |
| 284 | 299 | } | |
| 285 | 300 | ||
| 286 | 301 | test("pack/unpack integer values") { | |
| 287 | - val sampleData = Seq[Long](Int.MinValue.toLong - | ||
| 288 | - 10, | ||
| 289 | - -65535, | ||
| 290 | - -8191, | ||
| 291 | - -1024, | ||
| 292 | - -255, | ||
| 293 | - -127, | ||
| 294 | - -63, | ||
| 295 | - -31, | ||
| 296 | - -15, | ||
| 297 | - -7, | ||
| 298 | - -3, | ||
| 299 | - -1, | ||
| 300 | - 0, | ||
| 301 | - 2, | ||
| 302 | - 4, | ||
| 303 | - 8, | ||
| 304 | - 16, | ||
| 305 | - 32, | ||
| 306 | - 64, | ||
| 307 | - 128, | ||
| 308 | - 256, | ||
| 309 | - 1024, | ||
| 310 | - 8192, | ||
| 311 | - 65536, | ||
| 312 | - Int.MaxValue.toLong + 10) | ||
| 302 | + val sampleData = Seq[Long]( | ||
| 303 | + Int.MinValue.toLong - | ||
| 304 | + 10, | ||
| 305 | + -65535, | ||
| 306 | + -8191, | ||
| 307 | + -1024, | ||
| 308 | + -255, | ||
| 309 | + -127, | ||
| 310 | + -63, | ||
| 311 | + -31, | ||
| 312 | + -15, | ||
| 313 | + -7, | ||
| 314 | + -3, | ||
| 315 | + -1, | ||
| 316 | + 0, | ||
| 317 | + 2, | ||
| 318 | + 4, | ||
| 319 | + 8, | ||
| 320 | + 16, | ||
| 321 | + 32, | ||
| 322 | + 64, | ||
| 323 | + 128, | ||
| 324 | + 256, | ||
| 325 | + 1024, | ||
| 326 | + 8192, | ||
| 327 | + 65536, | ||
| 328 | + Int.MaxValue.toLong + 10 | ||
| 329 | + ) | ||
| 313 | 330 | for (v <- sampleData) { | |
| 314 | 331 | check(v, _.packLong(v), _.unpackLong) | |
| 315 | 332 | ||
@@ -399,10 +416,14 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark { | |||
| 399 | 416 | } | |
| 400 | 417 | ||
| 401 | 418 | try { | |
| 402 | - checkException(malformed, { packer => | ||
| 403 | - packer.packRawStringHeader(malformedBytes.length) | ||
| 404 | - packer.writePayload(malformedBytes) | ||
| 405 | - }, _.unpackString()) | ||
| 419 | + checkException( | ||
| 420 | + malformed, | ||
| 421 | + { packer => | ||
| 422 | + packer.packRawStringHeader(malformedBytes.length) | ||
| 423 | + packer.writePayload(malformedBytes) | ||
| 424 | + }, | ||
| 425 | + _.unpackString() | ||
| 426 | + ) | ||
| 406 | 427 | } catch { | |
| 407 | 428 | case e: MessageStringCodingException => // OK | |
| 408 | 429 | } | |
@@ -421,10 +442,16 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark { | |||
| 421 | 442 | ||
| 422 | 443 | for (bytes <- Seq(unmappable)) { | |
| 423 | 444 | try { | |
| 424 | - checkException(bytes, { packer => | ||
| 425 | - packer.packRawStringHeader(bytes.length) | ||
| 426 | - packer.writePayload(bytes) | ||
| 427 | - }, _.unpackString(), new PackerConfig(), unpackerConfig) | ||
| 445 | + checkException( | ||
| 446 | + bytes, | ||
| 447 | + { packer => | ||
| 448 | + packer.packRawStringHeader(bytes.length) | ||
| 449 | + packer.writePayload(bytes) | ||
| 450 | + }, | ||
| 451 | + _.unpackString(), | ||
| 452 | + new PackerConfig(), | ||
| 453 | + unpackerConfig | ||
| 454 | + ) | ||
| 428 | 455 | } catch { | |
| 429 | 456 | case e: MessageStringCodingException => // OK | |
| 430 | 457 | } | |
@@ -434,9 +461,11 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark { | |||
| 434 | 461 | test("pack/unpack binary") { | |
| 435 | 462 | forAll { (v: Array[Byte]) => | |
| 436 | 463 | check( | |
| 437 | - v, { packer => | ||
| 464 | + v, | ||
| 465 | + { packer => | ||
| 438 | 466 | packer.packBinaryHeader(v.length); packer.writePayload(v) | |
| 439 | - }, { unpacker => | ||
| 467 | + }, | ||
| 468 | + { unpacker => | ||
| 440 | 469 | val len = unpacker.unpackBinaryHeader() | |
| 441 | 470 | val out = new Array[Byte](len) | |
| 442 | 471 | unpacker.readPayload(out, 0, len) | |
@@ -450,9 +479,11 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark { | |||
| 450 | 479 | val v = new Array[Byte](l) | |
| 451 | 480 | Random.nextBytes(v) | |
| 452 | 481 | check( | |
| 453 | - v, { packer => | ||
| 482 | + v, | ||
| 483 | + { packer => | ||
| 454 | 484 | packer.packBinaryHeader(v.length); packer.writePayload(v) | |
| 455 | - }, { unpacker => | ||
| 485 | + }, | ||
| 486 | + { unpacker => | ||
| 456 | 487 | val len = unpacker.unpackBinaryHeader() | |
| 457 | 488 | val out = new Array[Byte](len) | |
| 458 | 489 | unpacker.readPayload(out, 0, len) | |
@@ -467,10 +498,12 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark { | |||
| 467 | 498 | test("pack/unpack arrays") { | |
| 468 | 499 | forAll { (v: Array[Int]) => | |
| 469 | 500 | check( | |
| 470 | - v, { packer => | ||
| 501 | + v, | ||
| 502 | + { packer => | ||
| 471 | 503 | packer.packArrayHeader(v.length) | |
| 472 | 504 | v.map(packer.packInt(_)) | |
| 473 | - }, { unpacker => | ||
| 505 | + }, | ||
| 506 | + { unpacker => | ||
| 474 | 507 | val len = unpacker.unpackArrayHeader() | |
| 475 | 508 | val out = new Array[Int](len) | |
| 476 | 509 | for (i <- 0 until v.length) { | |
@@ -498,20 +531,22 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark { | |||
| 498 | 531 | val m = v.map(i => (i, i.toString)).toSeq | |
| 499 | 532 | ||
| 500 | 533 | check( | |
| 501 | - m, { packer => | ||
| 534 | + m, | ||
| 535 | + { packer => | ||
| 502 | 536 | packer.packMapHeader(v.length) | |
| 503 | 537 | m.map { | |
| 504 | 538 | case (k: Int, v: String) => | |
| 505 | 539 | packer.packInt(k) | |
| 506 | 540 | packer.packString(v) | |
| 507 | 541 | } | |
| 508 | - }, { unpacker => | ||
| 542 | + }, | ||
| 543 | + { unpacker => | ||
| 509 | 544 | val len = unpacker.unpackMapHeader() | |
| 510 | 545 | val b = Seq.newBuilder[(Int, String)] | |
| 511 | 546 | for (i <- 0 until len) { | |
| 512 | 547 | b += ((unpacker.unpackInt, unpacker.unpackString)) | |
| 513 | 548 | } | |
| 514 | - b.result | ||
| 549 | + b.result() | ||
| 515 | 550 | } | |
| 516 | 551 | ) | |
| 517 | 552 | } | |
@@ -549,7 +584,8 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark { | |||
| 549 | 584 | val aMap = List(Map("f" -> "x")) | |
| 550 | 585 | ||
| 551 | 586 | check( | |
| 552 | - aMap, { packer => | ||
| 587 | + aMap, | ||
| 588 | + { packer => | ||
| 553 | 589 | packer.packArrayHeader(aMap.size) | |
| 554 | 590 | for (m <- aMap) { | |
| 555 | 591 | packer.packMapHeader(m.size) | |
@@ -558,10 +594,11 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark { | |||
| 558 | 594 | packer.packString(v) | |
| 559 | 595 | } | |
| 560 | 596 | } | |
| 561 | - }, { unpacker => | ||
| 597 | + }, | ||
| 598 | + { unpacker => | ||
| 562 | 599 | val v = new Variable() | |
| 563 | 600 | unpacker.unpackValue(v) | |
| 564 | - import scala.collection.JavaConverters._ | ||
| 601 | + import scala.jdk.CollectionConverters._ | ||
| 565 | 602 | v.asArrayValue().asScala | |
| 566 | 603 | .map { m => | |
| 567 | 604 | val mv = m.asMapValue() | |
@@ -605,12 +642,14 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark { | |||
| 605 | 642 | } | |
| 606 | 643 | ||
| 607 | 644 | // Corner-cases around uint32 boundaries | |
| 608 | - for (v <- Seq( | ||
| 609 | - Instant.ofEpochSecond(Instant.now().getEpochSecond, 123456789L), // uint32 nanoseq (out of int32 range) | ||
| 610 | - Instant.ofEpochSecond(-1302749144L, 0), // 1928-09-19T21:14:16Z | ||
| 611 | - Instant.ofEpochSecond(-747359729L, 0), // 1946-04-27T00:04:31Z | ||
| 612 | - Instant.ofEpochSecond(4257387427L, 0) // 2104-11-29T07:37:07Z | ||
| 613 | - )) { | ||
| 645 | + for ( | ||
| 646 | + v <- Seq( | ||
| 647 | + Instant.ofEpochSecond(Instant.now().getEpochSecond, 123456789L), // uint32 nanoseq (out of int32 range) | ||
| 648 | + Instant.ofEpochSecond(-1302749144L, 0), // 1928-09-19T21:14:16Z | ||
| 649 | + Instant.ofEpochSecond(-747359729L, 0), // 1946-04-27T00:04:31Z | ||
| 650 | + Instant.ofEpochSecond(4257387427L, 0) // 2104-11-29T07:37:07Z | ||
| 651 | + ) | ||
| 652 | + ) { | ||
| 614 | 653 | check(v, _.packTimestamp(v), _.unpackTimestamp()) | |
| 615 | 654 | } | |
| 616 | 655 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -25,17 +25,16 @@ import java.io.{ByteArrayOutputStream, File, FileInputStream, FileOutputStream} | |||
| 25 | 25 | import scala.util.Random | |
| 26 | 26 | ||
| 27 | 27 | /** | |
| 28 | - * | ||
| 29 | 28 | */ | |
| 30 | 29 | class MessagePackerTest extends AirSpec with Benchmark { | |
| 31 | 30 | ||
| 32 | - private def verifyIntSeq(answer: Array[Int], packed: Array[Byte]) { | ||
| 31 | + private def verifyIntSeq(answer: Array[Int], packed: Array[Byte]): Unit = { | ||
| 33 | 32 | val unpacker = MessagePack.newDefaultUnpacker(packed) | |
| 34 | 33 | val b = Array.newBuilder[Int] | |
| 35 | 34 | while (unpacker.hasNext) { | |
| 36 | 35 | b += unpacker.unpackInt() | |
| 37 | 36 | } | |
| 38 | - val result = b.result | ||
| 37 | + val result = b.result() | ||
| 39 | 38 | result.size shouldBe answer.size | |
| 40 | 39 | result shouldBe answer | |
| 41 | 40 | } | |
@@ -61,7 +60,7 @@ class MessagePackerTest extends AirSpec with Benchmark { | |||
| 61 | 60 | test("MessagePacker") { | |
| 62 | 61 | ||
| 63 | 62 | test("reset the internal states") { | |
| 64 | - val intSeq = (0 until 100).map(i => Random.nextInt).toArray | ||
| 63 | + val intSeq = (0 until 100).map(i => Random.nextInt()).toArray | ||
| 65 | 64 | ||
| 66 | 65 | val b = new ByteArrayOutputStream | |
| 67 | 66 | val packer = MessagePack.newDefaultPacker(b) | |
@@ -239,12 +238,12 @@ class MessagePackerTest extends AirSpec with Benchmark { | |||
| 239 | 238 | val packerTotalWrittenBytes = | |
| 240 | 239 | withResource(MessagePack.newDefaultPacker(out)) { packer => | |
| 241 | 240 | packer | |
| 242 | - .packByte(0) // 1 | ||
| 243 | - .packBoolean(true) // 1 | ||
| 244 | - .packShort(12) // 1 | ||
| 245 | - .packInt(1024) // 3 | ||
| 241 | + .packByte(0) // 1 | ||
| 242 | + .packBoolean(true) // 1 | ||
| 243 | + .packShort(12) // 1 | ||
| 244 | + .packInt(1024) // 3 | ||
| 246 | 245 | .packLong(Long.MaxValue) // 5 | |
| 247 | - .packString("foobar") // 7 | ||
| 246 | + .packString("foobar") // 7 | ||
| 248 | 247 | .flush() | |
| 249 | 248 | ||
| 250 | 249 | packer.getTotalWrittenBytes | |
| Back | FazBrowse Home | New Git URL |
0 commit comments