Skip to content

Commit e198e0f

Browse files
authored
Merge pull request #177 from JuliaSymbolics/s/fuzz-cosntruct
Fuzz term construction
2 parents 83511d8 + 8716244 commit e198e0f

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

test/fuzz.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,18 @@ seed!(6174)
2020
fuzz_test(5, bool_spec)
2121
end
2222
end
23+
@testset "fuzz addmulpow" begin
24+
for i=1:100;
25+
fuzz_addmulpow(1)
26+
end
27+
for i=1:50;
28+
fuzz_addmulpow(2)
29+
end
30+
for i=1:25;
31+
fuzz_addmulpow(3)
32+
end
33+
for i=1:12;
34+
fuzz_addmulpow(4)
35+
end
36+
end
2337
end

test/fuzzlib.jl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,56 @@ function fuzz_test(ntrials, spec, simplify=simplify;kwargs...)
184184
""")
185185
end
186186
end
187+
188+
leaves = [(@syms a b c d e g)..., a^3, a^-2, b^2, b^(-1), big.((3//5, 0//2, -1//2, 1//2, 1//2+2im))...]
189+
function gen_expr(lvl=5)
190+
if lvl == 0
191+
x = rand(leaves)
192+
x, x
193+
elseif rand() < 0.5
194+
f = rand((+, *))
195+
n = rand(1:5)
196+
args = [gen_expr(lvl-1) for i in 1:n]
197+
198+
Term{Number}(f, first.(args)), f(last.(args)...)
199+
else
200+
f = rand((-,/))
201+
l = gen_expr(lvl-1)
202+
r = gen_expr(lvl-1)
203+
if f === (/) && r[2] isa Number && iszero(r[2])
204+
return gen_expr(lvl)
205+
end
206+
args = [l, r]
207+
208+
Term{Number}(f, first.(args)), f(last.(args)...)
209+
end
210+
end
211+
212+
test_dict = Dict{Any, Rational{BigInt}}(a=>1,b=>-1,c=>2,d=>-2,e=>5//3,g=>-2//3)
213+
function fuzz_addmulpow(lvl, d=test_dict)
214+
l, r = gen_expr()
215+
rl = try
216+
substitute(l, d)
217+
catch err
218+
err
219+
end
220+
rr = try
221+
substitute(r, d)
222+
catch err
223+
err
224+
end
225+
226+
if !(rl isa Number) && rr isa Number
227+
return # lhs errored, rhs did not
228+
end
229+
if rl isa Number || rr isa Number
230+
if isequal(rl, rr)
231+
@test true
232+
else
233+
println("Weird bug here:")
234+
@show r l
235+
@show rl rr
236+
@test false
237+
end
238+
end
239+
end

0 commit comments

Comments
 (0)