diff --git a/Project.toml b/Project.toml index bc8d305..61aa960 100644 --- a/Project.toml +++ b/Project.toml @@ -4,7 +4,9 @@ authors = ["Samuel Belko and contributors"] version = "1.1.0" [compat] +AllocCheck = "0.2" Aqua = "0.8" +BenchmarkTools = "1" JET = "0.9, 0.10, 0.11" LinearAlgebra = "1.10" SafeTestsets = "0.1" @@ -13,7 +15,9 @@ Test = "1.10" julia = "1.10" [extras] +AllocCheck = "9b6a8646-10ed-4001-bbdc-1d2f46dfbb1a" Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" +BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f" @@ -21,4 +25,4 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Aqua", "JET", "LinearAlgebra", "SafeTestsets", "Statistics", "Test"] +test = ["AllocCheck", "Aqua", "BenchmarkTools", "JET", "LinearAlgebra", "SafeTestsets", "Statistics", "Test"] diff --git a/test/alloc_tests.jl b/test/alloc_tests.jl new file mode 100644 index 0000000..3e8343e --- /dev/null +++ b/test/alloc_tests.jl @@ -0,0 +1,54 @@ +using SurrogatesBase +using AllocCheck +using BenchmarkTools +using Test + +# This file tests that implementations of the SurrogatesBase interface +# can be made allocation-free when properly implemented. +# Since SurrogatesBase is an interface-only package, we test with +# simple concrete implementations. + +# Simple deterministic surrogate that returns a constant value +struct ConstantSurrogate <: AbstractDeterministicSurrogate + value::Float64 +end + +# Allocation-free call operator +(s::ConstantSurrogate)(::Float64) = s.value + +# Simple linear surrogate: f(x) = a*x + b +struct LinearSurrogate <: AbstractDeterministicSurrogate + a::Float64 + b::Float64 +end + +# Allocation-free call operator +(s::LinearSurrogate)(x::Float64) = s.a * x + s.b + +@testset "Allocation Tests" begin + @testset "ConstantSurrogate allocation-free" begin + s = ConstantSurrogate(42.0) + x = 1.0 + + # Warmup + s(x) + + # Use BenchmarkTools for accurate allocation measurement + result = @benchmark $s($x) + @test result.memory == 0 + @test result.allocs == 0 + end + + @testset "LinearSurrogate allocation-free" begin + s = LinearSurrogate(2.0, 1.0) + x = 3.0 + + # Warmup + s(x) + + # Use BenchmarkTools for accurate allocation measurement + result = @benchmark $s($x) + @test result.memory == 0 + @test result.allocs == 0 + end +end diff --git a/test/runtests.jl b/test/runtests.jl index aa4385f..6823a99 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -117,3 +117,8 @@ end @test length(m) == 3 @test m[1] ≈ parameters(ss) end + +# Allocation tests - run in nopre group to avoid precompilation interference +if get(ENV, "GROUP", "all") == "all" || get(ENV, "GROUP", "all") == "nopre" + @safetestset "Allocation Tests" include("alloc_tests.jl") +end