Skip to content

Commit

Permalink
Use native julia for solver tests (#151)
Browse files Browse the repository at this point in the history
Closes #150.
  • Loading branch information
FriesischScott committed Jan 16, 2024
1 parent d77d552 commit 582e1be
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 85 deletions.
2 changes: 0 additions & 2 deletions src/solvers/solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ function run(solver::Solver, folder::String)
source = solver.source
args = solver.args

old_pwd = pwd()

out = basename(binary) * "UncertaintyQuantification.out"
err = basename(binary) * "UncertaintyQuantification.err"

Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ HypothesisTests = "09f84164-cd44-5f33-b23f-e6b0d136a0d5"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
QuasiMonteCarlo = "8a4e6c94-4038-4cdc-81c3-7e6ffdb2a71b"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
43 changes: 24 additions & 19 deletions test/models/externalmodel.jl
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
@testset "ExternalModel" begin
sourcedir = tempdir()
sourcefile = ["in.txt"]
sourcefile = ["radius.jl"]

# create source file for radius solver
open(joinpath(sourcedir, "radius.jl"), "w") do input
println(input, "x = {{{:x}}}")
println(input, "y = {{{:y}}}")
println(input, "z = sqrt(x^2+y^2)")
println(input, "write(\"out.txt\", string(z))")
end

# create source file for squared solver
open(joinpath(sourcedir, "squared.jl"), "w") do input
println(input, "x = parse(Float64, readline(\"out.txt\"))")
println(input, "y = x^2")
println(input, "write(\"out-squared.txt\", string(y))")
end

numberformats = Dict(:x => ".8e", :* => ".8e")

radius = Extractor(
base -> begin
map(x -> parse(Float64, x), readlines(joinpath(base, "out.txt")))[1]
return parse(Float64, readline(joinpath(base, "out.txt")))
end, :r
)

if Sys.iswindows()
solver = Solver(joinpath(pwd(), "solvers/bin/radius.exe"), "in.txt")
solver2 = Solver(joinpath(pwd(), "solvers/bin/squared.exe"), "out.txt")
elseif Sys.isapple()
solver = Solver(joinpath(pwd(), "solvers/bin/radius-mac"), "in.txt")
solver2 = Solver(joinpath(pwd(), "solvers/bin/squared-mac"), "out.txt")
else
solver = Solver(joinpath(pwd(), "solvers/bin/radius"), "in.txt")
solver2 = Solver(joinpath(pwd(), "solvers/bin/squared"), "out.txt")
end
binary = joinpath(Sys.BINDIR, "julia")

open(joinpath(sourcedir, "in.txt"), "w") do input
println(input, "{{{ :x }}}")
println(input, "{{{ :y }}}")
end
solver = Solver(binary, "radius.jl")
solver2 = Solver(binary, "squared.jl")

open(joinpath(sourcedir, "extra.txt"), "w") do input
println(input, "This is an extra file")
Expand All @@ -47,10 +51,11 @@
)

evaluate!(ext, df)

@test length(readdir(readdir(ext.workdir; join=true)[1])) == 5
@test "extra.txt" in
readdir(readdir(readdir(ext.workdir; join=true)[1]; join=true)[1])
@test isapprox(df.r, sqrt.(df.x .^ 2 .+ df.y .^ 2))
@test df.r sqrt.(df.x .^ 2 .+ df.y .^ 2)
end

@testset "Cleanup" begin
Expand All @@ -77,12 +82,12 @@

squared = Extractor(
base -> begin
map(x -> parse(Float64, x), readlines(joinpath(base, "out-squared.txt")))[1]
return parse(Float64, readline(joinpath(base, "out-squared.txt")))
end,
:r2,
)

ext2 = ExternalModel("", "", squared, solver2; workdir=workdir, cleanup=true)
ext2 = ExternalModel(sourcedir, ["squared.jl"], squared, solver2; workdir=workdir, cleanup=true)

evaluate!([ext1, ext2], df)
@test df.r2 df.r .^ 2
Expand Down
Binary file removed test/solvers/bin/radius
Binary file not shown.
Binary file removed test/solvers/bin/radius-mac
Binary file not shown.
Binary file removed test/solvers/bin/radius.exe
Binary file not shown.
Binary file removed test/solvers/bin/squared
Binary file not shown.
Binary file removed test/solvers/bin/squared-mac
Binary file not shown.
Binary file removed test/solvers/bin/squared.exe
Binary file not shown.
23 changes: 9 additions & 14 deletions test/solvers/solvers.jl
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
@testset "Solvers" begin
binary = ""
if Sys.iswindows()
binary = joinpath(pwd(), "solvers/bin/radius.exe")
elseif Sys.isapple()
binary = joinpath(pwd(), "solvers/bin/radius-mac")
else
binary = joinpath(pwd(), "solvers/bin/radius")
end
binary = joinpath(Sys.BINDIR, "julia")

solver = Solver(binary, "", "in.txt")
solver = Solver(binary, "radius.jl"; args="--project")

tmp = tempdir()

open(joinpath(tmp, "in.txt"), "w") do input
println(input, "0.5")
println(input, "0.5")
open(joinpath(tmp, "radius.jl"), "w") do input
println(input, "x = 0.5")
println(input, "y = 0.5")
println(input, "z = sqrt(x^2+y^2)")
println(input, "write(\"out.txt\", string(z))")
end

run(solver, tmp)

radius = map(x -> parse(Float64, x), readlines(joinpath(tmp, "out.txt")))
radius = parse(Float64, readline(joinpath(tmp, "out.txt")))

@test radius[1] == sqrt(0.5^2 + 0.5^2)
@test radius == sqrt(0.5^2 + 0.5^2)

current_dir = pwd()

Expand Down
27 changes: 0 additions & 27 deletions test/solvers/src/radius.c

This file was deleted.

23 changes: 0 additions & 23 deletions test/solvers/src/squared.c

This file was deleted.

0 comments on commit 582e1be

Please sign in to comment.