Some DRY of tests refactoring where appropriate and a quick format/lint

This commit is contained in:
2024-02-15 13:18:33 +00:00
parent 43a3d7a098
commit 94838669da
8 changed files with 88 additions and 50 deletions

View File

@@ -13,7 +13,7 @@ codebases=("bhmie-f.zip" "bhmie-c.zip")
# if bhmie_dir containers bhmie-c/bhmie.so, bhmie-f/bhmie.so, and bhmie-f/bhmie_f77.so then we can skip the build
bhmie_dir=$1
if [ -f $bhmie_dir/bhmie-c/bhmie.so ] && [ -f $bhmie_dir/bhmie-f/bhmie.so ] && [ -f $bhmie_dir/bhmie-f/bhmie_f77.so ]; then
echo "bhmie-c/bhmie.so, bhmie-f/bhmie.so, and bhmie-f/bhmie_f77.so already exist. Skipping build."
echo "FFI shared objects already exist. Skipping build."
exit 0
fi

View File

@@ -1,7 +1,5 @@
module FFIWraps
include("../anchors.jl")
if !@isdefined TEST_DIR
include("../anchors.jl")
import .Anchors: TEST_DIR

View File

@@ -4,25 +4,17 @@ using PropCheck
using Debugger
using PyCall
if !@isdefined TestUtils
include("testutils.jl")
end
include("../anchors.jl")
TestUtils.init_pyenv()
TestUtils.singleton_include("../anchors.jl", :Anchors, @__MODULE__)
import .Anchors: TEST_DIR, SRC_DIR, ROOT_DIR
if !@isdefined TestUtils
include(joinpath(TEST_DIR, "testutils.jl"))
end
if !@isdefined miemfp
include(joinpath(SRC_DIR, "miemfp.jl"))
end
if !@isdefined FFIWraps
include(joinpath(TEST_DIR, "ffi_wraps.jl"))
end
# Set up the Python environment
run(`$ROOT_DIR/setup_venv.sh`)
ENV["PYTHON"] = joinpath(ROOT_DIR, ".venv/bin/python")
TestUtils.singleton_include(joinpath(SRC_DIR, "miemfp.jl"), :miemfp, @__MODULE__)
TestUtils.singleton_include(joinpath(TEST_DIR, "ffi_wraps.jl"), :FFIWraps, @__MODULE__)
@pyinclude(joinpath(TEST_DIR, "miemfp_tests.py"))

View File

