* =act #21237 fix regression in ByteString.slice * Update ByteStringSpec.scala
This commit is contained in:
parent
9ef93c6200
commit
fb45dd03f3
2 changed files with 43 additions and 15 deletions
|
|
@ -21,11 +21,7 @@ import scala.collection.mutable.Builder
|
||||||
|
|
||||||
class ByteStringSpec extends WordSpec with Matchers with Checkers {
|
class ByteStringSpec extends WordSpec with Matchers with Checkers {
|
||||||
|
|
||||||
// // uncomment when developing locally to get better coverage
|
implicit val betterGeneratorDrivenConfig = PropertyCheckConfig().copy(minSuccessful = 1000)
|
||||||
// implicit override val generatorDrivenConfig =
|
|
||||||
// PropertyCheckConfig(
|
|
||||||
// minSuccessful = 1000,
|
|
||||||
// minSize = 0, maxSize = 100)
|
|
||||||
|
|
||||||
def genSimpleByteString(min: Int, max: Int) = for {
|
def genSimpleByteString(min: Int, max: Int) = for {
|
||||||
n ← Gen.choose(min, max)
|
n ← Gen.choose(min, max)
|
||||||
|
|
@ -365,13 +361,48 @@ class ByteStringSpec extends WordSpec with Matchers with Checkers {
|
||||||
ByteStrings(ByteString1.fromString(""), ByteString1.fromString("ab")).dropRight(Int.MinValue) should ===(ByteString("ab"))
|
ByteStrings(ByteString1.fromString(""), ByteString1.fromString("ab")).dropRight(Int.MinValue) should ===(ByteString("ab"))
|
||||||
}
|
}
|
||||||
"slice" in {
|
"slice" in {
|
||||||
ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("")).slice(0, 1) should ===(ByteString("a"))
|
|
||||||
ByteStrings(ByteString1.fromString(""), ByteString1.fromString("a")).slice(1, 1) should ===(ByteString(""))
|
ByteStrings(ByteString1.fromString(""), ByteString1.fromString("a")).slice(1, 1) should ===(ByteString(""))
|
||||||
|
// We explicitly test all edge cases to always test them, refs bug #21237
|
||||||
|
ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("")).slice(-10, 10) should ===(ByteString("a"))
|
||||||
|
ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("")).slice(-10, 0) should ===(ByteString(""))
|
||||||
|
ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("")).slice(-10, 1) should ===(ByteString("a"))
|
||||||
|
ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("")).slice(0, 1) should ===(ByteString("a"))
|
||||||
|
ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("")).slice(0, 10) should ===(ByteString("a"))
|
||||||
|
ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("")).slice(1, 10) should ===(ByteString(""))
|
||||||
|
ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("")).slice(1, -2) should ===(ByteString(""))
|
||||||
|
ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("")).slice(-10, -100) should ===(ByteString(""))
|
||||||
|
ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("")).slice(-100, -10) should ===(ByteString(""))
|
||||||
|
// Get an empty if `from` is greater then `until`
|
||||||
|
ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("")).slice(1, 0) should ===(ByteString(""))
|
||||||
|
|
||||||
ByteStrings(ByteString1.fromString("ab"), ByteString1.fromString("cd")).slice(2, 2) should ===(ByteString(""))
|
ByteStrings(ByteString1.fromString("ab"), ByteString1.fromString("cd")).slice(2, 2) should ===(ByteString(""))
|
||||||
ByteStrings(ByteString1.fromString("ab"), ByteString1.fromString("cd")).slice(2, 3) should ===(ByteString("c"))
|
ByteStrings(ByteString1.fromString("ab"), ByteString1.fromString("cd")).slice(2, 3) should ===(ByteString("c"))
|
||||||
ByteStrings(ByteString1.fromString("ab"), ByteString1.fromString("cd")).slice(2, 4) should ===(ByteString("cd"))
|
ByteStrings(ByteString1.fromString("ab"), ByteString1.fromString("cd")).slice(2, 4) should ===(ByteString("cd"))
|
||||||
ByteStrings(ByteString1.fromString("ab"), ByteString1.fromString("cd")).slice(3, 4) should ===(ByteString("d"))
|
ByteStrings(ByteString1.fromString("ab"), ByteString1.fromString("cd")).slice(3, 4) should ===(ByteString("d"))
|
||||||
ByteStrings(ByteString1.fromString("ab"), ByteString1.fromString("cd")).slice(10, 100) should ===(ByteString(""))
|
// Can obtain expected results from 6 basic patterns
|
||||||
|
ByteStrings(ByteString1.fromString("ab"), ByteString1.fromString("cd")).slice(-10, 10) should ===(ByteString("abcd"))
|
||||||
|
ByteStrings(ByteString1.fromString("ab"), ByteString1.fromString("cd")).slice(-10, 0) should ===(ByteString(""))
|
||||||
|
ByteStrings(ByteString1.fromString("ab"), ByteString1.fromString("cd")).slice(-10, 4) should ===(ByteString("abcd"))
|
||||||
|
ByteStrings(ByteString1.fromString("ab"), ByteString1.fromString("cd")).slice(0, 4) should ===(ByteString("abcd"))
|
||||||
|
ByteStrings(ByteString1.fromString("ab"), ByteString1.fromString("cd")).slice(1, -2) should ===(ByteString(""))
|
||||||
|
ByteStrings(ByteString1.fromString("ab"), ByteString1.fromString("cd")).slice(0, 10) should ===(ByteString("abcd"))
|
||||||
|
ByteStrings(ByteString1.fromString("ab"), ByteString1.fromString("cd")).slice(-10, -100) should ===(ByteString(""))
|
||||||
|
ByteStrings(ByteString1.fromString("ab"), ByteString1.fromString("cd")).slice(-100, -10) should ===(ByteString(""))
|
||||||
|
|
||||||
|
ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("")).slice(1, -2) should ===(ByteString(""))
|
||||||
|
ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("")).slice(-10, -100) should ===(ByteString(""))
|
||||||
|
ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("")).slice(-100, -10) should ===(ByteString(""))
|
||||||
|
|
||||||
|
// various edge cases using raw ByteString1
|
||||||
|
ByteString1.fromString("cd").slice(100, 10) should ===(ByteString(""))
|
||||||
|
ByteString1.fromString("cd").slice(100, 1000) should ===(ByteString(""))
|
||||||
|
ByteString1.fromString("cd").slice(-10, -5) should ===(ByteString(""))
|
||||||
|
ByteString1.fromString("cd").slice(-2, -5) should ===(ByteString(""))
|
||||||
|
ByteString1.fromString("cd").slice(-2, 1) should ===(ByteString("c"))
|
||||||
|
ByteString1.fromString("abcd").slice(1, -1) should ===(ByteString(""))
|
||||||
|
|
||||||
|
// Get an empty if `from` is greater than `until`
|
||||||
|
ByteStrings(ByteString1.fromString("ab"), ByteString1.fromString("cd")).slice(4, 0) should ===(ByteString(""))
|
||||||
}
|
}
|
||||||
"dropRight" in {
|
"dropRight" in {
|
||||||
ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("")).dropRight(0) should ===(ByteString("a"))
|
ByteStrings(ByteString1.fromString("a"), ByteString1.fromString("")).dropRight(0) should ===(ByteString("a"))
|
||||||
|
|
|
||||||
|
|
@ -163,8 +163,8 @@ object ByteString {
|
||||||
else toByteString1.drop(n)
|
else toByteString1.drop(n)
|
||||||
|
|
||||||
override def slice(from: Int, until: Int): ByteString =
|
override def slice(from: Int, until: Int): ByteString =
|
||||||
if ((from == 0) && (until == length)) this
|
if (from <= 0 && until >= length) this
|
||||||
else if (from > length) ByteString.empty
|
else if (from >= length || until <= 0 || from >= until) ByteString.empty
|
||||||
else toByteString1.slice(from, until)
|
else toByteString1.slice(from, until)
|
||||||
|
|
||||||
private[akka] override def writeToOutputStream(os: ObjectOutputStream): Unit =
|
private[akka] override def writeToOutputStream(os: ObjectOutputStream): Unit =
|
||||||
|
|
@ -252,11 +252,8 @@ object ByteString {
|
||||||
if (n <= 0) ByteString.empty
|
if (n <= 0) ByteString.empty
|
||||||
else ByteString1(bytes, startIndex, Math.min(n, length))
|
else ByteString1(bytes, startIndex, Math.min(n, length))
|
||||||
|
|
||||||
override def slice(from: Int, until: Int): ByteString = {
|
override def slice(from: Int, until: Int): ByteString =
|
||||||
if (from <= 0 && until >= length) this // we can do < / > since we're Compact
|
drop(from).take(until - Math.max(0, from))
|
||||||
else if (until <= from) ByteString1.empty
|
|
||||||
else ByteString1(bytes, startIndex + from, until - from)
|
|
||||||
}
|
|
||||||
|
|
||||||
override def copyToBuffer(buffer: ByteBuffer): Int =
|
override def copyToBuffer(buffer: ByteBuffer): Int =
|
||||||
writeToBuffer(buffer)
|
writeToBuffer(buffer)
|
||||||
|
|
@ -466,7 +463,7 @@ object ByteString {
|
||||||
}
|
}
|
||||||
|
|
||||||
override def slice(from: Int, until: Int): ByteString =
|
override def slice(from: Int, until: Int): ByteString =
|
||||||
if ((from == 0) && (until == length)) this
|
if (from <= 0 && until >= length) this
|
||||||
else if (from > length || until <= from) ByteString.empty
|
else if (from > length || until <= from) ByteString.empty
|
||||||
else drop(from).dropRight(length - until)
|
else drop(from).dropRight(length - until)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue