Skip to content

Commit

Permalink
pw_unit_test migration: lib support batch #1 (project-chip#33091)
Browse files Browse the repository at this point in the history
* pw_unit_test migration: lib support batch #1

* apply restyled patch

* integrate comments
  • Loading branch information
Alami-Amine authored Apr 24, 2024
1 parent 03bf252 commit 3380876
Show file tree
Hide file tree
Showing 11 changed files with 498 additions and 599 deletions.
1 change: 1 addition & 0 deletions src/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ if (chip_build_tests) {
"${chip_root}/src/credentials/tests",
"${chip_root}/src/lib/format/tests",
"${chip_root}/src/lib/support/tests",
"${chip_root}/src/lib/support/tests:tests_nltest",
"${chip_root}/src/protocols/secure_channel/tests",
"${chip_root}/src/system/tests",
"${chip_root}/src/transport/tests",
Expand Down
40 changes: 34 additions & 6 deletions src/lib/support/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,61 @@
import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")
import("//build_overrides/nlunit_test.gni")
import("//build_overrides/pigweed.gni")

import("${chip_root}/build/chip/chip_test_suite.gni")

chip_test_suite_using_nltest("tests") {
chip_test_suite("tests") {
output_name = "libSupportTests"

test_sources = [
"TestBitMask.cpp",
"TestBufferReader.cpp",
"TestDefer.cpp",
"TestFixedBufferAllocator.cpp",
"TestFold.cpp",
"TestIniEscaping.cpp",
"TestSafeInt.cpp",
]
sources = []

cflags = [
"-Wconversion",

# TODO(#21255): work-around for SimpleStateMachine constructor issue.
"-Wno-uninitialized",

# TestStringSplitter intentionally validates string overflows.
"-Wno-stringop-truncation",
]

public_deps = [
"${chip_root}/src/credentials",
"${chip_root}/src/lib/core",
"${chip_root}/src/lib/support:static-support",
"${chip_root}/src/lib/support:testing",
"${chip_root}/src/lib/support/jsontlv",
"${chip_root}/src/platform",
]
}

chip_test_suite_using_nltest("tests_nltest") {
output_name = "libSupportTestsNL"

test_sources = [
"TestBufferWriter.cpp",
"TestBytesCircularBuffer.cpp",
"TestBytesToHex.cpp",
"TestCHIPCounter.cpp",
"TestCHIPMem.cpp",
"TestCHIPMemString.cpp",
"TestDefer.cpp",
"TestErrorStr.cpp",
"TestFixedBufferAllocator.cpp",
"TestFold.cpp",
"TestIniEscaping.cpp",
"TestIntrusiveList.cpp",
"TestJsonToTlv.cpp",
"TestJsonToTlvToJson.cpp",
"TestPersistedCounter.cpp",
"TestPool.cpp",
"TestPrivateHeap.cpp",
"TestSafeInt.cpp",
"TestSafeString.cpp",
"TestScoped.cpp",
"TestScopedBuffer.cpp",
Expand Down
60 changes: 20 additions & 40 deletions src/lib/support/tests/TestBitMask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <lib/support/BitMask.h>
#include <lib/support/UnitTestRegistration.h>

#include <algorithm>
#include <cstring>
#include <initializer_list>
#include <nlunit-test.h>

using namespace chip;

Expand All @@ -37,68 +36,68 @@ enum class TestEnum : uint16_t
kBits_High8 = 0xFF00,
};

void TestBitMaskOperations(nlTestSuite * inSuite, void * inContext)
TEST(TestBitMask, TestBitMaskOperations)
{
BitMask<TestEnum> mask;

NL_TEST_ASSERT(inSuite, mask.Raw() == 0);
EXPECT_EQ(mask.Raw(), 0);

mask.SetField(TestEnum::kBits_1_2, 2);
NL_TEST_ASSERT(inSuite, mask.Raw() == 0x0004);
EXPECT_EQ(mask.Raw(), 0x0004);

mask.SetRaw(0);
mask.SetField(TestEnum::kBits_4_7, 0x0B);
NL_TEST_ASSERT(inSuite, mask.Raw() == 0x00B0);
EXPECT_EQ(mask.Raw(), 0x00B0);

mask.SetRaw(0);

for (uint16_t i = 0; i < 0x10; i++)
{
mask.SetField(TestEnum::kBits_High4, i);
NL_TEST_ASSERT(inSuite, mask.Raw() == (i << 12));
EXPECT_EQ(mask.Raw(), (i << 12));
}

mask.SetField(TestEnum::kBits_High8, 0x23);
NL_TEST_ASSERT(inSuite, mask.Raw() == 0x2300);
EXPECT_EQ(mask.Raw(), 0x2300);

mask.SetField(TestEnum::kBits_High4, 0xA);
NL_TEST_ASSERT(inSuite, mask.Raw() == 0xA300);
EXPECT_EQ(mask.Raw(), 0xA300);
}

void TestBitFieldLogic(nlTestSuite * inSuite, void * inContext)
TEST(TestBitMask, TestBitFieldLogic)
{
BitMask<TestEnum> mask;

// some general logic that still applies for bit fields just in case
NL_TEST_ASSERT(inSuite, !mask.HasAny(TestEnum::kBits_High4));
NL_TEST_ASSERT(inSuite, !mask.HasAny(TestEnum::kBits_High8));
EXPECT_FALSE(mask.HasAny(TestEnum::kBits_High4));
EXPECT_FALSE(mask.HasAny(TestEnum::kBits_High8));

// setting something non-zero in the upper 4 bits sets "something" in both
// upper and 4 and 8 bits
mask.SetField(TestEnum::kBits_High4, 0x01);
NL_TEST_ASSERT(inSuite, mask.HasAny(TestEnum::kBits_High4));
NL_TEST_ASSERT(inSuite, mask.HasAny(TestEnum::kBits_High8));
EXPECT_TRUE(mask.HasAny(TestEnum::kBits_High4));
EXPECT_TRUE(mask.HasAny(TestEnum::kBits_High8));

// sets something visible in high 8 bits, but not high 4 bits
mask.SetField(TestEnum::kBits_High8, 0x01);
NL_TEST_ASSERT(inSuite, !mask.HasAny(TestEnum::kBits_High4));
NL_TEST_ASSERT(inSuite, mask.HasAny(TestEnum::kBits_High8));
EXPECT_FALSE(mask.HasAny(TestEnum::kBits_High4));
EXPECT_TRUE(mask.HasAny(TestEnum::kBits_High8));
}

void TestBitMaskInvalid(nlTestSuite * inSuite, void * inContext)
TEST(TestBitMask, TestBitMaskInvalid)
{
BitMask<TestEnum> mask;

// This generally tests for no infinite loops. Nothing to set here
mask.SetField(TestEnum::kZero, 0x01);
NL_TEST_ASSERT(inSuite, mask.Raw() == 0);
EXPECT_EQ(mask.Raw(), 0);

mask.SetRaw(0x1234);
mask.SetField(TestEnum::kZero, 0x01);
NL_TEST_ASSERT(inSuite, mask.Raw() == 0x1234);
EXPECT_EQ(mask.Raw(), 0x1234);
}

void TestClear(nlTestSuite * inSuite, void * inContext)
TEST(TestBitMask, TestClear)
{
BitMask<TestEnum> mask1;
BitMask<TestEnum> mask2;
Expand All @@ -110,26 +109,7 @@ void TestClear(nlTestSuite * inSuite, void * inContext)
mask2.Set(TestEnum::kBits_1_2);
mask1.Clear(mask2);

NL_TEST_ASSERT(inSuite, mask1.Raw() == 0xFF01);
EXPECT_EQ(mask1.Raw(), 0xFF01);
}

const nlTest sTests[] = {
NL_TEST_DEF("BitMask operations", TestBitMaskOperations), //
NL_TEST_DEF("BitFields logic", TestBitFieldLogic), //
NL_TEST_DEF("Invalid operations", TestBitMaskInvalid), //
NL_TEST_DEF("Clear operations", TestClear), //
NL_TEST_SENTINEL() //
};

} // namespace

int TestBitMask()
{
nlTestSuite theSuite = { "BitMask tests", &sTests[0], nullptr, nullptr };

// Run test suite against one context.
nlTestRunner(&theSuite, nullptr);
return nlTestRunnerStats(&theSuite);
}

CHIP_REGISTER_TEST_SUITE(TestBitMask)
Loading

0 comments on commit 3380876

Please sign in to comment.