Skip to content

Commit 52e0c55

Browse files
test: make rewrite tests independent of hash order
1 parent 7cd7e3f commit 52e0c55

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

test/rewrite.jl

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,24 @@ end
5555
@test r1(exp(sin(x)+cos(x))) === x
5656
r2 = @rule (~x+~y)*(~z+~w)^(~m) => (~x, ~y, ~z, ~w, ~m)
5757
r3 = @rule (~z+~w)^(~m)*(~x+~y) => (~x, ~y, ~z, ~w, ~m)
58-
@test r2((a+b)*(x+c)^b) === (a, b, x, c, b)
59-
@test r3((a+b)*(x+c)^b) === (a, b, x, c, b)
58+
res1 = r2((a+b)*(x+c)^b)
59+
@test issetequal(res1[1:2], [a, b])
60+
@test issetequal(res1[3:4], [x, c])
61+
@test isequal(res1[5], b)
62+
res2 = r3((a+b)*(x+c)^b)
63+
@test issetequal(res2[1:2], [a, b])
64+
@test issetequal(res2[3:4], [x, c])
65+
@test isequal(res2[5], b)
6066
rPredicate1 = @rule ~x::(x->isa(x,Number)) + ~y => (~x, ~y)
6167
rPredicate2 = @rule ~y + ~x::(x->isa(x,Number)) => (~x, ~y)
6268
@test rPredicate1(2+x) === (2, x)
6369
@test rPredicate2(2+x) === (2, x)
6470
r5 = @rule (~y*(~z+~w))+~x => (~x, ~y, ~z, ~w)
6571
r6 = @rule ~x+((~z+~w)*~y) => (~x, ~y, ~z, ~w)
66-
@test r5(c*(a+b)+d) === (d, c, a, b)
67-
@test r6(c*(a+b)+d) === (d, c, a, b)
72+
res3 = r5(c*(a+b)+d)
73+
@test res3 === (d, c, a, b) || res3 === (d, c, b, a)
74+
res4 = r6(c*(a+b)+d)
75+
@test res4 === (d, c, a, b) || res4 === (d, c, b, a)
6876
end
6977

7078
@testset "Slot matcher with default value" begin
@@ -98,8 +106,10 @@ end
98106
@test r_pow2(a+b) === 1
99107

100108
r_mix = @rule (~x + (~y)*(~!c))^(~!m) => (~m, ~c)
101-
@test r_mix((a + b*c)^2) === (2, c)
102-
@test r_mix((a + b*c)) === (1, c)
109+
res = r_mix((a + b*c)^2)
110+
@test res === (2, c) || res === (2, b)
111+
res = r_mix((a + b*c))
112+
@test res === (1, c) || res === (1, b)
103113
@test r_mix((a + b)) === (1, 1)
104114

105115
r_more_than_two_arguments = @rule (~!a)*exp(~x)*sin(~x) => (~a, ~x)
@@ -114,7 +124,7 @@ end
114124
# predicate checked in normal matching process
115125
r_predicate1 = @rule x + (~!m::(var->isa(var, Int))) => ~m
116126
@test r_predicate1(x+2) === 2
117-
@test r_predicate1(x+2.0) === nothing
127+
@test r_predicate1(x+2.1) === nothing
118128

119129
# predicate checked in defslot matching process
120130
r_predicate2 = @rule x + ~!m::(var->!(var===0)) => ~m
@@ -135,7 +145,8 @@ end
135145
@test r1(1/a) === (a, -1)
136146

137147
r2 = @rule (~x)^(~y + ~z) => (~x, ~y, ~z) # rule with term as exponent
138-
@test r2(1/a^(b+2c)) === (a, -b, -2c) # uses frankestein
148+
res = r2(1/a^(b+2c))
149+
@test res === (a, -b, -2c) || res === (a, -2c, -b) # uses frankestein
139150
@test r2(1/a^3) === nothing # should use a term_matcher that flips the sign, but is not implemented
140151

141152
r1defslot = @rule (~x)^(~!y) => (~x, ~y) # rule with defslot as exponent
@@ -145,7 +156,8 @@ end
145156
@test r1defslot(a) === (a, 1)
146157

147158
r = @rule (~x + ~y)^(~m) => (~x, ~y, ~m) # rule to match (1/...)^(...)
148-
@test r((1/(a+b))^3) === (a,b,-3)
159+
res = r((1/(a+b))^3)
160+
@test res === (a,b,-3) || res === (b, a, -3)
149161
end
150162

151163
@testset "Return the matches dictionary" begin
@@ -226,5 +238,5 @@ end
226238
ex = setmetadata(ex, MetaData, :metadata)
227239
ex1 = ex * b
228240

229-
@test getmetadata(arguments(ex1)[1], MetaData) == :metadata
241+
@test getmetadata(sorted_arguments(ex1)[1], MetaData) == :metadata
230242
end

0 commit comments

Comments
 (0)