Skip to content

Commit

Permalink
fixes for 0.7 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
davidssmith committed Oct 28, 2017
1 parent 98a3eea commit 5fa40bd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/LittleEndianBase128.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
25 changes: 12 additions & 13 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -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 ...")
Expand All @@ -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

0 comments on commit 5fa40bd

Please sign in to comment.