Files
yoga/tests/YGTreeMutationTest.cpp
Nick Gerleman 776065d7c7 Breaking: size_t indices (#1366)
Summary:
Pull Request resolved: https://github.com/facebook/yoga/pull/1366

X-link: https://github.com/facebook/react-native/pull/39371

Yoga's public API exposes indices most often as `uint32_t`, with exception of clone callbacks which are `int32_t`. Yoga internally represents these indices as `size_t` when dealing with the child vector, and this is the true index.

This changes the API to consistently be `size_t`. This should not be breaking for most users, but will cause breaks where:

1. Users set a clone node callback (I think this should be rare. RN uses it, but only because it relies on a separate private API).
2. Callers of `YGNodeGetChildCount()` are assigning to an int with less width than `size_t` and have strong warnings enabled.
3. Using a newer Yoga binary with older source, since we are not preserving ABI compatibility (Yoga in general does not aim to be ABI stable between major versions, only ABI safe for a given set of sources).

Changelog: [Internal]

Reviewed By: sammy-SC

Differential Revision: D49130914

fbshipit-source-id: 6a004c160c4c50f68047b108508fd437156f5fac
2023-09-11 19:51:40 -07:00

116 lines
3.7 KiB
C++

/*
* 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);
}