diff --git a/Project.toml b/Project.toml index f15a9ff..a2d23ed 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "LightBSON" uuid = "a4a7f996-b3a6-4de6-b9db-2fa5f350df41" authors = ["Christian Rorvik "] -version = "0.2.11" +version = "0.2.12" [deps] DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" diff --git a/src/representations.jl b/src/representations.jl index 7edee2a..a1934bf 100644 --- a/src/representations.jl +++ b/src/representations.jl @@ -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) diff --git a/test/representation_tests.jl b/test/representation_tests.jl index 3282923..930d7ee 100644 --- a/test/representation_tests.jl +++ b/test/representation_tests.jl @@ -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