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 TEST(YogaTest, assert_layout_trees_are_same) {
13 YGConfig* config = YGConfigNew();
14 YGConfigSetUseLegacyStretchBehaviour(config, true);
15 const YGNodeRef root1 = YGNodeNewWithConfig(config);
16 YGNodeStyleSetWidth(root1, 500);
17 YGNodeStyleSetHeight(root1, 500);
19 const YGNodeRef root1_child0 = YGNodeNewWithConfig(config);
20 YGNodeStyleSetAlignItems(root1_child0, YGAlignFlexStart);
21 YGNodeInsertChild(root1, root1_child0, 0);
23 const YGNodeRef root1_child0_child0 = YGNodeNewWithConfig(config);
24 YGNodeStyleSetFlexGrow(root1_child0_child0, 1);
25 YGNodeStyleSetFlexShrink(root1_child0_child0, 1);
26 YGNodeInsertChild(root1_child0, root1_child0_child0, 0);
28 const YGNodeRef root1_child0_child0_child0 = YGNodeNewWithConfig(config);
29 YGNodeStyleSetFlexGrow(root1_child0_child0_child0, 1);
30 YGNodeStyleSetFlexShrink(root1_child0_child0_child0, 1);
31 YGNodeInsertChild(root1_child0_child0, root1_child0_child0_child0, 0);
33 const int32_t cal1_configInstanceCount = YGConfigGetInstanceCount();
34 const int32_t cal1_nodeInstanceCount = YGNodeGetInstanceCount();
36 YGNodeCalculateLayout(root1, YGUndefined, YGUndefined, YGDirectionLTR);
38 ASSERT_EQ(YGConfigGetInstanceCount(), cal1_configInstanceCount);
39 ASSERT_EQ(YGNodeGetInstanceCount(), cal1_nodeInstanceCount);
41 const YGNodeRef root2 = YGNodeNewWithConfig(config);
42 YGNodeStyleSetWidth(root2, 500);
43 YGNodeStyleSetHeight(root2, 500);
45 const YGNodeRef root2_child0 = YGNodeNewWithConfig(config);
46 YGNodeStyleSetAlignItems(root2_child0, YGAlignFlexStart);
47 YGNodeInsertChild(root2, root2_child0, 0);
49 const YGNodeRef root2_child0_child0 = YGNodeNewWithConfig(config);
50 YGNodeStyleSetFlexGrow(root2_child0_child0, 1);
51 YGNodeStyleSetFlexShrink(root2_child0_child0, 1);
52 YGNodeInsertChild(root2_child0, root2_child0_child0, 0);
54 const YGNodeRef root2_child0_child0_child0 = YGNodeNewWithConfig(config);
55 YGNodeStyleSetFlexGrow(root2_child0_child0_child0, 1);
56 YGNodeStyleSetFlexShrink(root2_child0_child0_child0, 1);
57 YGNodeInsertChild(root2_child0_child0, root2_child0_child0_child0, 0);
59 const int32_t cal2_configInstanceCount = YGConfigGetInstanceCount();
60 const int32_t cal2_nodeInstanceCount = YGNodeGetInstanceCount();
62 YGNodeCalculateLayout(root2, YGUndefined, YGUndefined, YGDirectionLTR);
64 ASSERT_EQ(YGConfigGetInstanceCount(), cal2_configInstanceCount);
65 ASSERT_EQ(YGNodeGetInstanceCount(), cal2_nodeInstanceCount);
67 ASSERT_TRUE(YGNodeLayoutGetDidUseLegacyFlag(root1));
68 ASSERT_TRUE(YGNodeLayoutGetDidUseLegacyFlag(root2));
69 ASSERT_TRUE(root1->isLayoutTreeEqualToNode(*root2));
71 YGNodeStyleSetAlignItems(root2, YGAlignFlexEnd);
73 const int32_t cal3_configInstanceCount = YGConfigGetInstanceCount();
74 const int32_t cal3_nodeInstanceCount = YGNodeGetInstanceCount();
76 YGNodeCalculateLayout(root2, YGUndefined, YGUndefined, YGDirectionLTR);
78 ASSERT_EQ(YGConfigGetInstanceCount(), cal3_configInstanceCount);
79 ASSERT_EQ(YGNodeGetInstanceCount(), cal3_nodeInstanceCount);
81 ASSERT_FALSE(root1->isLayoutTreeEqualToNode(*root2));
83 YGNodeFreeRecursive(root1);
84 YGNodeFreeRecursive(root2);