diff --git a/test/internal_rules.jl b/test/internal_rules.jl new file mode 100644 index 00000000000..e3793c21b3b --- /dev/null +++ b/test/internal_rules.jl @@ -0,0 +1,29 @@ +module InternalRules + +using Enzyme +using Enzyme.EnzymeRules +using Test + +@testset "Internal rules" begin + function f1(x) + a = [1.0, 3.0, x] + sort!(a) + return a[2] + end + + @test autodiff(Forward, f1, Duplicated(2.0, 1.0))[1] == 1 + @test autodiff(Reverse, f1, Active, Active(2.0))[1][1] == 1 + @test autodiff(Forward, f1, Duplicated(4.0, 1.0))[1] == 0 + @test autodiff(Reverse, f1, Active, Active(4.0))[1][1] == 0 + + function f2(x) + a = [1.0, -3.0, -x, -2x, x] + sort!(a; rev=true, lt=(x, y) -> abs(x) < abs(y) || (abs(x) == abs(y) && x < y)) + return sum(a .* [1, 2, 3, 4, 5]) + end + + @test autodiff(Forward, f2, Duplicated(2.0, 1.0))[1] == -3 + @test autodiff(Reverse, f2, Active, Active(2.0))[1][1] == -3 +end + +end # InternalRules diff --git a/test/runtests.jl b/test/runtests.jl index beed52c511e..5cf3f25a18d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -72,6 +72,7 @@ include("typetree.jl") include("rrules.jl") include("kwrules.jl") include("kwrrules.jl") + include("internal_rules.jl") @static if VERSION ≥ v"1.9-" # XXX invalidation does not work on Julia 1.8 include("ruleinvalidation.jl")