Skip to content

Commit

Permalink
Small integer support
Browse files Browse the repository at this point in the history
  • Loading branch information
ancapdev committed Jul 17, 2022
1 parent d9b24e6 commit 5dd8e4f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LightBSON"
uuid = "a4a7f996-b3a6-4de6-b9db-2fa5f350df41"
authors = ["Christian Rorvik <christian.rorvik@gmail.com>"]
version = "0.2.11"
version = "0.2.12"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Expand Down
5 changes: 5 additions & 0 deletions src/representations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,17 @@ end
bson_representation_convert(DefaultBSONConversions(), T, x)
end

const SmallInt = Union{Int8, UInt8, Int16, UInt16}

@inline bson_representation_type(::NumericBSONConversions, ::Type{<:SmallInt}) = Int32
@inline bson_representation_type(::NumericBSONConversions, ::Type{UInt32}) = Int32
@inline bson_representation_type(::NumericBSONConversions, ::Type{UInt64}) = Int64
@inline bson_representation_type(::NumericBSONConversions, ::Type{Float32}) = Float64

@inline bson_representation_convert(::NumericBSONConversions, ::Type{Float64}, x::Float32) = Float64(x)
@inline bson_representation_convert(::NumericBSONConversions, ::Type{Float32}, x::Float64) = Float32(x)
@inline bson_representation_convert(::NumericBSONConversions, ::Type{Int32}, x::SmallInt) = Int32(x)
@inline bson_representation_convert(::NumericBSONConversions, ::Type{T}, x::Int32) where T <: SmallInt = T(x)
@inline bson_representation_convert(::NumericBSONConversions, ::Type{Int32}, x::UInt32) = reinterpret(Int32, x)
@inline bson_representation_convert(::NumericBSONConversions, ::Type{UInt32}, x::Int32) = reinterpret(UInt32, x)
@inline bson_representation_convert(::NumericBSONConversions, ::Type{Int64}, x::UInt64) = reinterpret(Int64, x)
Expand Down
16 changes: 5 additions & 11 deletions test/representation_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,16 @@ end
@test BSONReader(buf, StrictBSONValidator())["y"][Sockets.InetAddr{IPv6}] == y
end

@testset "Unsigned integers" begin
@testset "Integer $T" for T in [Int8, UInt8, Int16, UInt16, UInt32, UInt64]
buf = empty!(fill(0xff, 1000))
writer = BSONWriter(buf, NumericBSONConversions())
x1 = UInt64(0xffff_ffff_ffff_ffff)
x2 = UInt64(123)
y1 = UInt32(0xffff_ffff)
y2 = UInt32(123)
x1 = typemin(T)
x2 = typemax(T)
writer["x1"] = x1
writer["x2"] = x2
writer["y1"] = y1
writer["y2"] = y2
close(writer)
@test BSONReader(buf, StrictBSONValidator(), NumericBSONConversions())["x1"][UInt64] == x1
@test BSONReader(buf, StrictBSONValidator(), NumericBSONConversions())["x2"][UInt64] == x2
@test BSONReader(buf, StrictBSONValidator(), NumericBSONConversions())["y1"][UInt32] == y1
@test BSONReader(buf, StrictBSONValidator(), NumericBSONConversions())["y2"][UInt32] == y2
@test BSONReader(buf, StrictBSONValidator(), NumericBSONConversions())["x1"][T] == x1
@test BSONReader(buf, StrictBSONValidator(), NumericBSONConversions())["x2"][T] == x2
end

@testset "Float32" begin
Expand Down

2 comments on commit 5dd8e4f

@ancapdev
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/64396

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.12 -m "<description of version>" 5dd8e4fc7fd62daf8875e52146a95bea41e793af
git push origin v0.2.12

Please sign in to comment.