|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +// Regression test for https://github.com/facebook/yoga/issues/1665 |
| 9 | +// flexBasis:0 + flexShrink:1 + borderWidth + minWidth produces an astronomically |
| 10 | +// large width (~1.65e11) instead of being clamped to minWidth. |
| 11 | + |
| 12 | +#include <gtest/gtest.h> |
| 13 | +#include <yoga/Yoga.h> |
| 14 | + |
| 15 | +TEST(YGFlexShrinkBorderBug, flex_basis_0_border_minwidth_row) { |
| 16 | + YGConfigRef config = YGConfigNew(); |
| 17 | + YGNodeRef root = YGNodeNewWithConfig(config); |
| 18 | + YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow); |
| 19 | + YGNodeStyleSetWidth(root, 393.0f); |
| 20 | + YGNodeStyleSetHeight(root, 100.0f); |
| 21 | + |
| 22 | + // Four row children: flexBasis:0, flexGrow:1, flexShrink:1, minWidth:160 |
| 23 | + // First child has borderWidth:0.594443, rest 0.594442. |
| 24 | + // Bug: first child (and all others) get width ~1.65e11 instead of 160. |
| 25 | + float borders[] = {0.594443f, 0.594442f, 0.594442f, 0.594442f}; |
| 26 | + for (int i = 0; i < 4; i++) { |
| 27 | + YGNodeRef child = YGNodeNewWithConfig(config); |
| 28 | + YGNodeStyleSetFlexBasis(child, 0.0f); |
| 29 | + YGNodeStyleSetFlexGrow(child, 1.0f); |
| 30 | + YGNodeStyleSetFlexShrink(child, 1.0f); |
| 31 | + YGNodeStyleSetMinWidth(child, 160.0f); |
| 32 | + YGNodeStyleSetBorder(child, YGEdgeAll, borders[i]); |
| 33 | + YGNodeInsertChild(root, child, i); |
| 34 | + } |
| 35 | + |
| 36 | + YGNodeCalculateLayout(root, 393.0f, 100.0f, YGDirectionLTR); |
| 37 | + |
| 38 | + for (int i = 0; i < 4; i++) { |
| 39 | + YGNodeRef child = YGNodeGetChild(root, i); |
| 40 | + EXPECT_GE(YGNodeLayoutGetWidth(child), 160.0f) |
| 41 | + << "child[" << i << "] width below minWidth"; |
| 42 | + EXPECT_LE(YGNodeLayoutGetWidth(child), 200.0f) |
| 43 | + << "child[" << i << "] width is astronomically large (bug)"; |
| 44 | + } |
| 45 | + |
| 46 | + YGNodeFreeRecursive(root); |
| 47 | + YGConfigFree(config); |
| 48 | +} |
0 commit comments