Skip to content

Commit

Permalink
Better handle packed tables, add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottPJones committed Jan 30, 2017
1 parent 4964739 commit d1fcf6a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 44 deletions.
13 changes: 2 additions & 11 deletions src/StrTables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,6 @@ const type_tab = (UInt8, UInt16, UInt32, UInt64, UInt128,
Float16, Float32, Float64)
const SupTypes = Union{type_tab...}

_get_code{T<:Union{UInt16,UInt32}}(::Type{StrTable{T}}) = STRTAB_CODE
_get_code{T,S,O<:Union{UInt16,UInt32}}(::Type{PackedTable{T,S,O}}) = PACKED_CODE
_get_code(::Type{String}) = STRING_CODE
for (i, typ) in enumerate(type_tab)
@eval _get_code(::Type{$typ}) = $((i+BASE_CODE)%UInt8)
end
Expand Down Expand Up @@ -182,14 +179,8 @@ end
write_value{T<:SupTypes}(io::IO, val::Vector{T}) =
(write(io, _get_code(T) | 0x80, length(val)%UInt32, val))

function write_value(io::IO, tab::StrTable)
write(io, STRTAB_CODE)
write_value(io, tab.offsetvec)
write_value(io, tab.namtab)
end

function write_value(io::IO, tab::PackedTable)
write(io, PACKED_CODE)
function write_value{T,S,O}(io::IO, tab::PackedTable{T,S,O})
write(io, (T == String && S == UInt8) ? STRTAB_CODE : PACKED_CODE)
write_value(io, tab.offsetvec)
write_value(io, tab.namtab)
end
Expand Down
66 changes: 33 additions & 33 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ ST = StrTables

const testfile = joinpath(Pkg.dir("StrTables"), "test", "test.dat")

@testset "StrTables" begin
teststrs = sort(["AA", "AAAAA",
"JuliaLang", "Julia", "JuliaIO", "JuliaDB", "JuliaString",
"Scott", "Zulima", "David", "Alex", "Jules", "Gandalf",
"\U1f596 Spock Hands"])
stab = StrTable(teststrs)
teststrs = sort(["AA", "AAAAA",
"JuliaLang", "Julia", "JuliaIO", "JuliaDB", "JuliaString",
"Scott", "Zulima", "David", "Alex", "Jules", "Gandalf",
"\U1f596 Spock Hands"])
stab = StrTable(teststrs)
testbin = [Vector{UInt8}(s) for s in stab]
btab = PackedTable(testbin)

@testset "StrTables" begin
@test length(stab) == length(teststrs)
@test stab == teststrs
@test stab[1] == "AA"
Expand All @@ -21,34 +23,32 @@ const testfile = joinpath(Pkg.dir("StrTables"), "test", "test.dat")
@test ST.matchfirst(stab, "A") == ["AA", "AAAAA", "Alex"]
@test ST.matchfirst(stab, "Julia") ==
["Julia", "JuliaDB", "JuliaIO", "JuliaLang", "JuliaString"]
testout = [stab, 0x1, 2%UInt16, 3%UInt32, 4%UInt64,
5%Int8, 6%Int16, 7%Int32, 8%Int64,
Float32(9.87654321), 1.23456789,
"Test case",
"\U1f596"]
ST.save(testfile, testout)
@test ST.load(testfile) == testout
end
@testset "PackedTable" begin
teststrs = [b"AA", b"AAAAA", b"Alex", b"David", b"Gandalf",
b"Jules", b"Julia", b"JuliaDB", b"JuliaIO", b"JuliaLang", b"JuliaString",
b"Scott", b"Zulima", b"\U1f596 Spock Hands"]
stab = PackedTable(teststrs)

@test length(stab) == length(teststrs)
@test stab == teststrs
@test stab[1] == b"AA"
@test stab[end] == b"\U1f596 Spock Hands"
@test ST.matchfirstrng(stab, b"A") == 1:3
@test ST.matchfirstrng(stab, b"Julia") == 7:11
@test ST.matchfirst(stab, b"A") == [b"AA", b"AAAAA", b"Alex"]
@test ST.matchfirst(stab, b"Julia") ==
@testset "PackedTable" begin
@test length(btab) == length(testbin)
@test btab == testbin
@test btab[1] == b"AA"
@test btab[end] == b"\U1f596 Spock Hands"
@test ST.matchfirstrng(btab, b"A") == 1:3
@test ST.matchfirstrng(btab, b"Julia") == 7:11
@test ST.matchfirst(btab, b"A") == [b"AA", b"AAAAA", b"Alex"]
@test ST.matchfirst(btab, b"Julia") ==
[b"Julia", b"JuliaDB", b"JuliaIO", b"JuliaLang", b"JuliaString"]
testout = [stab, 0x1, 2%UInt16, 3%UInt32, 4%UInt64,
5%Int8, 6%Int16, 7%Int32, 8%Int64,
Float32(9.87654321), 1.23456789,
"New test",
"\U1f595"]
# ST.save(testfile, testout)
# @test ST.load(testfile) == testout
end

medstr = String(rand(Char,300))
bigstr = repeat("abcdefgh",8200)
testout = [stab, btab,
0x1, 2%UInt16, 3%UInt32, 4%UInt64, 5%UInt128,
6%Int8, 7%Int16, 8%Int32, 9%Int64, 10%Int128,
Float32(9.87654321), 1.23456789,
"Test case",
"\U1f596",
medstr,
bigstr]

@testset "Save/Load tables" begin
ST.save(testfile, testout)
@test ST.load(testfile) == testout
end

0 comments on commit d1fcf6a

Please sign in to comment.