// FCS Glass - tools/logic_tests_m2m4.cpp // // Tests for src/core/{spring,atlas,glyph_cache,sdf}. Compiles against the REAL headers // (the B-06 route), so there is no extraction step and the same file builds under MSVC // and gcc. Windows-free by construction. // // Linux: c++ -std=c++17 -I src tools/logic_tests_m2m4.cpp src/core/spring.cpp // src/core/atlas.cpp src/core/glyph_cache.cpp -o /tmp/t && /tmp/t // MSVC: cl /std:c++17 /W4 /EHsc /I src tools\logic_tests_m2m4.cpp src\core\spring.cpp // src\core\atlas.cpp src\core\glyph_cache.cpp // // Falsify-first: each group names the mutation that must break it (see the review note). #include #include #include #include #include #include #include "core/atlas.h" #include "core/glyph_cache.h" #include "core/sdf.h" #include "core/spring.h" using namespace fcs::core; static int g_pass = 0; static int g_fail = 0; #define CHECK(cond) \ do { \ if (cond) { \ ++g_pass; \ } else { \ ++g_fail; \ std::printf("FAIL %s:%d %s\n", __FILE__, __LINE__, #cond); \ } \ } while (0) #define CHECK_NEAR(a, b, eps) CHECK(std::fabs((a) - (b)) <= (eps)) namespace { const SpringRest kNoRest = {0.0, 0.0}; // never snaps: lets a test see the raw trajectory Spring Make(double zeta, double freq, double x0, double target, double v0, bool no_rest) { Spring s(x0); SpringParams p; p.freq_hz = freq; p.damping = zeta; s.SetParams(p); if (no_rest) s.SetRest(kNoRest); s.SetTarget(target); if (v0 != 0.0) s.Kick(v0); return s; } // Independent reference: RK4 on d'' = -2 z w d' - w^2 d, fine fixed step. void Rk4(double z, double freq, double d0, double v0, double t_end, double* d_out, double* v_out) { const double w = 2.0 * 3.14159265358979323846 * freq; double d = d0; double v = v0; const double h = 1e-5; const long n = static_cast(t_end / h + 0.5); auto acc = [&](double dd, double vv) { return -2.0 * z * w * vv - w * w * dd; }; for (long i = 0; i < n; ++i) { const double k1d = v, k1v = acc(d, v); const double k2d = v + 0.5 * h * k1v, k2v = acc(d + 0.5 * h * k1d, v + 0.5 * h * k1v); const double k3d = v + 0.5 * h * k2v, k3v = acc(d + 0.5 * h * k2d, v + 0.5 * h * k2v); const double k4d = v + h * k3v, k4v = acc(d + h * k3d, v + h * k3v); d += h / 6.0 * (k1d + 2 * k2d + 2 * k3d + k4d); v += h / 6.0 * (k1v + 2 * k2v + 2 * k3v + k4v); } *d_out = d; *v_out = v; } void TestSpring() { // S1: critical damping from rest never overshoots and never moves backwards. { Spring s = Make(1.0, 4.0, 0.0, 1.0, 0.0, true); double prev = s.value(); bool mono = true; bool under = true; for (int i = 0; i < 480; ++i) { s.Step(1.0 / 240.0); if (s.value() < prev - 1e-12) mono = false; if (s.value() > 1.0 + 1e-12) under = false; prev = s.value(); } CHECK(mono); CHECK(under); CHECK(s.value() > 0.999999); } // S2: under-damped overshoots by the textbook amount, exp(-pi z / sqrt(1 - z^2)). { const double z = 0.3; Spring s = Make(z, 4.0, 0.0, 1.0, 0.0, true); double peak = 0.0; for (int i = 0; i < 6000; ++i) { s.Step(1.0 / 4000.0); if (s.value() > peak) peak = s.value(); } const double expect = 1.0 + std::exp(-3.14159265358979323846 * z / std::sqrt(1.0 - z * z)); CHECK_NEAR(peak, expect, 1e-3); } // S3: over-damped never overshoots and is slower than critical at the same instant. { Spring over = Make(2.0, 4.0, 0.0, 1.0, 0.0, true); Spring crit = Make(1.0, 4.0, 0.0, 1.0, 0.0, true); bool no_over = true; for (int i = 0; i < 120; ++i) { over.Step(1.0 / 240.0); crit.Step(1.0 / 240.0); if (over.value() > 1.0 + 1e-12) no_over = false; } CHECK(no_over); CHECK(over.value() < crit.value()); } // S4: path independence. Ten small steps equal one big step, in every regime, with a // non-zero initial velocity. (A semi-implicit Euler step fails this.) { const double zs[] = {0.3, 1.0, 2.5}; for (double z : zs) { Spring a = Make(z, 3.0, 0.0, 2.0, 3.0, true); Spring b = Make(z, 3.0, 0.0, 2.0, 3.0, true); for (int i = 0; i < 10; ++i) a.Step(0.016); b.Step(0.16); CHECK_NEAR(a.value(), b.value(), 1e-9); CHECK_NEAR(a.velocity(), b.velocity(), 1e-8); } } // S5: agrees with an independent RK4 integration, in every regime. { const double zs[] = {0.3, 1.0, 2.5}; for (double z : zs) { Spring s = Make(z, 3.0, 0.0, 2.0, 3.0, true); s.Step(0.7); double d = 0.0; double v = 0.0; Rk4(z, 3.0, -2.0, 3.0, 0.7, &d, &v); CHECK_NEAR(s.value() - 2.0, d, 1e-7); CHECK_NEAR(s.velocity(), v, 1e-6); } } // S6: a spring settles EXACTLY on its target and reports it (R6 depends on this). { const double zs[] = {0.3, 1.0, 2.5}; for (double z : zs) { Spring s = Make(z, 4.0, 0.0, 100.0, 0.0, false); CHECK(s.Active()); s.Step(10.0); CHECK(!s.Active()); CHECK(s.value() == 100.0); CHECK(s.velocity() == 0.0); } } // S7: waking. Re-aiming at the same target does not wake a settled spring; a new one does. { Spring s = Make(1.0, 4.0, 0.0, 100.0, 0.0, false); s.Step(10.0); s.SetTarget(100.0); CHECK(!s.Active()); s.SetTarget(101.0); CHECK(s.Active()); s.Reset(5.0); CHECK(!s.Active() && s.value() == 5.0 && s.target() == 5.0); } // S8: a bad clock cannot corrupt state. { Spring s = Make(0.5, 4.0, 0.0, 1.0, 0.0, false); s.Step(0.05); const double x = s.value(); const double v = s.velocity(); const double bad[] = {0.0, -1.0, std::numeric_limits::quiet_NaN(), std::numeric_limits::infinity()}; for (double dt : bad) { s.Step(dt); CHECK(s.value() == x && s.velocity() == v && s.Active()); } } // S9: no restoring force (zero or NaN frequency) arrives at once instead of hanging. { Spring z0 = Make(1.0, 0.0, 0.0, 5.0, 0.0, false); z0.Step(0.016); CHECK(!z0.Active() && z0.value() == 5.0); Spring zn = Make(1.0, std::numeric_limits::quiet_NaN(), 0.0, 5.0, 0.0, false); zn.Step(0.016); CHECK(!zn.Active() && zn.value() == 5.0); } // S10: no numerical blow-up next to critical damping. { Spring ref = Make(1.0, 3.0, 0.0, 2.0, 3.0, true); ref.Step(0.3); const double zs[] = {1.0 - 1e-7, 1.0 + 1e-7, 1.0 + 1e-5, 1.0 - 1e-5}; const double tol[] = {1e-6, 1e-6, 1e-3, 1e-3}; for (int i = 0; i < 4; ++i) { Spring s = Make(zs[i], 3.0, 0.0, 2.0, 3.0, true); s.Step(0.3); CHECK(std::isfinite(s.value())); CHECK_NEAR(s.value(), ref.value(), tol[i]); } } // S11: a bad damping value cannot make a perpetual oscillator (idle loop must be able to sleep). { const double bad[] = {0.0, -3.0, std::numeric_limits::quiet_NaN()}; for (double zd : bad) { Spring s = Make(zd, 4.0, 0.0, 1.0, 0.0, false); int steps = 0; while (s.Active() && steps < 3600) { s.Step(1.0 / 60.0); ++steps; } CHECK(!s.Active()); CHECK(s.value() == 1.0); } } // S12: while far from the target it stays active (it must keep asking for frames). { Spring s = Make(1.0, 4.0, 0.0, 100.0, 0.0, false); s.Step(1.0 / 60.0); CHECK(s.Active() && s.value() > 0.0 && s.value() < 100.0); } } // --------------------------------------------------------------------------------------- bool Disjoint(const AtlasRect& a, const AtlasRect& b, int grow) { return a.x - grow >= b.x + b.w + grow || b.x - grow >= a.x + a.w + grow || a.y - grow >= b.y + b.h + grow || b.y - grow >= a.y + a.h + grow; } void TestAtlas() { // A1: many random sizes: all inside the atlas, none overlapping, padding respected. { const int W = 512, H = 512, P = 1; ShelfPacker pk(W, H, P); std::vector rects; std::uint32_t seed = 12345u; auto rnd = [&seed](int lo, int hi) { seed = seed * 1664525u + 1013904223u; return lo + static_cast((seed >> 8) % static_cast(hi - lo + 1)); }; long inner_area = 0; for (int i = 0; i < 5000; ++i) { AtlasRect r; const int w = rnd(4, 40), h = rnd(8, 36); if (!pk.Pack(w, h, &r)) break; rects.push_back(r); inner_area += static_cast(w) * h; } CHECK(rects.size() > 100); bool inside = true; bool apart = true; for (size_t i = 0; i < rects.size(); ++i) { const AtlasRect& a = rects[i]; if (a.x < P || a.y < P || a.x + a.w > W - P || a.y + a.h > H - P) inside = false; for (size_t j = i + 1; j < rects.size(); ++j) if (!Disjoint(a, rects[j], P)) apart = false; // each grown by P: padded cells never touch } CHECK(inside); CHECK(apart); CHECK(pk.used_height() <= H); std::printf(" info: atlas utilisation, synthetic 4-40 x 8-36 mix: %zu rects, %.1f%% of %dx%d\n", rects.size(), 100.0 * static_cast(inner_area) / (static_cast(W) * H), W, H); } // A2: full is reported, out is zeroed. { ShelfPacker pk(20, 20, 1); AtlasRect r; CHECK(pk.Pack(18, 18, &r)); CHECK(r.x == 1 && r.y == 1 && r.w == 18 && r.h == 18); r.x = 99; CHECK(!pk.Pack(1, 1, &r)); CHECK(r.x == 0 && r.y == 0 && r.w == 0 && r.h == 0); } // A3: a blank glyph consumes nothing. { ShelfPacker pk(64, 64, 1); AtlasRect r; r.x = 7; CHECK(pk.Pack(0, 5, &r)); CHECK(r.w == 0 && r.h == 0 && pk.used_height() == 0); CHECK(pk.Pack(5, -1, &r) && pk.used_height() == 0); } // A4: too wide or too tall for the atlas is refused, not wrapped or clipped. { ShelfPacker pk(20, 20, 1); AtlasRect r; CHECK(!pk.Pack(19, 5, &r)); CHECK(!pk.Pack(5, 19, &r)); CHECK(!pk.Pack(2000000000, 5, &r)); CHECK(pk.Pack(18, 5, &r)); } // A5: Reset starts over at the top left. { ShelfPacker pk(64, 64, 1); AtlasRect r; pk.Pack(10, 10, &r); pk.Pack(10, 10, &r); pk.Reset(); CHECK(pk.Pack(10, 10, &r) && r.x == 1 && r.y == 1 && pk.used_height() == 12); } // A6: growing the height lets a refused bitmap in; it never shrinks, and old rects stay put. { ShelfPacker pk(20, 20, 1); AtlasRect first, second; CHECK(pk.Pack(18, 18, &first)); CHECK(!pk.Pack(18, 18, &second)); pk.SetMaxHeight(40); CHECK(pk.Pack(18, 18, &second) && second.y == 21); pk.SetMaxHeight(10); CHECK(pk.max_height() == 40); CHECK(first.x == 1 && first.y == 1); } // A7: a short bitmap reuses an existing tall shelf instead of opening a new one. { ShelfPacker pk(100, 100, 1); AtlasRect tall, shorter; CHECK(pk.Pack(10, 20, &tall)); CHECK(pk.Pack(10, 8, &shorter)); CHECK(shorter.y == tall.y && shorter.x == 13); CHECK(pk.used_height() == 22); } } // --------------------------------------------------------------------------------------- GlyphKey Key(std::uint32_t face, std::uint32_t glyph, double em = 14.0, double scale = 1.5) { GlyphKey k; k.face = face; k.glyph = glyph; k.em_q = QuantizeEm(em); k.scale_q = QuantizeScale(scale); return k; } void TestGlyphCache() { // G1: insert, find, insert again returns the same slot without packing twice. { GlyphCache c(256, 256, 1); const GlyphSlot* a = c.Insert(Key(1, 65), 10, 12, 1, 11, 9); CHECK(a != nullptr && c.Find(Key(1, 65)) == a); CHECK(a->rect.w == 10 && a->rect.h == 12 && a->bearing_y == 11 && a->advance_px == 9); const int used = c.packer().used_height(); CHECK(c.Insert(Key(1, 65), 10, 12, 1, 11, 9) == a); CHECK(c.size() == 1 && c.packer().used_height() == used); CHECK(c.Find(Key(1, 66)) == nullptr); } // G1b: a duplicate insert packs NOTHING. Width 14 holds exactly one padded 12 px cell per // shelf, so a wrongly re-packed duplicate would open a second shelf and the height would double. { GlyphCache c(14, 100, 1); CHECK(c.Insert(Key(1, 65), 12, 12, 0, 0, 12) != nullptr); CHECK(c.packer().used_height() == 14); CHECK(c.Insert(Key(1, 65), 12, 12, 0, 0, 12) != nullptr); CHECK(c.packer().used_height() == 14); CHECK(c.Insert(Key(1, 66), 12, 12, 0, 0, 12) != nullptr); CHECK(c.packer().used_height() == 28); } // G2: the hash is order dependent (a plain xor would collide on this swap). { GlyphKeyHash h; CHECK(h(Key(1, 2)) != h(Key(2, 1))); CHECK(h(Key(1, 2, 14.0, 1.5)) != h(Key(1, 2, 14.0, 2.0))); CHECK(h(Key(1, 2, 14.0, 1.5)) != h(Key(1, 2, 16.0, 1.5))); GlyphKey sub = Key(1, 2); sub.subpx = 1; CHECK(h(sub) != h(Key(1, 2))); std::unordered_set seen; for (std::uint32_t f = 0; f < 4; ++f) for (std::uint32_t g = 0; g < 5000; ++g) seen.insert(h(Key(f, g))); CHECK(seen.size() >= 20000 - 2); } // G3: quantization keeps the spec's scales exact and absorbs float noise. { CHECK(QuantizeScale(1.0) == 256 && QuantizeScale(1.25) == 320 && QuantizeScale(1.5) == 384); CHECK(QuantizeScale(1.75) == 448 && QuantizeScale(2.0) == 512 && QuantizeScale(2.5) == 640); CHECK(QuantizeScale(1.5000001) == 384 && QuantizeScale(1.4999999) == 384); CHECK(QuantizeEm(14.0) == 224 && QuantizeEm(12.0) == 192); CHECK(QuantizeScale(0.0) == 1 && QuantizeScale(-2.0) == 1); CHECK(QuantizeScale(std::numeric_limits::quiet_NaN()) == 1); CHECK(QuantizeScale(1e9) == 65535 && QuantizeEm(1e9) == 65535); } // G4: a full atlas reports, keeps earlier slots valid, and recovers on Flush. { GlyphCache c(64, 64, 1); const GlyphSlot* first = c.Insert(Key(1, 0), 12, 12, 0, 0, 12); CHECK(first != nullptr); const AtlasRect first_rect = first->rect; int ok = 1; std::uint32_t g = 1; while (c.Insert(Key(1, g), 12, 12, 0, 0, 12) != nullptr && g < 100) { ++ok; ++g; } CHECK(ok == 16); // four 14 px cells per shelf, four shelves CHECK(c.overflowed()); CHECK(c.Find(Key(1, 0)) == first); // pointer still valid after many inserts CHECK(first->rect.x == first_rect.x && first->rect.y == first_rect.y); const unsigned gen = c.generation(); c.Flush(); CHECK(c.size() == 0 && !c.overflowed() && c.generation() == gen + 1); CHECK(c.Find(Key(1, 0)) == nullptr); CHECK(c.Insert(Key(1, 0), 12, 12, 0, 0, 12) != nullptr); } // G5: a blank glyph is cached with its advance and costs no atlas space. { GlyphCache c(64, 64, 1); const GlyphSlot* sp = c.Insert(Key(1, 32), 0, 0, 0, 0, 7); CHECK(sp != nullptr && sp->rect.w == 0 && sp->rect.h == 0 && sp->advance_px == 7); CHECK(c.packer().used_height() == 0 && !c.overflowed()); } } // --------------------------------------------------------------------------------------- void TestSdf() { const float hx = 50.0f, hy = 30.0f, r = 8.0f, bw = 1.0f; PremulColor fill; fill.a = 0.35f; PremulColor border; border.r = border.g = border.b = 0.10f; border.a = 0.10f; // D1: distance values at known points. CHECK_NEAR(SdRoundRect(0, 0, hx, hy, r), -30.0f, 1e-5f); CHECK_NEAR(SdRoundRect(50, 0, hx, hy, r), 0.0f, 1e-5f); CHECK_NEAR(SdRoundRect(0, 30, hx, hy, r), 0.0f, 1e-5f); const float arc = 0.70710678f * r; CHECK_NEAR(SdRoundRect(hx - r + arc, hy - r + arc, hx, hy, r), 0.0f, 1e-5f); // on the arc CHECK_NEAR(SdRoundRect(50, 30, hx, hy, r), r * (1.41421356f - 1.0f), 1e-4f); // sharp corner is outside // D2: symmetric in both axes. CHECK_NEAR(SdRoundRect(13, 7, hx, hy, r), SdRoundRect(-13, 7, hx, hy, r), 1e-6f); CHECK_NEAR(SdRoundRect(13, 7, hx, hy, r), SdRoundRect(13, -7, hx, hy, r), 1e-6f); CHECK_NEAR(SdRoundRect(13, 7, hx, hy, r), SdRoundRect(-13, -7, hx, hy, r), 1e-6f); // D3: an oversized radius degrades to a pill; the edge stays an edge. CHECK_NEAR(SdRoundRect(0, 0, hx, hy, 1000.0f), -30.0f, 1e-5f); CHECK_NEAR(SdRoundRect(50, 0, hx, hy, 1000.0f), 0.0f, 1e-5f); CHECK_NEAR(SdRoundRect(0, 0, hx, hy, -5.0f), -30.0f, 1e-5f); // negative radius = square corners // D4: distance grows monotonically walking out along +x. { bool mono = true; float prev = SdRoundRect(0, 0, hx, hy, r); for (int i = 1; i <= 70; ++i) { const float d = SdRoundRect(static_cast(i), 0, hx, hy, r); if (d < prev) mono = false; prev = d; } CHECK(mono); } // D5: panel colours at the pixel offsets that matter. { const PremulColor mid = PanelColor(-30.0f, bw, fill, border); // deep inside: pure fill CHECK_NEAR(mid.a, 0.35f, 1e-6f); const PremulColor edge = PanelColor(0.0f, bw, fill, border); // on the edge: half the border CHECK_NEAR(edge.a, 0.05f, 1e-6f); const PremulColor ring = PanelColor(-0.5f, bw, fill, border); // centre of a 1 px ring: pure border CHECK_NEAR(ring.a, 0.10f, 1e-6f); CHECK_NEAR(ring.r, 0.10f, 1e-6f); const PremulColor inner = PanelColor(-1.0f, bw, fill, border); // inner edge of the ring: half and half CHECK_NEAR(inner.a, 0.5f * 0.35f + 0.5f * 0.10f, 1e-6f); const PremulColor out = PanelColor(0.5f, bw, fill, border); // just outside: nothing CHECK(out.a == 0.0f && out.r == 0.0f); const PremulColor far = PanelColor(5.0f, bw, fill, border); CHECK(far.a == 0.0f); } // D6: the output stays a valid premultiplied colour everywhere (rgb <= a, a in 0..1). { bool valid = true; for (int i = -4000; i <= 500; ++i) { const PremulColor c = PanelColor(static_cast(i) * 0.01f, bw, fill, border); if (c.a < 0.0f || c.a > 1.0f || c.r > c.a + 1e-6f || c.g > c.a + 1e-6f || c.b > c.a + 1e-6f) valid = false; } CHECK(valid); } // D7: a wider ring keeps its width: at d = -(bw/2) the ring is fully covered for any bw >= 1. { const PremulColor c = PanelColor(-1.5f, 3.0f, fill, border); CHECK_NEAR(c.a, 0.10f, 1e-6f); } } } // namespace int main() { TestSpring(); TestAtlas(); TestGlyphCache(); TestSdf(); std::printf("m2m4 core: %d PASS / %d FAIL\n", g_pass, g_fail); return g_fail == 0 ? 0 : 1; }