Skip to content

Commit 8c45ab3

Browse files
committed
Add trimming test
1 parent 69b54a1 commit 8c45ab3

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

test/trim/main.jl

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

test/trim/runtests.jl

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using SafeTestsets
2+
3+
@safetestset "Clean implementation (non-trimmable)" begin
4+
using JET
5+
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),))
11+
end
12+
13+
@safetestset "Trimmable implementation" begin
14+
using JET
15+
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),))
21+
end
22+
23+
@safetestset "Run trim" begin
24+
# https://discourse.julialang.org/t/capture-stdout-and-stderr-in-case-a-command-fails/101772/3?u=romeov
25+
"Run a Cmd object, returning the stdout & stderr contents plus the exit code"
26+
function _execute(cmd::Cmd)
27+
out = Pipe()
28+
err = Pipe()
29+
process = run(pipeline(ignorestatus(cmd); stdout = out, stderr = err))
30+
close(out.in)
31+
close(err.in)
32+
out = (
33+
stdout = String(read(out)), stderr = String(read(err)),
34+
exitcode = process.exitcode,
35+
)
36+
return out
37+
end
38+
39+
JULIAC = normpath(
40+
joinpath(
41+
Sys.BINDIR, Base.DATAROOTDIR, "julia", "juliac",
42+
"juliac.jl"
43+
)
44+
)
45+
@test isfile(JULIAC)
46+
binpath = tempname()
47+
cmd = `$(Base.julia_cmd()) --project=. --depwarn=error $(JULIAC) --experimental --trim=unsafe-warn --output-exe $(binpath) main.jl`
48+
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
61+
end
62+
@test trimcall.exitcode == 0
63+
@test isfile(binpath)
64+
@test success(`$(binpath) 1.0`)
65+
end

test/trim/src/TrimTest.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module TrimTest
2+
include("../trimmable_optimization.jl")
3+
end

0 commit comments

Comments
 (0)