Skip to content

Commit 076b9f0

Browse files
committed
Add test that optimization_clean doesn't trim, marked broken
1 parent 8ec2de0 commit 076b9f0

File tree

6 files changed

+51
-34
lines changed

6 files changed

+51
-34
lines changed

test/trim/main.jl renamed to test/trim/main_clean.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ using TrimTest
22

33
function (@main)(argv::Vector{String})::Cint
44
λ = parse(Float64, argv[2])
5-
sol = TrimTest.minimize(λ)
5+
sol = TrimTest.TestModuleClean.minimize(λ)
66
println(Core.stdout, sum(sol.u))
77
return 0
88
end

test/trim/main_trimmable.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using TrimTest
2+
3+
function (@main)(argv::Vector{String})::Cint
4+
λ = parse(Float64, argv[2])
5+
sol = TrimTest.TestModuleTrimmable.minimize(λ)
6+
println(Core.stdout, sum(sol.u))
7+
return 0
8+
end

test/trim/clean_optimization.jl renamed to test/trim/optimization_clean.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
module TestModuleClean
12
using NonlinearSolveFirstOrder
23
using ADTypes: AutoForwardDiff
34
using ForwardDiff
@@ -23,10 +24,11 @@ struct MyParams{T, M}
2324
end
2425

2526
function minimize(x)
26-
autodiff = AutoForwardDiff(; chunksize=1)
27-
alg = TrustRegion(; autodiff, linsolve=LS.CholeskyFactorization())
28-
ps = MyParams(rand(), hermitianpart(rand(2,2)+2I))
27+
autodiff = AutoForwardDiff(; chunksize = 1)
28+
alg = TrustRegion(; autodiff, linsolve = LS.CholeskyFactorization())
29+
ps = MyParams(rand(), hermitianpart(rand(2, 2) + 2I))
2930
prob = NonlinearLeastSquaresProblem{false}(f, rand(2), ps)
3031
sol = solve(prob, alg)
3132
return sol
3233
end
34+
end

test/trim/trimmable_optimization.jl renamed to test/trim/optimization_trimmable.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
module TestModuleTrimmable
12
using NonlinearSolveFirstOrder
23
using DiffEqBase
34
using ADTypes: AutoForwardDiff
@@ -35,3 +36,4 @@ function minimize(x)
3536
solve!(cache)
3637
return cache
3738
end
39+
end

test/trim/runtests.jl

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ using SafeTestsets
33
@safetestset "Clean implementation (non-trimmable)" begin
44
using JET
55
using SciMLBase: successful_retcode
6-
include("clean_optimization.jl")
7-
@test successful_retcode(minimize(1.0).retcode)
8-
# can't use `@test_opt` macro here because it would eval before `using JET`
9-
# is processed
10-
test_opt(minimize, (typeof(1.0),))
6+
include("optimization_clean.jl")
7+
@test successful_retcode(TestModuleClean.minimize(1.0).retcode)
8+
# can't use `@test_opt` macro here because it would try to eval before
9+
# `using JET` is processed
10+
test_opt(TestModuleClean.minimize, (typeof(1.0),))
1111
end
1212

1313
@safetestset "Trimmable implementation" begin
1414
using JET
1515
using SciMLBase: successful_retcode
16-
include("trimmable_optimization.jl")
17-
@test successful_retcode(minimize(1.0).retcode)
18-
# can't use `@test_opt` macro here because it would eval before `using JET`
19-
# is processed
20-
test_opt(minimize, (typeof(1.0),))
16+
include("optimization_trimmable.jl")
17+
@test successful_retcode(TestModuleClean.minimize(1.0).retcode)
18+
# can't use `@test_opt` macro here because it would try to eval before
19+
# `using JET` is processed
20+
test_opt(TestModuleTrimmable.minimize, (typeof(1.0),))
2121
end
2222

2323
@safetestset "Run trim" begin
@@ -43,23 +43,27 @@ end
4343
)
4444
)
4545
@test isfile(JULIAC)
46-
binpath = tempname()
47-
cmd = `$(Base.julia_cmd()) --project=. --depwarn=error $(JULIAC) --experimental --trim=unsafe-warn --output-exe $(binpath) main.jl`
4846

49-
# since we are calling Julia from Julia, we first need to clean some
50-
# environment variables
51-
clean_env = copy(ENV)
52-
delete!(clean_env, "JULIA_PROJECT")
53-
delete!(clean_env, "JULIA_LOAD_PATH")
54-
# We could just check for success, but then failures are hard to debug.
55-
# Instead we use `_execute` to also capture `stdout` and `stderr`.
56-
# @test success(setenv(cmd, clean_env))
57-
trimcall = _execute(setenv(cmd, clean_env; dir = @__DIR__))
58-
if trimcall.exitcode != 0
59-
@show trimcall.stdout
60-
@show trimcall.stderr
47+
for (mainfile, shouldpass) in [("main_trimmable.jl", true),
48+
("main_clean.jl", false)]
49+
binpath = tempname()
50+
cmd = `$(Base.julia_cmd()) --project=. --depwarn=error $(JULIAC) --experimental --trim=unsafe-warn --output-exe $(binpath) $(mainfile)`
51+
52+
# since we are calling Julia from Julia, we first need to clean some
53+
# environment variables
54+
clean_env = copy(ENV)
55+
delete!(clean_env, "JULIA_PROJECT")
56+
delete!(clean_env, "JULIA_LOAD_PATH")
57+
# We could just check for success, but then failures are hard to debug.
58+
# Instead we use `_execute` to also capture `stdout` and `stderr`.
59+
# @test success(setenv(cmd, clean_env))
60+
trimcall = _execute(setenv(cmd, clean_env; dir = @__DIR__))
61+
if trimcall.exitcode != 0 && shouldpass
62+
@show trimcall.stdout
63+
@show trimcall.stderr
64+
end
65+
@test trimcall.exitcode == 0 broken=!shouldpass
66+
@test isfile(binpath) broken=!shouldpass
67+
@test success(`$(binpath) 1.0`) broken=!shouldpass
6168
end
62-
@test trimcall.exitcode == 0
63-
@test isfile(binpath)
64-
@test success(`$(binpath) 1.0`)
6569
end

test/trim/src/TrimTest.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module TrimTest
22
#=
33
Currently, trimming only works if the target code is in a package. I.e., trying to trim
44
```julia
5-
include("trimmable_optimization.jl")
5+
include("optimization_trimmable.jl")
66
function (@main)(argv::Vector{String})::Cint
77
minimize(1.0)
88
return 0
@@ -11,7 +11,7 @@ end
1111
or even
1212
```julia
1313
mod MyMod
14-
include("trimmable_optimization.jl")
14+
include("optimization_trimmable.jl")
1515
end
1616
function (@main)(argv::Vector{String})::Cint
1717
MyMod.minimize(1.0)
@@ -22,5 +22,6 @@ segfaults `juliac`. Looking at the segfault stacktrace it seems the culprit is
2222
`const cache = init(...)`. Either way, we circumvent the segfault by putting
2323
this below code into a package definition.
2424
=#
25-
include("../trimmable_optimization.jl")
25+
include("../optimization_trimmable.jl")
26+
include("../optimization_clean.jl")
2627
end

0 commit comments

Comments
 (0)