From 22233afd150a59a5df9f1bd5ea1015f2fa1913d7 Mon Sep 17 00:00:00 2001 From: Claude Code Date: Tue, 30 Dec 2025 07:05:46 -0500 Subject: [PATCH 1/2] Add AllocCheck tests for allocation regression prevention MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The package is already well-optimized with zero allocations and type-stable code. This commit adds AllocCheck.jl tests to ensure future changes don't introduce allocations. Test coverage: - count_rand (small 位 algorithm) - ad_rand (large 位 algorithm) - pois_rand (main entry point) - pois_rand with PassthroughRNG - procf (probability calculation helper) 馃 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- Project.toml | 4 +++- test/alloc_tests.jl | 49 +++++++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 6 ++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 test/alloc_tests.jl 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..3330957 --- /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 From 30e1766640abe09f6c86b12d5835544bf2d6016b Mon Sep 17 00:00:00 2001 From: Claude Code Date: Tue, 30 Dec 2025 07:13:13 -0500 Subject: [PATCH 2/2] Fix formatting in alloc_tests.jl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add space after comma in NTuple type annotations to pass format check. 馃 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- test/alloc_tests.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/alloc_tests.jl b/test/alloc_tests.jl index 3330957..9fa5f29 100644 --- a/test/alloc_tests.jl +++ b/test/alloc_tests.jl @@ -43,7 +43,7 @@ using Random @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} + @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