@@ -1,9 +1,14 @@
from typing import List, Tuple
import asyncio
import numpy as np
from hypothesis import errors, given, settings, strategies as st # type: ignore
from typing import List, Tuple
def compare_bhmie_functions(f1, f2, event1: asyncio.Event, event2: asyncio.Event) -> Tuple[bool, str]:
import numpy as np
from hypothesis import errors, given, settings # type: ignore
from hypothesis import strategies as st # type: ignore
def compare_bhmie_functions(
f1, f2, event1: asyncio.Event, event2: asyncio.Event
) -> Tuple[bool, str]:
async def async_closure(
x: float,
cxref: Tuple[float, float],
@@ -28,9 +33,26 @@ def compare_bhmie_functions(f1, f2, event1: asyncio.Event, event2: asyncio.Event
# Must be bigger than an atom but still nanoscale
x=st.floats(min_value=0.1, max_value=100),
# Refractive indeces must be within a physically reasonable range
cxref=st.tuples(st.floats(min_value=0.1, max_value=4.0), st.floats(min_value=0.1, max_value=4.0)),
cxs1=st.lists(st.tuples(st.floats(min_value=0.1, allow_infinity=False), st.floats(min_value=0.1, allow_infinity=False)), min_size=10, max_size=100),
cxs2=st.lists(st.tuples(st.floats(min_value=0.1, allow_infinity=False), st.floats(min_value=0.1, allow_infinity=False)), min_size=10, max_size=100),
cxref=st.tuples(
st.floats(min_value=0.1, max_value=4.0),
st.floats(min_value=0.1, max_value=4.0),
),
cxs1=st.lists(
st.tuples(
st.floats(min_value=0.1, allow_infinity=False),
st.floats(min_value=0.1, allow_infinity=False),
),
min_size=10,
max_size=100,
),
cxs2=st.lists(
st.tuples(
st.floats(min_value=0.1, allow_infinity=False),
st.floats(min_value=0.1, allow_infinity=False),
),
min_size=10,
max_size=100,
),
)
def sync_closure(
x: float,

View File

@@ -3,9 +3,12 @@ using Test
include("testutils.jl")
include("../src/nanoconc.jl")
# Set up the Python environment
TestUtils.init_pyenv()
# include("nanoconc_tests.jl")
include("miemfp_tests.jl")
TestUtils.singleton_include("miemfp_tests.jl", :miemfp, @__MODULE__)
# include("quantumcalc_tests.jl")
include("benchmarks.jl")
TestUtils.singleton_include("benchmarks.jl", :Benchmarks, @__MODULE__)
Benchmarks.bench_vs_ffi()

View File

@@ -3,16 +3,39 @@ using Serialization
using Test
function singleton_include(filepath::String, m::Symbol, calling_module::Module)
try
target = getproperty(calling_module, m)
if !isdefined(target, :Module)
raise("Module $m not defined in $calling_module")
end
catch
Base.include(calling_module, filepath)
end
end
singleton_include("../anchors.jl", :Anchors, @__MODULE__)
import .Anchors: TEST_DIR, SRC_DIR, ROOT_DIR
function init_pyenv()
if "PYTHON" in keys(ENV) && ENV["PYTHON"] != joinpath(ROOT_DIR, ".venv/bin/python")
run(`$ROOT_DIR/setup_venv.sh`)
ENV["PYTHON"] = joinpath(ROOT_DIR, ".venv/bin/python")
end
end
function fieldvalues(obj)
[getfield(obj, f) for f in fieldnames(typeof(obj))]
end
deep_compare(a, b; rtol::Real=sqrt(eps()), atol::Real=0.) = a == b # base case: use ==
deep_compare(a::Union{AbstractFloat, Complex}, b::Union{AbstractFloat, Complex}; rtol::Real=sqrt(eps()), atol::Real=0.) = isapprox(a, b) # for floats, use isapprox
deep_compare(a::Union{Array{AbstractFloat}, Array{Complex}}, b::Union{Array{AbstractFloat}, Array{Complex}}; rtol::Real=sqrt(eps()), atol::Real=0.) = isapprox(a, b) # for arrays of floats, use isapprox element-wise
deep_compare(a::AbstractArray, b::AbstractArray; rtol::Real=sqrt(eps()), atol::Real=0.) = all(deep_compare.(a, b; rtol, atol)) # for arrays of other types, recurse
deep_compare(a::Tuple, b::Tuple; rtol::Real=sqrt(eps()), atol::Real=0.) = all(deep_compare.(a, b; rtol, atol)) # for tuples, recurse
deep_compare(a::T, b::T; rtol::Real=sqrt(eps()), atol::Real=0.) where {T <: Any} = deep_compare(fieldvalues(a), fieldvalues(b); rtol, atol) # for composite types, recurse
deep_compare(a, b; rtol::Real=sqrt(eps()), atol::Real=0.0) = a == b # base case: use ==
deep_compare(a::Union{AbstractFloat,Complex}, b::Union{AbstractFloat,Complex}; rtol::Real=sqrt(eps()), atol::Real=0.0) = isapprox(a, b) # for floats, use isapprox
deep_compare(a::Union{Array{AbstractFloat},Array{Complex}}, b::Union{Array{AbstractFloat},Array{Complex}}; rtol::Real=sqrt(eps()), atol::Real=0.0) = isapprox(a, b) # for arrays of floats, use isapprox element-wise
deep_compare(a::AbstractArray, b::AbstractArray; rtol::Real=sqrt(eps()), atol::Real=0.0) = all(deep_compare.(a, b; rtol, atol)) # for arrays of other types, recurse
deep_compare(a::Tuple, b::Tuple; rtol::Real=sqrt(eps()), atol::Real=0.0) = all(deep_compare.(a, b; rtol, atol)) # for tuples, recurse
deep_compare(a::T, b::T; rtol::Real=sqrt(eps()), atol::Real=0.0) where {T<:Any} = deep_compare(fieldvalues(a), fieldvalues(b); rtol, atol) # for composite types, recurse
function test_from_serialized(fn::Function, filename::String)
argskwargs, out = open(filename, "r") do f