Skip to content

Commit

Permalink
Increase coverage of utility functions & Dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Seelengrab committed Feb 24, 2024
1 parent 5b850ac commit 9d2fdda
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ function check_func(e::Expr, tsargs)
end

function argtypes(T)
T === Union{} && throw(ArgumentError("Can't `produce` from a Type, did you mean to pass an instance?"))
T = Base.unwrap_unionall(T)
if T.name == @NamedTuple{}.name
# normalize to `Tuple`
Expand Down
12 changes: 10 additions & 2 deletions src/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -551,15 +551,23 @@ struct Dicts{K,V} <: Possibility{Dict{K,V}}
min_size::Int
max_size::Int
function Dicts(keys::Possibility{K}, values::Possibility{V}; min_size=0, max_size=10_000) where {K,V}
min_size <= max_size || throw(ArgumentError("`min_size` must be `<= max_size`!"))
new{K,V}(keys, values, min_size, max_size)
end
end

function produce(d::Dicts{K,V}, tc::TestCase) where {K,V}
dict = Dict{K,V}()

bound = produce(Data.Integers(d.min_size, d.max_size), tc)
for _ in 1:bound
while true
if length(dict) < d.min_size
forced_choice!(tc, UInt(1))
elseif (length(dict)+1) >= d.max_size
forced_choice!(tc, UInt(0))
break
elseif !weighted!(tc, 0.9)
break
end
k = produce(d.keys, tc)
v = produce(d.values, tc)
dict[k] = v
Expand Down
19 changes: 19 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -720,4 +720,23 @@ const verb = VERSION.major == 1 && VERSION.minor < 11
@test Supposition.results(broke_err_sr).isbroken
end
end

@testset "Utility" begin
@testset for T in (Float16, Float32, Float64)
@check function floatfunc(f=Data.Floats{T}())
orig = bitstring(f)
reassembled = bitstring(Supposition.assemble(T, Supposition.tear(f)...))
orig == reassembled
end
end
end

@testset "Dicts" begin
@check function dictlen(m=Data.Integers(0, 200),n=Data.Integers(0,m))
k = Data.Integers{UInt8}()
v = Data.Integers{Int8}()
d = Data.produce!(Data.Dicts(k,v;min_size=n,max_size=m))
n <= length(d) <= m
end
end
end

0 comments on commit 9d2fdda

Please sign in to comment.