From d1fcf6ad8459b7204e8c30a345a6b5beb1b80c11 Mon Sep 17 00:00:00 2001 From: ScottPJones Date: Mon, 30 Jan 2017 12:53:37 -0500 Subject: [PATCH] Better handle packed tables, add more tests --- src/StrTables.jl | 13 ++-------- test/runtests.jl | 66 ++++++++++++++++++++++++------------------------ 2 files changed, 35 insertions(+), 44 deletions(-) diff --git a/src/StrTables.jl b/src/StrTables.jl index 9c43a30..02d8290 100644 --- a/src/StrTables.jl +++ b/src/StrTables.jl @@ -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 @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 6cf994e..0c2766b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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" @@ -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