Skip to content

Commit

Permalink
test: add more tests (#50)
Browse files Browse the repository at this point in the history
* test: add more tests

* test: add moe tests

* test: add complex type test
  • Loading branch information
ianna authored Dec 4, 2023
1 parent b75414d commit 4ad5062
Showing 1 changed file with 130 additions and 3 deletions.
133 changes: 130 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2859,6 +2859,96 @@ end

### to_buffers ###########################################################

begin
layout = AwkwardArray.PrimitiveArray([true, false, true, false])
form, len, containers = AwkwardArray.to_buffers(layout)

@test JSON.parse(form) == JSON.parse(
"""{"class": "NumpyArray", "primitive": "bool", "inner_shape": [], "parameters": {}, "form_key": "node0"}""",
)
@test len == 4
@test containers == Dict{String,Vector{UInt8}}(
"node0-data" => Vector{UInt8}(
b"\x01\x00\x01\x00",
),
)
end

begin
layout = AwkwardArray.PrimitiveArray([Int8(-1), Int8(0), Int8(-33), Int8(66)])
form, len, containers = AwkwardArray.to_buffers(layout)

@test JSON.parse(form) == JSON.parse(
"""{"class": "NumpyArray", "primitive": "int8", "inner_shape": [], "parameters": {}, "form_key": "node0"}""",
)
@test len == 4
@test containers == Dict{String,Vector{UInt8}}(
"node0-data" => Vector{UInt8}(
b"\xff\x00\xdf\x42",
),
)
end

begin
layout = AwkwardArray.PrimitiveArray([Int16(-1), Int16(0), Int16(-33), Int16(66)])
form, len, containers = AwkwardArray.to_buffers(layout)

@test JSON.parse(form) == JSON.parse(
"""{"class": "NumpyArray", "primitive": "int16", "inner_shape": [], "parameters": {}, "form_key": "node0"}""",
)
@test len == 4
@test containers == Dict{String,Vector{UInt8}}(
"node0-data" => Vector{UInt8}(
b"\xff\xff\x00\x00\xdf\xff\x42\x00",
),
)
end

begin
layout = AwkwardArray.PrimitiveArray([UInt16(1), UInt16(0), UInt16(33), UInt16(66)])
form, len, containers = AwkwardArray.to_buffers(layout)

@test JSON.parse(form) == JSON.parse(
"""{"class": "NumpyArray", "primitive": "uint16", "inner_shape": [], "parameters": {}, "form_key": "node0"}""",
)
@test len == 4
@test containers == Dict{String,Vector{UInt8}}(
"node0-data" => Vector{UInt8}(
b"\x01\x00\x00\x00\x21\x00\x42\x00",
),
)
end

begin
layout = AwkwardArray.PrimitiveArray([Int32(-1), Int32(0), Int32(-33), Int32(66)])
form, len, containers = AwkwardArray.to_buffers(layout)

@test JSON.parse(form) == JSON.parse(
"""{"class": "NumpyArray", "primitive": "int32", "inner_shape": [], "parameters": {}, "form_key": "node0"}""",
)
@test len == 4
@test containers == Dict{String,Vector{UInt8}}(
"node0-data" => Vector{UInt8}(
b"\xff\xff\xff\xff\x00\x00\x00\x00\xdf\xff\xff\xff\x42\x00\x00\x00",
),
)
end

begin
layout = AwkwardArray.PrimitiveArray([UInt32(1), UInt32(0), UInt32(33), Int32(66)])
form, len, containers = AwkwardArray.to_buffers(layout)

@test JSON.parse(form) == JSON.parse(
"""{"class": "NumpyArray", "primitive": "uint32", "inner_shape": [], "parameters": {}, "form_key": "node0"}""",
)
@test len == 4
@test containers == Dict{String,Vector{UInt8}}(
"node0-data" => Vector{UInt8}(
b"\x01\x00\x00\x00\x00\x00\x00\x00\x21\x00\x00\x00\x42\x00\x00\x00",
),
)
end

begin
layout = AwkwardArray.PrimitiveArray([1.1, 2.2, 3.3, 4.4, 5.5])
form, len, containers = AwkwardArray.to_buffers(layout)
Expand Down Expand Up @@ -2890,6 +2980,20 @@ end
),
)
end

begin
layout = AwkwardArray.PrimitiveArray([1+1im, 0+0.2im, -3.3 + 0.1im, 66])
form, len, containers = AwkwardArray.to_buffers(layout)
@test JSON.parse(form) == JSON.parse(
"""{"class": "NumpyArray", "primitive": "complex128", "inner_shape": [], "parameters": {}, "form_key": "node0"}""",
)
@test len == 4
@test containers == Dict{String,Vector{UInt8}}(
"node0-data" => Vector{UInt8}(
b"\x00\x00\x00\x00\x00\x00\xf0\x3f\x00\x00\x00\x00\x00\x00\xf0\x3f\x00\x00\x00\x00\x00\x00\x00\x00\x9a\x99\x99\x99\x99\x99\xc9\x3f\x66\x66\x66\x66\x66\x66\x0a\xc0\x9a\x99\x99\x99\x99\x99\xb9\x3f\x00\x00\x00\x00\x00\x80\x50\x40\x00\x00\x00\x00\x00\x00\x00\x00",
),
)
end

begin
layout = AwkwardArray.EmptyArray()
Expand Down Expand Up @@ -3158,16 +3262,39 @@ end
"node5-data" => Vector{UInt8}(b"five"),
)
end
end

@testset "Parameters compatible tests" begin
begin
doc_parameters = AwkwardArray.Parameters("__doc__" => "nice list")

nice_list_layout = AwkwardArray.ListOffsetArray(
[0, 3, 3, 8],
AwkwardArray.PrimitiveArray([0x68, 0x65, 0x79, 0x74, 0x68, 0x65, 0x72, 0x65],),
parameters = AwkwardArray.Parameters("__doc__" => "nice list"),
)

not_so_nice_list_layout = AwkwardArray.ListOffsetArray(
[0, 3, 3, 8],
AwkwardArray.PrimitiveArray([0x68, 0x65, 0x79, 0x74, 0x68, 0x65, 0x72, 0x65],),
parameters = AwkwardArray.Parameters("__array__" => "not so nice list"),
)

@test AwkwardArray.compatible(nice_list_layout.parameters, not_so_nice_list_layout.parameters) == false
@test AwkwardArray.compatible(doc_parameters, nice_list_layout.parameters) == true
end

end # @testset "AwkwardArray.jl"

@testset "Tables.jl intergration" begin
@testset "Tables.jl intergration" begin
begin
df = (; x = [[1], [2], [1, 2, 3]], y = [4.0, 5, 6])
awt = AwkwardArray.from_table(df)
@test Tables.schema(df) == Tables.schema(awt)

@test df.x == AwkwardArray.to_vector(awt[:x])
@test df.y == AwkwardArray.to_vector(awt[:y])
end

end # @testset "AwkwardArray.jl"
end # @testset "Tables.jl"

include("./runpytests.jl")

0 comments on commit 4ad5062

Please sign in to comment.