Files
yoga/tests/NumericBitfieldTest.cpp
Nick Gerleman 31b6c0ddc9 C++ Cleanup 7/N: BitUtils (#1351)
Summary:
X-link: https://github.com/facebook/react-native/pull/39223

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

Pull Request resolved: https://github.com/facebook/yoga/pull/1351

## This diff

This splits up `BitUtils.h`, does some minor renaming, and namespace consistency fixes.

## This stack

The organization of the C++ internals of Yoga are in need of attention.
1. Some of the C++ internals are namespaced, but others not.
2. Some of the namespaces include `detail`, but are meant to be used outside of the translation unit (FB Clang Tidy rules warn on any usage of these)
2. Most of the files are in a flat hierarchy, except for event tracing in its own folder
3. Some files and functions begin with YG, others don’t
4. Some functions are uppercase, others are not
5. Almost all of the interesting logic is in Yoga.cpp, and the file is too large to reason about
6. There are multiple grab bag files where folks put random functions they need in (Utils, BitUtils, Yoga-Internal.h)
7. There is no clear indication from file structure or type naming what is private vs not
8. Handles like `YGNodeRef` and `YGConfigRef` can be used to access internals just by importing headers

This stack does some much needed spring cleaning:
1. All non-public headers and C++ implementation details are in separate folders from the root level `yoga`. This will give us room to split up logic and add more files without too large a flat hierarchy
3. All private C++ internals are under the `facebook::yoga` namespace. Details namespaces are only ever used within the same header, as they are intended
4. Utils files are split
5. Most C++ internals drop the YG prefix
6. Most C++ internal function names are all lower camel case
7. We start to split up Yoga.cpp
8. Every header beginning with YG or at the top-level directory is public and C only, with the exception of Yoga-Internal.h which has non-public functions for bindings
9. It is not possible to use private APIs without static casting handles to internal classes

This will give us more leeway to continue splitting monolithic files, and consistent guidelines for style in new files as well.

These changes should not be breaking to any project using only public Yoga headers. This includes every usage of Yoga in fbsource except for RN Fabric which is currently tied to internals. This refactor should make that boundary clearer.

bypass-github-export-checks

Reviewed By: shwanton

Differential Revision: D48847255

fbshipit-source-id: 4b9722303372f43e936118f8187c0127bceeb1d4
2023-08-31 01:17:39 -07:00

205 lines
6.1 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 <cstdint>
#include <yoga/bits/NumericBitfield.h>
namespace facebook::yoga {
TEST(NumericBitfield, one_boolean_defaults_to_false) {
constexpr uint8_t flags = 0;
ASSERT_EQ(getBooleanData(flags, 0), false);
static_assert(
getBooleanData(flags, 0) == false,
"first boolean member must default to false");
}
TEST(NumericBitfield, one_boolean_can_be_initialized_to_true) {
constexpr uint8_t flags = 1;
ASSERT_EQ(getBooleanData(flags, 0), true);
static_assert(
getBooleanData(flags, 0) == true,
"first boolean member must be initialized to true");
}
TEST(NumericBitfield, one_boolean_can_be_set_to_true) {
uint8_t flags = 0;
setBooleanData(flags, 0, true);
ASSERT_EQ(getBooleanData(flags, 0), true);
}
TEST(NumericBitfield, second_boolean_defaults_to_false) {
constexpr uint8_t flags = 0;
ASSERT_EQ(getBooleanData(flags, 1), false);
static_assert(
getBooleanData(flags, 1) == false,
"second boolean member must default to false");
}
TEST(NumericBitfield, second_boolean_can_be_initialized_to_true) {
constexpr uint8_t flags = 2;
ASSERT_EQ(getBooleanData(flags, 0), false);
ASSERT_EQ(getBooleanData(flags, 1), true);
static_assert(
getBooleanData(flags, 0) == false,
"first boolean member must default to false");
static_assert(
getBooleanData(flags, 1) == true,
"second boolean member must be initialized to true");
}
TEST(NumericBitfield, second_boolean_can_be_set_to_true) {
uint8_t flags = 0;
setBooleanData(flags, 1, true);
ASSERT_EQ(getBooleanData(flags, 0), false);
ASSERT_EQ(getBooleanData(flags, 1), true);
}
TEST(NumericBitfield, third_boolean_defaults_to_false) {
constexpr uint8_t flags = 0;
ASSERT_EQ(getBooleanData(flags, 2), false);
static_assert(
getBooleanData(flags, 2) == false,
"second boolean member must default to false");
}
TEST(NumericBitfield, third_boolean_can_be_initialized_to_true) {
constexpr uint8_t flags = 4;
ASSERT_EQ(getBooleanData(flags, 0), false);
ASSERT_EQ(getBooleanData(flags, 1), false);
ASSERT_EQ(getBooleanData(flags, 2), true);
static_assert(
getBooleanData(flags, 0) == false,
"first boolean member must default to false");
static_assert(
getBooleanData(flags, 1) == false,
"second boolean member must default to false");
static_assert(
getBooleanData(flags, 2) == true,
"second boolean member must be initialized to true");
}
TEST(NumericBitfield, third_boolean_can_be_set_to_true) {
uint8_t flags = 0;
setBooleanData(flags, 2, true);
ASSERT_EQ(getBooleanData(flags, 0), false);
ASSERT_EQ(getBooleanData(flags, 1), false);
ASSERT_EQ(getBooleanData(flags, 2), true);
}
TEST(NumericBitfield, setting_boolean_values_does_not_spill_over) {
uint8_t flags = 0;
setBooleanData(flags, 1, (bool) 7);
ASSERT_EQ(getBooleanData(flags, 0), false);
ASSERT_EQ(getBooleanData(flags, 1), true);
ASSERT_EQ(getBooleanData(flags, 2), false);
}
TEST(NumericBitfield, first_enum_defaults_to_0) {
constexpr uint8_t flags = 0;
ASSERT_EQ(getEnumData<YGAlign>(flags, 0), YGAlignAuto);
static_assert(
getEnumData<YGAlign>(flags, 0) == YGAlignAuto,
"first enum member must default to 0");
}
TEST(NumericBitfield, first_enum_can_be_set) {
uint8_t flags = 0;
setEnumData<YGAlign>(flags, 0, YGAlignSpaceBetween);
ASSERT_EQ(getEnumData<YGAlign>(flags, 0), YGAlignSpaceBetween);
}
TEST(NumericBitfield, second_enum_defaults_to_0) {
constexpr uint8_t flags = 0;
static constexpr size_t alignOffset = 0;
static constexpr size_t edgeOffset = 3;
ASSERT_EQ(getEnumData<YGAlign>(flags, alignOffset), YGAlignAuto);
ASSERT_EQ(getEnumData<YGEdge>(flags, edgeOffset), YGEdgeLeft);
static_assert(
getEnumData<YGAlign>(flags, alignOffset) == YGAlignAuto,
"first enum member must default to 0");
static_assert(
getEnumData<YGEdge>(flags, edgeOffset) == YGEdgeLeft,
"second enum member must default to 0");
}
TEST(NumericBitfield, second_enum_can_be_set) {
uint8_t flags = 0;
static constexpr size_t alignOffset = 0;
static constexpr size_t edgeOffset = 3;
setEnumData<YGEdge>(flags, edgeOffset, YGEdgeAll);
ASSERT_EQ(getEnumData<YGAlign>(flags, alignOffset), YGAlignAuto);
ASSERT_EQ(getEnumData<YGEdge>(flags, edgeOffset), YGEdgeAll);
}
TEST(NumericBitfield, third_enum_defaults_to_0) {
constexpr uint8_t flags = 0;
static constexpr size_t alignOffset = 0;
static constexpr size_t boolOffset = 3;
static constexpr size_t edgesOffset = 4;
ASSERT_EQ(getEnumData<YGAlign>(flags, alignOffset), YGAlignAuto);
ASSERT_EQ(getBooleanData(flags, boolOffset), false);
ASSERT_EQ(getEnumData<YGEdge>(flags, edgesOffset), YGEdgeLeft);
static_assert(
getEnumData<YGAlign>(flags, alignOffset) == YGAlignAuto,
"first enum member must default to 0");
static_assert(
getBooleanData(flags, boolOffset) == false,
"middle boolean member must default to false");
static_assert(
getEnumData<YGEdge>(flags, edgesOffset) == YGEdgeLeft,
"last enum member must default to 0");
}
TEST(NumericBitfield, third_enum_can_be_set) {
uint8_t flags = 0;
static constexpr size_t alignOffset = 0;
static constexpr size_t boolOffset = 3;
static constexpr size_t edgesOffset = 4;
setEnumData<YGEdge>(flags, edgesOffset, YGEdgeVertical);
ASSERT_EQ(getEnumData<YGAlign>(flags, alignOffset), YGAlignAuto);
ASSERT_EQ(getBooleanData(flags, boolOffset), false);
ASSERT_EQ(getEnumData<YGEdge>(flags, edgesOffset), YGEdgeVertical);
}
TEST(NumericBitfield, setting_values_does_not_spill_over) {
uint8_t flags = 0;
static constexpr size_t alignOffset = 0;
static constexpr size_t edgesOffset = 3;
static constexpr size_t boolOffset = 7;
setEnumData<YGEdge>(flags, edgesOffset, (YGEdge) 0xffffff);
ASSERT_EQ(getEnumData<YGAlign>(flags, alignOffset), 0);
ASSERT_EQ(getBooleanData(flags, boolOffset), false);
ASSERT_EQ(getEnumData<YGEdge>(flags, edgesOffset), 0xf);
}
} // namespace facebook::yoga