diff --git a/packages/cultmath/tests/CultMath.Tests/NoiseGradTests.cs b/packages/cultmath/tests/CultMath.Tests/NoiseGradTests.cs index e12cdfe0..77ec4819 100644 --- a/packages/cultmath/tests/CultMath.Tests/NoiseGradTests.cs +++ b/packages/cultmath/tests/CultMath.Tests/NoiseGradTests.cs @@ -8,42 +8,17 @@ public sealed class NoiseGradTests { // ---- Invariant-8 property harness (shared by snoise_grad, fbm_grad, ridged_grad) ---- // - // eps=0.0001f: a sweep at {0.001, 0.0005, 0.0002, 0.0001, 0.00005} against a self-consistency - // filter (below) found the clean central-difference error bottoming out at 0.0001 for all three - // functions (0.008 for snoise_grad, 0.034 for fbm_grad at 4 octaves, 0.107 for ridged_grad at 3 - // octaves), then rising again at 0.00005 as float32 rounding noise starts to dominate: this is - // the same "eps too large -> curvature error, eps too small -> rounding noise" shape as - // CellularAndSminGradTests, just with the sweet spot an order of magnitude smaller than - // cellular's 0.0005f, because snoise's simplex lattice has real curvature at a finer length - // scale than cellular's Euclidean distance field. Each tolerance below keeps roughly 4-5x - // headroom over its own measured clean floor (Hands, cut 2a-ii-a, seeds 0xA11CE/0xA11CF/0xA11D0, - // 50,000-80,000 samples in [-3, 3]^3 per function). - // - // Self-consistency filter: snoise_grad's simplex lattice has internal corner-selection - // boundaries (where two of x0's components tie) that are exactly smooth in real arithmetic but, - // at specific eps values, a float32-precision coincidence can put one of the two central- - // difference sample points close enough to such a boundary to spike the finite-difference - // estimate by orders of magnitude (confirmed by hand: at one such point central differences at - // eps=1e-5 and 1e-6 agreed with the analytic gradient to within noise, while eps=1e-4 alone was - // off by 8+, i.e. a fluke tied to that one step size, not a real gradient error). Comparing the - // central difference at eps against the same estimate at 4*eps and skipping the point when they - // disagree by more than consistencyThreshold catches these flukes without needing an analytic - // description of every lattice seam (cellular's F1=F2 seam has one; the simplex lattice's - // corner-tie seams do not have as clean a closed form here). This plays the same role as - // cellular's F1=F2/F2=F3 band exclusion in CellularAndSminGradTests. + // Each test compares a function's analytic gradient with central differences of its own .w at + // random points in [-3, 3]^3. eps trades curvature error (eps too large) against float32 + // rounding noise (eps too small). snoise is continuous across simplex-cell boundaries (the + // continuity test below), so no point needs excluding for a seam; ridged_grad excludes a band + // around its own deliberate crease. Each tolerance is at least 5x the largest error the harness + // itself produces at its seed. private const float PropertyEps = 0.0001f; - private const float PropertyCoarseEps = PropertyEps * 4.0f; - private const float ConsistencyThreshold = 0.02f; - private const float SnoiseGradTolerance = 0.04f; - private const float FbmGradTolerance = 0.15f; - private const float RidgedGradTolerance = 0.5f; + private const float SnoiseGradTolerance = 0.03f; + private const float FbmGradTolerance = 0.05f; + private const float RidgedGradTolerance = 0.05f; - // Detection floor (Hands, cut 2a-ii-a, snoise_grad harness, 1000 clean points, seed 0xDE7EC7, - // PropertyEps/SnoiseGradTolerance above): a uniform multiplicative scale on the returned gradient - // is caught at 1.01x, survives at 1.005x; a uniform additive offset is caught at 0.035, survives - // at 0.03. fbm_grad and ridged_grad share the same central-difference harness and per-octave - // scaling by snoise_grad's gradient, so a mutant of the same shape in either is caught at a - // similar or tighter scale (their tolerances have comparable headroom over their own floors). [Fact] public void SnoiseGradGradientMatchesCentralDifferencesOfItsOwnValue() { @@ -57,15 +32,12 @@ public sealed class NoiseGradTests var g = math.snoise_grad(p); float F(float3 q) => math.snoise_grad(q).w; var fine = CentralDifference(F, p, PropertyEps); - var coarse = CentralDifference(F, p, PropertyCoarseEps); - if (ChebyshevDistance(fine, coarse) > ConsistencyThreshold) - continue; tested++; AssertGradientWithinTolerance(fine, g, SnoiseGradTolerance); } - Assert.True(tested >= 150, $"only {tested} of {attempts} points were clean of a lattice seam"); + Assert.True(tested >= 150, $"only {tested} of {attempts} points were usable"); } [Fact] @@ -84,15 +56,12 @@ public sealed class NoiseGradTests var g = math.fbm_grad(p, octaves, lacunarity, gain); float F(float3 q) => math.fbm_grad(q, octaves, lacunarity, gain).w; var fine = CentralDifference(F, p, PropertyEps); - var coarse = CentralDifference(F, p, PropertyCoarseEps); - if (ChebyshevDistance(fine, coarse) > ConsistencyThreshold) - continue; tested++; AssertGradientWithinTolerance(fine, g, FbmGradTolerance); } - Assert.True(tested >= 150, $"only {tested} of {attempts} points were clean of a lattice seam"); + Assert.True(tested >= 150, $"only {tested} of {attempts} points were usable"); } [Fact] @@ -118,15 +87,12 @@ public sealed class NoiseGradTests var g = math.ridged_grad(p, octaves, lacunarity, gain); float F(float3 q) => math.ridged_grad(q, octaves, lacunarity, gain).w; var fine = CentralDifference(F, p, PropertyEps); - var coarse = CentralDifference(F, p, PropertyCoarseEps); - if (ChebyshevDistance(fine, coarse) > ConsistencyThreshold) - continue; tested++; AssertGradientWithinTolerance(fine, g, RidgedGradTolerance); } - Assert.True(tested >= 150, $"only {tested} of {attempts} points were clean of a zero band or lattice seam"); + Assert.True(tested >= 150, $"only {tested} of {attempts} points were outside the zero band"); } private static bool AnyOctaveNearZero(float3 p, int octaves, float lacunarity, float band) @@ -150,9 +116,6 @@ public sealed class NoiseGradTests (f(p + new float3(0.0f, eps, 0.0f)) - f(p - new float3(0.0f, eps, 0.0f))) / (2.0f * eps), (f(p + new float3(0.0f, 0.0f, eps)) - f(p - new float3(0.0f, 0.0f, eps))) / (2.0f * eps)); - private static double ChebyshevDistance(float3 a, float3 b) => - Math.Max(Math.Abs(a.x - b.x), Math.Max(Math.Abs(a.y - b.y), Math.Abs(a.z - b.z))); - private static void AssertGradientWithinTolerance(float3 numeric, float4 analytic, float tolerance) { AssertWithinTolerance(numeric.x, analytic.x, tolerance); @@ -232,10 +195,8 @@ public sealed class NoiseGradTests [Fact] public void SnoiseGradValueIsBitEqualToSnoise() { - // Both compute 42 * dot(m4, px) via the exact same operation order (math.cs comment on - // snoise_grad); measured bit-equal on 20,000 random points in [-100, 100]^3 (Hands, cut - // 2a-ii-a, seed 0x50F2, maxDiff 0), so this asserts the stronger bit-exact claim rather than - // only the spec's 1e-6 floor. + // Both compute 105 * dot(m4, px) via the exact same operation order (math.cs comment on + // snoise_grad), so this asserts bit equality over 5,000 random points in [-100, 100]^3. var random = new System.Random(0x50F2); for (var i = 0; i < 5000; i++) { @@ -326,8 +287,10 @@ public sealed class NoiseGradTests const float gain = 0.6f; var random = new System.Random(0xF6B12); var tested = 0; - while (tested < 32) + var attempts = 0; + while (tested < 32 && attempts < 4000) { + attempts++; var p = RandomPoint(random); if (AnyOctaveNearZero(p, 3, lacunarity, 0.1f)) continue; @@ -361,6 +324,8 @@ public sealed class NoiseGradTests Assert.Equal(expectedGradient.y, actual.y); Assert.Equal(expectedGradient.z, actual.z); } + + Assert.True(tested == 32, $"only {tested} of {attempts} points were outside the zero band"); } // ---- ridged_grad's deliberate crease at n = 0 ----