-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
ByteStringSpec.scala
526 lines (467 loc) · 23.8 KB
/
ByteStringSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
package akka.util
import org.scalatest.WordSpec
import org.scalatest.matchers.MustMatchers
import org.scalatest.BeforeAndAfterAll
import org.scalatest.prop.Checkers
import org.scalacheck._
import org.scalacheck.Arbitrary._
import org.scalacheck.Prop._
import org.scalacheck.Gen._
import scala.collection.mutable.Builder
import java.nio.{ ByteBuffer }
import java.nio.ByteOrder, ByteOrder.{ BIG_ENDIAN, LITTLE_ENDIAN }
import java.lang.Float.floatToRawIntBits
import java.lang.Double.doubleToRawLongBits
class ByteStringSpec extends WordSpec with MustMatchers with Checkers {
def genSimpleByteString(min: Int, max: Int) = for {
n ← choose(min, max)
b ← Gen.containerOfN[Array, Byte](n, arbitrary[Byte])
from ← choose(0, b.length)
until ← choose(from, b.length)
} yield ByteString(b).slice(from, until)
implicit val arbitraryByteString: Arbitrary[ByteString] = Arbitrary {
Gen.sized { s ⇒
for {
chunks ← choose(0, s)
bytes ← listOfN(chunks, genSimpleByteString(1, s / (chunks max 1)))
} yield (ByteString.empty /: bytes)(_ ++ _)
}
}
type ByteStringSlice = (ByteString, Int, Int)
implicit val arbitraryByteStringSlice: Arbitrary[ByteStringSlice] = Arbitrary {
for {
xs ← arbitraryByteString.arbitrary
from ← choose(0, xs.length)
until ← choose(from, xs.length)
} yield (xs, from, until)
}
type ArraySlice[A] = (Array[A], Int, Int)
def arbSlice[A](arbArray: Arbitrary[Array[A]]): Arbitrary[ArraySlice[A]] = Arbitrary {
for {
xs ← arbArray.arbitrary
from ← choose(0, xs.length)
until ← choose(from, xs.length)
} yield (xs, from, until)
}
val arbitraryByteArray: Arbitrary[Array[Byte]] = Arbitrary { Gen.sized { n ⇒ Gen.containerOfN[Array, Byte](n, arbitrary[Byte]) } }
implicit val arbitraryByteArraySlice: Arbitrary[ArraySlice[Byte]] = arbSlice(arbitraryByteArray)
val arbitraryShortArray: Arbitrary[Array[Short]] = Arbitrary { Gen.sized { n ⇒ Gen.containerOfN[Array, Short](n, arbitrary[Short]) } }
implicit val arbitraryShortArraySlice: Arbitrary[ArraySlice[Short]] = arbSlice(arbitraryShortArray)
val arbitraryIntArray: Arbitrary[Array[Int]] = Arbitrary { Gen.sized { n ⇒ Gen.containerOfN[Array, Int](n, arbitrary[Int]) } }
implicit val arbitraryIntArraySlice: Arbitrary[ArraySlice[Int]] = arbSlice(arbitraryIntArray)
val arbitraryLongArray: Arbitrary[Array[Long]] = Arbitrary { Gen.sized { n ⇒ Gen.containerOfN[Array, Long](n, arbitrary[Long]) } }
implicit val arbitraryLongArraySlice: Arbitrary[ArraySlice[Long]] = arbSlice(arbitraryLongArray)
val arbitraryFloatArray: Arbitrary[Array[Float]] = Arbitrary { Gen.sized { n ⇒ Gen.containerOfN[Array, Float](n, arbitrary[Float]) } }
implicit val arbitraryFloatArraySlice: Arbitrary[ArraySlice[Float]] = arbSlice(arbitraryFloatArray)
val arbitraryDoubleArray: Arbitrary[Array[Double]] = Arbitrary { Gen.sized { n ⇒ Gen.containerOfN[Array, Double](n, arbitrary[Double]) } }
implicit val arbitraryDoubleArraySlice: Arbitrary[ArraySlice[Double]] = arbSlice(arbitraryDoubleArray)
def likeVector(bs: ByteString)(body: IndexedSeq[Byte] ⇒ Any): Boolean = {
val vec = Vector(bs: _*)
body(bs) == body(vec)
}
def likeVectors(bsA: ByteString, bsB: ByteString)(body: (IndexedSeq[Byte], IndexedSeq[Byte]) ⇒ Any): Boolean = {
val vecA = Vector(bsA: _*)
val vecB = Vector(bsB: _*)
body(bsA, bsB) == body(vecA, vecB)
}
def likeVecIt(bs: ByteString)(body: BufferedIterator[Byte] ⇒ Any, strict: Boolean = true): Boolean = {
val bsIterator = bs.iterator
val vecIterator = Vector(bs: _*).iterator.buffered
(body(bsIterator) == body(vecIterator)) &&
(!strict || (bsIterator.toSeq == vecIterator.toSeq))
}
def likeVecIts(a: ByteString, b: ByteString)(body: (BufferedIterator[Byte], BufferedIterator[Byte]) ⇒ Any, strict: Boolean = true): Boolean = {
val (bsAIt, bsBIt) = (a.iterator, b.iterator)
val (vecAIt, vecBIt) = (Vector(a: _*).iterator.buffered, Vector(b: _*).iterator.buffered)
(body(bsAIt, bsBIt) == body(vecAIt, vecBIt)) &&
(!strict || (bsAIt.toSeq, bsBIt.toSeq) == (vecAIt.toSeq, vecBIt.toSeq))
}
def likeVecBld(body: Builder[Byte, _] ⇒ Unit): Boolean = {
val bsBuilder = ByteString.newBuilder
val vecBuilder = Vector.newBuilder[Byte]
body(bsBuilder)
body(vecBuilder)
bsBuilder.result == vecBuilder.result
}
def testShortDecoding(slice: ByteStringSlice, byteOrder: ByteOrder): Boolean = {
val elemSize = 2
val (bytes, from, until) = slice
val (n, a, b) = (bytes.length / elemSize, from / elemSize, until / elemSize)
val reference = Array.ofDim[Short](n)
bytes.asByteBuffer.order(byteOrder).asShortBuffer.get(reference, 0, n)
val input = bytes.iterator
val decoded = Array.ofDim[Short](n)
for (i ← 0 to a - 1) decoded(i) = input.getShort(byteOrder)
input.getShorts(decoded, a, b - a)(byteOrder)
for (i ← b to n - 1) decoded(i) = input.getShort(byteOrder)
(decoded.toSeq == reference.toSeq) && (input.toSeq == bytes.drop(n * elemSize))
}
def testIntDecoding(slice: ByteStringSlice, byteOrder: ByteOrder): Boolean = {
val elemSize = 4
val (bytes, from, until) = slice
val (n, a, b) = (bytes.length / elemSize, from / elemSize, until / elemSize)
val reference = Array.ofDim[Int](n)
bytes.asByteBuffer.order(byteOrder).asIntBuffer.get(reference, 0, n)
val input = bytes.iterator
val decoded = Array.ofDim[Int](n)
for (i ← 0 to a - 1) decoded(i) = input.getInt(byteOrder)
input.getInts(decoded, a, b - a)(byteOrder)
for (i ← b to n - 1) decoded(i) = input.getInt(byteOrder)
(decoded.toSeq == reference.toSeq) && (input.toSeq == bytes.drop(n * elemSize))
}
def testLongDecoding(slice: ByteStringSlice, byteOrder: ByteOrder): Boolean = {
val elemSize = 8
val (bytes, from, until) = slice
val (n, a, b) = (bytes.length / elemSize, from / elemSize, until / elemSize)
val reference = Array.ofDim[Long](n)
bytes.asByteBuffer.order(byteOrder).asLongBuffer.get(reference, 0, n)
val input = bytes.iterator
val decoded = Array.ofDim[Long](n)
for (i ← 0 to a - 1) decoded(i) = input.getLong(byteOrder)
input.getLongs(decoded, a, b - a)(byteOrder)
for (i ← b to n - 1) decoded(i) = input.getLong(byteOrder)
(decoded.toSeq == reference.toSeq) && (input.toSeq == bytes.drop(n * elemSize))
}
def testFloatDecoding(slice: ByteStringSlice, byteOrder: ByteOrder): Boolean = {
val elemSize = 4
val (bytes, from, until) = slice
val (n, a, b) = (bytes.length / elemSize, from / elemSize, until / elemSize)
val reference = Array.ofDim[Float](n)
bytes.asByteBuffer.order(byteOrder).asFloatBuffer.get(reference, 0, n)
val input = bytes.iterator
val decoded = Array.ofDim[Float](n)
for (i ← 0 to a - 1) decoded(i) = input.getFloat(byteOrder)
input.getFloats(decoded, a, b - a)(byteOrder)
for (i ← b to n - 1) decoded(i) = input.getFloat(byteOrder)
((decoded.toSeq map floatToRawIntBits) == (reference.toSeq map floatToRawIntBits)) &&
(input.toSeq == bytes.drop(n * elemSize))
}
def testDoubleDecoding(slice: ByteStringSlice, byteOrder: ByteOrder): Boolean = {
val elemSize = 8
val (bytes, from, until) = slice
val (n, a, b) = (bytes.length / elemSize, from / elemSize, until / elemSize)
val reference = Array.ofDim[Double](n)
bytes.asByteBuffer.order(byteOrder).asDoubleBuffer.get(reference, 0, n)
val input = bytes.iterator
val decoded = Array.ofDim[Double](n)
for (i ← 0 to a - 1) decoded(i) = input.getDouble(byteOrder)
input.getDoubles(decoded, a, b - a)(byteOrder)
for (i ← b to n - 1) decoded(i) = input.getDouble(byteOrder)
((decoded.toSeq map doubleToRawLongBits) == (reference.toSeq map doubleToRawLongBits)) &&
(input.toSeq == bytes.drop(n * elemSize))
}
def testShortEncoding(slice: ArraySlice[Short], byteOrder: ByteOrder): Boolean = {
val elemSize = 2
val (data, from, until) = slice
val reference = Array.ofDim[Byte](data.length * elemSize)
ByteBuffer.wrap(reference).order(byteOrder).asShortBuffer.put(data)
val builder = ByteString.newBuilder
for (i ← 0 to from - 1) builder.putShort(data(i))(byteOrder)
builder.putShorts(data, from, until - from)(byteOrder)
for (i ← until to data.length - 1) builder.putShort(data(i))(byteOrder)
reference.toSeq == builder.result
}
def testIntEncoding(slice: ArraySlice[Int], byteOrder: ByteOrder): Boolean = {
val elemSize = 4
val (data, from, until) = slice
val reference = Array.ofDim[Byte](data.length * elemSize)
ByteBuffer.wrap(reference).order(byteOrder).asIntBuffer.put(data)
val builder = ByteString.newBuilder
for (i ← 0 to from - 1) builder.putInt(data(i))(byteOrder)
builder.putInts(data, from, until - from)(byteOrder)
for (i ← until to data.length - 1) builder.putInt(data(i))(byteOrder)
reference.toSeq == builder.result
}
def testLongEncoding(slice: ArraySlice[Long], byteOrder: ByteOrder): Boolean = {
val elemSize = 8
val (data, from, until) = slice
val reference = Array.ofDim[Byte](data.length * elemSize)
ByteBuffer.wrap(reference).order(byteOrder).asLongBuffer.put(data)
val builder = ByteString.newBuilder
for (i ← 0 to from - 1) builder.putLong(data(i))(byteOrder)
builder.putLongs(data, from, until - from)(byteOrder)
for (i ← until to data.length - 1) builder.putLong(data(i))(byteOrder)
reference.toSeq == builder.result
}
def testFloatEncoding(slice: ArraySlice[Float], byteOrder: ByteOrder): Boolean = {
val elemSize = 4
val (data, from, until) = slice
val reference = Array.ofDim[Byte](data.length * elemSize)
ByteBuffer.wrap(reference).order(byteOrder).asFloatBuffer.put(data)
val builder = ByteString.newBuilder
for (i ← 0 to from - 1) builder.putFloat(data(i))(byteOrder)
builder.putFloats(data, from, until - from)(byteOrder)
for (i ← until to data.length - 1) builder.putFloat(data(i))(byteOrder)
reference.toSeq == builder.result
}
def testDoubleEncoding(slice: ArraySlice[Double], byteOrder: ByteOrder): Boolean = {
val elemSize = 8
val (data, from, until) = slice
val reference = Array.ofDim[Byte](data.length * elemSize)
ByteBuffer.wrap(reference).order(byteOrder).asDoubleBuffer.put(data)
val builder = ByteString.newBuilder
for (i ← 0 to from - 1) builder.putDouble(data(i))(byteOrder)
builder.putDoubles(data, from, until - from)(byteOrder)
for (i ← until to data.length - 1) builder.putDouble(data(i))(byteOrder)
reference.toSeq == builder.result
}
"A ByteString" must {
"have correct size" when {
"concatenating" in { check((a: ByteString, b: ByteString) ⇒ (a ++ b).size == a.size + b.size) }
"dropping" in { check((a: ByteString, b: ByteString) ⇒ (a ++ b).drop(b.size).size == a.size) }
}
"be sequential" when {
"taking" in { check((a: ByteString, b: ByteString) ⇒ (a ++ b).take(a.size) == a) }
"dropping" in { check((a: ByteString, b: ByteString) ⇒ (a ++ b).drop(a.size) == b) }
}
"be equal to the original" when {
"compacting" in { check { xs: ByteString ⇒ val ys = xs.compact; (xs == ys) && ys.isCompact } }
"recombining" in {
check { (xs: ByteString, from: Int, until: Int) ⇒
val (tmp, c) = xs.splitAt(until)
val (a, b) = tmp.splitAt(from)
(a ++ b ++ c) == xs
}
}
}
"behave as expected" when {
"created from and decoding to String" in { check { s: String ⇒ ByteString(s, "UTF-8").decodeString("UTF-8") == s } }
"compacting" in {
check { a: ByteString ⇒
val wasCompact = a.isCompact
val b = a.compact
((!wasCompact) || (b eq a)) &&
(b == a) &&
b.isCompact &&
(b.compact eq b)
}
}
"asByteBuffers" in {
check { (a: ByteString) ⇒ if (a.isCompact) a.asByteBuffers.size == 1 && a.asByteBuffers.head == a.asByteBuffer else a.asByteBuffers.size > 0 }
check { (a: ByteString) ⇒ a.asByteBuffers.foldLeft(ByteString.empty) { (bs, bb) ⇒ bs ++ ByteString(bb) } == a }
check { (a: ByteString) ⇒ a.asByteBuffers.forall(_.isReadOnly) }
check { (a: ByteString) ⇒
import scala.collection.JavaConverters.iterableAsScalaIterableConverter;
a.asByteBuffers.zip(a.getByteBuffers().asScala).forall(x ⇒ x._1 == x._2)
}
}
}
"behave like a Vector" when {
"concatenating" in { check { (a: ByteString, b: ByteString) ⇒ likeVectors(a, b) { _ ++ _ } } }
"calling apply" in {
check { slice: ByteStringSlice ⇒
slice match {
case (xs, i1, i2) ⇒ likeVector(xs) { seq ⇒
(if ((i1 >= 0) && (i1 < seq.length)) seq(i1) else 0,
if ((i2 >= 0) && (i2 < seq.length)) seq(i2) else 0)
}
}
}
}
"calling head" in { check { a: ByteString ⇒ a.isEmpty || likeVector(a) { _.head } } }
"calling tail" in { check { a: ByteString ⇒ a.isEmpty || likeVector(a) { _.tail } } }
"calling last" in { check { a: ByteString ⇒ a.isEmpty || likeVector(a) { _.last } } }
"calling init" in { check { a: ByteString ⇒ a.isEmpty || likeVector(a) { _.init } } }
"calling length" in { check { a: ByteString ⇒ likeVector(a) { _.length } } }
"calling span" in { check { (a: ByteString, b: Byte) ⇒ likeVector(a)({ _.span(_ != b) match { case (a, b) ⇒ (a, b) } }) } }
"calling takeWhile" in { check { (a: ByteString, b: Byte) ⇒ likeVector(a)({ _.takeWhile(_ != b) }) } }
"calling dropWhile" in { check { (a: ByteString, b: Byte) ⇒ likeVector(a) { _.dropWhile(_ != b) } } }
"calling indexWhere" in { check { (a: ByteString, b: Byte) ⇒ likeVector(a) { _.indexWhere(_ == b) } } }
"calling indexOf" in { check { (a: ByteString, b: Byte) ⇒ likeVector(a) { _.indexOf(b) } } }
"calling foreach" in { check { a: ByteString ⇒ likeVector(a) { it ⇒ var acc = 0; it foreach { acc += _ }; acc } } }
"calling foldLeft" in { check { a: ByteString ⇒ likeVector(a) { _.foldLeft(0) { _ + _ } } } }
"calling toArray" in { check { a: ByteString ⇒ likeVector(a) { _.toArray.toSeq } } }
"calling slice" in {
check { slice: ByteStringSlice ⇒
slice match {
case (xs, from, until) ⇒ likeVector(xs)({
_.slice(from, until)
})
}
}
}
"calling take and drop" in {
check { slice: ByteStringSlice ⇒
slice match {
case (xs, from, until) ⇒ likeVector(xs)({
_.drop(from).take(until - from)
})
}
}
}
"calling copyToArray" in {
check { slice: ByteStringSlice ⇒
slice match {
case (xs, from, until) ⇒ likeVector(xs)({ it ⇒
val array = Array.ofDim[Byte](xs.length)
it.slice(from, until).copyToArray(array, from, until)
array.toSeq
})
}
}
}
}
}
"A ByteStringIterator" must {
"behave like a buffered Vector Iterator" when {
"concatenating" in { check { (a: ByteString, b: ByteString) ⇒ likeVecIts(a, b) { (a, b) ⇒ (a ++ b).toSeq } } }
"calling head" in { check { a: ByteString ⇒ a.isEmpty || likeVecIt(a) { _.head } } }
"calling next" in { check { a: ByteString ⇒ a.isEmpty || likeVecIt(a) { _.next() } } }
"calling hasNext" in { check { a: ByteString ⇒ likeVecIt(a) { _.hasNext } } }
"calling length" in { check { a: ByteString ⇒ likeVecIt(a) { _.length } } }
"calling duplicate" in { check { a: ByteString ⇒ likeVecIt(a)({ _.duplicate match { case (a, b) ⇒ (a.toSeq, b.toSeq) } }, strict = false) } }
// Have to used toList instead of toSeq here, iterator.span (new in
// Scala-2.9) seems to be broken in combination with toSeq for the
// scala.collection default Iterator (see Scala issue SI-5838).
"calling span" in { check { (a: ByteString, b: Byte) ⇒ likeVecIt(a)({ _.span(_ != b) match { case (a, b) ⇒ (a.toList, b.toList) } }, strict = false) } }
"calling takeWhile" in { check { (a: ByteString, b: Byte) ⇒ likeVecIt(a)({ _.takeWhile(_ != b).toSeq }, strict = false) } }
"calling dropWhile" in { check { (a: ByteString, b: Byte) ⇒ likeVecIt(a) { _.dropWhile(_ != b).toSeq } } }
"calling indexWhere" in { check { (a: ByteString, b: Byte) ⇒ likeVecIt(a) { _.indexWhere(_ == b) } } }
"calling indexOf" in { check { (a: ByteString, b: Byte) ⇒ likeVecIt(a) { _.indexOf(b) } } }
"calling toSeq" in { check { a: ByteString ⇒ likeVecIt(a) { _.toSeq } } }
"calling foreach" in { check { a: ByteString ⇒ likeVecIt(a) { it ⇒ var acc = 0; it foreach { acc += _ }; acc } } }
"calling foldLeft" in { check { a: ByteString ⇒ likeVecIt(a) { _.foldLeft(0) { _ + _ } } } }
"calling toArray" in { check { a: ByteString ⇒ likeVecIt(a) { _.toArray.toSeq } } }
"calling slice" in {
check { slice: ByteStringSlice ⇒
slice match {
case (xs, from, until) ⇒ likeVecIt(xs)({
_.slice(from, until).toSeq
}, strict = false)
}
}
}
"calling take and drop" in {
check { slice: ByteStringSlice ⇒
slice match {
case (xs, from, until) ⇒ likeVecIt(xs)({
_.drop(from).take(until - from).toSeq
}, strict = false)
}
}
}
"calling copyToArray" in {
check { slice: ByteStringSlice ⇒
slice match {
case (xs, from, until) ⇒ likeVecIt(xs)({ it ⇒
val array = Array.ofDim[Byte](xs.length)
it.slice(from, until).copyToArray(array, from, until)
array.toSeq
}, strict = false)
}
}
}
}
"function as expected" when {
"getting Bytes, using getByte and getBytes" in {
// mixing getByte and getBytes here for more rigorous testing
check { slice: ByteStringSlice ⇒
val (bytes, from, until) = slice
val input = bytes.iterator
val output = Array.ofDim[Byte](bytes.length)
for (i ← 0 to from - 1) output(i) = input.getByte
input.getBytes(output, from, until - from)
for (i ← until to bytes.length - 1) output(i) = input.getByte
(output.toSeq == bytes) && (input.isEmpty)
}
}
"getting Bytes, using the InputStream wrapper" in {
// combining skip and both read methods here for more rigorous testing
check { slice: ByteStringSlice ⇒
val (bytes, from, until) = slice
val a = (0 max from) min bytes.length
val b = (a max until) min bytes.length
val input = bytes.iterator
val output = Array.ofDim[Byte](bytes.length)
input.asInputStream.skip(a)
val toRead = b - a
var (nRead, eof) = (0, false)
while ((nRead < toRead) && !eof) {
val n = input.asInputStream.read(output, a + nRead, toRead - nRead)
if (n == -1) eof = true
else nRead += n
}
if (eof) throw new RuntimeException("Unexpected EOF")
for (i ← b to bytes.length - 1) output(i) = input.asInputStream.read().toByte
(output.toSeq.drop(a) == bytes.drop(a)) &&
(input.asInputStream.read() == -1) &&
((output.length < 1) || (input.asInputStream.read(output, 0, 1) == -1))
}
}
"calling copyToBuffer" in {
check { bytes: ByteString ⇒
import java.nio.ByteBuffer
val buffer = ByteBuffer.allocate(bytes.size)
bytes.copyToBuffer(buffer)
buffer.flip()
val array = Array.ofDim[Byte](bytes.size)
buffer.get(array)
bytes == array.toSeq
}
}
}
"decode data correctly" when {
"decoding Short in big-endian" in { check { slice: ByteStringSlice ⇒ testShortDecoding(slice, BIG_ENDIAN) } }
"decoding Short in little-endian" in { check { slice: ByteStringSlice ⇒ testShortDecoding(slice, LITTLE_ENDIAN) } }
"decoding Int in big-endian" in { check { slice: ByteStringSlice ⇒ testIntDecoding(slice, BIG_ENDIAN) } }
"decoding Int in little-endian" in { check { slice: ByteStringSlice ⇒ testIntDecoding(slice, LITTLE_ENDIAN) } }
"decoding Long in big-endian" in { check { slice: ByteStringSlice ⇒ testLongDecoding(slice, BIG_ENDIAN) } }
"decoding Long in little-endian" in { check { slice: ByteStringSlice ⇒ testLongDecoding(slice, LITTLE_ENDIAN) } }
"decoding Float in big-endian" in { check { slice: ByteStringSlice ⇒ testFloatDecoding(slice, BIG_ENDIAN) } }
"decoding Float in little-endian" in { check { slice: ByteStringSlice ⇒ testFloatDecoding(slice, LITTLE_ENDIAN) } }
"decoding Double in big-endian" in { check { slice: ByteStringSlice ⇒ testDoubleDecoding(slice, BIG_ENDIAN) } }
"decoding Double in little-endian" in { check { slice: ByteStringSlice ⇒ testDoubleDecoding(slice, LITTLE_ENDIAN) } }
}
}
"A ByteStringBuilder" must {
"function like a VectorBuilder" when {
"adding various contents using ++= and +=" in {
check { (array1: Array[Byte], array2: Array[Byte], bs1: ByteString, bs2: ByteString, bs3: ByteString) ⇒
likeVecBld { builder ⇒
builder ++= array1
bs1 foreach { b ⇒ builder += b }
builder ++= bs2
bs3 foreach { b ⇒ builder += b }
builder ++= Vector(array2: _*)
}
}
}
}
"function as expected" when {
"putting Bytes, using putByte and putBytes" in {
// mixing putByte and putBytes here for more rigorous testing
check { slice: ArraySlice[Byte] ⇒
val (data, from, until) = slice
val builder = ByteString.newBuilder
for (i ← 0 to from - 1) builder.putByte(data(i))
builder.putBytes(data, from, until - from)
for (i ← until to data.length - 1) builder.putByte(data(i))
data.toSeq == builder.result
}
}
"putting Bytes, using the OutputStream wrapper" in {
// mixing the write methods here for more rigorous testing
check { slice: ArraySlice[Byte] ⇒
val (data, from, until) = slice
val builder = ByteString.newBuilder
for (i ← 0 to from - 1) builder.asOutputStream.write(data(i).toInt)
builder.asOutputStream.write(data, from, until - from)
for (i ← until to data.length - 1) builder.asOutputStream.write(data(i).toInt)
data.toSeq == builder.result
}
}
}
"encode data correctly" when {
"encoding Short in big-endian" in { check { slice: ArraySlice[Short] ⇒ testShortEncoding(slice, BIG_ENDIAN) } }
"encoding Short in little-endian" in { check { slice: ArraySlice[Short] ⇒ testShortEncoding(slice, LITTLE_ENDIAN) } }
"encoding Int in big-endian" in { check { slice: ArraySlice[Int] ⇒ testIntEncoding(slice, BIG_ENDIAN) } }
"encoding Int in little-endian" in { check { slice: ArraySlice[Int] ⇒ testIntEncoding(slice, LITTLE_ENDIAN) } }
"encoding Long in big-endian" in { check { slice: ArraySlice[Long] ⇒ testLongEncoding(slice, BIG_ENDIAN) } }
"encoding Long in little-endian" in { check { slice: ArraySlice[Long] ⇒ testLongEncoding(slice, LITTLE_ENDIAN) } }
"encoding Float in big-endian" in { check { slice: ArraySlice[Float] ⇒ testFloatEncoding(slice, BIG_ENDIAN) } }
"encoding Float in little-endian" in { check { slice: ArraySlice[Float] ⇒ testFloatEncoding(slice, LITTLE_ENDIAN) } }
"encoding Double in big-endian" in { check { slice: ArraySlice[Double] ⇒ testDoubleEncoding(slice, BIG_ENDIAN) } }
"encoding Double in little-endian" in { check { slice: ArraySlice[Double] ⇒ testDoubleEncoding(slice, LITTLE_ENDIAN) } }
}
}
}