-
-
Save LilithHafner/9b5f41820f532ce620a554df6d1e2d5c to your computer and use it in GitHub Desktop.
[draft 1] test serialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Test | |
using Printf | |
using Base.Order | |
fast = false | |
#Construct value lists | |
floats = [T[-π, -1.0, -1/π, 1/π, 1.0, π, -0.0, 0.0, Inf, -Inf, NaN, -NaN, | |
prevfloat(T(0)), nextfloat(T(0)), prevfloat(T(Inf)), nextfloat(T(-Inf))] | |
for T in [Float16, Float32, Float64]] | |
ints = [T[17, -T(17), 0, -one(T), 1, typemax(T), typemin(T), typemax(T)-1, typemin(T)+1] | |
for T in [Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128]] | |
vals = vcat(floats, ints) | |
#Add random vals | |
map(vals) do x | |
append!(x, rand(eltype(x), 4)) | |
append!(x, reinterpret.(eltype(x), rand(UIntN(Val{sizeof(eltype(x))*8}()), 4))) | |
end | |
#Add a small val | |
push!(vals, [7,3,9,1]) | |
#Construct orders | |
ord_c = [ | |
identity, | |
ReverseOrdering, | |
o -> (p = rand(1:4); Order.By(x -> x^p, o)), | |
o -> Order.By(identity, o), | |
# o -> Order.By(x -> abs(isfinite(x) ? ceil(Integer, x) : 17) % 20 + 1, Order.Perm(o, rand(1:7, 20))) | |
] | |
orders = Set(vec([a(b(Forward)) for a in ord_c, b in ord_c])) | |
simple_orders = map(f->f(Forward), ord_c) | |
@testset "Preserve lt under serialize" begin | |
for vals_t in vals | |
for order in orders | |
defined = true#!(eltype(vals_t) == Float16) | |
result = @test defined == serializable(order, first(vals_t)) | |
if !(result isa Test.Pass) | |
display(result) | |
@printf "%12s: %s\n%12s: %s\n" "order" order "Type" eltype(vals_t) | |
end | |
if !ismissing(serialize(order, first(vals_t))) | |
#if !isempty(Base.return_types(serialize, (typeof(order), eltype(vals_t)))) | |
for a in vals_t, b in vals_t | |
isnan(a) && isnan(b) && continue #TODO revisit this departure from the standard | |
result = @test lt(order, a, b) === (serialize(order, a) < serialize(order, b)) | |
if !(result isa Test.Pass) | |
display(result) | |
@printf "%12s: %s\n%12s: %s\n%12s: %s\n%12s: %s\n" "order" order "a" a "b" b "Type" typeof(a) | |
@goto end_of_order_preserved_test_set | |
end | |
end | |
else | |
result = @test eltype(vals_t) == Float16 | |
if !(result isa Test.Pass) | |
display(result) | |
@printf "%12s: %s\n%12s: %s\n" "order" order "Type" eltype(vals_t) | |
println("deserialize should be defined on these inputs.") | |
end | |
end | |
end | |
@label end_of_order_preserved_test_set | |
end | |
end | |
@testset "deserialize" begin | |
for vals_t in vals | |
T = eltype(vals_t) | |
for order in orders | |
#Ensure that deserialize is defined exactly where it should be | |
defined = !(#T == Float16 || | |
(order isa By && order.by !== identity) || | |
(order isa ReverseOrdering && order.fwd isa By && order.fwd.by !== identity) || | |
(order isa By && order.order isa By && order.order.by !== identity)) | |
result = @test defined == deserializable(T, order, serialize(order, first(vals_t)::T)) | |
if !(result isa Test.Pass) | |
display(result) | |
@printf "%12s: %s\n%12s: %s\n" "order" order "T" T | |
end | |
#If it is defined, deserialize is defined exactly where it should be | |
if deserializable(T, order, serialize(order, first(vals_t)::T)) | |
#if !isempty(Base.return_types(serialize, (typeof(order), eltype(vals_t)))) | |
# S = typeof(serialize(order, first(vals_t))) | |
# if !isempty(Base.return_types(deserialize, (T, typeof(order), S))) | |
for x in vals_t | |
result = @test x === deserialize(T, order, serialize(order, x::T)) | |
if !(result isa Test.Pass) | |
display(result) | |
@printf "%12s: %s\n%12s: %s\n%12s: %s\n" "order" order "x" x "T" T | |
@goto end_of_deserialize_test_set | |
end | |
end | |
# end | |
end | |
end | |
@label end_of_deserialize_test_set | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment