Skip to content

Commit

Permalink
Fix for Scala 2.13 syntax (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
xerial authored Jun 8, 2021
1 parent fc791f1 commit b8368dd
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import org.msgpack.core.MessagePackSpec.createMessagePackData
import wvlet.airspec.AirSpec

/**
*
*/
class InvalidDataReadTest extends AirSpec {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import scala.util.Random
class MessageFormatTest extends AirSpec with Benchmark {
test("MessageFormat") {
test("cover all byte codes") {
def checkV(b: Byte, tpe: ValueType) {
def checkV(b: Byte, tpe: ValueType): Unit = {
try MessageFormat.valueOf(b).getValueType shouldBe tpe
catch {
case e: AirSpecException =>
Expand All @@ -37,11 +37,11 @@ class MessageFormatTest extends AirSpec with Benchmark {
}
}

def checkF(b: Byte, f: MessageFormat) {
def checkF(b: Byte, f: MessageFormat): Unit = {
MessageFormat.valueOf(b) shouldBe f
}

def check(b: Byte, tpe: ValueType, f: MessageFormat) {
def check(b: Byte, tpe: ValueType, f: MessageFormat): Unit = {
checkV(b, tpe)
checkF(b, f)
}
Expand Down
189 changes: 114 additions & 75 deletions msgpack-core/src/test/scala/org/msgpack/core/MessagePackTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark {
Code.isPosFixInt(i.toByte) shouldBe true
}

for (i <- 0x80 until 0xFF) {
for (i <- 0x80 until 0xff) {
Code.isPosFixInt(i.toByte) shouldBe false
}
}
Expand Down Expand Up @@ -166,7 +166,7 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark {
Code.isNegFixInt(i.toByte) shouldBe false
}

for (i <- 0xe0 until 0xFF) {
for (i <- 0xe0 until 0xff) {
Code.isNegFixInt(i.toByte) shouldBe true
}

Expand Down Expand Up @@ -223,7 +223,7 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark {
fail("cannot not reach here")
}

private def checkOverflow[A](v: A, pack: MessagePacker => Unit, unpack: MessageUnpacker => A) {
private def checkOverflow[A](v: A, pack: MessagePacker => Unit, unpack: MessageUnpacker => A): Unit = {
try {
checkException[A](v, pack, unpack)
} catch {
Expand Down Expand Up @@ -253,63 +253,80 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark {
forAll { (v: Double) =>
check(v, _.packDouble(v), _.unpackDouble)
}
check(null, _.packNil, { unpacker =>
unpacker.unpackNil(); null
})
check(
null,
_.packNil,
{ unpacker =>
unpacker.unpackNil(); null
}
)
}

test("skipping a nil value") {
check(true, _.packNil, _.tryUnpackNil)
check(false, { packer =>
packer.packString("val")
}, { unpacker =>
unpacker.tryUnpackNil()
})
check("val", { packer =>
packer.packString("val")
}, { unpacker =>
unpacker.tryUnpackNil(); unpacker.unpackString()
})
check("val", { packer =>
packer.packNil(); packer.packString("val")
}, { unpacker =>
unpacker.tryUnpackNil(); unpacker.unpackString()
})
check(
false,
{ packer =>
packer.packString("val")
},
{ unpacker =>
unpacker.tryUnpackNil()
}
)
check(
"val",
{ packer =>
packer.packString("val")
},
{ unpacker =>
unpacker.tryUnpackNil(); unpacker.unpackString()
}
)
check(
"val",
{ packer =>
packer.packNil(); packer.packString("val")
},
{ unpacker =>
unpacker.tryUnpackNil(); unpacker.unpackString()
}
)
try {
checkException(null, { _ =>
}, _.tryUnpackNil)
checkException(null, { _ => }, _.tryUnpackNil)
} catch {
case e: MessageInsufficientBufferException => // OK
}
}

test("pack/unpack integer values") {
val sampleData = Seq[Long](Int.MinValue.toLong -
10,
-65535,
-8191,
-1024,
-255,
-127,
-63,
-31,
-15,
-7,
-3,
-1,
0,
2,
4,
8,
16,
32,
64,
128,
256,
1024,
8192,
65536,
Int.MaxValue.toLong + 10)
val sampleData = Seq[Long](
Int.MinValue.toLong -
10,
-65535,
-8191,
-1024,
-255,
-127,
-63,
-31,
-15,
-7,
-3,
-1,
0,
2,
4,
8,
16,
32,
64,
128,
256,
1024,
8192,
65536,
Int.MaxValue.toLong + 10
)
for (v <- sampleData) {
check(v, _.packLong(v), _.unpackLong)

Expand Down Expand Up @@ -399,10 +416,14 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark {
}

try {
checkException(malformed, { packer =>
packer.packRawStringHeader(malformedBytes.length)
packer.writePayload(malformedBytes)
}, _.unpackString())
checkException(
malformed,
{ packer =>
packer.packRawStringHeader(malformedBytes.length)
packer.writePayload(malformedBytes)
},
_.unpackString()
)
} catch {
case e: MessageStringCodingException => // OK
}
Expand All @@ -421,10 +442,16 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark {

for (bytes <- Seq(unmappable)) {
try {
checkException(bytes, { packer =>
packer.packRawStringHeader(bytes.length)
packer.writePayload(bytes)
}, _.unpackString(), new PackerConfig(), unpackerConfig)
checkException(
bytes,
{ packer =>
packer.packRawStringHeader(bytes.length)
packer.writePayload(bytes)
},
_.unpackString(),
new PackerConfig(),
unpackerConfig
)
} catch {
case e: MessageStringCodingException => // OK
}
Expand All @@ -434,9 +461,11 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark {
test("pack/unpack binary") {
forAll { (v: Array[Byte]) =>
check(
v, { packer =>
v,
{ packer =>
packer.packBinaryHeader(v.length); packer.writePayload(v)
}, { unpacker =>
},
{ unpacker =>
val len = unpacker.unpackBinaryHeader()
val out = new Array[Byte](len)
unpacker.readPayload(out, 0, len)
Expand All @@ -450,9 +479,11 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark {
val v = new Array[Byte](l)
Random.nextBytes(v)
check(
v, { packer =>
v,
{ packer =>
packer.packBinaryHeader(v.length); packer.writePayload(v)
}, { unpacker =>
},
{ unpacker =>
val len = unpacker.unpackBinaryHeader()
val out = new Array[Byte](len)
unpacker.readPayload(out, 0, len)
Expand All @@ -467,10 +498,12 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark {
test("pack/unpack arrays") {
forAll { (v: Array[Int]) =>
check(
v, { packer =>
v,
{ packer =>
packer.packArrayHeader(v.length)
v.map(packer.packInt(_))
}, { unpacker =>
},
{ unpacker =>
val len = unpacker.unpackArrayHeader()
val out = new Array[Int](len)
for (i <- 0 until v.length) {
Expand Down Expand Up @@ -498,20 +531,22 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark {
val m = v.map(i => (i, i.toString)).toSeq

check(
m, { packer =>
m,
{ packer =>
packer.packMapHeader(v.length)
m.map {
case (k: Int, v: String) =>
packer.packInt(k)
packer.packString(v)
}
}, { unpacker =>
},
{ unpacker =>
val len = unpacker.unpackMapHeader()
val b = Seq.newBuilder[(Int, String)]
for (i <- 0 until len) {
b += ((unpacker.unpackInt, unpacker.unpackString))
}
b.result
b.result()
}
)
}
Expand Down Expand Up @@ -549,7 +584,8 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark {
val aMap = List(Map("f" -> "x"))

check(
aMap, { packer =>
aMap,
{ packer =>
packer.packArrayHeader(aMap.size)
for (m <- aMap) {
packer.packMapHeader(m.size)
Expand All @@ -558,10 +594,11 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark {
packer.packString(v)
}
}
}, { unpacker =>
},
{ unpacker =>
val v = new Variable()
unpacker.unpackValue(v)
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
v.asArrayValue().asScala
.map { m =>
val mv = m.asMapValue()
Expand Down Expand Up @@ -605,12 +642,14 @@ class MessagePackTest extends AirSpec with PropertyCheck with Benchmark {
}

// Corner-cases around uint32 boundaries
for (v <- Seq(
Instant.ofEpochSecond(Instant.now().getEpochSecond, 123456789L), // uint32 nanoseq (out of int32 range)
Instant.ofEpochSecond(-1302749144L, 0), // 1928-09-19T21:14:16Z
Instant.ofEpochSecond(-747359729L, 0), // 1946-04-27T00:04:31Z
Instant.ofEpochSecond(4257387427L, 0) // 2104-11-29T07:37:07Z
)) {
for (
v <- Seq(
Instant.ofEpochSecond(Instant.now().getEpochSecond, 123456789L), // uint32 nanoseq (out of int32 range)
Instant.ofEpochSecond(-1302749144L, 0), // 1928-09-19T21:14:16Z
Instant.ofEpochSecond(-747359729L, 0), // 1946-04-27T00:04:31Z
Instant.ofEpochSecond(4257387427L, 0) // 2104-11-29T07:37:07Z
)
) {
check(v, _.packTimestamp(v), _.unpackTimestamp())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@ import java.io.{ByteArrayOutputStream, File, FileInputStream, FileOutputStream}
import scala.util.Random

/**
*
*/
class MessagePackerTest extends AirSpec with Benchmark {

private def verifyIntSeq(answer: Array[Int], packed: Array[Byte]) {
private def verifyIntSeq(answer: Array[Int], packed: Array[Byte]): Unit = {
val unpacker = MessagePack.newDefaultUnpacker(packed)
val b = Array.newBuilder[Int]
while (unpacker.hasNext) {
b += unpacker.unpackInt()
}
val result = b.result
val result = b.result()
result.size shouldBe answer.size
result shouldBe answer
}
Expand All @@ -61,7 +60,7 @@ class MessagePackerTest extends AirSpec with Benchmark {
test("MessagePacker") {

test("reset the internal states") {
val intSeq = (0 until 100).map(i => Random.nextInt).toArray
val intSeq = (0 until 100).map(i => Random.nextInt()).toArray

val b = new ByteArrayOutputStream
val packer = MessagePack.newDefaultPacker(b)
Expand Down Expand Up @@ -239,12 +238,12 @@ class MessagePackerTest extends AirSpec with Benchmark {
val packerTotalWrittenBytes =
withResource(MessagePack.newDefaultPacker(out)) { packer =>
packer
.packByte(0) // 1
.packBoolean(true) // 1
.packShort(12) // 1
.packInt(1024) // 3
.packByte(0) // 1
.packBoolean(true) // 1
.packShort(12) // 1
.packInt(1024) // 3
.packLong(Long.MaxValue) // 5
.packString("foobar") // 7
.packString("foobar") // 7
.flush()

packer.getTotalWrittenBytes
Expand Down
Loading

0 comments on commit b8368dd

Please sign in to comment.