File: YGTreeMutationTest.cpp

package info (click to toggle)
yoga 3.2.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 984 kB
  • sloc: cpp: 12,985; python: 260; sh: 32; makefile: 6
file content (115 lines) | stat: -rw-r--r-- 3,758 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#include <gtest/gtest.h>
#include <yoga/Yoga.h>

static std::vector<YGNodeRef> getChildren(YGNodeRef const node) {
  const auto count = YGNodeGetChildCount(node);
  std::vector<YGNodeRef> children;
  children.reserve(count);
  for (size_t i = 0; i < count; i++) {
    children.push_back(YGNodeGetChild(node, i));
  }
  return children;
}

TEST(YogaTest, set_children_adds_children_to_parent) {
  YGNodeRef const root = YGNodeNew();
  YGNodeRef const root_child0 = YGNodeNew();
  YGNodeRef const root_child1 = YGNodeNew();

  YGNodeRef children[] = {root_child0, root_child1};
  YGNodeSetChildren(root, children, 2);

  const std::vector<YGNodeRef> expectedChildren = {root_child0, root_child1};
  ASSERT_EQ(getChildren(root), expectedChildren);

  const std::vector<YGNodeRef> owners = {
      YGNodeGetOwner(root_child0), YGNodeGetOwner(root_child1)};
  const std::vector<YGNodeRef> expectedOwners = {root, root};
  ASSERT_EQ(owners, expectedOwners);

  YGNodeFreeRecursive(root);
}

TEST(YogaTest, set_children_to_empty_removes_old_children) {
  YGNodeRef const root = YGNodeNew();
  YGNodeRef const root_child0 = YGNodeNew();
  YGNodeRef const root_child1 = YGNodeNew();

  YGNodeRef children[] = {root_child0, root_child1};
  YGNodeSetChildren(root, children, 2);
  YGNodeSetChildren(root, nullptr, 0);

  const std::vector<YGNodeRef> expectedChildren = {};
  ASSERT_EQ(getChildren(root), expectedChildren);

  const std::vector<YGNodeRef> owners = {
      YGNodeGetOwner(root_child0), YGNodeGetOwner(root_child1)};
  const std::vector<YGNodeRef> expectedOwners = {nullptr, nullptr};
  ASSERT_EQ(owners, expectedOwners);

  YGNodeFreeRecursive(root);
}

TEST(YogaTest, set_children_replaces_non_common_children) {
  YGNodeRef const root = YGNodeNew();
  YGNodeRef const root_child0 = YGNodeNew();
  YGNodeRef const root_child1 = YGNodeNew();

  YGNodeRef children1[] = {root_child0, root_child1};
  YGNodeSetChildren(root, children1, 2);

  YGNodeRef const root_child2 = YGNodeNew();
  YGNodeRef const root_child3 = YGNodeNew();

  YGNodeRef children2[] = {root_child2, root_child3};
  YGNodeSetChildren(root, children2, 2);

  const std::vector<YGNodeRef> expectedChildren = {root_child2, root_child3};
  ASSERT_EQ(getChildren(root), expectedChildren);

  const std::vector<YGNodeRef> owners = {
      YGNodeGetOwner(root_child0), YGNodeGetOwner(root_child1)};
  const std::vector<YGNodeRef> expectedOwners = {nullptr, nullptr};
  ASSERT_EQ(owners, expectedOwners);

  YGNodeFreeRecursive(root);
  YGNodeFree(root_child0);
  YGNodeFree(root_child1);
}

TEST(YogaTest, set_children_keeps_and_reorders_common_children) {
  YGNodeRef const root = YGNodeNew();
  YGNodeRef const root_child0 = YGNodeNew();
  YGNodeRef const root_child1 = YGNodeNew();
  YGNodeRef const root_child2 = YGNodeNew();

  YGNodeRef children1[] = {root_child0, root_child1, root_child2};
  YGNodeSetChildren(root, children1, 3);

  YGNodeRef const root_child3 = YGNodeNew();

  YGNodeRef children2[] = {root_child2, root_child1, root_child3};
  YGNodeSetChildren(root, children2, 3);

  const std::vector<YGNodeRef> expectedChildren = {
      root_child2, root_child1, root_child3};
  ASSERT_EQ(getChildren(root), expectedChildren);

  const std::vector<YGNodeRef> owners = {
      YGNodeGetOwner(root_child0),
      YGNodeGetOwner(root_child1),
      YGNodeGetOwner(root_child2),
      YGNodeGetOwner(root_child3)};
  const std::vector<YGNodeRef> expectedOwners = {nullptr, root, root, root};
  ASSERT_EQ(owners, expectedOwners);

  YGNodeFreeRecursive(root);
  YGNodeFree(root_child0);
}