-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rewrite Blob to facilitate interop with other libs #1211
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
800bcb3
Rewrite Blob to facilitate interop with other libs
Baccata 1be8ffb
Further changes to Blob
Baccata 7bd1342
Remove concat smart constructor
Baccata aabf826
Add Blob-related unit-tests, fix bugs
Baccata b50d8d4
Aesthetic improvements
Baccata File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ package smithy4s | |
import munit._ | ||
|
||
import java.nio.ByteBuffer | ||
|
||
import java.io.ByteArrayOutputStream | ||
class BlobSpec() extends FunSuite { | ||
|
||
test("sameBytesAs works across data structures") { | ||
|
@@ -37,13 +37,6 @@ class BlobSpec() extends FunSuite { | |
) | ||
} | ||
|
||
test("ByteBufferBlob.toArray is idempotent, instantiation-wise") { | ||
val blob = Blob(ByteBuffer.wrap("foo".getBytes)) | ||
assert(blob.toArray != null) | ||
assert(blob.toArray.eq(blob.toArray)) | ||
assertEquals(Blob(blob.toArray), Blob("foo")) | ||
} | ||
|
||
test("ByteArrayBlob.hashcode is consistent") { | ||
def makeBlob(str: String) = Blob(str.getBytes) | ||
val blob1 = makeBlob("foo") | ||
|
@@ -62,4 +55,105 @@ class BlobSpec() extends FunSuite { | |
assertNotEquals(blob1.hashCode, blob3.hashCode) | ||
} | ||
|
||
test("Concat works as expected") { | ||
val blob = Blob("foo") ++ Blob("bar") | ||
assertEquals(blob.size, 6) | ||
assertEquals(blob(2), 'o'.toByte) | ||
assertEquals(blob(4), 'a'.toByte) | ||
java.util.Arrays | ||
.equals(blob.toArray, "foo".getBytes ++ "bar".getBytes()) | ||
} | ||
|
||
val all = List( | ||
"Queue" -> (Blob("foo") ++ Blob("bar")), | ||
"Array" -> Blob("foobar"), | ||
"Buffer" -> Blob(ByteBuffer.wrap("foobar".getBytes())) | ||
) | ||
|
||
for { | ||
x <- all | ||
y <- all | ||
} { | ||
test(s"${x._1} and ${y._1} : same bytes") { | ||
assert(x._2.sameBytesAs(y._2)) | ||
} | ||
} | ||
|
||
all.foreach { case (name, data) => | ||
test(s"$name: size") { | ||
assertEquals(data.size, 6) | ||
} | ||
|
||
test(s"$name: index access") { | ||
assertEquals(data(2), 'o'.toByte) | ||
} | ||
|
||
test(s"$name: out of bounds access") { | ||
try { | ||
val _ = data(6) | ||
fail("expected exception") | ||
} catch { | ||
case _: IndexOutOfBoundsException => () | ||
} | ||
} | ||
|
||
test(s"$name: utf8String") { | ||
assertEquals(data.toUTF8String, "foobar") | ||
} | ||
|
||
test(s"$name: toArraySliceBlob") { | ||
assertEquals[Blob, Blob]( | ||
data.toArraySliceBlob, | ||
Blob.slice("foobar".getBytes(), 0, 6) | ||
) | ||
} | ||
|
||
test(s"$name: copyToArray") { | ||
val target = Array.fill[Byte](6)(0) | ||
data.copyToArray(target, 0, 0, data.size) | ||
assert(target.sameElements("foobar".getBytes())) | ||
} | ||
|
||
test(s"$name: copyToBuffer") { | ||
val target = ByteBuffer.wrap(Array.fill[Byte](6)(0)) | ||
data.copyToBuffer(target, 0, data.size) | ||
assert(target.array.sameElements("foobar".getBytes())) | ||
} | ||
|
||
test(s"$name: copyToStream") { | ||
val stream = new ByteArrayOutputStream() | ||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the other day I discovered There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. I was under the impression it was a 2.13+ only thing |
||
data.copyToStream(stream, 0, data.size) | ||
assert(stream.toByteArray().sameElements("foobar".getBytes())) | ||
} finally { | ||
stream.close() | ||
} | ||
} | ||
} | ||
|
||
test("asByteBufferUnsafe") { | ||
val arr = "Hello, world!".getBytes | ||
assert(arr eq Blob.view(ByteBuffer.wrap(arr)).asByteBufferUnsafe.array) | ||
assert( | ||
arr eq Blob.view(ByteBuffer.wrap(arr)).asByteBufferUnsafe.array | ||
) | ||
} | ||
|
||
test("asByteBufferUnsafe has independent position+limit") { | ||
val bv = Blob.view(ByteBuffer.wrap("Hello, world!".getBytes)) | ||
val bb1 = bv.asByteBufferUnsafe | ||
assertEquals(bb1.position(), 0) | ||
assertEquals(bb1.limit(), 13) | ||
val bb2 = bv.asByteBufferUnsafe | ||
bb2.position(1) | ||
bb2.limit(2) | ||
assertEquals(bb1.position(), 0) | ||
assertEquals(bb1.limit(), 13) | ||
} | ||
|
||
test("Array slice: access") { | ||
val slice = Blob.slice("foobar".getBytes(), 1, 4) | ||
assert(slice.sameBytesAs(Blob("ooba"))) | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks ! b50d8d4