diff --git a/Project.toml b/Project.toml index 9320add..29123b5 100644 --- a/Project.toml +++ b/Project.toml @@ -7,6 +7,7 @@ LogExpFunctions = "2ab3a3ac-af41-5b50-aa03-7779005ae688" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" [compat] +AllocCheck = "0.2" Aqua = "0.8" Distributions = "0.25" ExplicitImports = "1.14.0" @@ -18,6 +19,7 @@ Test = "1" julia = "1.10" [extras] +AllocCheck = "9b6a8646-10ed-4001-bbdc-1d2f46dfbb1a" Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" ExplicitImports = "7d51a73a-1435-4ff3-83d9-f097790105c7" @@ -26,4 +28,4 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] -test = ["Aqua", "Statistics", "Test", "Distributions", "JET", "ExplicitImports"] +test = ["AllocCheck", "Aqua", "Statistics", "Test", "Distributions", "JET", "ExplicitImports"] diff --git a/test/alloc_tests.jl b/test/alloc_tests.jl new file mode 100644 index 0000000..9fa5f29 --- /dev/null +++ b/test/alloc_tests.jl @@ -0,0 +1,49 @@ +using AllocCheck +using PoissonRandom +using Random + +@testset "AllocCheck - Zero Allocations" begin + @testset "count_rand" begin + @check_allocs function test_count_rand(rng::TaskLocalRNG, λ::Float64) + PoissonRandom.count_rand(rng, λ) + end + rng = Random.default_rng() + @test test_count_rand(rng, 2.0) isa Int + @test test_count_rand(rng, 5.0) isa Int + end + + @testset "ad_rand" begin + @check_allocs function test_ad_rand(rng::TaskLocalRNG, λ::Float64) + PoissonRandom.ad_rand(rng, λ) + end + rng = Random.default_rng() + @test test_ad_rand(rng, 10.0) isa Int + @test test_ad_rand(rng, 50.0) isa Int + end + + @testset "pois_rand" begin + @check_allocs function test_pois_rand(rng::TaskLocalRNG, λ::Float64) + pois_rand(rng, λ) + end + rng = Random.default_rng() + @test test_pois_rand(rng, 2.0) isa Int + @test test_pois_rand(rng, 10.0) isa Int + end + + @testset "pois_rand with PassthroughRNG" begin + @check_allocs function test_pois_rand_passthrough(rng::PassthroughRNG, λ::Float64) + pois_rand(rng, λ) + end + passthrough = PassthroughRNG() + @test test_pois_rand_passthrough(passthrough, 2.0) isa Int + @test test_pois_rand_passthrough(passthrough, 10.0) isa Int + end + + @testset "procf" begin + @check_allocs function test_procf(λ::Float64, K::Int, s::Float64) + PoissonRandom.procf(λ, K, s) + end + @test test_procf(10.0, 5, 3.162) isa NTuple{4, Float64} + @test test_procf(10.0, 15, 3.162) isa NTuple{4, Float64} + end +end diff --git a/test/runtests.jl b/test/runtests.jl index a172671..e909f1a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -89,3 +89,9 @@ println("testing mixed random sampler") for λ in [5.0, 10.0, 15.0, 20.0, 30.0] test_samples(pois_rand, Distributions.Poisson(λ), n_tsamples) end + +if get(ENV, "GROUP", "all") == "all" || get(ENV, "GROUP", "all") == "nopre" + @testset "Allocation Tests" begin + include("alloc_tests.jl") + end +end