|
| 1 | +// Smoke test for the vendored CSS Grid algorithm (Yoga PR #1894), pinned |
| 2 | +// against real Safari for the identical layout (see /tmp/gl/grid.html). |
| 3 | +// |
| 4 | +// display: grid; width: 600px; gap: 10px; |
| 5 | +// grid-template-columns: 100px 1fr 2fr; |
| 6 | +// grid-template-rows: 50px auto; |
| 7 | +// six children with fixed heights 20/30/25/40/15/35 |
| 8 | +#include <yoga/Yoga.h> |
| 9 | +#include <cstdio> |
| 10 | +#include <cstdlib> |
| 11 | + |
| 12 | +struct Expected { |
| 13 | + const char* name; |
| 14 | + float x, y, w, h; |
| 15 | +}; |
| 16 | + |
| 17 | +// Ground truth read out of Safari 26 via safaridriver. |
| 18 | +static const Expected kSafari[] = { |
| 19 | + {"a", 0.0f, 0.0f, 100.0f, 20.0f}, |
| 20 | + {"b", 110.0f, 0.0f, 160.0f, 30.0f}, |
| 21 | + {"c", 280.0f, 0.0f, 320.0f, 25.0f}, |
| 22 | + {"d", 0.0f, 60.0f, 100.0f, 40.0f}, |
| 23 | + {"e", 110.0f, 60.0f, 160.0f, 15.0f}, |
| 24 | + {"f", 280.0f, 60.0f, 320.0f, 35.0f}, |
| 25 | +}; |
| 26 | +static const float kSafariContainerHeight = 100.0f; |
| 27 | + |
| 28 | +int main() { |
| 29 | + const float heights[] = {20.0f, 30.0f, 25.0f, 40.0f, 15.0f, 35.0f}; |
| 30 | + |
| 31 | + YGNodeRef root = YGNodeNew(); |
| 32 | + YGNodeStyleSetDisplay(root, YGDisplayGrid); |
| 33 | + YGNodeStyleSetWidth(root, 600.0f); |
| 34 | + YGNodeStyleSetGap(root, YGGutterAll, 10.0f); |
| 35 | + |
| 36 | + YGNodeStyleSetGridTemplateColumnsCount(root, 3); |
| 37 | + YGNodeStyleSetGridTemplateColumn(root, 0, YGGridTrackTypePoints, 100.0f); |
| 38 | + YGNodeStyleSetGridTemplateColumn(root, 1, YGGridTrackTypeFr, 1.0f); |
| 39 | + YGNodeStyleSetGridTemplateColumn(root, 2, YGGridTrackTypeFr, 2.0f); |
| 40 | + |
| 41 | + YGNodeStyleSetGridTemplateRowsCount(root, 2); |
| 42 | + YGNodeStyleSetGridTemplateRow(root, 0, YGGridTrackTypePoints, 50.0f); |
| 43 | + YGNodeStyleSetGridTemplateRow(root, 1, YGGridTrackTypeAuto, 0.0f); |
| 44 | + |
| 45 | + for (size_t i = 0; i < 6; i++) { |
| 46 | + YGNodeRef child = YGNodeNew(); |
| 47 | + YGNodeStyleSetHeight(child, heights[i]); |
| 48 | + YGNodeInsertChild(root, child, i); |
| 49 | + } |
| 50 | + |
| 51 | + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); |
| 52 | + |
| 53 | + int failures = 0; |
| 54 | + auto check = [&](const char* what, float actual, float expected) { |
| 55 | + const bool ok = std::abs(actual - expected) < 0.01f; |
| 56 | + if (!ok) { |
| 57 | + failures++; |
| 58 | + } |
| 59 | + std::printf( |
| 60 | + " %-4s %-2s %7.2f %s %7.2f\n", |
| 61 | + what, |
| 62 | + ok ? "ok" : "XX", |
| 63 | + actual, |
| 64 | + ok ? "==" : "!=", |
| 65 | + expected); |
| 66 | + }; |
| 67 | + |
| 68 | + std::printf("container height:\n"); |
| 69 | + check("h", YGNodeLayoutGetHeight(root), kSafariContainerHeight); |
| 70 | + |
| 71 | + for (size_t i = 0; i < 6; i++) { |
| 72 | + const YGNodeRef child = YGNodeGetChild(root, i); |
| 73 | + const Expected& e = kSafari[i]; |
| 74 | + std::printf("item %s:\n", e.name); |
| 75 | + check("x", YGNodeLayoutGetLeft(child), e.x); |
| 76 | + check("y", YGNodeLayoutGetTop(child), e.y); |
| 77 | + check("w", YGNodeLayoutGetWidth(child), e.w); |
| 78 | + check("h", YGNodeLayoutGetHeight(child), e.h); |
| 79 | + } |
| 80 | + |
| 81 | + YGNodeFreeRecursive(root); |
| 82 | + std::printf( |
| 83 | + "\n%s (%d mismatches vs Safari)\n", |
| 84 | + failures == 0 ? "PASS" : "FAIL", |
| 85 | + failures); |
| 86 | + return failures == 0 ? 0 : 1; |
| 87 | +} |
0 commit comments