|
95 | 95 | A = get_param_array(chn) |
96 | 96 | @test size(A) == (3, 2) |
97 | 97 | end |
| 98 | + |
| 99 | +@testitem "_safe_int_floor rejects non-finite input with a clear DomainError" begin |
| 100 | + using ComposableTuringIDModels: _safe_int_floor |
| 101 | + # A non-finite value must raise a `DomainError` naming the offending value, |
| 102 | + # not an `InexactError` from the eventual `BigInt` conversion. |
| 103 | + for x in (Inf, -Inf, NaN) |
| 104 | + @test_throws DomainError _safe_int_floor(x) |
| 105 | + end |
| 106 | + # Finite values, including ones outside the `Int` range, are unaffected. |
| 107 | + @test _safe_int_floor(3.7) == 3 |
| 108 | + @test _safe_int_floor(exp(48.0)) == floor(BigInt, exp(48.0)) |
| 109 | +end |
| 110 | + |
| 111 | +@testitem "SafePoisson and SafeNegativeBinomial reject non-finite rates" begin |
| 112 | + using ComposableTuringIDModels, Distributions, Random |
| 113 | + Random.seed!(140) |
| 114 | + # A non-finite `SafePoisson` rate must raise a `DomainError`. |
| 115 | + @test_throws DomainError rand(SafePoisson(Inf)) |
| 116 | + @test_throws DomainError rand(SafePoisson(NaN)) |
| 117 | + # `SafeNegativeBinomial` samples through an inner `SafePoisson` whose rate |
| 118 | + # is a Gamma mixing draw. Degenerate `r`/`p` make that draw non-finite, so |
| 119 | + # it reaches the same guard. |
| 120 | + @test_throws DomainError rand(SafeNegativeBinomial(Inf, 0.5)) |
| 121 | + @test_throws DomainError rand(SafeNegativeBinomial(1.0, 0.0)) |
| 122 | +end |
| 123 | + |
| 124 | +@testitem "quantile and mode reject non-finite parameters, not just rand" begin |
| 125 | + using ComposableTuringIDModels, Distributions |
| 126 | + # `quantile` (both types) and `SafeNegativeBinomial`'s `mode` delegate to |
| 127 | + # the wrapped `Distributions.Poisson` / `NegativeBinomial`, which floor |
| 128 | + # internally without passing through `_safe_int_floor`. They must raise the |
| 129 | + # same `DomainError` naming the offending value on a non-finite parameter. |
| 130 | + for λ in (Inf, NaN) |
| 131 | + @test_throws DomainError quantile(SafePoisson(λ), 0.5) |
| 132 | + end |
| 133 | + for (r, p) in ((Inf, 0.5), (NaN, 0.5), (1.0, Inf), (1.0, NaN)) |
| 134 | + @test_throws DomainError quantile(SafeNegativeBinomial(r, p), 0.5) |
| 135 | + @test_throws DomainError mode(SafeNegativeBinomial(r, p)) |
| 136 | + end |
| 137 | + # The guard must be transparent for finite parameters: each guarded entry |
| 138 | + # point still delegates to the wrapped distribution and returns its value. |
| 139 | + for q in (0.1, 0.5, 0.9) |
| 140 | + @test quantile(SafePoisson(4.0), q) == quantile(Poisson(4.0), q) |
| 141 | + @test quantile(SafeNegativeBinomial(3.0, 0.4), q) == |
| 142 | + quantile(NegativeBinomial(3.0, 0.4), q) |
| 143 | + end |
| 144 | + @test mode(SafeNegativeBinomial(3.0, 0.4)) == |
| 145 | + mode(NegativeBinomial(3.0, 0.4)) |
| 146 | +end |
0 commit comments