|
5 | 5 | * LICENSE file in the root directory of this source tree. |
6 | 6 | */ |
7 | 7 |
|
| 8 | +#include <stdexcept> |
| 9 | + |
8 | 10 | #include <gtest/gtest.h> |
9 | 11 | #include <yoga/Yoga.h> |
10 | 12 |
|
@@ -66,3 +68,43 @@ TEST(YogaTest, removed_child_can_be_reused_with_valid_layout) { |
66 | 68 |
|
67 | 69 | YGNodeFreeRecursive(root); |
68 | 70 | } |
| 71 | + |
| 72 | +TEST(YogaTest, swap_child_with_out_of_bounds_index_is_rejected) { |
| 73 | + YGNodeRef root = YGNodeNew(); |
| 74 | + YGNodeRef child = YGNodeNew(); |
| 75 | + YGNodeInsertChild(root, child, 0); |
| 76 | + |
| 77 | + // Replacing an existing index works. |
| 78 | + YGNodeRef replacement = YGNodeNew(); |
| 79 | + YGNodeSwapChild(root, replacement, 0); |
| 80 | + ASSERT_EQ(replacement, YGNodeGetChild(root, 0)); |
| 81 | + |
| 82 | + // An index past the last child is rejected rather than reading and writing |
| 83 | + // out of bounds on the underlying children vector. |
| 84 | + YGNodeRef stray = YGNodeNew(); |
| 85 | + ASSERT_THROW(YGNodeSwapChild(root, stray, 1), std::logic_error); |
| 86 | + |
| 87 | + YGNodeFree(stray); |
| 88 | + YGNodeFree(child); |
| 89 | + YGNodeFreeRecursive(root); |
| 90 | +} |
| 91 | + |
| 92 | +TEST(YogaTest, insert_child_with_out_of_bounds_index_is_rejected) { |
| 93 | + YGNodeRef root = YGNodeNew(); |
| 94 | + |
| 95 | + YGNodeRef first = YGNodeNew(); |
| 96 | + YGNodeInsertChild(root, first, 0); |
| 97 | + |
| 98 | + // Appending at the end (index == child count) is allowed. |
| 99 | + YGNodeRef second = YGNodeNew(); |
| 100 | + YGNodeInsertChild(root, second, 1); |
| 101 | + ASSERT_EQ(2u, YGNodeGetChildCount(root)); |
| 102 | + |
| 103 | + // An index beyond the end is rejected rather than constructing an invalid |
| 104 | + // iterator into the children vector. |
| 105 | + YGNodeRef stray = YGNodeNew(); |
| 106 | + ASSERT_THROW(YGNodeInsertChild(root, stray, 5), std::logic_error); |
| 107 | + |
| 108 | + YGNodeFree(stray); |
| 109 | + YGNodeFreeRecursive(root); |
| 110 | +} |
0 commit comments