Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes for 0.7 compatibility #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/LittleEndianBase128.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const version = v"0.0.1"

movesign{T}(x::T) = signed(xor(x >>> one(T), -(x & one(T))))

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 @@ -57,11 +57,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,18 +23,18 @@ for t in types
a = typemin(t)
b = typemax(t)
print("$t min ... ")
u = decode(encode(a),t)[1]
u = LittleEndianBase128.decode(LittleEndianBase128.encode(a),t)[1]
@test u == a
@test typeof(u) == typeof(a)
println("PASS")
print("$t max ... ")
u = decode(encode(b),t)[1]
u = LittleEndianBase128.decode(LittleEndianBase128.encode(b),t)[1]
@test u == b
@test typeof(u) == typeof(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