From 5fa40bd960d64a5d2db9a9c4f46f2651f4332435 Mon Sep 17 00:00:00 2001 From: David Smith Date: Sat, 28 Oct 2017 11:53:33 -0500 Subject: [PATCH] fixes for 0.7 compatibility --- src/LittleEndianBase128.jl | 8 ++++---- test/runtests.jl | 25 ++++++++++++------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/LittleEndianBase128.jl b/src/LittleEndianBase128.jl index 872e89f..e261dda 100644 --- a/src/LittleEndianBase128.jl +++ b/src/LittleEndianBase128.jl @@ -33,7 +33,7 @@ end const version = v"0.0.1" -function encode{T<:Unsigned,N}(input::Array{T,N}) +function encode(input::Array{T,N}) where {T <: Unsigned, N} # Encode array of unsigned integers using LEB128 maxbytes = ceil(Int, 8*sizeof(T)/ 7) output = Array{UInt8}(maxbytes*length(input)) # maximum possible length @@ -55,11 +55,11 @@ function encode{T<:Unsigned,N}(input::Array{T,N}) return output[1:k-1] end -encode{T<:Unsigned}(n::T) = encode([n]) +encode(n::T) where {T<:Unsigned} = encode([n]) -encode{T<:Signed}(n::T) = encode(unsigned(xor(n << 1, n >> (8*sizeof(T)-1)))) +encode(n::T) where {T<:Signed} = encode(unsigned(xor(n << 1, n >> (8*sizeof(T)-1)))) -encode{T<:Signed,N}(input::Array{T,N}) = encode(map(n -> unsigned(xor(n << 1, n >> 63)), input)) +encode(input::Array{T,N}) where {T<:Signed,N} = encode(map(n -> unsigned(xor(n << 1, n >> 63)), input)) function decodeunsigned(input::Array{UInt8,1}, dtype::DataType=UInt64, outsize::Integer=0) # Decode unsigned integer using LEB128 diff --git a/test/runtests.jl b/test/runtests.jl index 30a23fa..c091e31 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,20 +1,19 @@ # test file for LittleEndianBase128.jl -using Base.Test +using Test include("../src/LittleEndianBase128.jl") -using LittleEndianBase128 println("Testing LittleEndianBase128 encoding and decoding.") print("Known results ... ") -@test encode(UInt32(624485)) == UInt8[0xe5,0x8e,0x26] -@test encode(UInt64(2147483647)) == UInt8[0xff,0xff,0xff,0xff,0x07] -@test encode(-1) == UInt8[0x01] -@test encode(0) == UInt8[0x00] -@test encode(2147483647) == UInt8[0xfe,0xff,0xff,0xff,0x0f] -@test encode(-2147483647) == UInt8[0xfd,0xff,0xff,0xff,0x0f] -@test decodesigned(encode(-2147483647)) == [-2147483647] -@test decode(UInt8(0x01)) == [0x01] +@test LittleEndianBase128.encode(UInt32(624485)) == UInt8[0xe5,0x8e,0x26] +@test LittleEndianBase128.encode(UInt64(2147483647)) == UInt8[0xff,0xff,0xff,0xff,0x07] +@test LittleEndianBase128.encode(-1) == UInt8[0x01] +@test LittleEndianBase128.encode(0) == UInt8[0x00] +@test LittleEndianBase128.encode(2147483647) == UInt8[0xfe,0xff,0xff,0xff,0x0f] +@test LittleEndianBase128.encode(-2147483647) == UInt8[0xfd,0xff,0xff,0xff,0x0f] +@test LittleEndianBase128.decodesigned(LittleEndianBase128.encode(-2147483647)) == [-2147483647] +@test LittleEndianBase128.decode(UInt8(0x01)) == [0x01] println("PASS") println("Type min and max ...") @@ -24,14 +23,14 @@ for t in types a = typemin(t) b = typemax(t) print("$t min ... ") - @test decode(encode(a),t)[1] == a + @test LittleEndianBase128.decode(LittleEndianBase128.encode(a),t)[1] == a println("PASS") print("$t max ... ") - @test decode(encode(b),t)[1] == b + @test LittleEndianBase128.decode(LittleEndianBase128.encode(b),t)[1] == b println("PASS") print("$t random $n x $n matrix ...") x = rand(a:b, n, n) - y = reshape(decode(encode(x),t), n, n) + y = reshape(LittleEndianBase128.decode(LittleEndianBase128.encode(x),t), n, n) @test x == y println("PASS") end