2 * Copyright (c) 2014-present, Facebook, Inc.
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
8 #include <gtest/gtest.h>
9 #include <yoga/YGNode.h>
10 #include <yoga/Yoga.h>
12 static YGSize _measureFloor(YGNodeRef node,
14 YGMeasureMode widthMode,
16 YGMeasureMode heightMode) {
18 width = 10.2f, height = 10.2f,
22 static YGSize _measureCeil(YGNodeRef node,
24 YGMeasureMode widthMode,
26 YGMeasureMode heightMode) {
28 width = 10.5f, height = 10.5f,
32 static YGSize _measureFractial(YGNodeRef node,
34 YGMeasureMode widthMode,
36 YGMeasureMode heightMode) {
38 width = 0.5f, height = 0.5f,
42 TEST(YogaTest, rounding_feature_with_custom_measure_func_floor) {
43 const YGConfigRef config = YGConfigNew();
44 const YGNodeRef root = YGNodeNewWithConfig(config);
46 const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
47 root_child0->setMeasureFunc(_measureFloor);
48 YGNodeInsertChild(root, root_child0, 0);
50 YGConfigSetPointScaleFactor(config, 0.0f);
52 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionRTL);
54 ASSERT_FLOAT_EQ(10.2, YGNodeLayoutGetWidth(root_child0));
55 ASSERT_FLOAT_EQ(10.2, YGNodeLayoutGetHeight(root_child0));
57 YGConfigSetPointScaleFactor(config, 1.0f);
59 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
61 ASSERT_FLOAT_EQ(11, YGNodeLayoutGetWidth(root_child0));
62 ASSERT_FLOAT_EQ(11, YGNodeLayoutGetHeight(root_child0));
64 YGConfigSetPointScaleFactor(config, 2.0f);
66 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionRTL);
68 ASSERT_FLOAT_EQ(10.5, YGNodeLayoutGetWidth(root_child0));
69 ASSERT_FLOAT_EQ(10.5, YGNodeLayoutGetHeight(root_child0));
71 YGConfigSetPointScaleFactor(config, 4.0f);
73 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
75 ASSERT_FLOAT_EQ(10.25, YGNodeLayoutGetWidth(root_child0));
76 ASSERT_FLOAT_EQ(10.25, YGNodeLayoutGetHeight(root_child0));
78 YGConfigSetPointScaleFactor(config, 1.0f / 3.0f);
80 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionRTL);
82 ASSERT_FLOAT_EQ(12.0, YGNodeLayoutGetWidth(root_child0));
83 ASSERT_FLOAT_EQ(12.0, YGNodeLayoutGetHeight(root_child0));
85 YGNodeFreeRecursive(root);
90 TEST(YogaTest, rounding_feature_with_custom_measure_func_ceil) {
91 const YGConfigRef config = YGConfigNew();
92 const YGNodeRef root = YGNodeNewWithConfig(config);
94 const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
95 root_child0->setMeasureFunc(_measureCeil);
96 YGNodeInsertChild(root, root_child0, 0);
98 YGConfigSetPointScaleFactor(config, 1.0f);
100 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
102 ASSERT_FLOAT_EQ(11, YGNodeLayoutGetWidth(root_child0));
103 ASSERT_FLOAT_EQ(11, YGNodeLayoutGetHeight(root_child0));
105 YGNodeFreeRecursive(root);
107 YGConfigFree(config);
110 TEST(YogaTest, rounding_feature_with_custom_measure_and_fractial_matching_scale) {
111 const YGConfigRef config = YGConfigNew();
112 const YGNodeRef root = YGNodeNewWithConfig(config);
114 const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
115 YGNodeStyleSetPosition(root_child0, YGEdgeLeft, 73.625);
116 root_child0->setMeasureFunc(_measureFractial);
117 YGNodeInsertChild(root, root_child0, 0);
119 YGConfigSetPointScaleFactor(config, 2.0f);
121 YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
123 ASSERT_FLOAT_EQ(0.5, YGNodeLayoutGetWidth(root_child0));
124 ASSERT_FLOAT_EQ(0.5, YGNodeLayoutGetHeight(root_child0));
125 ASSERT_FLOAT_EQ(73.5, YGNodeLayoutGetLeft(root_child0));
127 YGNodeFreeRecursive(root);
129 YGConfigFree(config);