diff --git a/.travis.yml b/.travis.yml index 254878d..0b94e40 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,17 +1,10 @@ -language: cpp -compiler: - - clang +language: julia +os: + - osx + - linux +julia: + - nightly + - 0.4 + - 0.3 notifications: email: false -env: - matrix: - - JULIAVERSION="juliareleases" - - JULIAVERSION="julianightlies" -before_install: - - sudo add-apt-repository ppa:staticfloat/julia-deps -y - - sudo add-apt-repository ppa:staticfloat/${JULIAVERSION} -y - - sudo apt-get update -qq -y - - sudo apt-get install libpcre3-dev julia -y - - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi -script: - - julia -e 'Pkg.init(); Pkg.clone(pwd()); Pkg.test("MsgPack")' diff --git a/REQUIRE b/REQUIRE index 2c4ef82..df77a41 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1 +1,2 @@ julia 0.3 +Compat diff --git a/src/MsgPack.jl b/src/MsgPack.jl index 85b88b0..92073f3 100644 --- a/src/MsgPack.jl +++ b/src/MsgPack.jl @@ -1,6 +1,9 @@ module MsgPack +using Compat + export pack, unpack, Ext +import Base: == const INT_FP = 0x00 # - 0xf7 const MAP_F = 0x80 # - 0x8f @@ -40,9 +43,9 @@ const INT_FN = 0xe0 # - 0xff immutable Ext typecode::Int8 - data::Vector{Uint8} + data::Vector{UInt8} - function Ext(t::Integer, d::Vector{Uint8}; impltype=false) + function Ext(t::Integer, d::Vector{UInt8}; impltype=false) # -128 to -1 reserved for implementation if -128 <= t <= -1 impltype || error("MsgPack Ext typecode -128 through -1 reserved by implementation") @@ -67,54 +70,54 @@ extdeserialize(e::Ext) = (e.typecode, deserialize(IOBuffer(e.data))) readn(s, t) = ntoh(read(s, t)) -readi(s, t) = int64(readn(s, t)) +readi(s, t) = @compat Int64(readn(s, t)) readu64(s, t) = begin - v = uint64(readn(s, t)) + v = @compat UInt64(readn(s, t)) if v > 2^63-1 v else - int64(v) + @compat Int64(v) end end const DISPATCH = - [ NIL => s -> nothing - ,UNUSED => s -> error("unused") - ,FALSE => s -> false - ,TRUE => s -> true - ,BIN_8 => s -> unpack_bin(s, readn(s, Uint8)) - ,BIN_16 => s -> unpack_bin(s, readn(s, Uint16)) - ,BIN_32 => s -> unpack_bin(s, readn(s, Uint32)) - ,EXT_8 => s -> unpack_ext(s, readn(s, Uint8)) - ,EXT_16 => s -> unpack_ext(s, readn(s, Uint16)) - ,EXT_32 => s -> unpack_ext(s, readn(s, Uint32)) - ,FLOAT_32 => s -> readn(s, Float32) - ,FLOAT_64 => s -> readn(s, Float64) - ,UINT_8 => s -> readi(s, Uint8) - ,UINT_16 => s -> readi(s, Uint16) - ,UINT_32 => s -> readi(s, Uint32) - ,UINT_64 => s -> readu64(s, Uint64) - ,INT_8 => s -> readi(s, Int8) - ,INT_16 => s -> readi(s, Int16) - ,INT_32 => s -> readi(s, Int32) - ,INT_64 => s -> readi(s, Int64) - ,STR_8 => s -> unpack_str(s, readn(s, Uint8)) - ,STR_16 => s -> unpack_str(s, readn(s, Uint16)) - ,STR_32 => s -> unpack_str(s, readn(s, Uint32)) - ,ARR_16 => s -> unpack_arr(s, readn(s, Uint16)) - ,ARR_32 => s -> unpack_arr(s, readn(s, Uint32)) - ,MAP_16 => s -> unpack_map(s, readn(s, Uint16)) - ,MAP_32 => s -> unpack_map(s, readn(s, Uint32)) -] + @compat Dict( NIL => s -> nothing + ,UNUSED => s -> error("unused") + ,FALSE => s -> false + ,TRUE => s -> true + ,BIN_8 => s -> unpack_bin(s, readn(s, UInt8)) + ,BIN_16 => s -> unpack_bin(s, readn(s, UInt16)) + ,BIN_32 => s -> unpack_bin(s, readn(s, UInt32)) + ,EXT_8 => s -> unpack_ext(s, readn(s, UInt8)) + ,EXT_16 => s -> unpack_ext(s, readn(s, UInt16)) + ,EXT_32 => s -> unpack_ext(s, readn(s, UInt32)) + ,FLOAT_32 => s -> readn(s, Float32) + ,FLOAT_64 => s -> readn(s, Float64) + ,UINT_8 => s -> readi(s, UInt8) + ,UINT_16 => s -> readi(s, UInt16) + ,UINT_32 => s -> readi(s, UInt32) + ,UINT_64 => s -> readu64(s, UInt64) + ,INT_8 => s -> readi(s, Int8) + ,INT_16 => s -> readi(s, Int16) + ,INT_32 => s -> readi(s, Int32) + ,INT_64 => s -> readi(s, Int64) + ,STR_8 => s -> unpack_str(s, readn(s, UInt8)) + ,STR_16 => s -> unpack_str(s, readn(s, UInt16)) + ,STR_32 => s -> unpack_str(s, readn(s, UInt32)) + ,ARR_16 => s -> unpack_arr(s, readn(s, UInt16)) + ,ARR_32 => s -> unpack_arr(s, readn(s, UInt32)) + ,MAP_16 => s -> unpack_map(s, readn(s, UInt16)) + ,MAP_32 => s -> unpack_map(s, readn(s, UInt32)) + ) unpack(s) = unpack(IOBuffer(s)) unpack(s::IO) = begin - b = read(s, Uint8) + b = read(s, UInt8) if b <= 0x7f # positive fixint - int64(b) + @compat Int64(b) elseif b <= 0x8f # fixmap @@ -137,7 +140,7 @@ unpack(s::IO) = begin else # negative fixint - int64(reinterpret(Int8, b)) + @compat Int64(reinterpret(Int8, b)) end end @@ -175,35 +178,35 @@ pack(v) = begin end -pack(s, ::Nothing) = write(s, NIL) +@compat pack(s, ::Void) = write(s, NIL) pack(s, v::Bool) = if v write(s, TRUE) else write(s, FALSE) end pack(s, v::Integer) = begin if v < 0 if v >= -32 - write(s, int8(v)) + write(s, @compat Int8(v)) elseif v >= -2^7 - wh(s, INT_8, int8(v)) + wh(s, INT_8, @compat Int8(v)) elseif v >= -2^15 - wh(s, INT_16, int16(v)) + wh(s, INT_16, @compat Int16(v)) elseif v >= -2^31 - wh(s, INT_32, int32(v)) + wh(s, INT_32, @compat Int32(v)) elseif v >= -2^63 - wh(s, INT_64, int64(v)) + wh(s, INT_64, @compat Int64(v)) else error("MsgPack signed int overflow") end else if v <= 127 - write(s, uint8(v)) + write(s, @compat UInt8(v)) elseif v <= 2^8-1 - wh(s, UINT_8, uint8(v)) + wh(s, UINT_8, @compat UInt8(v)) elseif v <= 2^16-1 - wh(s, UINT_16, uint16(v)) + wh(s, UINT_16, @compat UInt16(v)) elseif v <= 2^32-1 - wh(s, UINT_32, uint32(v)) - elseif v <= uint64(2)^64-1 - wh(s, UINT_64, uint64(v)) + wh(s, UINT_32, @compat UInt32(v)) + elseif v <= @compat UInt64(2)^64-1 + wh(s, UINT_64, @compat UInt64(v)) else error("MsgPack unsigned int overflow") end @@ -214,21 +217,21 @@ pack(s, v::Float32) = wh(s, 0xca, v) pack(s, v::Float64) = wh(s, 0xcb, v) # str format -pack(s, v::String) = begin +pack(s, v::AbstractString) = begin n = sizeof(v) if n < 2^5 - write(s, STR_F | uint8(n)) - ## Note: with this section commented out, we do not have - ## the most compact format for a string. However, + write(s, STR_F | @compat UInt8(n)) + ## Note: with this section commented out, we do not have + ## the most compact format for a string. However, ## the string is still in spec, and some other ## msgpack libaries (*ahem* Python) can't decode ## strings created with this rule. #elseif n < 2^8 - # wh(s, 0xd9, uint8(n)) + # wh(s, 0xd9, @compat UInt8(n)) elseif n < 2^16 - wh(s, 0xda, uint16(n)) + wh(s, 0xda, @compat UInt16(n)) elseif n < 2^32 - wh(s, 0xdb, uint32(n)) + wh(s, 0xdb, @compat UInt32(n)) else error("MsgPack str overflow: ", n) end @@ -249,11 +252,11 @@ pack(s, v::Ext) = begin elseif n == 16 write(s, 0xd8) elseif n < 2^8 - wh(s, 0xc7, uint8(n)) + wh(s, 0xc7, @compat UInt8(n)) elseif n < 2^16 - wh(s, 0xc8, uint16(n)) + wh(s, 0xc8, @compat UInt16(n)) elseif n < 2^32 - wh(s, 0xc9, uint32(n)) + wh(s, 0xc9, @compat UInt32(n)) else error("MsgPack ext overflow: ", n) end @@ -262,14 +265,14 @@ pack(s, v::Ext) = begin end # bin format -pack(s, v::Vector{Uint8}) = begin +pack(s, v::Vector{UInt8}) = begin n = length(v) if n < 2^8 - wh(s, 0xc4, uint8(n)) + wh(s, 0xc4, @compat UInt8(n)) elseif n < 2^16 - wh(s, 0xc5, uint16(n)) + wh(s, 0xc5, @compat UInt16(n)) elseif n < 2^32 - wh(s, 0xc6, uint32(n)) + wh(s, 0xc6, @compat UInt32(n)) else error("MsgPack bin overflow: ", n) end @@ -277,14 +280,14 @@ pack(s, v::Vector{Uint8}) = begin end # Simple arrays -pack(s, v::Union(Vector, Tuple)) = begin +pack(s, @compat v::Union{Vector, Tuple}) = begin n = length(v) if n < 2^4 - write(s, ARR_F | uint8(n)) + write(s, ARR_F | @compat UInt8(n)) elseif n < 2^16 - wh(s, 0xdc, uint16(n)) + wh(s, 0xdc, @compat UInt16(n)) elseif n < 2^32 - wh(s, 0xdd, uint32(n)) + wh(s, 0xdd, @compat UInt32(n)) else error("MsgPack array overflow: ", n) end @@ -298,11 +301,11 @@ end pack(s, v::Dict) = begin n = length(v) if n < 2^4 - write(s, MAP_F | uint8(n)) + write(s, MAP_F | @compat UInt8(n)) elseif n < 2^16 - wh(s, 0xde, uint16(n)) + wh(s, 0xde, @compat UInt16(n)) elseif n < 2^32 - wh(s, 0xdf, uint32(n)) + wh(s, 0xdf, @compat UInt32(n)) else error("MsgPack map overflow: ", n) end diff --git a/test/runtests.jl b/test/runtests.jl index 85c862b..8337fe0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,5 +1,6 @@ using MsgPack using Base.Test +using Compat ck_pack(a, b) = pack(a) == b && unpack(b) == a @@ -12,35 +13,35 @@ ck_pack(a, b) = pack(a) == b && unpack(b) == a # positive fixint @test ck_pack(5, [0x05]) -# uint8 +# UInt8 @test ck_pack(128, [0xcc,0x80]) -# uint16 +# UInt16 @test ck_pack(2^8, [0xcd,0x01,0x00]) -# uint32 +# UInt32 @test ck_pack(2^16, [0xce,0x00,0x01,0x00,0x00]) -# uint64 +# UInt64 @test ck_pack(2^32, [0xcf,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00]) -@test ck_pack(uint64(2)^64-1, [0xcf,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff]) +@test ck_pack((@compat UInt64(2)^64-1), [0xcf,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff]) # negative fixint @test ck_pack(-5, [0xfb]) @test ck_pack(-32, [0xe0]) -# int8 +# Int8 @test ck_pack(-33, [0xd0,0xdf]) @test ck_pack(-2^7, [0xd0,0x80]) -# int16 +# Int16 @test ck_pack(-2^8, [0xd1,0xff,0x00]) @test ck_pack(-2^15, [0xd1,0x80,0x00]) -# int32 +# Int32 @test ck_pack(-2^16, [0xd2,0xff,0xff,0x00,0x00]) @test ck_pack(-2^31, [0xd2,0x80,0x00,0x00,0x00]) -# int64 +# Int64 @test ck_pack(-2^32, [0xd3,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0x00]) @test ck_pack(-2^63, [0xd3,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00]) -# float32 -@test ck_pack(float32(5.876), [0xca, 0x40, 0xbc, 0x08, 0x31]) -# float64 +# Float32 +@test ck_pack((@compat Float32(5.876)), [0xca, 0x40, 0xbc, 0x08, 0x31]) +# Float64 @test ck_pack(0.654789321, [0xcb, 0x3f, 0xe4, 0xf4, 0x08, 0xbb, 0xee, 0xe1, 0xa8]) # fixstr @@ -51,41 +52,41 @@ ck_pack(a, b) = pack(a) == b && unpack(b) == a # str8 - currently unimplemented # str16 s = randstring(2^8) -@test ck_pack(s, vcat(0xda, 0x01, 0x00, convert(Vector{Uint8}, s))) +@test ck_pack(s, vcat(0xda, 0x01, 0x00, convert(Vector{UInt8}, s))) s = randstring(2^16 - 1) -@test ck_pack(s, vcat(0xda, 0xff, 0xff, convert(Vector{Uint8}, s))) +@test ck_pack(s, vcat(0xda, 0xff, 0xff, convert(Vector{UInt8}, s))) # str32 s = randstring(2^16) -@test ck_pack(s, vcat(0xdb, 0x00, 0x01, 0x00, 0x00, convert(Vector{Uint8}, s))) +@test ck_pack(s, vcat(0xdb, 0x00, 0x01, 0x00, 0x00, convert(Vector{UInt8}, s))) s = "" # bin8 -b = rand(Uint8, 121) +b = rand(UInt8, 121) @test ck_pack(b, vcat(0xc4, 0x79, b)) # bin16 -b = rand(Uint8, 2^11) +b = rand(UInt8, 2^11) @test ck_pack(b, vcat(0xc5, 0x08, 0x00, b)) # bin32 -b = rand(Uint8, 2^20) +b = rand(UInt8, 2^20) @test ck_pack(b, vcat(0xc6, 0x00, 0x10, 0x00, 0x00, b)) b = [] # fixarray -@test ck_pack(Any[1, float32(2), Uint8[0x22, 0x54]], +@test ck_pack(Any[1, (@compat Float32(2)), UInt8[0x22, 0x54]], [0x93, 0x01, 0xca, 0x40, 0x00, 0x00, 0x00, 0xc4, 0x02, 0x22, 0x54]) @test ck_pack(Any[nothing, "heya", true, false], [0x94, 0xc0, 0xa4, 0x68, 0x65, 0x79, 0x61, 0xc3, 0xc2]) -@test ck_pack(Any[true, Uint8[0xff, 0xde, 0x11], [1, 2, 3]], +@test ck_pack(Any[true, UInt8[0xff, 0xde, 0x11], [1, 2, 3]], [0x93, 0xc3, 0xc4, 0x03, 0xff, 0xde, 0x11, 0x93, 0x01, 0x02, 0x03]) # tuple -@test pack(Any[(1, [true, false], Uint8[0xff, 0xde])]) == +@test pack(Any[(1, [true, false], UInt8[0xff, 0xde])]) == [0x91, 0x93, 0x01, 0x92, 0xc3, 0xc2, 0xc4, 0x02, 0xff, 0xde] -@test pack(("hi", (float32(2), 0xff))) == +@test pack(("hi", ((@compat Float32(2)), 0xff))) == [0x92, 0xa2, 0x68, 0x69, 0x92, 0xca, 0x40, 0x00, 0x00, 0x00, 0xcc, 0xff] # fixmap -@test ck_pack({1=>2, "hi"=>"mom"}, +@test ck_pack((@compat Dict(1=>2, "hi"=>"mom")), [0x82,0xa2,0x68,0x69,0xa3,0x6d,0x6f,0x6d,0x01,0x02]) # fixext 1 @@ -104,14 +105,14 @@ b = [] [0xd8, 0x4f, 0x00, 0x30, 0xd5, 0x64, 0x0f, 0x8d, 0x92, 0x90, 0x98, 0x99, 0x14, 0x57, 0x0e, 0x8d, 0xf1, 0x3a]) # no elements -@test ck_pack(Ext(-118, Uint8[], impltype=true), [0xc7, 0x00, 0x8a]) +@test ck_pack(Ext(-118, UInt8[], impltype=true), [0xc7, 0x00, 0x8a]) # ext 8 @test ck_pack(Ext(50, [0x62, 0x4c, 0x7c, 0x0f, 0x86, 0x04]), [0xc7, 0x06, 0x32, 0x62, 0x4c, 0x7c, 0x0f, 0x86, 0x04]) # ext 16 -b = rand(Uint8, 2^14) +b = rand(UInt8, 2^14) @test ck_pack(Ext(78, b), vcat(0xc8, 0x40, 0x00, 0x4e, b)) # ext 32 -b = rand(Uint8, 2^19) +b = rand(UInt8, 2^19) @test ck_pack(Ext(-123, b, impltype=true), vcat(0xc9, 0x00, 0x08, 0x00, 0x00, 0x85, b))