Skip to content

Commit

Permalink
sort! rule tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jgreener64 committed Aug 16, 2023
1 parent f56cbe8 commit 054b96d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test/internal_rules.jl
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 054b96d

Please sign in to comment.