Skip to content

Commit

Permalink
Merge pull request #14 from stripe/thomas-dict-off-by-1
Browse files Browse the repository at this point in the history
Fix off-by-1 error in DenseArrayLayout dict encoding.
  • Loading branch information
erik-stripe committed Jun 2, 2016
2 parents dc3cb13 + 0b05dfc commit e09f743
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ sealed abstract class DenseArrayLayout[A: ClassTag](

case DenseArrayLayout.ByteDictionaryEncoding =>
val dictLen = in.readInt()
require(dictLen <= 255)
require(dictLen <= 256)
val dict: Array[A] = new Array[A](dictLen)
readArray(in, dict)
val encoding: Array[Byte] = new Array[Byte](in.readInt())
Expand Down
19 changes: 15 additions & 4 deletions bonsai-core/src/test/scala/com/stripe/bonsai/LayoutSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ import org.scalacheck.Arbitrary._
import com.stripe.bonsai.layout._

class LayoutSpec extends WordSpec with Matchers with Checkers {
private[this] def roundTrip[A: Layout](vec: Vec[A]): Vec[A] = {
val baos = new ByteArrayOutputStream()
Layout[A].write(vec, new DataOutputStream(baos))
Layout[A].read(new DataInputStream(new ByteArrayInputStream(baos.toByteArray)))
}

"DenseArrayLayout" should {
"not use byte dict encoding if there are 256 unique values" in {
val xs: Seq[Double] = Seq.tabulate(256)(_.toDouble)
val vec: Vec[Double] = Vec[Double](xs: _*)
roundTrip(vec) == vec
}
}

def layoutCheck[A: Arbitrary: Layout: ClassTag](name: String): Unit = {
s"Vec[$name].equals" should {
"use structural equality" in {
Expand Down Expand Up @@ -61,10 +75,7 @@ class LayoutSpec extends WordSpec with Matchers with Checkers {
"round-trip through read" in {
Prop.forAll { (xs: Vector[A]) =>
val vec1 = Layout[A].newBuilder.++=(xs).result
val baos = new ByteArrayOutputStream()
Layout[A].write(vec1, new DataOutputStream(baos))
val vec2 = Layout[A].read(new DataInputStream(new ByteArrayInputStream(baos.toByteArray)))

val vec2 = roundTrip(vec1)
vec1 == vec2
}
}
Expand Down

0 comments on commit e09f743

Please sign in to comment.