From 83cef5b12fb323c7d10d995bc97abc911f18142a Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Mon, 9 Jan 2023 13:59:19 -0800 Subject: [PATCH] Fixup UTs for MSVC and Release Mode Summary: This fixes incompatibility with MSVC in /W3 (designated initializers, precision loss) along with guarding tests which will only pass in DEBUG builds Reviewed By: cortinico Differential Revision: D42406531 fbshipit-source-id: 2c0d59678f76decf9b9b4d91a7c9ec12136ca1b9 --- tests/EventsTest.cpp | 2 +- tests/YGAlignBaselineTest.cpp | 14 +++------ tests/YGAspectRatioTest.cpp | 4 +-- tests/YGLoggerTest.cpp | 4 +++ tests/YGMeasureCacheTest.cpp | 13 ++++----- tests/YGMeasureModeTest.cpp | 44 ++++++++++++++--------------- tests/YGMeasureTest.cpp | 27 ++++-------------- tests/YGNodeCallbackTest.cpp | 2 +- tests/YGRoundingFunctionTest.cpp | 6 ++-- tests/YGRoundingMeasureFuncTest.cpp | 4 +-- tests/YGStyleAccessorsTest.cpp | 5 ++++ 11 files changed, 55 insertions(+), 70 deletions(-) diff --git a/tests/EventsTest.cpp b/tests/EventsTest.cpp index aa371041..810fe53e 100644 --- a/tests/EventsTest.cpp +++ b/tests/EventsTest.cpp @@ -234,7 +234,7 @@ TEST_F(EventTest, layout_events_has_max_measure_cache) { YGNodeInsertChild(root, b, 1); YGNodeStyleSetFlexBasis(a, 10.0f); - for (auto s : {20, 30, 40}) { + for (auto s : {20.0f, 30.0f, 40.0f}) { YGNodeCalculateLayout(root, s, s, YGDirectionLTR); } diff --git a/tests/YGAlignBaselineTest.cpp b/tests/YGAlignBaselineTest.cpp index 9ef3b0b4..40d03fe7 100644 --- a/tests/YGAlignBaselineTest.cpp +++ b/tests/YGAlignBaselineTest.cpp @@ -22,10 +22,7 @@ static YGSize _measure1( YGMeasureMode widthMode, float height, YGMeasureMode heightMode) { - return YGSize{ - .width = 42, - .height = 50, - }; + return YGSize{42, 50}; } static YGSize _measure2( @@ -34,10 +31,7 @@ static YGSize _measure2( YGMeasureMode widthMode, float height, YGMeasureMode heightMode) { - return YGSize{ - .width = 279, - .height = 126, - }; + return YGSize{279, 126}; } static YGNodeRef createYGNode( @@ -51,8 +45,8 @@ static YGNodeRef createYGNode( if (alignBaseline) { YGNodeStyleSetAlignItems(node, YGAlignBaseline); } - YGNodeStyleSetWidth(node, width); - YGNodeStyleSetHeight(node, height); + YGNodeStyleSetWidth(node, (float) width); + YGNodeStyleSetHeight(node, (float) height); return node; } diff --git a/tests/YGAspectRatioTest.cpp b/tests/YGAspectRatioTest.cpp index 0d61dfd8..2dce80c9 100644 --- a/tests/YGAspectRatioTest.cpp +++ b/tests/YGAspectRatioTest.cpp @@ -16,8 +16,8 @@ static YGSize _measure( float height, YGMeasureMode heightMode) { return YGSize{ - .width = widthMode == YGMeasureModeExactly ? width : 50, - .height = heightMode == YGMeasureModeExactly ? height : 50, + widthMode == YGMeasureModeExactly ? width : 50, + heightMode == YGMeasureModeExactly ? height : 50, }; } diff --git a/tests/YGLoggerTest.cpp b/tests/YGLoggerTest.cpp index c46104f9..7c67b40b 100644 --- a/tests/YGLoggerTest.cpp +++ b/tests/YGLoggerTest.cpp @@ -9,6 +9,8 @@ #include #include +#if DEBUG + namespace { char writeBuffer[4096]; int _unmanagedLogger( @@ -131,3 +133,5 @@ TEST(YogaTest, logger_node_with_children_should_print_indented) { "style=\"\" >\n"; ASSERT_STREQ(expected, writeBuffer); } + +#endif diff --git a/tests/YGMeasureCacheTest.cpp b/tests/YGMeasureCacheTest.cpp index 5a6993d7..b3e053c4 100644 --- a/tests/YGMeasureCacheTest.cpp +++ b/tests/YGMeasureCacheTest.cpp @@ -19,8 +19,8 @@ static YGSize _measureMax( (*measureCount)++; return YGSize{ - .width = widthMode == YGMeasureModeUndefined ? 10 : width, - .height = heightMode == YGMeasureModeUndefined ? 10 : height, + widthMode == YGMeasureModeUndefined ? 10 : width, + heightMode == YGMeasureModeUndefined ? 10 : height, }; } @@ -33,11 +33,11 @@ static YGSize _measureMin( int* measureCount = (int*) node->getContext(); *measureCount = *measureCount + 1; return YGSize{ - .width = widthMode == YGMeasureModeUndefined || + widthMode == YGMeasureModeUndefined || (widthMode == YGMeasureModeAtMost && width > 10) ? 10 : width, - .height = heightMode == YGMeasureModeUndefined || + heightMode == YGMeasureModeUndefined || (heightMode == YGMeasureModeAtMost && height > 10) ? 10 : height, @@ -55,10 +55,7 @@ static YGSize _measure_84_49( (*measureCount)++; } - return YGSize{ - .width = 84.f, - .height = 49.f, - }; + return YGSize{84.f, 49.f}; } TEST(YogaTest, measure_once_single_flexible_child) { diff --git a/tests/YGMeasureModeTest.cpp b/tests/YGMeasureModeTest.cpp index 31004ebb..6547ed66 100644 --- a/tests/YGMeasureModeTest.cpp +++ b/tests/YGMeasureModeTest.cpp @@ -38,15 +38,15 @@ static YGSize _measure( constraintList->length = currentIndex + 1; return YGSize{ - .width = widthMode == YGMeasureModeUndefined ? 10 : width, - .height = heightMode == YGMeasureModeUndefined ? 10 : width, + widthMode == YGMeasureModeUndefined ? 10 : width, + heightMode == YGMeasureModeUndefined ? 10 : width, }; } TEST(YogaTest, exactly_measure_stretched_child_column) { struct _MeasureConstraintList constraintList = _MeasureConstraintList{ - .length = 0, - .constraints = (struct _MeasureConstraint*) malloc( + 0, + (struct _MeasureConstraint*) malloc( 10 * sizeof(struct _MeasureConstraint)), }; @@ -74,8 +74,8 @@ TEST(YogaTest, exactly_measure_stretched_child_column) { TEST(YogaTest, exactly_measure_stretched_child_row) { struct _MeasureConstraintList constraintList = _MeasureConstraintList{ - .length = 0, - .constraints = (struct _MeasureConstraint*) malloc( + 0, + (struct _MeasureConstraint*) malloc( 10 * sizeof(struct _MeasureConstraint)), }; @@ -103,8 +103,8 @@ TEST(YogaTest, exactly_measure_stretched_child_row) { TEST(YogaTest, at_most_main_axis_column) { struct _MeasureConstraintList constraintList = _MeasureConstraintList{ - .length = 0, - .constraints = (struct _MeasureConstraint*) malloc( + 0, + (struct _MeasureConstraint*) malloc( 10 * sizeof(struct _MeasureConstraint)), }; @@ -130,8 +130,8 @@ TEST(YogaTest, at_most_main_axis_column) { TEST(YogaTest, at_most_cross_axis_column) { struct _MeasureConstraintList constraintList = _MeasureConstraintList{ - .length = 0, - .constraints = (struct _MeasureConstraint*) malloc( + 0, + (struct _MeasureConstraint*) malloc( 10 * sizeof(struct _MeasureConstraint)), }; @@ -158,8 +158,8 @@ TEST(YogaTest, at_most_cross_axis_column) { TEST(YogaTest, at_most_main_axis_row) { struct _MeasureConstraintList constraintList = _MeasureConstraintList{ - .length = 0, - .constraints = (struct _MeasureConstraint*) malloc( + 0, + (struct _MeasureConstraint*) malloc( 10 * sizeof(struct _MeasureConstraint)), }; @@ -186,8 +186,8 @@ TEST(YogaTest, at_most_main_axis_row) { TEST(YogaTest, at_most_cross_axis_row) { struct _MeasureConstraintList constraintList = _MeasureConstraintList{ - .length = 0, - .constraints = (struct _MeasureConstraint*) malloc( + 0, + (struct _MeasureConstraint*) malloc( 10 * sizeof(struct _MeasureConstraint)), }; @@ -215,8 +215,8 @@ TEST(YogaTest, at_most_cross_axis_row) { TEST(YogaTest, flex_child) { struct _MeasureConstraintList constraintList = _MeasureConstraintList{ - .length = 0, - .constraints = (struct _MeasureConstraint*) malloc( + 0, + (struct _MeasureConstraint*) malloc( 10 * sizeof(struct _MeasureConstraint)), }; @@ -245,8 +245,8 @@ TEST(YogaTest, flex_child) { TEST(YogaTest, flex_child_with_flex_basis) { struct _MeasureConstraintList constraintList = _MeasureConstraintList{ - .length = 0, - .constraints = (struct _MeasureConstraint*) malloc( + 0, + (struct _MeasureConstraint*) malloc( 10 * sizeof(struct _MeasureConstraint)), }; @@ -273,8 +273,8 @@ TEST(YogaTest, flex_child_with_flex_basis) { TEST(YogaTest, overflow_scroll_column) { struct _MeasureConstraintList constraintList = _MeasureConstraintList{ - .length = 0, - .constraints = (struct _MeasureConstraint*) malloc( + 0, + (struct _MeasureConstraint*) malloc( 10 * sizeof(struct _MeasureConstraint)), }; @@ -305,8 +305,8 @@ TEST(YogaTest, overflow_scroll_column) { TEST(YogaTest, overflow_scroll_row) { struct _MeasureConstraintList constraintList = _MeasureConstraintList{ - .length = 0, - .constraints = (struct _MeasureConstraint*) malloc( + 0, + (struct _MeasureConstraint*) malloc( 10 * sizeof(struct _MeasureConstraint)), }; diff --git a/tests/YGMeasureTest.cpp b/tests/YGMeasureTest.cpp index 646a8941..11d4fd4d 100644 --- a/tests/YGMeasureTest.cpp +++ b/tests/YGMeasureTest.cpp @@ -20,10 +20,7 @@ static YGSize _measure( (*measureCount)++; } - return YGSize{ - .width = 10, - .height = 10, - }; + return YGSize{10, 10}; } static YGSize _simulate_wrapping_text( @@ -33,13 +30,10 @@ static YGSize _simulate_wrapping_text( float height, YGMeasureMode heightMode) { if (widthMode == YGMeasureModeUndefined || width >= 68) { - return YGSize{.width = 68, .height = 16}; + return YGSize{68, 16}; } - return YGSize{ - .width = 50, - .height = 32, - }; + return YGSize{50, 32}; } static YGSize _measure_assert_negative( @@ -51,10 +45,7 @@ static YGSize _measure_assert_negative( EXPECT_GE(width, 0); EXPECT_GE(height, 0); - return YGSize{ - .width = 0, - .height = 0, - }; + return YGSize{0, 0}; } TEST(YogaTest, dont_measure_single_grow_shrink_child) { @@ -656,10 +647,7 @@ static YGSize _measure_90_10( float height, YGMeasureMode heightMode) { - return YGSize{ - .width = 90, - .height = 10, - }; + return YGSize{90, 10}; } static YGSize _measure_100_100( @@ -669,10 +657,7 @@ static YGSize _measure_100_100( float height, YGMeasureMode heightMode) { - return YGSize{ - .width = 100, - .height = 100, - }; + return YGSize{100, 100}; } TEST(YogaTest, percent_with_text_node) { diff --git a/tests/YGNodeCallbackTest.cpp b/tests/YGNodeCallbackTest.cpp index b50f416f..8c438414 100644 --- a/tests/YGNodeCallbackTest.cpp +++ b/tests/YGNodeCallbackTest.cpp @@ -48,7 +48,7 @@ TEST(YGNode, measure_with_context_measure_fn) { return *(YGSize*) ctx; }); - auto result = YGSize{123.4, -56.7}; + auto result = YGSize{123.4f, -56.7f}; ASSERT_EQ( n.measure(0, YGMeasureModeUndefined, 0, YGMeasureModeUndefined, &result), result); diff --git a/tests/YGRoundingFunctionTest.cpp b/tests/YGRoundingFunctionTest.cpp index 60de9326..e99ced89 100644 --- a/tests/YGRoundingFunctionTest.cpp +++ b/tests/YGRoundingFunctionTest.cpp @@ -48,7 +48,7 @@ static YGSize measureText( YGMeasureMode widthMode, float height, YGMeasureMode heightMode) { - return (YGSize){.width = 10, .height = 10}; + return YGSize{10, 10}; } // Regression test for https://github.com/facebook/yoga/issues/824 @@ -57,7 +57,7 @@ TEST(YogaTest, consistent_rounding_during_repeated_layouts) { YGConfigSetPointScaleFactor(config, 2); const YGNodeRef root = YGNodeNewWithConfig(config); - YGNodeStyleSetMargin(root, YGEdgeTop, -1.49); + YGNodeStyleSetMargin(root, YGEdgeTop, -1.49f); YGNodeStyleSetWidth(root, 500); YGNodeStyleSetHeight(root, 500); @@ -70,7 +70,7 @@ TEST(YogaTest, consistent_rounding_during_repeated_layouts) { for (int i = 0; i < 5; i++) { // Dirty the tree so YGRoundToPixelGrid runs again - YGNodeStyleSetMargin(root, YGEdgeLeft, i + 1); + YGNodeStyleSetMargin(root, YGEdgeLeft, (float) (i + 1)); YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); ASSERT_FLOAT_EQ(10, YGNodeLayoutGetHeight(node1)); diff --git a/tests/YGRoundingMeasureFuncTest.cpp b/tests/YGRoundingMeasureFuncTest.cpp index 86935b2f..166be5db 100644 --- a/tests/YGRoundingMeasureFuncTest.cpp +++ b/tests/YGRoundingMeasureFuncTest.cpp @@ -57,8 +57,8 @@ TEST(YogaTest, rounding_feature_with_custom_measure_func_floor) { YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionRTL); - ASSERT_FLOAT_EQ(10.2, YGNodeLayoutGetWidth(root_child0)); - ASSERT_FLOAT_EQ(10.2, YGNodeLayoutGetHeight(root_child0)); + ASSERT_FLOAT_EQ(10.2f, YGNodeLayoutGetWidth(root_child0)); + ASSERT_FLOAT_EQ(10.2f, YGNodeLayoutGetHeight(root_child0)); YGConfigSetPointScaleFactor(config, 1.0f); diff --git a/tests/YGStyleAccessorsTest.cpp b/tests/YGStyleAccessorsTest.cpp index d792d644..2801536f 100644 --- a/tests/YGStyleAccessorsTest.cpp +++ b/tests/YGStyleAccessorsTest.cpp @@ -81,6 +81,9 @@ namespace yoga { using CompactValue = detail::CompactValue; +// TODO: MSVC doesn't like the macros +#ifndef _MSC_VER + ACCESSOR_TEST( direction, YGDirectionInherit, @@ -250,5 +253,7 @@ ACCESSOR_TEST( YGFloatOptional{0.0f}, YGFloatOptional{}); +#endif + } // namespace yoga } // namespace facebook