diff --git a/src/system/SystemPacketBuffer.h b/src/system/SystemPacketBuffer.h index 882a097d66cf38..513f0b4fbc0cdb 100644 --- a/src/system/SystemPacketBuffer.h +++ b/src/system/SystemPacketBuffer.h @@ -44,8 +44,6 @@ #include #endif // CHIP_SYSTEM_CONFIG_USE_LWIP -class PacketBufferTest; - namespace chip { namespace System { @@ -392,7 +390,7 @@ class DLL_EXPORT PacketBuffer : private pbuf const uint8_t * ReserveStart() const; friend class PacketBufferHandle; - friend class ::PacketBufferTest; + friend class TestSystemPacketBuffer; }; static_assert(sizeof(pbuf) == sizeof(PacketBuffer), "PacketBuffer must not have additional members"); @@ -696,7 +694,7 @@ class DLL_EXPORT PacketBufferHandle PacketBuffer * mBuffer; friend class PacketBuffer; - friend class ::PacketBufferTest; + friend class TestSystemPacketBuffer; }; inline void PacketBuffer::SetDataLength(uint16_t aNewLen, const PacketBufferHandle & aChainHead) diff --git a/src/system/SystemTimer.h b/src/system/SystemTimer.h index c512a0c845ddd6..698be028ce029a 100644 --- a/src/system/SystemTimer.h +++ b/src/system/SystemTimer.h @@ -44,7 +44,6 @@ namespace chip { namespace System { class Layer; -class TestTimer; /** * Basic Timer information: time and callback. @@ -238,7 +237,7 @@ class TimerPool } private: - friend class TestTimer; + friend class TestSystemTimer_CheckTimerPool_Test; ObjectPool mTimerPool; }; diff --git a/src/system/tests/BUILD.gn b/src/system/tests/BUILD.gn index 4e5e129c7351f7..2f2fd4de0a6a6d 100644 --- a/src/system/tests/BUILD.gn +++ b/src/system/tests/BUILD.gn @@ -14,11 +14,10 @@ import("//build_overrides/build.gni") import("//build_overrides/chip.gni") -import("//build_overrides/nlunit_test.gni") import("${chip_root}/build/chip/chip_test_suite.gni") -chip_test_suite_using_nltest("tests") { +chip_test_suite("tests") { output_name = "libSystemLayerTests" test_sources = [ @@ -47,9 +46,7 @@ chip_test_suite_using_nltest("tests") { public_deps = [ "${chip_root}/src/inet", - "${chip_root}/src/lib/support:testing_nlunit", "${chip_root}/src/platform", "${chip_root}/src/system", - "${nlunit_test_root}:nlunit-test", ] } diff --git a/src/system/tests/TestSystemClock.cpp b/src/system/tests/TestSystemClock.cpp index 63fcfaa3566e5d..84d701a17d3323 100644 --- a/src/system/tests/TestSystemClock.cpp +++ b/src/system/tests/TestSystemClock.cpp @@ -15,14 +15,13 @@ * limitations under the License. */ -#include +#include #include #include #include -#include -#include #include +#include #if !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME @@ -41,18 +40,18 @@ using namespace chip::System; namespace { -void TestRealClock(nlTestSuite * inSuite, void * inContext) +TEST(TestSystemClock, TestRealClock) { Clock::Milliseconds64 oldMilli = SystemClock().GetMonotonicMilliseconds64(); Clock::Milliseconds64 newMilli = SystemClock().GetMonotonicMilliseconds64(); - NL_TEST_ASSERT(inSuite, newMilli >= oldMilli); + EXPECT_GE(newMilli, oldMilli); Clock::Microseconds64 oldMicro = SystemClock().GetMonotonicMicroseconds64(); Clock::Microseconds64 newMicro = SystemClock().GetMonotonicMicroseconds64(); - NL_TEST_ASSERT(inSuite, newMicro >= oldMicro); + EXPECT_GE(newMicro, oldMicro); Clock::Microseconds64::rep microseconds = newMicro.count(); - NL_TEST_ASSERT(inSuite, (microseconds & 0x8000'0000'0000'0000) == 0); + EXPECT_EQ((microseconds & 0x8000'0000'0000'0000), 0UL); #if !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME && \ (CHIP_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME || CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS) @@ -65,62 +64,35 @@ void TestRealClock(nlTestSuite * inSuite, void * inContext) #if CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS struct timespec delay = { 0, kDelayMilliseconds * chip::kNanosecondsPerMillisecond }; while (nanosleep(&delay, &delay) == -1 && errno == EINTR) - { - } + continue; #endif // CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS newMilli = SystemClock().GetMonotonicMilliseconds64(); - NL_TEST_ASSERT(inSuite, newMilli > oldMilli); + EXPECT_GT(newMilli, oldMilli); newMicro = SystemClock().GetMonotonicMicroseconds64(); - NL_TEST_ASSERT(inSuite, newMicro > oldMicro); + EXPECT_GT(newMicro, oldMicro); -#endif // !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME && (CHIP_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME || - // CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS) +#endif // !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME && + // (CHIP_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME || CHIP_SYSTEM_CONFIG_USE_POSIX_TIME_FUNCTS) } -void TestMockClock(nlTestSuite * inSuite, void * inContext) +TEST(TestSystemClock, TestMockClock) { Clock::Internal::MockClock clock; Clock::ClockBase * savedRealClock = &SystemClock(); Clock::Internal::SetSystemClockForTesting(&clock); - NL_TEST_ASSERT(inSuite, SystemClock().GetMonotonicMilliseconds64() == Clock::kZero); - NL_TEST_ASSERT(inSuite, SystemClock().GetMonotonicMicroseconds64() == Clock::kZero); + EXPECT_EQ(SystemClock().GetMonotonicMilliseconds64(), Clock::kZero); + EXPECT_EQ(SystemClock().GetMonotonicMicroseconds64(), Clock::kZero); constexpr Clock::Milliseconds64 k1234 = Clock::Milliseconds64(1234); clock.SetMonotonic(k1234); - NL_TEST_ASSERT(inSuite, SystemClock().GetMonotonicMilliseconds64() == k1234); - NL_TEST_ASSERT(inSuite, SystemClock().GetMonotonicMicroseconds64() == k1234); + EXPECT_EQ(SystemClock().GetMonotonicMilliseconds64(), k1234); + EXPECT_EQ(SystemClock().GetMonotonicMicroseconds64(), k1234); Clock::Internal::SetSystemClockForTesting(savedRealClock); } } // namespace - -/** - * Test Suite. It lists all the test functions. - */ -// clang-format off -static const nlTest sTests[] = -{ - NL_TEST_DEF("TestRealClock", TestRealClock), - NL_TEST_DEF("TestMockClock", TestMockClock), - NL_TEST_SENTINEL() -}; -// clang-format on - -int TestSystemClock() -{ - nlTestSuite theSuite = { - "chip-systemclock", &sTests[0], nullptr /* setup */, nullptr /* teardown */ - }; - - // Run test suite against one context. - nlTestRunner(&theSuite, nullptr /* context */); - - return (nlTestRunnerStats(&theSuite)); -} - -CHIP_REGISTER_TEST_SUITE(TestSystemClock) diff --git a/src/system/tests/TestSystemErrorStr.cpp b/src/system/tests/TestSystemErrorStr.cpp index 0296eb957c5dea..bf6321fe0a4e9a 100644 --- a/src/system/tests/TestSystemErrorStr.cpp +++ b/src/system/tests/TestSystemErrorStr.cpp @@ -28,34 +28,28 @@ #include #include +#include + #include #include #include -#include - -#include using namespace chip; // Test input data. - -// clang-format off -static const CHIP_ERROR kTestElements[] = -{ +static const CHIP_ERROR kTestElements[] = { CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE, CHIP_ERROR_INVALID_ARGUMENT, CHIP_ERROR_INCORRECT_STATE, CHIP_ERROR_UNEXPECTED_EVENT, CHIP_ERROR_NO_MEMORY, CHIP_ERROR_REAL_TIME_NOT_SYNCED, - CHIP_ERROR_ACCESS_DENIED + CHIP_ERROR_ACCESS_DENIED, }; -// clang-format on -static void CheckSystemErrorStr(nlTestSuite * inSuite, void * inContext) +TEST(TestSystemErrorStr, CheckSystemErrorStr) { // Register the layer error formatter - RegisterCHIPLayerErrorFormatter(); // For each defined error... @@ -66,45 +60,12 @@ static void CheckSystemErrorStr(nlTestSuite * inSuite, void * inContext) // Assert that the error string contains the error number in hex. snprintf(expectedText, sizeof(expectedText), "%08" PRIX32, err.AsInteger()); - NL_TEST_ASSERT(inSuite, (strstr(errStr, expectedText) != nullptr)); + EXPECT_NE(strstr(errStr, expectedText), nullptr); #if !CHIP_CONFIG_SHORT_ERROR_STR // Assert that the error string contains a description, which is signaled // by a presence of a colon proceeding the description. - NL_TEST_ASSERT(inSuite, (strchr(errStr, ':') != nullptr)); + EXPECT_NE(strchr(errStr, ':'), nullptr); #endif // !CHIP_CONFIG_SHORT_ERROR_STR } } - -/** - * Test Suite. It lists all the test functions. - */ - -// clang-format off -static const nlTest sTests[] = -{ - NL_TEST_DEF("SystemErrorStr", CheckSystemErrorStr), - - NL_TEST_SENTINEL() -}; -// clang-format on - -int TestSystemErrorStr() -{ - // clang-format off - nlTestSuite theSuite = - { - "System-Error-Strings", - &sTests[0], - nullptr, - nullptr - }; - // clang-format on - - // Run test suite against one context. - nlTestRunner(&theSuite, nullptr); - - return (nlTestRunnerStats(&theSuite)); -} - -CHIP_REGISTER_TEST_SUITE(TestSystemErrorStr) diff --git a/src/system/tests/TestSystemPacketBuffer.cpp b/src/system/tests/TestSystemPacketBuffer.cpp index 527d3a19a32e4b..5b3f709a5bee67 100644 --- a/src/system/tests/TestSystemPacketBuffer.cpp +++ b/src/system/tests/TestSystemPacketBuffer.cpp @@ -30,10 +30,10 @@ #include #include +#include + #include #include -#include -#include #include #include @@ -42,8 +42,6 @@ #include #endif // CHIP_SYSTEM_CONFIG_USE_LWIP -#include - #if CHIP_SYSTEM_CONFIG_USE_LWIP #if (LWIP_VERSION_MAJOR == 2) && (LWIP_VERSION_MINOR == 0) #define PBUF_TYPE(pbuf) (pbuf)->type @@ -73,63 +71,49 @@ void ScrambleData(uint8_t * start, uint16_t length) } } // namespace +namespace chip { +namespace System { + /* - * An instance of this class created for the test suite. - * It is a friend class of `PacketBuffer` and `PacketBufferHandle` because some tests + * This class is a friend class of `PacketBuffer` and `PacketBufferHandle` because some tests * use or check private methods or properties. */ -class PacketBufferTest +class TestSystemPacketBuffer : public ::testing::Test { public: - struct TestContext + static constexpr auto kBlockSize = PacketBuffer::kBlockSize; + static constexpr auto kStructureSize = PacketBuffer::kStructureSize; + + static constexpr uint16_t kReservedSizes[] = { 0, 10, 128, 1536, PacketBuffer::kMaxSizeWithoutReserve, kBlockSize }; + static constexpr uint16_t kLengths[] = { 0, 1, 10, 128, kBlockSize, UINT16_MAX }; + + static void SetUpTestSuite() { - const uint16_t * const reserved_sizes; - size_t reserved_size_count; - const uint16_t * const lengths; - size_t length_count; - PacketBufferTest * test; - }; + ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); + ASSERT_EQ(chip::DeviceLayer::PlatformMgr().InitChipStack(), CHIP_NO_ERROR); + } + + static void TearDownTestSuite() + { + chip::DeviceLayer::PlatformMgr().Shutdown(); + chip::Platform::MemoryShutdown(); + } - static int TestSetup(void * inContext); - static int TestTeardown(void * inContext); - static int TestInitialize(void * inContext); - static int TestTerminate(void * inContext); - - static void CheckNew(nlTestSuite * inSuite, void * inContext); - static void CheckStart(nlTestSuite * inSuite, void * inContext); - static void CheckSetStart(nlTestSuite * inSuite, void * inContext); - static void CheckDataLength(nlTestSuite * inSuite, void * inContext); - static void CheckSetDataLength(nlTestSuite * inSuite, void * inContext); - static void CheckTotalLength(nlTestSuite * inSuite, void * inContext); - static void CheckMaxDataLength(nlTestSuite * inSuite, void * inContext); - static void CheckAvailableDataLength(nlTestSuite * inSuite, void * inContext); - static void CheckReservedSize(nlTestSuite * inSuite, void * inContext); - static void CheckHasChainedBuffer(nlTestSuite * inSuite, void * inContext); - static void CheckAddToEnd(nlTestSuite * inSuite, void * inContext); - static void CheckPopHead(nlTestSuite * inSuite, void * inContext); - static void CheckCompactHead(nlTestSuite * inSuite, void * inContext); - static void CheckConsumeHead(nlTestSuite * inSuite, void * inContext); - static void CheckConsume(nlTestSuite * inSuite, void * inContext); - static void CheckEnsureReservedSize(nlTestSuite * inSuite, void * inContext); - static void CheckAlignPayload(nlTestSuite * inSuite, void * inContext); - static void CheckNext(nlTestSuite * inSuite, void * inContext); - static void CheckLast(nlTestSuite * inSuite, void * inContext); - static void CheckRead(nlTestSuite * inSuite, void * inContext); - static void CheckAddRef(nlTestSuite * inSuite, void * inContext); - static void CheckFree(nlTestSuite * inSuite, void * inContext); - static void CheckFreeHead(nlTestSuite * inSuite, void * inContext); - static void CheckHandleConstruct(nlTestSuite * inSuite, void * inContext); - static void CheckHandleMove(nlTestSuite * inSuite, void * inContext); - static void CheckHandleRelease(nlTestSuite * inSuite, void * inContext); - static void CheckHandleFree(nlTestSuite * inSuite, void * inContext); - static void CheckHandleRetain(nlTestSuite * inSuite, void * inContext); - static void CheckHandleAdopt(nlTestSuite * inSuite, void * inContext); - static void CheckHandleHold(nlTestSuite * inSuite, void * inContext); - static void CheckHandleAdvance(nlTestSuite * inSuite, void * inContext); - static void CheckHandleRightSize(nlTestSuite * inSuite, void * inContext); - static void CheckHandleCloneData(nlTestSuite * inSuite, void * inContext); - static void CheckPacketBufferWriter(nlTestSuite * inSuite, void * inContext); - static void CheckBuildFreeList(nlTestSuite * inSuite, void * inContext); + void SetUp() + { + configurations.resize(0); + // Set up the buffer configuration vector for this suite. + for (auto size : kReservedSizes) + { + configurations.emplace_back(size); + } + } + + void TearDown() + { + ASSERT_TRUE(ResetConfigurations()); + ASSERT_TRUE(ResetHandles()); + } static void PrintHandle(const char * tag, const PacketBuffer * buffer) { @@ -138,9 +122,6 @@ class PacketBufferTest } static void PrintHandle(const char * tag, const PacketBufferHandle & handle) { PrintHandle(tag, handle.mBuffer); } - static constexpr uint16_t kBlockSize = PacketBuffer::kBlockSize; - -private: struct BufferConfiguration { BufferConfiguration(uint16_t aReservedSize = 0) : @@ -164,12 +145,6 @@ class PacketBufferTest PrintHandle("", config.handle); } - PacketBufferTest(TestContext * context); - ~PacketBufferTest(); - - int InitializeTest(TestContext * context); - int TerminateTest(TestContext * context); - /* * Buffers allocated through PrepareTestBuffer with kRecordHandle set will be recorded in `handles` so that their * reference counts can be verified by ResetHandles(). Initially they have two refs: the recorded one and the returned one. @@ -182,103 +157,54 @@ class PacketBufferTest * Checks and clears the recorded handles. Returns true if it detects no leaks or double frees. * Called from `TerminateTest()`, but tests may choose to call it more often to verify reference counts. */ + bool ResetConfigurations(); bool ResetHandles(); - TestContext * mContext; std::vector configurations; std::vector handles; -}; - -const uint16_t sTestReservedSizes[] = { 0, 10, 128, 1536, PacketBuffer::kMaxSizeWithoutReserve, PacketBufferTest::kBlockSize }; -const uint16_t sTestLengths[] = { 0, 1, 10, 128, PacketBufferTest::kBlockSize, UINT16_MAX }; -PacketBufferTest::TestContext sContext = { - sTestReservedSizes, - sizeof(sTestReservedSizes) / sizeof(uint16_t), - sTestLengths, - sizeof(sTestLengths) / sizeof(uint16_t), + void CheckAddRef(); + void CheckAddToEnd(); + void CheckCompactHead(); + void CheckConsume(); + void CheckConsumeHead(); + void CheckDataLength(); + void CheckEnsureReservedSize(); + void CheckFree(); + void CheckFreeHead(); + void CheckHandleAdopt(); + void CheckHandleAdvance(); + void CheckHandleCloneData(); + void CheckHandleConstruct(); + void CheckHandleFree(); + void CheckHandleHold(); + void CheckHandleMove(); + void CheckHandleRelease(); + void CheckHandleRetain(); + void CheckHandleRightSize(); + void CheckLast(); + void CheckNew(); + void CheckNext(); + void CheckPopHead(); + void CheckRead(); + void CheckSetDataLength(); + void CheckSetStart(); }; -PacketBufferTest::PacketBufferTest(TestContext * context) : mContext(context) -{ - // Set up the buffer configuration vector for this suite. - configurations.resize(0); - for (size_t i = 0; i < mContext->reserved_size_count; ++i) - { - configurations.emplace_back(mContext->reserved_sizes[i]); - } -} - -int PacketBufferTest::TestSetup(void * inContext) -{ - chip::Platform::MemoryInit(); - - if (chip::DeviceLayer::PlatformMgr().InitChipStack() != CHIP_NO_ERROR) - return FAILURE; - - TestContext * const theContext = reinterpret_cast(inContext); - theContext->test = new (std::nothrow) PacketBufferTest(theContext); - if (theContext->test == nullptr) - { - return FAILURE; - } - return SUCCESS; -} - -int PacketBufferTest::TestTeardown(void * inContext) -{ - chip::DeviceLayer::PlatformMgr().Shutdown(); - - chip::Platform::MemoryShutdown(); - - return SUCCESS; -} - -int PacketBufferTest::TestInitialize(void * inContext) -{ - TestContext * const theContext = reinterpret_cast(inContext); - if (theContext->test == nullptr) - { - return FAILURE; - } - return theContext->test->InitializeTest(theContext); -} - -int PacketBufferTest::InitializeTest(TestContext * context) -{ - if (context != mContext) - { - return FAILURE; - } - return SUCCESS; -} - -int PacketBufferTest::TestTerminate(void * inContext) -{ - TestContext * const theContext = reinterpret_cast(inContext); - if (theContext->test == nullptr) - { - return FAILURE; - } - return theContext->test->TerminateTest(theContext); -} - -int PacketBufferTest::TerminateTest(TestContext * context) -{ - const bool context_ok = (context == mContext); - // Clear the configurations' bufffer handles. - for (auto & configuration : configurations) - { - configuration.handle = nullptr; - } - const bool handles_ok = ResetHandles(); - return (context_ok && handles_ok) ? SUCCESS : FAILURE; -} +/* + * Run fixture's class function as a test. + */ +#define TEST_F_FROM_FIXTURE(test_fixture, test_name) \ + TEST_F(test_fixture, test_name) \ + { \ + test_name(); \ + } \ + void test_fixture::test_name() /** * Allocate memory for a test buffer and configure according to test buffer configuration. */ -void PacketBufferTest::PrepareTestBuffer(BufferConfiguration * config, int flags) +void TestSystemPacketBuffer::PrepareTestBuffer(BufferConfiguration * config, int flags) { if (config->handle.IsNull()) { @@ -300,11 +226,11 @@ void PacketBufferTest::PrepareTestBuffer(BufferConfiguration * config, int flags exit(EXIT_FAILURE); } - const size_t lInitialSize = PacketBuffer::kStructureSize + config->reserved_size; + const size_t lInitialSize = kStructureSize + config->reserved_size; const size_t lAllocSize = kBlockSize; uint8_t * const raw = reinterpret_cast(config->handle.Get()); - memset(raw + PacketBuffer::kStructureSize, 0, lAllocSize - PacketBuffer::kStructureSize); + memset(raw + kStructureSize, 0, lAllocSize - kStructureSize); config->start_buffer = raw; config->end_buffer = raw + lAllocSize; @@ -329,10 +255,18 @@ void PacketBufferTest::PrepareTestBuffer(BufferConfiguration * config, int flags config->handle->tot_len = config->init_len; } -bool PacketBufferTest::ResetHandles() +bool TestSystemPacketBuffer::ResetConfigurations() +{ + // Clear the configurations' buffer handles. + for (auto & configuration : configurations) + configuration.handle = nullptr; + return true; +} + +bool TestSystemPacketBuffer::ResetHandles() { // Check against leaks or double-frees in tests: every handle obtained through - // PacketBufferTest::NewPacketBuffer should have a reference count of 1. + // TestSystemPacketBuffer::NewPacketBuffer should have a reference count of 1. bool handles_ok = true; for (size_t i = 0; i < handles.size(); ++i) { @@ -356,8 +290,6 @@ bool PacketBufferTest::ResetHandles() return handles_ok; } -// Test functions invoked from the suite. - /** * Test PacketBufferHandle::New() function. * @@ -367,24 +299,20 @@ bool PacketBufferTest::ResetHandles() * the method returns nullptr. Otherwise, check for correctness of initializing * the new buffer's internal state. */ -void PacketBufferTest::CheckNew(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckNew) { - TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (const auto & config : test->configurations) + for (const auto & config : configurations) { const PacketBufferHandle buffer = PacketBufferHandle::New(0, config.reserved_size); if (config.reserved_size > PacketBuffer::kMaxSizeWithoutReserve) { - NL_TEST_ASSERT(inSuite, buffer.IsNull()); + EXPECT_TRUE(buffer.IsNull()); continue; } - NL_TEST_ASSERT(inSuite, config.reserved_size <= buffer->AllocSize()); - NL_TEST_ASSERT(inSuite, !buffer.IsNull()); + EXPECT_LE(config.reserved_size, buffer->AllocSize()); + ASSERT_FALSE(buffer.IsNull()); if (!buffer.IsNull()) { @@ -393,10 +321,10 @@ void PacketBufferTest::CheckNew(nlTestSuite * inSuite, void * inContext) const pbuf * const pb = TO_LWIP_PBUF(buffer.Get()); // NOLINTEND(bugprone-casting-through-void) - NL_TEST_ASSERT(inSuite, pb->len == 0); - NL_TEST_ASSERT(inSuite, pb->tot_len == 0); - NL_TEST_ASSERT(inSuite, pb->next == nullptr); - NL_TEST_ASSERT(inSuite, pb->ref == 1); + EXPECT_EQ(pb->len, 0); + EXPECT_EQ(pb->tot_len, 0); + EXPECT_EQ(pb->next, nullptr); + EXPECT_EQ(pb->ref, 1); } } @@ -419,16 +347,13 @@ void PacketBufferTest::CheckNew(nlTestSuite * inSuite, void * inContext) /** * Test PacketBuffer::Start() function. */ -void PacketBufferTest::CheckStart(nlTestSuite * inSuite, void * inContext) +TEST_F(TestSystemPacketBuffer, CheckStart) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - for (auto & config : test->configurations) + for (auto & config : configurations) { - test->PrepareTestBuffer(&config, kRecordHandle); - NL_TEST_ASSERT(inSuite, config.handle->Start() == config.payload_ptr); + PrepareTestBuffer(&config, kRecordHandle); + EXPECT_EQ(config.handle->Start(), config.payload_ptr); } } @@ -443,15 +368,11 @@ void PacketBufferTest::CheckStart(nlTestSuite * inSuite, void * inContext) * adjusted according to the offset value passed into the * SetStart() method. */ -void PacketBufferTest::CheckSetStart(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckSetStart) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - static constexpr ptrdiff_t sSizePacketBuffer = kBlockSize; - for (auto & config : test->configurations) + for (auto & config : configurations) { // clang-format off static constexpr ptrdiff_t start_offset[] = @@ -468,16 +389,16 @@ void PacketBufferTest::CheckSetStart(nlTestSuite * inSuite, void * inContext) for (ptrdiff_t offset : start_offset) { - test->PrepareTestBuffer(&config, kRecordHandle | kAllowHandleReuse); + PrepareTestBuffer(&config, kRecordHandle | kAllowHandleReuse); uint8_t * const test_start = config.payload_ptr + offset; uint8_t * verify_start = test_start; config.handle->SetStart(test_start); - if (verify_start < config.start_buffer + PacketBuffer::kStructureSize) + if (verify_start < config.start_buffer + kStructureSize) { // Set start before valid payload beginning. - verify_start = config.start_buffer + PacketBuffer::kStructureSize; + verify_start = config.start_buffer + kStructureSize; } if (verify_start > config.end_buffer) @@ -486,18 +407,18 @@ void PacketBufferTest::CheckSetStart(nlTestSuite * inSuite, void * inContext) verify_start = config.end_buffer; } - NL_TEST_ASSERT(inSuite, config.handle->payload == verify_start); + EXPECT_EQ(config.handle->payload, verify_start); if ((verify_start - config.payload_ptr) > config.init_len) { // Set start to the beginning of payload, right after handle's header. - NL_TEST_ASSERT(inSuite, config.handle->len == 0); + EXPECT_EQ(config.handle->len, 0); } else { // Set start to somewhere between the end of the handle's // header and the end of payload. - NL_TEST_ASSERT(inSuite, config.handle->len == (config.init_len - (verify_start - config.payload_ptr))); + EXPECT_EQ(config.handle->len, (config.init_len - (verify_start - config.payload_ptr))); } } } @@ -506,17 +427,12 @@ void PacketBufferTest::CheckSetStart(nlTestSuite * inSuite, void * inContext) /** * Test PacketBuffer::DataLength() function. */ -void PacketBufferTest::CheckDataLength(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckDataLength) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config : test->configurations) + for (auto & config : configurations) { - test->PrepareTestBuffer(&config, kRecordHandle); - - NL_TEST_ASSERT(inSuite, config.handle->DataLength() == config.handle->len); + PrepareTestBuffer(&config, kRecordHandle); + EXPECT_EQ(config.handle->DataLength(), config.handle->len); } } @@ -533,21 +449,16 @@ void PacketBufferTest::CheckDataLength(nlTestSuite * inSuite, void * inContext) * other one being passed as the head of the chain. After calling * the method verify that data lengths were correctly adjusted. */ -void PacketBufferTest::CheckSetDataLength(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckSetDataLength) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config_1 : test->configurations) + for (auto & config_1 : configurations) { - for (auto & config_2 : test->configurations) + for (auto & config_2 : configurations) { - for (size_t i = 0; i < theContext->length_count; ++i) + for (auto length : kLengths) { - const uint16_t length = theContext->lengths[i]; - test->PrepareTestBuffer(&config_1, kRecordHandle | kAllowHandleReuse); - test->PrepareTestBuffer(&config_2, kRecordHandle | kAllowHandleReuse); + PrepareTestBuffer(&config_1, kRecordHandle | kAllowHandleReuse); + PrepareTestBuffer(&config_2, kRecordHandle | kAllowHandleReuse); if (&config_1 == &config_2) { @@ -556,15 +467,15 @@ void PacketBufferTest::CheckSetDataLength(nlTestSuite * inSuite, void * inContex if (length > (config_2.end_buffer - config_2.payload_ptr)) { - NL_TEST_ASSERT(inSuite, config_2.handle->len == (config_2.end_buffer - config_2.payload_ptr)); - NL_TEST_ASSERT(inSuite, config_2.handle->tot_len == (config_2.end_buffer - config_2.payload_ptr)); - NL_TEST_ASSERT(inSuite, config_2.handle->next == nullptr); + EXPECT_EQ(config_2.handle->len, (config_2.end_buffer - config_2.payload_ptr)); + EXPECT_EQ(config_2.handle->tot_len, (config_2.end_buffer - config_2.payload_ptr)); + EXPECT_EQ(config_2.handle->next, nullptr); } else { - NL_TEST_ASSERT(inSuite, config_2.handle->len == length); - NL_TEST_ASSERT(inSuite, config_2.handle->tot_len == length); - NL_TEST_ASSERT(inSuite, config_2.handle->next == nullptr); + EXPECT_EQ(config_2.handle->len, length); + EXPECT_EQ(config_2.handle->tot_len, length); + EXPECT_EQ(config_2.handle->next, nullptr); } } else @@ -574,25 +485,22 @@ void PacketBufferTest::CheckSetDataLength(nlTestSuite * inSuite, void * inContex if (length > (config_2.end_buffer - config_2.payload_ptr)) { - NL_TEST_ASSERT(inSuite, config_2.handle->len == (config_2.end_buffer - config_2.payload_ptr)); - NL_TEST_ASSERT(inSuite, config_2.handle->tot_len == (config_2.end_buffer - config_2.payload_ptr)); - NL_TEST_ASSERT(inSuite, config_2.handle->next == nullptr); - - NL_TEST_ASSERT(inSuite, - config_1.handle->tot_len == - (config_1.init_len + static_cast(config_2.end_buffer - config_2.payload_ptr) - - static_cast(config_2.init_len))); + EXPECT_EQ(config_2.handle->len, (config_2.end_buffer - config_2.payload_ptr)); + EXPECT_EQ(config_2.handle->tot_len, (config_2.end_buffer - config_2.payload_ptr)); + EXPECT_EQ(config_2.handle->next, nullptr); + + EXPECT_EQ(config_1.handle->tot_len, + (config_1.init_len + static_cast(config_2.end_buffer - config_2.payload_ptr) - + static_cast(config_2.init_len))); } else { - NL_TEST_ASSERT(inSuite, config_2.handle->len == length); - NL_TEST_ASSERT(inSuite, config_2.handle->tot_len == length); - NL_TEST_ASSERT(inSuite, config_2.handle->next == nullptr); - - NL_TEST_ASSERT( - inSuite, - config_1.handle->tot_len == - (config_1.init_len + static_cast(length) - static_cast(config_2.init_len))); + EXPECT_EQ(config_2.handle->len, length); + EXPECT_EQ(config_2.handle->tot_len, length); + EXPECT_EQ(config_2.handle->next, nullptr); + + EXPECT_EQ(config_1.handle->tot_len, + (config_1.init_len + static_cast(length) - static_cast(config_2.init_len))); } } } @@ -603,75 +511,56 @@ void PacketBufferTest::CheckSetDataLength(nlTestSuite * inSuite, void * inContex /** * Test PacketBuffer::TotalLength() function. */ -void PacketBufferTest::CheckTotalLength(nlTestSuite * inSuite, void * inContext) +TEST_F(TestSystemPacketBuffer, CheckTotalLength) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config : test->configurations) + for (auto & config : configurations) { - test->PrepareTestBuffer(&config, kRecordHandle); - NL_TEST_ASSERT(inSuite, config.handle->TotalLength() == config.init_len); + PrepareTestBuffer(&config, kRecordHandle); + EXPECT_EQ(config.handle->TotalLength(), config.init_len); } } /** * Test PacketBuffer::MaxDataLength() function. */ -void PacketBufferTest::CheckMaxDataLength(nlTestSuite * inSuite, void * inContext) +TEST_F(TestSystemPacketBuffer, CheckMaxDataLength) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config : test->configurations) + for (auto & config : configurations) { - test->PrepareTestBuffer(&config, kRecordHandle); - - NL_TEST_ASSERT(inSuite, config.handle->MaxDataLength() == (config.end_buffer - config.payload_ptr)); + PrepareTestBuffer(&config, kRecordHandle); + EXPECT_EQ(config.handle->MaxDataLength(), (config.end_buffer - config.payload_ptr)); } } /** * Test PacketBuffer::AvailableDataLength() function. */ -void PacketBufferTest::CheckAvailableDataLength(nlTestSuite * inSuite, void * inContext) +TEST_F(TestSystemPacketBuffer, CheckAvailableDataLength) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config : test->configurations) + for (auto & config : configurations) { - test->PrepareTestBuffer(&config, kRecordHandle); - - NL_TEST_ASSERT(inSuite, - config.handle->AvailableDataLength() == ((config.end_buffer - config.payload_ptr) - config.init_len)); + PrepareTestBuffer(&config, kRecordHandle); + EXPECT_EQ(config.handle->AvailableDataLength(), ((config.end_buffer - config.payload_ptr) - config.init_len)); } } /** * Test PacketBuffer::ReservedSize() function. */ -void PacketBufferTest::CheckReservedSize(nlTestSuite * inSuite, void * inContext) +TEST_F(TestSystemPacketBuffer, CheckReservedSize) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config : test->configurations) + for (auto & config : configurations) { - test->PrepareTestBuffer(&config, kRecordHandle); + PrepareTestBuffer(&config, kRecordHandle); const size_t kAllocSize = config.handle->AllocSize(); if (config.reserved_size > kAllocSize) { - NL_TEST_ASSERT(inSuite, config.handle->ReservedSize() == kAllocSize); + EXPECT_EQ(config.handle->ReservedSize(), kAllocSize); } else { - NL_TEST_ASSERT(inSuite, config.handle->ReservedSize() == config.reserved_size); + EXPECT_EQ(config.handle->ReservedSize(), config.reserved_size); } } } @@ -679,30 +568,26 @@ void PacketBufferTest::CheckReservedSize(nlTestSuite * inSuite, void * inContext /** * Test PacketBuffer::HasChainedBuffer() function. */ -void PacketBufferTest::CheckHasChainedBuffer(nlTestSuite * inSuite, void * inContext) +TEST_F(TestSystemPacketBuffer, CheckHasChainedBuffer) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config_1 : test->configurations) + for (auto & config_1 : configurations) { - for (auto & config_2 : test->configurations) + for (auto & config_2 : configurations) { if (&config_1 == &config_2) { continue; } - test->PrepareTestBuffer(&config_1); - test->PrepareTestBuffer(&config_2); + PrepareTestBuffer(&config_1); + PrepareTestBuffer(&config_2); - NL_TEST_ASSERT(inSuite, config_1.handle->HasChainedBuffer() == false); - NL_TEST_ASSERT(inSuite, config_2.handle->HasChainedBuffer() == false); + EXPECT_FALSE(config_1.handle->HasChainedBuffer()); + EXPECT_FALSE(config_2.handle->HasChainedBuffer()); config_1.handle->AddToEnd(config_2.handle.Retain()); - NL_TEST_ASSERT(inSuite, config_1.handle->HasChainedBuffer() == true); - NL_TEST_ASSERT(inSuite, config_2.handle->HasChainedBuffer() == false); + EXPECT_TRUE(config_1.handle->HasChainedBuffer()); + EXPECT_FALSE(config_2.handle->HasChainedBuffer()); config_1.handle = nullptr; config_2.handle = nullptr; @@ -721,49 +606,45 @@ void PacketBufferTest::CheckHasChainedBuffer(nlTestSuite * inSuite, void * inCon * This test function tests linking any combination of three * buffer-configurations passed within inContext. */ -void PacketBufferTest::CheckAddToEnd(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckAddToEnd) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config_1 : test->configurations) + for (auto & config_1 : configurations) { - for (auto & config_2 : test->configurations) + for (auto & config_2 : configurations) { - for (auto & config_3 : test->configurations) + for (auto & config_3 : configurations) { if (&config_1 == &config_2 || &config_1 == &config_3 || &config_2 == &config_3) { continue; } - test->PrepareTestBuffer(&config_1); - test->PrepareTestBuffer(&config_2); - test->PrepareTestBuffer(&config_3); - NL_TEST_ASSERT(inSuite, config_1.handle->ref == 1); - NL_TEST_ASSERT(inSuite, config_2.handle->ref == 1); - NL_TEST_ASSERT(inSuite, config_3.handle->ref == 1); + PrepareTestBuffer(&config_1); + PrepareTestBuffer(&config_2); + PrepareTestBuffer(&config_3); + EXPECT_EQ(config_1.handle->ref, 1); + EXPECT_EQ(config_2.handle->ref, 1); + EXPECT_EQ(config_3.handle->ref, 1); config_1.handle->AddToEnd(config_2.handle.Retain()); - NL_TEST_ASSERT(inSuite, config_1.handle->ref == 1); // config_1.handle - NL_TEST_ASSERT(inSuite, config_2.handle->ref == 2); // config_2.handle and config_1.handle->next - NL_TEST_ASSERT(inSuite, config_3.handle->ref == 1); // config_3.handle + EXPECT_EQ(config_1.handle->ref, 1); // config_1.handle + EXPECT_EQ(config_2.handle->ref, 2); // config_2.handle and config_1.handle->next + EXPECT_EQ(config_3.handle->ref, 1); // config_3.handle - NL_TEST_ASSERT(inSuite, config_1.handle->tot_len == (config_1.init_len + config_2.init_len)); - NL_TEST_ASSERT(inSuite, config_1.handle->next == config_2.handle.Get()); - NL_TEST_ASSERT(inSuite, config_2.handle->next == nullptr); - NL_TEST_ASSERT(inSuite, config_3.handle->next == nullptr); + EXPECT_EQ(config_1.handle->tot_len, (config_1.init_len + config_2.init_len)); + EXPECT_EQ(config_1.handle->next, config_2.handle.Get()); + EXPECT_EQ(config_2.handle->next, nullptr); + EXPECT_EQ(config_3.handle->next, nullptr); config_1.handle->AddToEnd(config_3.handle.Retain()); - NL_TEST_ASSERT(inSuite, config_1.handle->ref == 1); // config_1.handle - NL_TEST_ASSERT(inSuite, config_2.handle->ref == 2); // config_2.handle and config_1.handle->next - NL_TEST_ASSERT(inSuite, config_3.handle->ref == 2); // config_3.handle and config_2.handle->next + EXPECT_EQ(config_1.handle->ref, 1); // config_1.handle + EXPECT_EQ(config_2.handle->ref, 2); // config_2.handle and config_1.handle->next + EXPECT_EQ(config_3.handle->ref, 2); // config_3.handle and config_2.handle->next - NL_TEST_ASSERT(inSuite, config_1.handle->tot_len == (config_1.init_len + config_2.init_len + config_3.init_len)); - NL_TEST_ASSERT(inSuite, config_1.handle->next == config_2.handle.Get()); - NL_TEST_ASSERT(inSuite, config_2.handle->next == config_3.handle.Get()); - NL_TEST_ASSERT(inSuite, config_3.handle->next == nullptr); + EXPECT_EQ(config_1.handle->tot_len, (config_1.init_len + config_2.init_len + config_3.init_len)); + EXPECT_EQ(config_1.handle->next, config_2.handle.Get()); + EXPECT_EQ(config_2.handle->next, config_3.handle.Get()); + EXPECT_EQ(config_3.handle->next, nullptr); config_1.handle = nullptr; config_2.handle = nullptr; @@ -783,50 +664,47 @@ void PacketBufferTest::CheckAddToEnd(nlTestSuite * inSuite, void * inContext) * on the first buffer to unlink the second buffer. After the call, * verify correct internal state of the first buffer. */ -void PacketBufferTest::CheckPopHead(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckPopHead) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - // Single buffer test. - for (auto & config_1 : test->configurations) + for (auto & config_1 : configurations) { - test->PrepareTestBuffer(&config_1, kRecordHandle | kAllowHandleReuse); - NL_TEST_ASSERT(inSuite, config_1.handle->ref == 2); + PrepareTestBuffer(&config_1, kRecordHandle | kAllowHandleReuse); + EXPECT_EQ(config_1.handle->ref, 2); const PacketBuffer * const buffer_1 = config_1.handle.mBuffer; const PacketBufferHandle popped = config_1.handle.PopHead(); - NL_TEST_ASSERT(inSuite, config_1.handle.IsNull()); - NL_TEST_ASSERT(inSuite, popped.mBuffer == buffer_1); - NL_TEST_ASSERT(inSuite, popped->next == nullptr); - NL_TEST_ASSERT(inSuite, popped->tot_len == config_1.init_len); - NL_TEST_ASSERT(inSuite, popped->ref == 2); + EXPECT_TRUE(config_1.handle.IsNull()); + EXPECT_EQ(popped.mBuffer, buffer_1); + EXPECT_EQ(popped->next, nullptr); + EXPECT_EQ(popped->tot_len, config_1.init_len); + EXPECT_EQ(popped->ref, 2); } - test->ResetHandles(); + + ResetHandles(); // Chained buffers test. - for (auto & config_1 : test->configurations) + for (auto & config_1 : configurations) { - for (auto & config_2 : test->configurations) + for (auto & config_2 : configurations) { if (&config_1 == &config_2) { continue; } - test->PrepareTestBuffer(&config_1, kRecordHandle | kAllowHandleReuse); - test->PrepareTestBuffer(&config_2, kRecordHandle | kAllowHandleReuse); + PrepareTestBuffer(&config_1, kRecordHandle | kAllowHandleReuse); + PrepareTestBuffer(&config_2, kRecordHandle | kAllowHandleReuse); config_1.handle->AddToEnd(config_2.handle.Retain()); const PacketBufferHandle popped = config_1.handle.PopHead(); - NL_TEST_ASSERT(inSuite, config_1.handle == config_2.handle); - NL_TEST_ASSERT(inSuite, config_1.handle->next == nullptr); - NL_TEST_ASSERT(inSuite, config_1.handle->tot_len == config_1.init_len); + EXPECT_EQ(config_1.handle, config_2.handle); + EXPECT_EQ(config_1.handle->next, nullptr); + EXPECT_EQ(config_1.handle->tot_len, config_1.init_len); } } } @@ -842,37 +720,32 @@ void PacketBufferTest::CheckPopHead(nlTestSuite * inSuite, void * inContext) * the chain. After calling the method, verify correctly adjusted * state of the first buffer. */ -void PacketBufferTest::CheckCompactHead(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckCompactHead) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - // Single buffer test. - for (auto & config : test->configurations) + for (auto & config : configurations) { - for (size_t i = 0; i < theContext->length_count; ++i) + for (auto length : kLengths) { - const uint16_t length = theContext->lengths[i]; - - test->PrepareTestBuffer(&config, kRecordHandle | kAllowHandleReuse); + PrepareTestBuffer(&config, kRecordHandle | kAllowHandleReuse); config.handle->SetDataLength(length, config.handle); const uint16_t data_length = config.handle->DataLength(); config.handle->CompactHead(); - NL_TEST_ASSERT(inSuite, config.handle->payload == (config.start_buffer + PacketBuffer::kStructureSize)); - NL_TEST_ASSERT(inSuite, config.handle->tot_len == data_length); + EXPECT_EQ(config.handle->payload, (config.start_buffer + kStructureSize)); + EXPECT_EQ(config.handle->tot_len, data_length); } config.handle = nullptr; } - NL_TEST_ASSERT(inSuite, test->ResetHandles()); + + EXPECT_TRUE(ResetHandles()); // Chained buffers test. - for (auto & config_1 : test->configurations) + for (auto & config_1 : configurations) { - for (auto & config_2 : test->configurations) + for (auto & config_2 : configurations) { if (&config_1 == &config_2) { @@ -880,24 +753,20 @@ void PacketBufferTest::CheckCompactHead(nlTestSuite * inSuite, void * inContext) } // start with various initial length for the first buffer - for (size_t i = 0; i < theContext->length_count; ++i) + for (auto length_1 : kLengths) { - const uint16_t length_1 = theContext->lengths[i]; - // start with various initial length for the second buffer - for (size_t j = 0; j < theContext->length_count; ++j) + for (auto length_2 : kLengths) { - const uint16_t length_2 = theContext->lengths[j]; - - test->PrepareTestBuffer(&config_1, kRecordHandle | kAllowHandleReuse); - NL_TEST_ASSERT(inSuite, config_1.handle->ref == 2); + PrepareTestBuffer(&config_1, kRecordHandle | kAllowHandleReuse); + EXPECT_EQ(config_1.handle->ref, 2); // CompactHead requires that there be no other references to the chained buffer, // so we manage it manually. - test->PrepareTestBuffer(&config_2); - NL_TEST_ASSERT(inSuite, config_2.handle->ref == 1); + PrepareTestBuffer(&config_2); + EXPECT_EQ(config_2.handle->ref, 1); PacketBuffer * buffer_2 = std::move(config_2.handle).UnsafeRelease(); - NL_TEST_ASSERT(inSuite, config_2.handle.IsNull()); + EXPECT_TRUE(config_2.handle.IsNull()); config_1.handle->SetDataLength(length_1, config_1.handle); const uint16_t data_length_1 = config_1.handle->DataLength(); @@ -911,38 +780,38 @@ void PacketBufferTest::CheckCompactHead(nlTestSuite * inSuite, void * inContext) config_1.handle->CompactHead(); - NL_TEST_ASSERT(inSuite, config_1.handle->payload == (config_1.start_buffer + PacketBuffer::kStructureSize)); + EXPECT_EQ(config_1.handle->payload, (config_1.start_buffer + kStructureSize)); if (config_1.handle->tot_len > config_1.handle->MaxDataLength()) { - NL_TEST_ASSERT(inSuite, config_1.handle->len == config_1.handle->MaxDataLength()); - NL_TEST_ASSERT(inSuite, buffer_2->len == config_1.handle->tot_len - config_1.handle->MaxDataLength()); - NL_TEST_ASSERT(inSuite, config_1.handle->next == buffer_2); - NL_TEST_ASSERT(inSuite, config_1.handle->ref == 2); - NL_TEST_ASSERT(inSuite, buffer_2->ref == 1); + EXPECT_EQ(config_1.handle->len, config_1.handle->MaxDataLength()); + EXPECT_EQ(buffer_2->len, config_1.handle->tot_len - config_1.handle->MaxDataLength()); + EXPECT_EQ(config_1.handle->next, buffer_2); + EXPECT_EQ(config_1.handle->ref, 2); + EXPECT_EQ(buffer_2->ref, 1); } else { - NL_TEST_ASSERT(inSuite, config_1.handle->len == config_1.handle->tot_len); + EXPECT_EQ(config_1.handle->len, config_1.handle->tot_len); if (data_length_1 >= config_1.handle->MaxDataLength() && data_length_2 == 0) { /* make sure the second buffer is not freed */ - NL_TEST_ASSERT(inSuite, config_1.handle->next == buffer_2); - NL_TEST_ASSERT(inSuite, buffer_2->ref == 1); + EXPECT_EQ(config_1.handle->next, buffer_2); + EXPECT_EQ(buffer_2->ref, 1); } else { /* make sure the second buffer is freed */ - NL_TEST_ASSERT(inSuite, config_1.handle->next == nullptr); + EXPECT_EQ(config_1.handle->next, nullptr); buffer_2 = nullptr; } } - NL_TEST_ASSERT(inSuite, config_1.handle->ref == 2); + EXPECT_EQ(config_1.handle->ref, 2); config_1.handle = nullptr; // Verify and release handles. - NL_TEST_ASSERT(inSuite, test->ResetHandles()); + EXPECT_TRUE(ResetHandles()); } } } @@ -959,32 +828,27 @@ void PacketBufferTest::CheckCompactHead(nlTestSuite * inSuite, void * inContext) * the internal state of the buffer has been correctly * adjusted according to the value passed into the method. */ -void PacketBufferTest::CheckConsumeHead(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckConsumeHead) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config : test->configurations) + for (auto & config : configurations) { - for (size_t i = 0; i < theContext->length_count; ++i) + for (auto length : kLengths) { - const uint16_t length = theContext->lengths[i]; - test->PrepareTestBuffer(&config, kRecordHandle | kAllowHandleReuse); + PrepareTestBuffer(&config, kRecordHandle | kAllowHandleReuse); config.handle->ConsumeHead(length); if (length > config.init_len) { - NL_TEST_ASSERT(inSuite, config.handle->payload == (config.payload_ptr + config.init_len)); - NL_TEST_ASSERT(inSuite, config.handle->len == 0); - NL_TEST_ASSERT(inSuite, config.handle->tot_len == 0); + EXPECT_EQ(config.handle->payload, (config.payload_ptr + config.init_len)); + EXPECT_EQ(config.handle->len, 0); + EXPECT_EQ(config.handle->tot_len, 0); } else { - NL_TEST_ASSERT(inSuite, config.handle->payload == (config.payload_ptr + length)); - NL_TEST_ASSERT(inSuite, config.handle->len == (config.handle->len - length)); - NL_TEST_ASSERT(inSuite, config.handle->tot_len == (config.handle->tot_len - length)); + EXPECT_EQ(config.handle->payload, (config.payload_ptr + length)); + EXPECT_EQ(config.handle->len, (config.handle->len - length)); + EXPECT_EQ(config.handle->tot_len, (config.handle->tot_len - length)); } } } @@ -1002,15 +866,11 @@ void PacketBufferTest::CheckConsumeHead(nlTestSuite * inSuite, void * inContext) * method, verify correctly adjusted the state of the first * buffer and appropriate return pointer from the method's call. */ -void PacketBufferTest::CheckConsume(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckConsume) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config_1 : test->configurations) + for (auto & config_1 : configurations) { - for (auto & config_2 : test->configurations) + for (auto & config_2 : configurations) { if (&config_1 == &config_2) { @@ -1018,22 +878,18 @@ void PacketBufferTest::CheckConsume(nlTestSuite * inSuite, void * inContext) } // consume various amounts of memory - for (size_t i = 0; i < theContext->length_count; ++i) + for (auto consumeLength : kLengths) { - const uint16_t consumeLength = theContext->lengths[i]; // start with various initial length for the first buffer - for (size_t j = 0; j < theContext->length_count; ++j) + for (auto len_1 : kLengths) { - const uint16_t len_1 = theContext->lengths[j]; // start with various initial length for the second buffer - for (size_t k = 0; k < theContext->length_count; ++k) + for (auto len_2 : kLengths) { - const uint16_t len_2 = theContext->lengths[k]; - - test->PrepareTestBuffer(&config_1); - test->PrepareTestBuffer(&config_2); - NL_TEST_ASSERT(inSuite, config_1.handle->ref == 1); - NL_TEST_ASSERT(inSuite, config_2.handle->ref == 1); + PrepareTestBuffer(&config_1); + PrepareTestBuffer(&config_2); + EXPECT_EQ(config_1.handle->ref, 1); + EXPECT_EQ(config_2.handle->ref, 1); config_1.handle->AddToEnd(config_2.handle.Retain()); @@ -1045,40 +901,40 @@ void PacketBufferTest::CheckConsume(nlTestSuite * inSuite, void * inContext) const uint16_t buf_2_len = config_2.handle->len; PacketBufferHandle original_handle_1 = config_1.handle.Retain(); - NL_TEST_ASSERT(inSuite, config_1.handle->ref == 2); // config_1.handle and original_handle_1 - NL_TEST_ASSERT(inSuite, config_2.handle->ref == 2); // config_2.handle and config_1.handle->next + EXPECT_EQ(config_1.handle->ref, 2); // config_1.handle and original_handle_1 + EXPECT_EQ(config_2.handle->ref, 2); // config_2.handle and config_1.handle->next config_1.handle.Consume(consumeLength); if (consumeLength == 0) { - NL_TEST_ASSERT(inSuite, config_1.handle == original_handle_1); - NL_TEST_ASSERT(inSuite, config_1.handle->len == buf_1_len); - NL_TEST_ASSERT(inSuite, config_2.handle->len == buf_2_len); - NL_TEST_ASSERT(inSuite, config_1.handle->ref == 2); // config_1.handle and original_handle_1 - NL_TEST_ASSERT(inSuite, config_2.handle->ref == 2); // config_2.handle and config_1.handle->next + EXPECT_EQ(config_1.handle, original_handle_1); + EXPECT_EQ(config_1.handle->len, buf_1_len); + EXPECT_EQ(config_2.handle->len, buf_2_len); + EXPECT_EQ(config_1.handle->ref, 2); // config_1.handle and original_handle_1 + EXPECT_EQ(config_2.handle->ref, 2); // config_2.handle and config_1.handle->next } else if (consumeLength < buf_1_len) { - NL_TEST_ASSERT(inSuite, config_1.handle == original_handle_1); - NL_TEST_ASSERT(inSuite, config_1.handle->len == buf_1_len - consumeLength); - NL_TEST_ASSERT(inSuite, config_2.handle->len == buf_2_len); - NL_TEST_ASSERT(inSuite, config_1.handle->ref == 2); // config_1.handle and original_handle_1 - NL_TEST_ASSERT(inSuite, config_2.handle->ref == 2); // config_2.handle and config_1.handle->next + EXPECT_EQ(config_1.handle, original_handle_1); + EXPECT_EQ(config_1.handle->len, buf_1_len - consumeLength); + EXPECT_EQ(config_2.handle->len, buf_2_len); + EXPECT_EQ(config_1.handle->ref, 2); // config_1.handle and original_handle_1 + EXPECT_EQ(config_2.handle->ref, 2); // config_2.handle and config_1.handle->next } else if ((consumeLength < buf_1_len + buf_2_len || (consumeLength == buf_1_len + buf_2_len && buf_2_len == 0))) { - NL_TEST_ASSERT(inSuite, config_1.handle == config_2.handle); - NL_TEST_ASSERT(inSuite, config_2.handle->len == buf_1_len + buf_2_len - consumeLength); - NL_TEST_ASSERT(inSuite, original_handle_1->ref == 1); // original_handle_1 - NL_TEST_ASSERT(inSuite, config_2.handle->ref == 2); // config_1.handle and config_2.handle + EXPECT_EQ(config_1.handle, config_2.handle); + EXPECT_EQ(config_2.handle->len, buf_1_len + buf_2_len - consumeLength); + EXPECT_EQ(original_handle_1->ref, 1); // original_handle_1 + EXPECT_EQ(config_2.handle->ref, 2); // config_1.handle and config_2.handle } else { - NL_TEST_ASSERT(inSuite, config_1.handle.IsNull()); - NL_TEST_ASSERT(inSuite, original_handle_1->ref == 1); // original_handle_1 - NL_TEST_ASSERT(inSuite, config_2.handle->ref == 1); // config_2.handle + EXPECT_TRUE(config_1.handle.IsNull()); + EXPECT_EQ(original_handle_1->ref, 1); // original_handle_1 + EXPECT_EQ(config_2.handle->ref, 1); // config_2.handle } original_handle_1 = nullptr; @@ -1100,41 +956,35 @@ void PacketBufferTest::CheckConsume(nlTestSuite * inSuite, void * inContext) * Then, verify that EnsureReservedSize() method correctly * retrieves the amount of the reserved space. */ -void PacketBufferTest::CheckEnsureReservedSize(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckEnsureReservedSize) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config : test->configurations) + for (auto & config : configurations) { - for (size_t i = 0; i < theContext->length_count; ++i) + for (auto length : kLengths) { - const uint16_t length = theContext->lengths[i]; - - test->PrepareTestBuffer(&config, kRecordHandle | kAllowHandleReuse); + PrepareTestBuffer(&config, kRecordHandle | kAllowHandleReuse); const uint16_t kAllocSize = config.handle->AllocSize(); uint16_t reserved_size = config.reserved_size; - if (PacketBuffer::kStructureSize + config.reserved_size > kAllocSize) + if (kStructureSize + config.reserved_size > kAllocSize) { - reserved_size = static_cast(kAllocSize - PacketBuffer::kStructureSize); + reserved_size = static_cast(kAllocSize - kStructureSize); } if (length <= reserved_size) { - NL_TEST_ASSERT(inSuite, config.handle->EnsureReservedSize(length) == true); + EXPECT_EQ(config.handle->EnsureReservedSize(length), true); continue; } - if ((length + config.init_len) > (kAllocSize - PacketBuffer::kStructureSize)) + if ((length + config.init_len) > (kAllocSize - kStructureSize)) { - NL_TEST_ASSERT(inSuite, config.handle->EnsureReservedSize(length) == false); + EXPECT_FALSE(config.handle->EnsureReservedSize(length)); continue; } - NL_TEST_ASSERT(inSuite, config.handle->EnsureReservedSize(length) == true); - NL_TEST_ASSERT(inSuite, config.handle->payload == (config.payload_ptr + length - reserved_size)); + EXPECT_EQ(config.handle->EnsureReservedSize(length), true); + EXPECT_EQ(config.handle->payload, (config.payload_ptr + length - reserved_size)); } } } @@ -1148,22 +998,18 @@ void PacketBufferTest::CheckEnsureReservedSize(nlTestSuite * inSuite, void * inC * required payload shift. Then, verify that AlignPayload() * method correctly aligns the payload start pointer. */ -void PacketBufferTest::CheckAlignPayload(nlTestSuite * inSuite, void * inContext) +TEST_F(TestSystemPacketBuffer, CheckAlignPayload) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config : test->configurations) + for (auto & config : configurations) { - for (size_t n = 0; n < theContext->length_count; ++n) + for (auto length : kLengths) { - test->PrepareTestBuffer(&config, kRecordHandle | kAllowHandleReuse); + PrepareTestBuffer(&config, kRecordHandle | kAllowHandleReuse); const uint16_t kAllocSize = config.handle->AllocSize(); - if (theContext->lengths[n] == 0) + if (length == 0) { - NL_TEST_ASSERT(inSuite, config.handle->AlignPayload(theContext->lengths[n]) == false); + EXPECT_FALSE(config.handle->AlignPayload(length)); continue; } @@ -1173,20 +1019,19 @@ void PacketBufferTest::CheckAlignPayload(nlTestSuite * inSuite, void * inContext reserved_size = kAllocSize; } - const uint16_t payload_offset = - static_cast(reinterpret_cast(config.handle->Start()) % theContext->lengths[n]); - uint16_t payload_shift = 0; + const uint16_t payload_offset = static_cast(reinterpret_cast(config.handle->Start()) % length); + uint16_t payload_shift = 0; if (payload_offset > 0) - payload_shift = static_cast(theContext->lengths[n] - payload_offset); + payload_shift = static_cast(length - payload_offset); if (payload_shift <= kAllocSize - reserved_size) { - NL_TEST_ASSERT(inSuite, config.handle->AlignPayload(theContext->lengths[n]) == true); - NL_TEST_ASSERT(inSuite, ((unsigned long) config.handle->Start() % theContext->lengths[n]) == 0); + EXPECT_EQ(config.handle->AlignPayload(length), true); + EXPECT_EQ(((unsigned long) config.handle->Start() % length), 0UL); } else { - NL_TEST_ASSERT(inSuite, config.handle->AlignPayload(theContext->lengths[n]) == false); + EXPECT_FALSE(config.handle->AlignPayload(length)); } } } @@ -1195,34 +1040,30 @@ void PacketBufferTest::CheckAlignPayload(nlTestSuite * inSuite, void * inContext /** * Test PacketBuffer::Next() function. */ -void PacketBufferTest::CheckNext(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckNext) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config_1 : test->configurations) + for (auto & config_1 : configurations) { - for (auto & config_2 : test->configurations) + for (auto & config_2 : configurations) { - test->PrepareTestBuffer(&config_1, kRecordHandle | kAllowHandleReuse); - test->PrepareTestBuffer(&config_2, kRecordHandle | kAllowHandleReuse); + PrepareTestBuffer(&config_1, kRecordHandle | kAllowHandleReuse); + PrepareTestBuffer(&config_2, kRecordHandle | kAllowHandleReuse); if (&config_1 != &config_2) { - NL_TEST_ASSERT(inSuite, config_1.handle->Next().IsNull()); + EXPECT_TRUE(config_1.handle->Next().IsNull()); config_1.handle->AddToEnd(config_2.handle.Retain()); - NL_TEST_ASSERT(inSuite, config_1.handle->Next() == config_2.handle); - NL_TEST_ASSERT(inSuite, config_1.handle->ChainedBuffer() == config_2.handle.Get()); + EXPECT_EQ(config_1.handle->Next(), config_2.handle); + EXPECT_EQ(config_1.handle->ChainedBuffer(), config_2.handle.Get()); } else { - NL_TEST_ASSERT(inSuite, !config_1.handle->HasChainedBuffer()); + EXPECT_FALSE(config_1.handle->HasChainedBuffer()); } - NL_TEST_ASSERT(inSuite, !config_2.handle->HasChainedBuffer()); + EXPECT_FALSE(config_2.handle->HasChainedBuffer()); } } } @@ -1230,42 +1071,38 @@ void PacketBufferTest::CheckNext(nlTestSuite * inSuite, void * inContext) /** * Test PacketBuffer::Last() function. */ -void PacketBufferTest::CheckLast(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckLast) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config_1 : test->configurations) + for (auto & config_1 : configurations) { - for (auto & config_2 : test->configurations) + for (auto & config_2 : configurations) { - for (auto & config_3 : test->configurations) + for (auto & config_3 : configurations) { if (&config_1 == &config_2 || &config_1 == &config_3 || &config_2 == &config_3) { continue; } - test->PrepareTestBuffer(&config_1); - test->PrepareTestBuffer(&config_2); - test->PrepareTestBuffer(&config_3); + PrepareTestBuffer(&config_1); + PrepareTestBuffer(&config_2); + PrepareTestBuffer(&config_3); - NL_TEST_ASSERT(inSuite, config_1.handle->Last() == config_1.handle); - NL_TEST_ASSERT(inSuite, config_2.handle->Last() == config_2.handle); - NL_TEST_ASSERT(inSuite, config_3.handle->Last() == config_3.handle); + EXPECT_EQ(config_1.handle->Last(), config_1.handle); + EXPECT_EQ(config_2.handle->Last(), config_2.handle); + EXPECT_EQ(config_3.handle->Last(), config_3.handle); config_1.handle->AddToEnd(config_2.handle.Retain()); - NL_TEST_ASSERT(inSuite, config_1.handle->Last() == config_2.handle); - NL_TEST_ASSERT(inSuite, config_2.handle->Last() == config_2.handle); - NL_TEST_ASSERT(inSuite, config_3.handle->Last() == config_3.handle); + EXPECT_EQ(config_1.handle->Last(), config_2.handle); + EXPECT_EQ(config_2.handle->Last(), config_2.handle); + EXPECT_EQ(config_3.handle->Last(), config_3.handle); config_1.handle->AddToEnd(config_3.handle.Retain()); - NL_TEST_ASSERT(inSuite, config_1.handle->Last() == config_3.handle); - NL_TEST_ASSERT(inSuite, config_2.handle->Last() == config_3.handle); - NL_TEST_ASSERT(inSuite, config_3.handle->Last() == config_3.handle); + EXPECT_EQ(config_1.handle->Last(), config_3.handle); + EXPECT_EQ(config_2.handle->Last(), config_3.handle); + EXPECT_EQ(config_3.handle->Last(), config_3.handle); config_1.handle = nullptr; config_2.handle = nullptr; @@ -1278,12 +1115,8 @@ void PacketBufferTest::CheckLast(nlTestSuite * inSuite, void * inContext) /** * Test PacketBuffer::Read() function. */ -void PacketBufferTest::CheckRead(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckRead) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - uint8_t payloads[2 * kBlockSize] = { 1 }; uint8_t result[2 * kBlockSize]; for (size_t i = 1; i < sizeof(payloads); ++i) @@ -1291,47 +1124,47 @@ void PacketBufferTest::CheckRead(nlTestSuite * inSuite, void * inContext) payloads[i] = static_cast(random()); } - for (auto & config_1 : test->configurations) + for (auto & config_1 : configurations) { - for (auto & config_2 : test->configurations) + for (auto & config_2 : configurations) { if (&config_1 == &config_2) { continue; } - test->PrepareTestBuffer(&config_1, kAllowHandleReuse); - test->PrepareTestBuffer(&config_2, kAllowHandleReuse); + PrepareTestBuffer(&config_1, kAllowHandleReuse); + PrepareTestBuffer(&config_2, kAllowHandleReuse); const uint16_t length_1 = config_1.handle->MaxDataLength(); const uint16_t length_2 = config_2.handle->MaxDataLength(); const size_t length_sum = length_1 + length_2; const uint16_t length_total = static_cast(length_sum); - NL_TEST_ASSERT(inSuite, length_total == length_sum); + EXPECT_EQ(length_total, length_sum); memcpy(config_1.handle->Start(), payloads, length_1); memcpy(config_2.handle->Start(), payloads + length_1, length_2); config_1.handle->SetDataLength(length_1); config_2.handle->SetDataLength(length_2); config_1.handle->AddToEnd(config_2.handle.Retain()); - NL_TEST_ASSERT(inSuite, config_1.handle->TotalLength() == length_total); + EXPECT_EQ(config_1.handle->TotalLength(), length_total); if (length_1 >= 1) { // Check a read that does not span packet buffers. CHIP_ERROR err = config_1.handle->Read(result, 1); - NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, result[0] == payloads[0]); + EXPECT_EQ(err, CHIP_NO_ERROR); + EXPECT_EQ(result[0], payloads[0]); } // Check a read that spans packet buffers. CHIP_ERROR err = config_1.handle->Read(result, length_total); - NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, memcmp(payloads, result, length_total) == 0); + EXPECT_EQ(err, CHIP_NO_ERROR); + EXPECT_EQ(memcmp(payloads, result, length_total), 0); // Check a read that is too long fails. err = config_1.handle->Read(result, length_total + 1); - NL_TEST_ASSERT(inSuite, err == CHIP_ERROR_BUFFER_TOO_SMALL); + EXPECT_EQ(err, CHIP_ERROR_BUFFER_TOO_SMALL); // Check that running off the end of a corrupt buffer chain is detected. if (length_total < UINT16_MAX) @@ -1339,7 +1172,7 @@ void PacketBufferTest::CheckRead(nlTestSuite * inSuite, void * inContext) // First case: TotalLength() is wrong. config_1.handle->tot_len = static_cast(config_1.handle->tot_len + 1); err = config_1.handle->Read(result, length_total + 1); - NL_TEST_ASSERT(inSuite, err == CHIP_ERROR_INTERNAL); + EXPECT_EQ(err, CHIP_ERROR_INTERNAL); config_1.handle->tot_len = static_cast(config_1.handle->tot_len - 1); } if (length_1 >= 1) @@ -1347,7 +1180,7 @@ void PacketBufferTest::CheckRead(nlTestSuite * inSuite, void * inContext) // Second case: an individual buffer's DataLength() is wrong. config_1.handle->len = static_cast(config_1.handle->len - 1); err = config_1.handle->Read(result, length_total); - NL_TEST_ASSERT(inSuite, err == CHIP_ERROR_INTERNAL); + EXPECT_EQ(err, CHIP_ERROR_INTERNAL); config_1.handle->len = static_cast(config_1.handle->len + 1); } @@ -1360,18 +1193,14 @@ void PacketBufferTest::CheckRead(nlTestSuite * inSuite, void * inContext) /** * Test PacketBuffer::AddRef() function. */ -void PacketBufferTest::CheckAddRef(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckAddRef) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config : test->configurations) + for (auto & config : configurations) { - test->PrepareTestBuffer(&config, kRecordHandle); + PrepareTestBuffer(&config, kRecordHandle); const auto refs = config.handle->ref; config.handle->AddRef(); - NL_TEST_ASSERT(inSuite, config.handle->ref == refs + 1); + EXPECT_EQ(config.handle->ref, refs + 1); config.handle->ref = refs; // Don't leak buffers. } } @@ -1387,18 +1216,14 @@ void PacketBufferTest::CheckAddRef(nlTestSuite * inSuite, void * inContext) * the chain and verify correctly adjusted states of the two * buffers. */ -void PacketBufferTest::CheckFree(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckFree) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - const decltype(PacketBuffer::ref) init_ref_count[] = { 1, 2, 3 }; constexpr size_t kRefs = sizeof(init_ref_count) / sizeof(init_ref_count[0]); - for (auto & config_1 : test->configurations) + for (auto & config_1 : configurations) { - for (auto & config_2 : test->configurations) + for (auto & config_2 : configurations) { if (&config_1 == &config_2) { @@ -1410,13 +1235,13 @@ void PacketBufferTest::CheckFree(nlTestSuite * inSuite, void * inContext) { config_1.handle = PacketBufferHandle::New(chip::System::PacketBuffer::kMaxSizeWithoutReserve, 0); config_2.handle = PacketBufferHandle::New(chip::System::PacketBuffer::kMaxSizeWithoutReserve, 0); - NL_TEST_ASSERT(inSuite, !config_1.handle.IsNull()); - NL_TEST_ASSERT(inSuite, !config_2.handle.IsNull()); + ASSERT_FALSE(config_1.handle.IsNull()); + ASSERT_FALSE(config_2.handle.IsNull()); - test->PrepareTestBuffer(&config_1, kAllowHandleReuse); - test->PrepareTestBuffer(&config_2, kAllowHandleReuse); - NL_TEST_ASSERT(inSuite, config_1.handle->ref == 1); - NL_TEST_ASSERT(inSuite, config_2.handle->ref == 1); + PrepareTestBuffer(&config_1, kAllowHandleReuse); + PrepareTestBuffer(&config_2, kAllowHandleReuse); + EXPECT_EQ(config_1.handle->ref, 1); + EXPECT_EQ(config_2.handle->ref, 1); // Chain buffers. config_1.handle->next = config_2.handle.Get(); @@ -1436,18 +1261,18 @@ void PacketBufferTest::CheckFree(nlTestSuite * inSuite, void * inContext) if (initial_refs_1 > 1) { // Verify that head ref count is decremented. - NL_TEST_ASSERT(inSuite, config_1.handle->ref == initial_refs_1 - 1); + EXPECT_EQ(config_1.handle->ref, initial_refs_1 - 1); // Verify that chain is maintained. - NL_TEST_ASSERT(inSuite, config_1.handle->next == config_2.handle.Get()); + EXPECT_EQ(config_1.handle->next, config_2.handle.Get()); // Verify that chained buffer ref count has not changed. - NL_TEST_ASSERT(inSuite, config_2.handle->ref == initial_refs_2); + EXPECT_EQ(config_2.handle->ref, initial_refs_2); } else { if (initial_refs_2 > 1) { // Verify that chained buffer ref count is decremented. - NL_TEST_ASSERT(inSuite, config_2.handle->ref == initial_refs_2 - 1); + EXPECT_EQ(config_2.handle->ref, initial_refs_2 - 1); } else { @@ -1483,15 +1308,11 @@ void PacketBufferTest::CheckFree(nlTestSuite * inSuite, void * inContext) * FreeHead() on the first buffer in the chain and verify that * the method returned pointer to the second buffer. */ -void PacketBufferTest::CheckFreeHead(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckFreeHead) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config_1 : test->configurations) + for (auto & config_1 : configurations) { - for (auto & config_2 : test->configurations) + for (auto & config_2 : configurations) { if (&config_1 == &config_2) { @@ -1500,47 +1321,47 @@ void PacketBufferTest::CheckFreeHead(nlTestSuite * inSuite, void * inContext) // Test PacketBuffer::FreeHead - test->PrepareTestBuffer(&config_1, kAllowHandleReuse); - test->PrepareTestBuffer(&config_2, kAllowHandleReuse); - NL_TEST_ASSERT(inSuite, config_1.handle->ref == 1); - NL_TEST_ASSERT(inSuite, config_2.handle->ref == 1); + PrepareTestBuffer(&config_1, kAllowHandleReuse); + PrepareTestBuffer(&config_2, kAllowHandleReuse); + EXPECT_EQ(config_1.handle->ref, 1); + EXPECT_EQ(config_2.handle->ref, 1); PacketBufferHandle handle_1 = config_1.handle.Retain(); config_1.handle->AddToEnd(config_2.handle.Retain()); - NL_TEST_ASSERT(inSuite, config_1.handle->ref == 2); - NL_TEST_ASSERT(inSuite, config_2.handle->ref == 2); // config_2.handle and config_1.handle->next + EXPECT_EQ(config_1.handle->ref, 2); + EXPECT_EQ(config_2.handle->ref, 2); // config_2.handle and config_1.handle->next PacketBuffer * const returned = PacketBuffer::FreeHead(std::move(config_1.handle).UnsafeRelease()); - NL_TEST_ASSERT(inSuite, handle_1->ref == 1); - NL_TEST_ASSERT(inSuite, config_2.handle->ref == 2); // config_2.handle and returned - NL_TEST_ASSERT(inSuite, returned == config_2.handle.Get()); + EXPECT_EQ(handle_1->ref, 1); + EXPECT_EQ(config_2.handle->ref, 2); // config_2.handle and returned + EXPECT_EQ(returned, config_2.handle.Get()); config_1.handle = nullptr; - NL_TEST_ASSERT(inSuite, config_2.handle->ref == 2); + EXPECT_EQ(config_2.handle->ref, 2); config_2.handle = nullptr; - NL_TEST_ASSERT(inSuite, returned->ref == 1); + EXPECT_EQ(returned->ref, 1); PacketBuffer::Free(returned); // Test PacketBufferHandle::FreeHead - test->PrepareTestBuffer(&config_1, kAllowHandleReuse); - test->PrepareTestBuffer(&config_2, kAllowHandleReuse); - NL_TEST_ASSERT(inSuite, config_1.handle->ref == 1); - NL_TEST_ASSERT(inSuite, config_2.handle->ref == 1); + PrepareTestBuffer(&config_1, kAllowHandleReuse); + PrepareTestBuffer(&config_2, kAllowHandleReuse); + EXPECT_EQ(config_1.handle->ref, 1); + EXPECT_EQ(config_2.handle->ref, 1); handle_1 = config_1.handle.Retain(); config_1.handle->AddToEnd(config_2.handle.Retain()); - NL_TEST_ASSERT(inSuite, config_1.handle->ref == 2); - NL_TEST_ASSERT(inSuite, config_2.handle->ref == 2); // config_2.handle and config_1.handle->next + EXPECT_EQ(config_1.handle->ref, 2); + EXPECT_EQ(config_2.handle->ref, 2); // config_2.handle and config_1.handle->next PacketBuffer * const buffer_1 = config_1.handle.Get(); config_1.handle.FreeHead(); - NL_TEST_ASSERT(inSuite, buffer_1->ref == 1); - NL_TEST_ASSERT(inSuite, config_1.handle == config_2.handle); - NL_TEST_ASSERT(inSuite, config_2.handle->ref == 2); // config_2.handle and config_1.handle + EXPECT_EQ(buffer_1->ref, 1); + EXPECT_EQ(config_1.handle, config_2.handle); + EXPECT_EQ(config_2.handle->ref, 2); // config_2.handle and config_1.handle config_1.handle = nullptr; config_2.handle = nullptr; @@ -1548,193 +1369,161 @@ void PacketBufferTest::CheckFreeHead(nlTestSuite * inSuite, void * inContext) } } -void PacketBufferTest::CheckHandleConstruct(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckHandleConstruct) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - PacketBufferHandle handle_1; - NL_TEST_ASSERT(inSuite, handle_1.IsNull()); + EXPECT_TRUE(handle_1.IsNull()); PacketBufferHandle handle_2(nullptr); - NL_TEST_ASSERT(inSuite, handle_2.IsNull()); + EXPECT_TRUE(handle_2.IsNull()); PacketBufferHandle handle_3(PacketBufferHandle::New(chip::System::PacketBuffer::kMaxSize)); - NL_TEST_ASSERT(inSuite, !handle_3.IsNull()); + ASSERT_FALSE(handle_3.IsNull()); // Private constructor. PacketBuffer * const buffer_3 = std::move(handle_3).UnsafeRelease(); PacketBufferHandle handle_4(buffer_3); - NL_TEST_ASSERT(inSuite, handle_4.Get() == buffer_3); + EXPECT_EQ(handle_4.Get(), buffer_3); } -void PacketBufferTest::CheckHandleMove(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckHandleMove) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config_1 : test->configurations) + for (auto & config_1 : configurations) { - for (auto & config_2 : test->configurations) + for (auto & config_2 : configurations) { if (&config_1 == &config_2) { continue; } - test->PrepareTestBuffer(&config_1, kRecordHandle); - test->PrepareTestBuffer(&config_2, kRecordHandle); + PrepareTestBuffer(&config_1, kRecordHandle); + PrepareTestBuffer(&config_2, kRecordHandle); const PacketBuffer * const buffer_1 = config_1.handle.Get(); const PacketBuffer * const buffer_2 = config_2.handle.Get(); - NL_TEST_ASSERT(inSuite, buffer_1 != buffer_2); - NL_TEST_ASSERT(inSuite, buffer_1->ref == 2); // test.handles and config_1.handle - NL_TEST_ASSERT(inSuite, buffer_2->ref == 2); // test.handles and config_2.handle + EXPECT_NE(buffer_1, buffer_2); + EXPECT_EQ(buffer_1->ref, 2); // test.handles and config_1.handle + EXPECT_EQ(buffer_2->ref, 2); // test.handles and config_2.handle config_1.handle = std::move(config_2.handle); - NL_TEST_ASSERT(inSuite, config_1.handle.Get() == buffer_2); - NL_TEST_ASSERT(inSuite, config_2.handle.Get() == nullptr); - NL_TEST_ASSERT(inSuite, buffer_1->ref == 1); // test.handles - NL_TEST_ASSERT(inSuite, buffer_2->ref == 2); // test.handles and config_1.handle + EXPECT_EQ(config_1.handle.Get(), buffer_2); + EXPECT_EQ(config_2.handle.Get(), nullptr); + EXPECT_EQ(buffer_1->ref, 1); // test.handles + EXPECT_EQ(buffer_2->ref, 2); // test.handles and config_1.handle config_1.handle = nullptr; } // Verify and release handles. - NL_TEST_ASSERT(inSuite, test->ResetHandles()); + EXPECT_TRUE(ResetHandles()); } } -void PacketBufferTest::CheckHandleRelease(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckHandleRelease) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config_1 : test->configurations) + for (auto & config_1 : configurations) { - test->PrepareTestBuffer(&config_1); + PrepareTestBuffer(&config_1); PacketBuffer * const buffer_1 = config_1.handle.Get(); PacketBuffer * const taken_1 = std::move(config_1.handle).UnsafeRelease(); - NL_TEST_ASSERT(inSuite, buffer_1 == taken_1); - NL_TEST_ASSERT(inSuite, config_1.handle.IsNull()); - NL_TEST_ASSERT(inSuite, buffer_1->ref == 1); + EXPECT_EQ(buffer_1, taken_1); + EXPECT_TRUE(config_1.handle.IsNull()); + EXPECT_EQ(buffer_1->ref, 1); PacketBuffer::Free(buffer_1); } } -void PacketBufferTest::CheckHandleFree(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckHandleFree) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config_1 : test->configurations) + for (auto & config_1 : configurations) { - test->PrepareTestBuffer(&config_1, kRecordHandle); + PrepareTestBuffer(&config_1, kRecordHandle); const PacketBuffer * const buffer_1 = config_1.handle.Get(); - NL_TEST_ASSERT(inSuite, buffer_1->ref == 2); // test.handles and config_1.handle + EXPECT_EQ(buffer_1->ref, 2); // test.handles and config_1.handle config_1.handle = nullptr; - NL_TEST_ASSERT(inSuite, config_1.handle.IsNull()); - NL_TEST_ASSERT(inSuite, config_1.handle.Get() == nullptr); - NL_TEST_ASSERT(inSuite, buffer_1->ref == 1); // test.handles only + EXPECT_TRUE(config_1.handle.IsNull()); + EXPECT_EQ(config_1.handle.Get(), nullptr); + EXPECT_EQ(buffer_1->ref, 1); // test.handles only } } -void PacketBufferTest::CheckHandleRetain(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckHandleRetain) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config_1 : test->configurations) + for (auto & config_1 : configurations) { - test->PrepareTestBuffer(&config_1, kRecordHandle); + PrepareTestBuffer(&config_1, kRecordHandle); - NL_TEST_ASSERT(inSuite, config_1.handle->ref == 2); // test.handles and config_1.handle + EXPECT_EQ(config_1.handle->ref, 2); // test.handles and config_1.handle PacketBufferHandle handle_1 = config_1.handle.Retain(); - NL_TEST_ASSERT(inSuite, config_1.handle == handle_1); - NL_TEST_ASSERT(inSuite, config_1.handle->ref == 3); // test.handles and config_1.handle and handle_1 + EXPECT_EQ(config_1.handle, handle_1); + EXPECT_EQ(config_1.handle->ref, 3); // test.handles and config_1.handle and handle_1 } } -void PacketBufferTest::CheckHandleAdopt(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckHandleAdopt) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config_1 : test->configurations) + for (auto & config_1 : configurations) { - test->PrepareTestBuffer(&config_1, kRecordHandle); + PrepareTestBuffer(&config_1, kRecordHandle); PacketBuffer * buffer_1 = std::move(config_1.handle).UnsafeRelease(); - NL_TEST_ASSERT(inSuite, config_1.handle.IsNull()); - NL_TEST_ASSERT(inSuite, buffer_1->ref == 2); // test.handles and buffer_1 + EXPECT_TRUE(config_1.handle.IsNull()); + EXPECT_EQ(buffer_1->ref, 2); // test.handles and buffer_1 config_1.handle = PacketBufferHandle::Adopt(buffer_1); - NL_TEST_ASSERT(inSuite, config_1.handle.Get() == buffer_1); - NL_TEST_ASSERT(inSuite, config_1.handle->ref == 2); // test.handles and config_1.handle + EXPECT_EQ(config_1.handle.Get(), buffer_1); + EXPECT_EQ(config_1.handle->ref, 2); // test.handles and config_1.handle config_1.handle = nullptr; - NL_TEST_ASSERT(inSuite, config_1.handle.IsNull()); - NL_TEST_ASSERT(inSuite, buffer_1->ref == 1); // test.handles only + EXPECT_TRUE(config_1.handle.IsNull()); + EXPECT_EQ(buffer_1->ref, 1); // test.handles only } } -void PacketBufferTest::CheckHandleHold(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckHandleHold) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config_1 : test->configurations) + for (auto & config_1 : configurations) { - test->PrepareTestBuffer(&config_1, kRecordHandle); + PrepareTestBuffer(&config_1, kRecordHandle); PacketBuffer * buffer_1 = std::move(config_1.handle).UnsafeRelease(); - NL_TEST_ASSERT(inSuite, config_1.handle.IsNull()); - NL_TEST_ASSERT(inSuite, buffer_1->ref == 2); // test.handles and buffer_1 + EXPECT_TRUE(config_1.handle.IsNull()); + EXPECT_EQ(buffer_1->ref, 2); // test.handles and buffer_1 config_1.handle = PacketBufferHandle::Hold(buffer_1); - NL_TEST_ASSERT(inSuite, config_1.handle.Get() == buffer_1); - NL_TEST_ASSERT(inSuite, config_1.handle->ref == 3); // test.handles and config_1.handle and buffer_1 + EXPECT_EQ(config_1.handle.Get(), buffer_1); + EXPECT_EQ(config_1.handle->ref, 3); // test.handles and config_1.handle and buffer_1 config_1.handle = nullptr; - NL_TEST_ASSERT(inSuite, config_1.handle.IsNull()); - NL_TEST_ASSERT(inSuite, buffer_1->ref == 2); // test.handles only and buffer_1 + EXPECT_TRUE(config_1.handle.IsNull()); + EXPECT_EQ(buffer_1->ref, 2); // test.handles only and buffer_1 PacketBuffer::Free(buffer_1); } } -void PacketBufferTest::CheckHandleAdvance(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckHandleAdvance) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - - for (auto & config_1 : test->configurations) + for (auto & config_1 : configurations) { - for (auto & config_2 : test->configurations) + for (auto & config_2 : configurations) { - for (auto & config_3 : test->configurations) + for (auto & config_3 : configurations) { if (&config_1 == &config_2 || &config_1 == &config_3 || &config_2 == &config_3) { continue; } - test->PrepareTestBuffer(&config_1); - test->PrepareTestBuffer(&config_2); - test->PrepareTestBuffer(&config_3); + PrepareTestBuffer(&config_1); + PrepareTestBuffer(&config_2); + PrepareTestBuffer(&config_3); PacketBufferHandle handle_1 = config_1.handle.Retain(); PacketBufferHandle handle_2 = config_2.handle.Retain(); @@ -1743,26 +1532,26 @@ void PacketBufferTest::CheckHandleAdvance(nlTestSuite * inSuite, void * inContex config_1.handle->AddToEnd(config_2.handle.Retain()); config_1.handle->AddToEnd(config_3.handle.Retain()); - NL_TEST_ASSERT(inSuite, config_1.handle->ChainedBuffer() == config_2.handle.Get()); - NL_TEST_ASSERT(inSuite, config_2.handle->ChainedBuffer() == config_3.handle.Get()); - NL_TEST_ASSERT(inSuite, config_3.handle->HasChainedBuffer() == false); - NL_TEST_ASSERT(inSuite, handle_1->ref == 2); // handle_1 and config_1.handle - NL_TEST_ASSERT(inSuite, handle_2->ref == 3); // handle_2 and config_2.handle and config_1.handle->next - NL_TEST_ASSERT(inSuite, handle_3->ref == 3); // handle_3 and config_3.handle and config_2.handle->next + EXPECT_EQ(config_1.handle->ChainedBuffer(), config_2.handle.Get()); + EXPECT_EQ(config_2.handle->ChainedBuffer(), config_3.handle.Get()); + EXPECT_FALSE(config_3.handle->HasChainedBuffer()); + EXPECT_EQ(handle_1->ref, 2); // handle_1 and config_1.handle + EXPECT_EQ(handle_2->ref, 3); // handle_2 and config_2.handle and config_1.handle->next + EXPECT_EQ(handle_3->ref, 3); // handle_3 and config_3.handle and config_2.handle->next config_1.handle.Advance(); - NL_TEST_ASSERT(inSuite, config_1.handle == handle_2); - NL_TEST_ASSERT(inSuite, handle_1->ref == 1); // handle_1 only - NL_TEST_ASSERT(inSuite, handle_2->ref == 4); // handle_2, config_[12].handle, handle_1->next - NL_TEST_ASSERT(inSuite, handle_3->ref == 3); // handle_3, config_3.handle, config_2.handle->next + EXPECT_EQ(config_1.handle, handle_2); + EXPECT_EQ(handle_1->ref, 1); // handle_1 only + EXPECT_EQ(handle_2->ref, 4); // handle_2, config_[12].handle, handle_1->next + EXPECT_EQ(handle_3->ref, 3); // handle_3, config_3.handle, config_2.handle->next config_1.handle.Advance(); - NL_TEST_ASSERT(inSuite, config_1.handle == handle_3); - NL_TEST_ASSERT(inSuite, handle_1->ref == 1); // handle_1 only - NL_TEST_ASSERT(inSuite, handle_2->ref == 3); // handle_2, config_2.handle, handle_1->next - NL_TEST_ASSERT(inSuite, handle_3->ref == 4); // handle_3, config_[13].handle, handle_2->next + EXPECT_EQ(config_1.handle, handle_3); + EXPECT_EQ(handle_1->ref, 1); // handle_1 only + EXPECT_EQ(handle_2->ref, 3); // handle_2, config_2.handle, handle_1->next + EXPECT_EQ(handle_3->ref, 4); // handle_3, config_[13].handle, handle_2->next config_1.handle = nullptr; config_2.handle = nullptr; @@ -1772,66 +1561,58 @@ void PacketBufferTest::CheckHandleAdvance(nlTestSuite * inSuite, void * inContex } } -void PacketBufferTest::CheckHandleRightSize(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckHandleRightSize) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - static const char kPayload[] = "Joy!"; PacketBufferHandle handle = PacketBufferHandle::New(chip::System::PacketBuffer::kMaxSizeWithoutReserve, 0); PacketBuffer * buffer = handle.mBuffer; memcpy(handle->Start(), kPayload, sizeof kPayload); buffer->SetDataLength(sizeof kPayload); - NL_TEST_ASSERT(inSuite, handle->ref == 1); + EXPECT_EQ(handle->ref, 1); // RightSize should do nothing if there is another reference to the buffer. { PacketBufferHandle anotherHandle = handle.Retain(); handle.RightSize(); - NL_TEST_ASSERT(inSuite, handle.mBuffer == buffer); + EXPECT_EQ(handle.mBuffer, buffer); } #if CHIP_SYSTEM_PACKETBUFFER_HAS_RIGHTSIZE handle.RightSize(); - NL_TEST_ASSERT(inSuite, handle.mBuffer != buffer); - NL_TEST_ASSERT(inSuite, handle->DataLength() == sizeof kPayload); - NL_TEST_ASSERT(inSuite, memcmp(handle->Start(), kPayload, sizeof kPayload) == 0); + EXPECT_NE(handle.mBuffer, buffer); + EXPECT_EQ(handle->DataLength(), sizeof kPayload); + EXPECT_EQ(memcmp(handle->Start(), kPayload, sizeof kPayload), 0); #else // CHIP_SYSTEM_PACKETBUFFER_HAS_RIGHTSIZE // For this configuration, RightSize() does nothing. handle.RightSize(); - NL_TEST_ASSERT(inSuite, handle.mBuffer == buffer); + EXPECT_EQ(handle.mBuffer, buffer); #endif // CHIP_SYSTEM_PACKETBUFFER_HAS_RIGHTSIZE } -void PacketBufferTest::CheckHandleCloneData(nlTestSuite * inSuite, void * inContext) +TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckHandleCloneData) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - uint8_t lPayload[2 * PacketBuffer::kMaxSizeWithoutReserve]; for (uint8_t & payload : lPayload) { payload = static_cast(random()); } - for (auto & config_1 : test->configurations) + for (auto & config_1 : configurations) { - for (auto & config_2 : test->configurations) + for (auto & config_2 : configurations) { if (&config_1 == &config_2) { continue; } - test->PrepareTestBuffer(&config_1); - test->PrepareTestBuffer(&config_2); + PrepareTestBuffer(&config_1); + PrepareTestBuffer(&config_2); const uint8_t * payload_1 = lPayload; memcpy(config_1.handle->Start(), payload_1, config_1.handle->MaxDataLength()); @@ -1843,40 +1624,40 @@ void PacketBufferTest::CheckHandleCloneData(nlTestSuite * inSuite, void * inCont // Clone single buffer. PacketBufferHandle clone_1 = config_1.handle.CloneData(); - NL_TEST_ASSERT(inSuite, !clone_1.IsNull()); - NL_TEST_ASSERT(inSuite, clone_1->DataLength() == config_1.handle->DataLength()); - NL_TEST_ASSERT(inSuite, memcmp(clone_1->Start(), payload_1, clone_1->DataLength()) == 0); + ASSERT_FALSE(clone_1.IsNull()); + EXPECT_EQ(clone_1->DataLength(), config_1.handle->DataLength()); + EXPECT_EQ(memcmp(clone_1->Start(), payload_1, clone_1->DataLength()), 0); if (clone_1->DataLength()) { // Verify that modifying the clone does not affect the original. ScrambleData(clone_1->Start(), clone_1->DataLength()); - NL_TEST_ASSERT(inSuite, memcmp(clone_1->Start(), payload_1, clone_1->DataLength()) != 0); - NL_TEST_ASSERT(inSuite, memcmp(config_1.handle->Start(), payload_1, config_1.handle->DataLength()) == 0); + EXPECT_NE(memcmp(clone_1->Start(), payload_1, clone_1->DataLength()), 0); + EXPECT_EQ(memcmp(config_1.handle->Start(), payload_1, config_1.handle->DataLength()), 0); } // Clone buffer chain. config_1.handle->AddToEnd(config_2.handle.Retain()); - NL_TEST_ASSERT(inSuite, config_1.handle->HasChainedBuffer()); + EXPECT_TRUE(config_1.handle->HasChainedBuffer()); clone_1 = config_1.handle.CloneData(); PacketBufferHandle clone_1_next = clone_1->Next(); - NL_TEST_ASSERT(inSuite, !clone_1.IsNull()); - NL_TEST_ASSERT(inSuite, clone_1->HasChainedBuffer()); - NL_TEST_ASSERT(inSuite, clone_1->DataLength() == config_1.handle->DataLength()); - NL_TEST_ASSERT(inSuite, clone_1->TotalLength() == config_1.handle->TotalLength()); - NL_TEST_ASSERT(inSuite, clone_1_next->DataLength() == config_2.handle->DataLength()); - NL_TEST_ASSERT(inSuite, memcmp(clone_1->Start(), payload_1, clone_1->DataLength()) == 0); - NL_TEST_ASSERT(inSuite, memcmp(clone_1_next->Start(), payload_2, clone_1_next->DataLength()) == 0); + ASSERT_FALSE(clone_1.IsNull()); + EXPECT_TRUE(clone_1->HasChainedBuffer()); + EXPECT_EQ(clone_1->DataLength(), config_1.handle->DataLength()); + EXPECT_EQ(clone_1->TotalLength(), config_1.handle->TotalLength()); + EXPECT_EQ(clone_1_next->DataLength(), config_2.handle->DataLength()); + EXPECT_EQ(memcmp(clone_1->Start(), payload_1, clone_1->DataLength()), 0); + EXPECT_EQ(memcmp(clone_1_next->Start(), payload_2, clone_1_next->DataLength()), 0); if (clone_1->DataLength()) { ScrambleData(clone_1->Start(), clone_1->DataLength()); - NL_TEST_ASSERT(inSuite, memcmp(clone_1->Start(), payload_1, clone_1->DataLength()) != 0); - NL_TEST_ASSERT(inSuite, memcmp(config_1.handle->Start(), payload_1, config_1.handle->DataLength()) == 0); + EXPECT_NE(memcmp(clone_1->Start(), payload_1, clone_1->DataLength()), 0); + EXPECT_EQ(memcmp(config_1.handle->Start(), payload_1, config_1.handle->DataLength()), 0); } if (clone_1_next->DataLength()) { ScrambleData(clone_1_next->Start(), clone_1_next->DataLength()); - NL_TEST_ASSERT(inSuite, memcmp(clone_1_next->Start(), payload_2, clone_1_next->DataLength()) != 0); - NL_TEST_ASSERT(inSuite, memcmp(config_2.handle->Start(), payload_2, config_2.handle->DataLength()) == 0); + EXPECT_NE(memcmp(clone_1_next->Start(), payload_2, clone_1_next->DataLength()), 0); + EXPECT_EQ(memcmp(config_2.handle->Start(), payload_2, config_2.handle->DataLength()), 0); } config_1.handle = nullptr; @@ -1895,12 +1676,11 @@ void PacketBufferTest::CheckHandleCloneData(nlTestSuite * inSuite, void * inCont // construct an oversize buffer. constexpr uint16_t kOversizeDataSize = PacketBuffer::kMaxSizeWithoutReserve + 99; - PacketBuffer * p = - reinterpret_cast(chip::Platform::MemoryAlloc(PacketBuffer::kStructureSize + kOversizeDataSize)); - NL_TEST_ASSERT(inSuite, p != nullptr); + PacketBuffer * p = reinterpret_cast(chip::Platform::MemoryAlloc(kStructureSize + kOversizeDataSize)); + ASSERT_NE(p, nullptr); p->next = nullptr; - p->payload = reinterpret_cast(p) + PacketBuffer::kStructureSize; + p->payload = reinterpret_cast(p) + kStructureSize; p->tot_len = 0; p->len = 0; p->ref = 1; @@ -1912,20 +1692,20 @@ void PacketBufferTest::CheckHandleCloneData(nlTestSuite * inSuite, void * inCont memset(handle->Start(), 1, PacketBuffer::kMaxSizeWithoutReserve); handle->SetDataLength(PacketBuffer::kMaxSizeWithoutReserve); - NL_TEST_ASSERT(inSuite, handle->DataLength() == PacketBuffer::kMaxSizeWithoutReserve); + EXPECT_EQ(handle->DataLength(), PacketBuffer::kMaxSizeWithoutReserve); PacketBufferHandle clone = handle.CloneData(); - NL_TEST_ASSERT(inSuite, !clone.IsNull()); - NL_TEST_ASSERT(inSuite, clone->DataLength() == PacketBuffer::kMaxSizeWithoutReserve); - NL_TEST_ASSERT(inSuite, memcmp(handle->Start(), clone->Start(), PacketBuffer::kMaxSizeWithoutReserve) == 0); + ASSERT_FALSE(clone.IsNull()); + EXPECT_EQ(clone->DataLength(), PacketBuffer::kMaxSizeWithoutReserve); + EXPECT_EQ(memcmp(handle->Start(), clone->Start(), PacketBuffer::kMaxSizeWithoutReserve), 0); // Overfill the buffer and verify that it can not be cloned. memset(handle->Start(), 2, kOversizeDataSize); handle->SetDataLength(kOversizeDataSize); - NL_TEST_ASSERT(inSuite, handle->DataLength() == kOversizeDataSize); + EXPECT_EQ(handle->DataLength(), kOversizeDataSize); clone = handle.CloneData(); - NL_TEST_ASSERT(inSuite, clone.IsNull()); + EXPECT_TRUE(clone.IsNull()); // Free the packet buffer memory ourselves, since we allocated it ourselves. chip::Platform::MemoryFree(std::move(handle).UnsafeRelease()); @@ -1933,97 +1713,30 @@ void PacketBufferTest::CheckHandleCloneData(nlTestSuite * inSuite, void * inCont #endif // CHIP_SYSTEM_PACKETBUFFER_FROM_CHIP_HEAP } -void PacketBufferTest::CheckPacketBufferWriter(nlTestSuite * inSuite, void * inContext) +TEST_F(TestSystemPacketBuffer, CheckPacketBufferWriter) { - struct TestContext * const theContext = static_cast(inContext); - PacketBufferTest * const test = theContext->test; - NL_TEST_ASSERT(inSuite, test->mContext == theContext); - static const char kPayload[] = "Hello, world!"; PacketBufferWriter yay(PacketBufferHandle::New(sizeof(kPayload))); PacketBufferWriter nay(PacketBufferHandle::New(sizeof(kPayload)), sizeof(kPayload) - 2); - NL_TEST_ASSERT(inSuite, !yay.IsNull()); - NL_TEST_ASSERT(inSuite, !nay.IsNull()); + ASSERT_FALSE(yay.IsNull()); + ASSERT_FALSE(nay.IsNull()); yay.Put(kPayload); yay.Put('\0'); nay.Put(kPayload); nay.Put('\0'); - NL_TEST_ASSERT(inSuite, yay.Fit()); - NL_TEST_ASSERT(inSuite, !nay.Fit()); + EXPECT_TRUE(yay.Fit()); + EXPECT_FALSE(nay.Fit()); PacketBufferHandle yayBuffer = yay.Finalize(); PacketBufferHandle nayBuffer = nay.Finalize(); - NL_TEST_ASSERT(inSuite, yay.IsNull()); - NL_TEST_ASSERT(inSuite, nay.IsNull()); - NL_TEST_ASSERT(inSuite, !yayBuffer.IsNull()); - NL_TEST_ASSERT(inSuite, nayBuffer.IsNull()); - NL_TEST_ASSERT(inSuite, memcmp(yayBuffer->Start(), kPayload, sizeof kPayload) == 0); -} - -/** - * Test Suite. It lists all the test functions. - */ -// clang-format off -const nlTest sTests[] = -{ - NL_TEST_DEF("PacketBuffer::New", PacketBufferTest::CheckNew), - NL_TEST_DEF("PacketBuffer::Start", PacketBufferTest::CheckStart), - NL_TEST_DEF("PacketBuffer::SetStart", PacketBufferTest::CheckSetStart), - NL_TEST_DEF("PacketBuffer::DataLength", PacketBufferTest::CheckDataLength), - NL_TEST_DEF("PacketBuffer::SetDataLength", PacketBufferTest::CheckSetDataLength), - NL_TEST_DEF("PacketBuffer::TotalLength", PacketBufferTest::CheckTotalLength), - NL_TEST_DEF("PacketBuffer::MaxDataLength", PacketBufferTest::CheckMaxDataLength), - NL_TEST_DEF("PacketBuffer::AvailableDataLength", PacketBufferTest::CheckAvailableDataLength), - NL_TEST_DEF("PacketBuffer::HasChainedBuffer", PacketBufferTest::CheckHasChainedBuffer), - NL_TEST_DEF("PacketBuffer::ReservedSize", PacketBufferTest::CheckReservedSize), - NL_TEST_DEF("PacketBuffer::AddToEnd", PacketBufferTest::CheckAddToEnd), - NL_TEST_DEF("PacketBuffer::PopHead", PacketBufferTest::CheckPopHead), - NL_TEST_DEF("PacketBuffer::CompactHead", PacketBufferTest::CheckCompactHead), - NL_TEST_DEF("PacketBuffer::ConsumeHead", PacketBufferTest::CheckConsumeHead), - NL_TEST_DEF("PacketBuffer::Consume", PacketBufferTest::CheckConsume), - NL_TEST_DEF("PacketBuffer::EnsureReservedSize", PacketBufferTest::CheckEnsureReservedSize), - NL_TEST_DEF("PacketBuffer::AlignPayload", PacketBufferTest::CheckAlignPayload), - NL_TEST_DEF("PacketBuffer::Next", PacketBufferTest::CheckNext), - NL_TEST_DEF("PacketBuffer::Last", PacketBufferTest::CheckLast), - NL_TEST_DEF("PacketBuffer::Read", PacketBufferTest::CheckRead), - NL_TEST_DEF("PacketBuffer::AddRef", PacketBufferTest::CheckAddRef), - NL_TEST_DEF("PacketBuffer::Free", PacketBufferTest::CheckFree), - NL_TEST_DEF("PacketBuffer::FreeHead", PacketBufferTest::CheckFreeHead), - NL_TEST_DEF("PacketBuffer::HandleConstruct", PacketBufferTest::CheckHandleConstruct), - NL_TEST_DEF("PacketBuffer::HandleMove", PacketBufferTest::CheckHandleMove), - NL_TEST_DEF("PacketBuffer::HandleRelease", PacketBufferTest::CheckHandleRelease), - NL_TEST_DEF("PacketBuffer::HandleFree", PacketBufferTest::CheckHandleFree), - NL_TEST_DEF("PacketBuffer::HandleRetain", PacketBufferTest::CheckHandleRetain), - NL_TEST_DEF("PacketBuffer::HandleAdopt", PacketBufferTest::CheckHandleAdopt), - NL_TEST_DEF("PacketBuffer::HandleHold", PacketBufferTest::CheckHandleHold), - NL_TEST_DEF("PacketBuffer::HandleAdvance", PacketBufferTest::CheckHandleAdvance), - NL_TEST_DEF("PacketBuffer::HandleRightSize", PacketBufferTest::CheckHandleRightSize), - NL_TEST_DEF("PacketBuffer::HandleCloneData", PacketBufferTest::CheckHandleCloneData), - NL_TEST_DEF("PacketBuffer::PacketBufferWriter", PacketBufferTest::CheckPacketBufferWriter), - - NL_TEST_SENTINEL() -}; -// clang-format on - -int TestSystemPacketBuffer() -{ - // clang-format off - nlTestSuite theSuite = { - .name ="chip-system-packetbuffer", - .tests = &sTests[0], - .setup = PacketBufferTest::TestSetup, - .tear_down = PacketBufferTest::TestTeardown, - // .initialize = PacketBufferTest::TestInitialize, - .terminate = PacketBufferTest::TestTerminate - }; - // clang-format on - - // Run test suite against one context. - nlTestRunner(&theSuite, &sContext); - - return (nlTestRunnerStats(&theSuite)); + EXPECT_TRUE(yay.IsNull()); + EXPECT_TRUE(nay.IsNull()); + ASSERT_FALSE(yayBuffer.IsNull()); + EXPECT_TRUE(nayBuffer.IsNull()); + EXPECT_EQ(memcmp(yayBuffer->Start(), kPayload, sizeof kPayload), 0); } -CHIP_REGISTER_TEST_SUITE(TestSystemPacketBuffer) +} // namespace System +} // namespace chip diff --git a/src/system/tests/TestSystemScheduleLambda.cpp b/src/system/tests/TestSystemScheduleLambda.cpp index f43fadd94ab2eb..66919780b5d7fe 100644 --- a/src/system/tests/TestSystemScheduleLambda.cpp +++ b/src/system/tests/TestSystemScheduleLambda.cpp @@ -14,83 +14,36 @@ * limitations under the License. */ -#include +#include #include #include -#include -#include #include +#include -// Test input data. +class TestSystemScheduleLambda : public ::testing::Test +{ +public: + static void SetUpTestSuite() + { + ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); + ASSERT_EQ(chip::DeviceLayer::PlatformMgr().InitChipStack(), CHIP_NO_ERROR); + } + + static void TearDownTestSuite() + { + chip::DeviceLayer::PlatformMgr().Shutdown(); + chip::Platform::MemoryShutdown(); + } +}; -static void CheckScheduleLambda(nlTestSuite * inSuite, void * aContext) +TEST_F(TestSystemScheduleLambda, CheckScheduleLambda) { - bool * called = new bool(false); - chip::DeviceLayer::SystemLayer().ScheduleLambda([called] { - *called = true; + bool called = false; + chip::DeviceLayer::SystemLayer().ScheduleLambda([&called] { + called = true; chip::DeviceLayer::PlatformMgr().StopEventLoopTask(); }); chip::DeviceLayer::PlatformMgr().RunEventLoop(); - NL_TEST_ASSERT(inSuite, *called); - delete called; -} - -// Test Suite - -/** - * Test Suite. It lists all the test functions. - */ -// clang-format off -static const nlTest sTests[] = -{ - NL_TEST_DEF("System::TestScheduleLambda", CheckScheduleLambda), - NL_TEST_SENTINEL() -}; -// clang-format on - -static int TestSetup(void * aContext); -static int TestTeardown(void * aContext); - -// clang-format off -static nlTestSuite kTheSuite = -{ - "chip-system-schedule-lambda", - &sTests[0], - TestSetup, - TestTeardown -}; -// clang-format on - -/** - * Set up the test suite. - */ -static int TestSetup(void * aContext) -{ - if (chip::Platform::MemoryInit() != CHIP_NO_ERROR) - return FAILURE; - if (chip::DeviceLayer::PlatformMgr().InitChipStack() != CHIP_NO_ERROR) - return FAILURE; - return (SUCCESS); + EXPECT_TRUE(called); } - -/** - * Tear down the test suite. - * Free memory reserved at TestSetup. - */ -static int TestTeardown(void * aContext) -{ - chip::DeviceLayer::PlatformMgr().Shutdown(); - chip::Platform::MemoryShutdown(); - return (SUCCESS); -} - -int TestSystemScheduleLambda() -{ - // Run test suit againt one lContext. - nlTestRunner(&kTheSuite, nullptr); - - return nlTestRunnerStats(&kTheSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestSystemScheduleLambda) diff --git a/src/system/tests/TestSystemScheduleWork.cpp b/src/system/tests/TestSystemScheduleWork.cpp index 29c4a6eda75efd..793e267be929e0 100644 --- a/src/system/tests/TestSystemScheduleWork.cpp +++ b/src/system/tests/TestSystemScheduleWork.cpp @@ -14,13 +14,28 @@ * limitations under the License. */ -#include +#include #include #include -#include -#include #include +#include + +class TestSystemScheduleWork : public ::testing::Test +{ +public: + static void SetUpTestSuite() + { + ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); + ASSERT_EQ(chip::DeviceLayer::PlatformMgr().InitChipStack(), CHIP_NO_ERROR); + } + + static void TearDownTestSuite() + { + chip::DeviceLayer::PlatformMgr().Shutdown(); + chip::Platform::MemoryShutdown(); + } +}; static void IncrementIntCounter(chip::System::Layer *, void * state) { @@ -32,76 +47,12 @@ static void StopEventLoop(chip::System::Layer *, void *) chip::DeviceLayer::PlatformMgr().StopEventLoopTask(); } -static void CheckScheduleWorkTwice(nlTestSuite * inSuite, void * aContext) +TEST_F(TestSystemScheduleWork, CheckScheduleWork) { - int * callCount = new int(0); - NL_TEST_ASSERT(inSuite, chip::DeviceLayer::SystemLayer().ScheduleWork(IncrementIntCounter, callCount) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, chip::DeviceLayer::SystemLayer().ScheduleWork(IncrementIntCounter, callCount) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, chip::DeviceLayer::SystemLayer().ScheduleWork(StopEventLoop, nullptr) == CHIP_NO_ERROR); + int callCount = 0; + EXPECT_EQ(chip::DeviceLayer::SystemLayer().ScheduleWork(IncrementIntCounter, &callCount), CHIP_NO_ERROR); + EXPECT_EQ(chip::DeviceLayer::SystemLayer().ScheduleWork(IncrementIntCounter, &callCount), CHIP_NO_ERROR); + EXPECT_EQ(chip::DeviceLayer::SystemLayer().ScheduleWork(StopEventLoop, nullptr), CHIP_NO_ERROR); chip::DeviceLayer::PlatformMgr().RunEventLoop(); - NL_TEST_ASSERT(inSuite, *callCount == 2); - delete callCount; -} - -// Test Suite - -/** - * Test Suite. It lists all the test functions. - */ -// clang-format off -static const nlTest sTests[] = -{ - NL_TEST_DEF("System::TestScheduleWorkTwice", CheckScheduleWorkTwice), - NL_TEST_SENTINEL() -}; -// clang-format on - -static int TestSetup(void * aContext); -static int TestTeardown(void * aContext); - -// clang-format off -static nlTestSuite kTheSuite = -{ - "chip-system-schedule-work", - &sTests[0], - TestSetup, - TestTeardown -}; -// clang-format on - -/** - * Set up the test suite. - */ -static int TestSetup(void * aContext) -{ - if (chip::Platform::MemoryInit() != CHIP_NO_ERROR) - { - return FAILURE; - } - - if (chip::DeviceLayer::PlatformMgr().InitChipStack() != CHIP_NO_ERROR) - return FAILURE; - - return (SUCCESS); + EXPECT_EQ(callCount, 2); } - -/** - * Tear down the test suite. - * Free memory reserved at TestSetup. - */ -static int TestTeardown(void * aContext) -{ - chip::DeviceLayer::PlatformMgr().Shutdown(); - chip::Platform::MemoryShutdown(); - return (SUCCESS); -} - -int TestSystemScheduleWork() -{ - // Run test suit againt one lContext. - nlTestRunner(&kTheSuite, nullptr); - - return nlTestRunnerStats(&kTheSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestSystemScheduleWork) diff --git a/src/system/tests/TestSystemTimer.cpp b/src/system/tests/TestSystemTimer.cpp index 5cd499fdcb816f..6c065b96a01f9a 100644 --- a/src/system/tests/TestSystemTimer.cpp +++ b/src/system/tests/TestSystemTimer.cpp @@ -23,13 +23,15 @@ * */ -#include +#include +#include +#include + +#include #include #include -#include -#include -#include +#include #include #include @@ -39,10 +41,6 @@ #include #endif // CHIP_SYSTEM_CONFIG_USE_LWIP -#include -#include -#include - using chip::ErrorStr; using namespace chip::System; @@ -94,52 +92,64 @@ class LayerEvents mGreedyTimer; // for greedy timer - uint32_t mNumTimersHandled; + static void SetUpTestSuite() + { + ASSERT_EQ(::chip::Platform::MemoryInit(), CHIP_NO_ERROR); + +#if CHIP_SYSTEM_CONFIG_USE_LWIP && (LWIP_VERSION_MAJOR == 2) && (LWIP_VERSION_MINOR == 0) && !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) + static sys_mbox_t * sLwIPEventQueue = NULL; + sys_mbox_new(sLwIPEventQueue, 100); + tcpip_init(NULL, NULL); +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP && (LWIP_VERSION_MAJOR == 2) && (LWIP_VERSION_MINOR == 0) && + // !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) + + mLayer.Init(); + } - void GreedyTimer() + static void TearDownTestSuite() { - NL_TEST_ASSERT(mTestSuite, mNumTimersHandled < MAX_NUM_TIMERS); + mLayer.Shutdown(); - if (mNumTimersHandled >= MAX_NUM_TIMERS) - { - return; - } +#if CHIP_SYSTEM_CONFIG_USE_LWIP && (LWIP_VERSION_MAJOR == 2) && (LWIP_VERSION_MINOR == 0) && !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) + tcpip_finish(NULL, NULL); +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP && (LWIP_VERSION_MAJOR == 2) && (LWIP_VERSION_MINOR == 0) && + // !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) - mNumTimersHandled++; + ::chip::Platform::MemoryShutdown(); } - static void GreedyTimer(void * p) + + template + static auto GetTimerPoolObject(T && pool) { - TestContext * lContext = static_cast(p); - lContext->GreedyTimer(); + return pool.mTimerPool; } - TestContext() : mGreedyTimer(GreedyTimer, this), mNumTimersHandled(0) {} + static LayerImpl mLayer; }; -static TestContext * gCurrentTestContext = nullptr; +LayerImpl TestSystemTimer::mLayer; + +static TestSystemTimer * gCurrentTestContext = nullptr; class ScopedGlobalTestContext { public: - ScopedGlobalTestContext(TestContext * ctx) { gCurrentTestContext = ctx; } + ScopedGlobalTestContext(TestSystemTimer * ctx) { gCurrentTestContext = ctx; } ~ScopedGlobalTestContext() { gCurrentTestContext = nullptr; } }; -// Test input data. - static volatile bool sOverflowTestDone; void TimerFailed(void * aState) { - TestContext * lContext = static_cast(aState); - NL_TEST_ASSERT(lContext->mTestSuite, false); sOverflowTestDone = true; + FAIL() << "Timer failed"; } void HandleTimerFailed(Layer * systemLayer, void * aState) @@ -150,12 +160,11 @@ void HandleTimerFailed(Layer * systemLayer, void * aState) void HandleTimer10Success(Layer * systemLayer, void * aState) { - TestContext & lContext = *static_cast(aState); - NL_TEST_ASSERT(lContext.mTestSuite, true); + EXPECT_TRUE(true); sOverflowTestDone = true; } -static void CheckOverflow(nlTestSuite * inSuite, void * aContext) +TEST_F(TestSystemTimer, CheckOverflow) { if (!LayerEvents::HasServiceEvents()) return; @@ -163,29 +172,27 @@ static void CheckOverflow(nlTestSuite * inSuite, void * aContext) chip::System::Clock::Milliseconds32 timeout_overflow_0ms = chip::System::Clock::Milliseconds32(652835029); chip::System::Clock::Milliseconds32 timeout_10ms = chip::System::Clock::Milliseconds32(10); - TestContext & lContext = *static_cast(aContext); - Layer & lSys = *lContext.mLayer; + Layer & lSys = mLayer; sOverflowTestDone = false; - lSys.StartTimer(timeout_overflow_0ms, HandleTimerFailed, aContext); - lSys.StartTimer(timeout_10ms, HandleTimer10Success, aContext); + lSys.StartTimer(timeout_overflow_0ms, HandleTimerFailed, this); + lSys.StartTimer(timeout_10ms, HandleTimer10Success, this); while (!sOverflowTestDone) { LayerEvents::ServiceEvents(lSys); } - lSys.CancelTimer(HandleTimerFailed, aContext); + lSys.CancelTimer(HandleTimerFailed, this); // cb timer is cancelled by destructor - lSys.CancelTimer(HandleTimer10Success, aContext); + lSys.CancelTimer(HandleTimer10Success, this); } void HandleGreedyTimer(Layer * aLayer, void * aState) { static uint32_t sNumTimersHandled = 0; - TestContext & lContext = *static_cast(aState); - NL_TEST_ASSERT(lContext.mTestSuite, sNumTimersHandled < MAX_NUM_TIMERS); + EXPECT_LT(sNumTimersHandled, MAX_NUM_TIMERS); if (sNumTimersHandled >= MAX_NUM_TIMERS) { @@ -196,27 +203,24 @@ void HandleGreedyTimer(Layer * aLayer, void * aState) sNumTimersHandled++; } -static void CheckStarvation(nlTestSuite * inSuite, void * aContext) +TEST_F(TestSystemTimer, CheckStarvation) { if (!LayerEvents::HasServiceEvents()) return; - TestContext & lContext = *static_cast(aContext); - Layer & lSys = *lContext.mLayer; + Layer & lSys = mLayer; - lSys.StartTimer(chip::System::Clock::kZero, HandleGreedyTimer, aContext); + lSys.StartTimer(chip::System::Clock::kZero, HandleGreedyTimer, this); LayerEvents::ServiceEvents(lSys); } -static void CheckOrder(nlTestSuite * inSuite, void * aContext) +TEST_F(TestSystemTimer, CheckOrder) { if (!LayerEvents::HasServiceEvents()) return; - TestContext & testContext = *static_cast(aContext); - Layer & systemLayer = *testContext.mLayer; - nlTestSuite * const suite = testContext.mTestSuite; + Layer & systemLayer = mLayer; struct TestState { @@ -236,7 +240,7 @@ static void CheckOrder(nlTestSuite * inSuite, void * aContext) char record[5] = { 0 }; }; TestState testState; - NL_TEST_ASSERT(suite, testState.record[0] == 0); + EXPECT_EQ(testState.record[0], 0); Clock::ClockBase * const savedClock = &SystemClock(); Clock::Internal::MockClock mockClock; @@ -249,27 +253,25 @@ static void CheckOrder(nlTestSuite * inSuite, void * aContext) systemLayer.StartTimer(0_ms, TestState::A, &testState); LayerEvents::ServiceEvents(systemLayer); - NL_TEST_ASSERT(suite, strcmp(testState.record, "A") == 0); + EXPECT_EQ(strcmp(testState.record, "A"), 0); mockClock.AdvanceMonotonic(100_ms); LayerEvents::ServiceEvents(systemLayer); - NL_TEST_ASSERT(suite, strcmp(testState.record, "AB") == 0); + EXPECT_EQ(strcmp(testState.record, "AB"), 0); mockClock.AdvanceMonotonic(200_ms); LayerEvents::ServiceEvents(systemLayer); - NL_TEST_ASSERT(suite, strcmp(testState.record, "ABCD") == 0); + EXPECT_EQ(strcmp(testState.record, "ABCD"), 0); Clock::Internal::SetSystemClockForTesting(savedClock); } -static void CheckCancellation(nlTestSuite * inSuite, void * aContext) +TEST_F(TestSystemTimer, CheckCancellation) { if (!LayerEvents::HasServiceEvents()) return; - TestContext & testContext = *static_cast(aContext); - Layer & systemLayer = *testContext.mLayer; - nlTestSuite * const suite = testContext.mTestSuite; + Layer & systemLayer = mLayer; struct TestState { @@ -305,7 +307,7 @@ static void CheckCancellation(nlTestSuite * inSuite, void * aContext) Layer & mSystemLayer; }; TestState testState(systemLayer); - NL_TEST_ASSERT(suite, testState.record[0] == 0); + EXPECT_EQ(testState.record[0], 0); Clock::ClockBase * const savedClock = &SystemClock(); Clock::Internal::MockClock mockClock; @@ -320,7 +322,7 @@ static void CheckCancellation(nlTestSuite * inSuite, void * aContext) mockClock.AdvanceMonotonic(100_ms); LayerEvents::ServiceEvents(systemLayer); - NL_TEST_ASSERT(suite, strcmp(testState.record, "AC") == 0); + EXPECT_EQ(strcmp(testState.record, "AC"), 0); Clock::Internal::SetSystemClockForTesting(savedClock); } @@ -335,11 +337,11 @@ constexpr unsigned kCancelTimerCount = CHIP_SYSTEM_CONFIG_NUM_TIMERS - 4; int gCallbackProcessed[kCancelTimerCount]; /// Validates that gCallbackProcessed has valid values (0 or 1) -void ValidateExecutedTimerCounts(nlTestSuite * suite) +void ValidateExecutedTimerCounts() { for (int processed : gCallbackProcessed) { - NL_TEST_ASSERT(suite, (processed == 0) || (processed == 1)); + EXPECT_TRUE((processed == 0) || (processed == 1)); } } @@ -376,13 +378,13 @@ void Callback(Layer * layer, void * state) continue; } ChipLogProgress(Test, "Timer %u is being cancelled", i); - gCurrentTestContext->mLayer->CancelTimer(Callback, reinterpret_cast(static_cast(i))); + gCurrentTestContext->mLayer.CancelTimer(Callback, reinterpret_cast(static_cast(i))); gCallbackProcessed[i]++; // pretend executed. } } } -void Test(nlTestSuite * inSuite, void * aContext) +TEST_F(TestSystemTimer, CancelTimerTest) { // Validates that timers can cancel other timers. Generally the test will // do the following: @@ -395,11 +397,10 @@ void Test(nlTestSuite * inSuite, void * aContext) // other timers, even if they are expiring at the same time) memset(gCallbackProcessed, 0, sizeof(gCallbackProcessed)); - TestContext & testContext = *static_cast(aContext); - ScopedGlobalTestContext testScope(&testContext); + // TestContext & testContext = *static_cast(aContext); + // ScopedGlobalTestContext testScope(&testContext); - Layer & systemLayer = *testContext.mLayer; - nlTestSuite * const suite = testContext.mTestSuite; + Layer & systemLayer = mLayer; Clock::ClockBase * const savedClock = &SystemClock(); Clock::Internal::MockClock mockClock; @@ -408,19 +409,18 @@ void Test(nlTestSuite * inSuite, void * aContext) for (unsigned i = 0; i < kCancelTimerCount; i++) { - NL_TEST_ASSERT( - suite, systemLayer.StartTimer(10_ms, Callback, reinterpret_cast(static_cast(i))) == CHIP_NO_ERROR); + EXPECT_EQ(systemLayer.StartTimer(10_ms, Callback, reinterpret_cast(static_cast(i))), CHIP_NO_ERROR); } LayerEvents::ServiceEvents(systemLayer); - ValidateExecutedTimerCounts(suite); - NL_TEST_ASSERT(suite, ExecutedTimerCount() == 0); + ValidateExecutedTimerCounts(); + EXPECT_EQ(ExecutedTimerCount(), 0U); mockClock.AdvanceMonotonic(20_ms); LayerEvents::ServiceEvents(systemLayer); - ValidateExecutedTimerCounts(suite); - NL_TEST_ASSERT(suite, ExecutedTimerCount() == kCancelTimerCount); + ValidateExecutedTimerCounts(); + EXPECT_EQ(ExecutedTimerCount(), kCancelTimerCount); Clock::Internal::SetSystemClockForTesting(savedClock); } @@ -429,22 +429,8 @@ void Test(nlTestSuite * inSuite, void * aContext) } // namespace // Test the implementation helper classes TimerPool, TimerList, and TimerData. -namespace chip { -namespace System { -class TestTimer +TEST_F(TestSystemTimer, CheckTimerPool) { -public: - static void CheckTimerPool(nlTestSuite * inSuite, void * aContext); -}; -} // namespace System -} // namespace chip - -void chip::System::TestTimer::CheckTimerPool(nlTestSuite * inSuite, void * aContext) -{ - TestContext & testContext = *static_cast(aContext); - Layer & systemLayer = *testContext.mLayer; - nlTestSuite * const suite = testContext.mTestSuite; - using Timer = TimerList::Node; struct TestState { @@ -468,122 +454,121 @@ void chip::System::TestTimer::CheckTimerPool(nlTestSuite * inSuite, void * aCont }; TimerPool pool; - NL_TEST_ASSERT(suite, pool.mTimerPool.Allocated() == 0); + EXPECT_EQ(pool.mTimerPool.Allocated(), 0U); SYSTEM_STATS_RESET(Stats::kSystemLayer_NumTimers); SYSTEM_STATS_RESET_HIGH_WATER_MARK_FOR_TESTING(Stats::kSystemLayer_NumTimers); - NL_TEST_ASSERT(suite, SYSTEM_STATS_TEST_IN_USE(Stats::kSystemLayer_NumTimers, 0)); - NL_TEST_ASSERT(suite, SYSTEM_STATS_TEST_HIGH_WATER_MARK(Stats::kSystemLayer_NumTimers, 0)); + EXPECT_TRUE(SYSTEM_STATS_TEST_IN_USE(Stats::kSystemLayer_NumTimers, 0)); + EXPECT_TRUE(SYSTEM_STATS_TEST_HIGH_WATER_MARK(Stats::kSystemLayer_NumTimers, 0)); // Test TimerPool::Create() and TimerData accessors. for (auto & timer : testTimer) { - timer.timer = pool.Create(systemLayer, timer.awakenTime, timer.onComplete, &testState); + timer.timer = pool.Create(mLayer, timer.awakenTime, timer.onComplete, &testState); } - NL_TEST_ASSERT(suite, SYSTEM_STATS_TEST_IN_USE(Stats::kSystemLayer_NumTimers, 4)); + EXPECT_TRUE(SYSTEM_STATS_TEST_IN_USE(Stats::kSystemLayer_NumTimers, 4)); for (auto & timer : testTimer) { - NL_TEST_ASSERT(suite, timer.timer != nullptr); - NL_TEST_ASSERT(suite, timer.timer->AwakenTime() == timer.awakenTime); - NL_TEST_ASSERT(suite, timer.timer->GetCallback().GetOnComplete() == timer.onComplete); - NL_TEST_ASSERT(suite, timer.timer->GetCallback().GetAppState() == &testState); - NL_TEST_ASSERT(suite, timer.timer->GetCallback().GetSystemLayer() == &systemLayer); + ASSERT_NE(timer.timer, nullptr); + EXPECT_EQ(timer.timer->AwakenTime(), timer.awakenTime); + // TODO: Fix casting and use EXPECT_EQ + EXPECT_TRUE(timer.timer->GetCallback().GetOnComplete() == timer.onComplete); + EXPECT_EQ(timer.timer->GetCallback().GetAppState(), &testState); + EXPECT_EQ(timer.timer->GetCallback().GetSystemLayer(), &mLayer); } // Test TimerList operations. TimerList list; - NL_TEST_ASSERT(suite, list.Remove(nullptr) == nullptr); - NL_TEST_ASSERT(suite, list.Remove(nullptr, nullptr) == nullptr); - NL_TEST_ASSERT(suite, list.PopEarliest() == nullptr); - NL_TEST_ASSERT(suite, list.PopIfEarlier(500_ms) == nullptr); - NL_TEST_ASSERT(suite, list.Earliest() == nullptr); - NL_TEST_ASSERT(suite, list.Empty()); + EXPECT_EQ(list.Remove(nullptr), nullptr); + EXPECT_EQ(list.Remove(nullptr, nullptr), nullptr); + EXPECT_EQ(list.PopEarliest(), nullptr); + EXPECT_EQ(list.PopIfEarlier(500_ms), nullptr); + EXPECT_EQ(list.Earliest(), nullptr); + EXPECT_TRUE(list.Empty()); Timer * earliest = list.Add(testTimer[0].timer); // list: () → (0) returns: 0 - NL_TEST_ASSERT(suite, earliest == testTimer[0].timer); - NL_TEST_ASSERT(suite, list.PopIfEarlier(10_ms) == nullptr); - NL_TEST_ASSERT(suite, list.Earliest() == testTimer[0].timer); - NL_TEST_ASSERT(suite, !list.Empty()); + EXPECT_EQ(earliest, testTimer[0].timer); + EXPECT_EQ(list.PopIfEarlier(10_ms), nullptr); + EXPECT_EQ(list.Earliest(), testTimer[0].timer); + EXPECT_FALSE(list.Empty()); earliest = list.Add(testTimer[1].timer); // list: (0) → (1 0) returns: 1 - NL_TEST_ASSERT(suite, earliest == testTimer[1].timer); - NL_TEST_ASSERT(suite, list.Earliest() == testTimer[1].timer); + EXPECT_EQ(earliest, testTimer[1].timer); + EXPECT_EQ(list.Earliest(), testTimer[1].timer); earliest = list.Add(testTimer[2].timer); // list: (1 0) → (1 0 2) returns: 1 - NL_TEST_ASSERT(suite, earliest == testTimer[1].timer); - NL_TEST_ASSERT(suite, list.Earliest() == testTimer[1].timer); + EXPECT_EQ(earliest, testTimer[1].timer); + EXPECT_EQ(list.Earliest(), testTimer[1].timer); earliest = list.Add(testTimer[3].timer); // list: (1 0 2) → (1 0 2 3) returns: 1 - NL_TEST_ASSERT(suite, earliest == testTimer[1].timer); - NL_TEST_ASSERT(suite, list.Earliest() == testTimer[1].timer); + EXPECT_EQ(earliest, testTimer[1].timer); + EXPECT_EQ(list.Earliest(), testTimer[1].timer); earliest = list.Remove(earliest); // list: (1 0 2 3) → (0 2 3) returns: 0 - NL_TEST_ASSERT(suite, earliest == testTimer[0].timer); - NL_TEST_ASSERT(suite, list.Earliest() == testTimer[0].timer); + EXPECT_EQ(earliest, testTimer[0].timer); + EXPECT_EQ(list.Earliest(), testTimer[0].timer); earliest = list.Remove(TestState::Reset, &testState); // list: (0 2 3) → (0 3) returns: 2 - NL_TEST_ASSERT(suite, earliest == testTimer[2].timer); - NL_TEST_ASSERT(suite, list.Earliest() == testTimer[0].timer); + EXPECT_EQ(earliest, testTimer[2].timer); + EXPECT_EQ(list.Earliest(), testTimer[0].timer); earliest = list.PopEarliest(); // list: (0 3) → (3) returns: 0 - NL_TEST_ASSERT(suite, earliest == testTimer[0].timer); - NL_TEST_ASSERT(suite, list.Earliest() == testTimer[3].timer); + EXPECT_EQ(earliest, testTimer[0].timer); + EXPECT_EQ(list.Earliest(), testTimer[3].timer); earliest = list.PopIfEarlier(10_ms); // list: (3) → (3) returns: nullptr - NL_TEST_ASSERT(suite, earliest == nullptr); + EXPECT_EQ(earliest, nullptr); earliest = list.PopIfEarlier(500_ms); // list: (3) → () returns: 3 - NL_TEST_ASSERT(suite, earliest == testTimer[3].timer); - NL_TEST_ASSERT(suite, list.Empty()); + EXPECT_EQ(earliest, testTimer[3].timer); + EXPECT_TRUE(list.Empty()); earliest = list.Add(testTimer[3].timer); // list: () → (3) returns: 3 list.Clear(); // list: (3) → () - NL_TEST_ASSERT(suite, earliest == testTimer[3].timer); - NL_TEST_ASSERT(suite, list.Empty()); + EXPECT_EQ(earliest, testTimer[3].timer); + EXPECT_TRUE(list.Empty()); for (auto & timer : testTimer) { list.Add(timer.timer); } TimerList early = list.ExtractEarlier(200_ms); // list: (1 0 2 3) → (2 3) returns: (1 0) - NL_TEST_ASSERT(suite, list.PopEarliest() == testTimer[2].timer); - NL_TEST_ASSERT(suite, list.PopEarliest() == testTimer[3].timer); - NL_TEST_ASSERT(suite, list.PopEarliest() == nullptr); - NL_TEST_ASSERT(suite, early.PopEarliest() == testTimer[1].timer); - NL_TEST_ASSERT(suite, early.PopEarliest() == testTimer[0].timer); - NL_TEST_ASSERT(suite, early.PopEarliest() == nullptr); + EXPECT_EQ(list.PopEarliest(), testTimer[2].timer); + EXPECT_EQ(list.PopEarliest(), testTimer[3].timer); + EXPECT_EQ(list.PopEarliest(), nullptr); + EXPECT_EQ(early.PopEarliest(), testTimer[1].timer); + EXPECT_EQ(early.PopEarliest(), testTimer[0].timer); + EXPECT_EQ(early.PopEarliest(), nullptr); // Test TimerPool::Invoke() - NL_TEST_ASSERT(suite, testState.count == 0); + EXPECT_EQ(testState.count, 0); pool.Invoke(testTimer[0].timer); testTimer[0].timer = nullptr; - NL_TEST_ASSERT(suite, testState.count == 1); - NL_TEST_ASSERT(suite, pool.mTimerPool.Allocated() == 3); - NL_TEST_ASSERT(suite, SYSTEM_STATS_TEST_IN_USE(Stats::kSystemLayer_NumTimers, 3)); + EXPECT_EQ(testState.count, 1); + EXPECT_EQ(pool.mTimerPool.Allocated(), 3U); + EXPECT_TRUE(SYSTEM_STATS_TEST_IN_USE(Stats::kSystemLayer_NumTimers, 3)); // Test TimerPool::Release() pool.Release(testTimer[1].timer); testTimer[1].timer = nullptr; - NL_TEST_ASSERT(suite, testState.count == 1); - NL_TEST_ASSERT(suite, pool.mTimerPool.Allocated() == 2); - NL_TEST_ASSERT(suite, SYSTEM_STATS_TEST_IN_USE(Stats::kSystemLayer_NumTimers, 2)); + EXPECT_EQ(testState.count, 1); + EXPECT_EQ(pool.mTimerPool.Allocated(), 2U); + EXPECT_TRUE(SYSTEM_STATS_TEST_IN_USE(Stats::kSystemLayer_NumTimers, 2)); pool.ReleaseAll(); - NL_TEST_ASSERT(suite, pool.mTimerPool.Allocated() == 0); - NL_TEST_ASSERT(suite, SYSTEM_STATS_TEST_IN_USE(Stats::kSystemLayer_NumTimers, 0)); - NL_TEST_ASSERT(suite, SYSTEM_STATS_TEST_HIGH_WATER_MARK(Stats::kSystemLayer_NumTimers, 4)); + EXPECT_EQ(pool.mTimerPool.Allocated(), 0U); + EXPECT_TRUE(SYSTEM_STATS_TEST_IN_USE(Stats::kSystemLayer_NumTimers, 0)); + EXPECT_TRUE(SYSTEM_STATS_TEST_HIGH_WATER_MARK(Stats::kSystemLayer_NumTimers, 4)); } -static void ExtendTimerToTest(nlTestSuite * inSuite, void * aContext) +TEST_F(TestSystemTimer, ExtendTimerToTest) { if (!LayerEvents::HasServiceEvents()) return; - TestContext & testContext = *static_cast(aContext); - Layer & systemLayer = *testContext.mLayer; - nlTestSuite * const suite = testContext.mTestSuite; + Layer & systemLayer = mLayer; struct TestState { @@ -603,7 +588,7 @@ static void ExtendTimerToTest(nlTestSuite * inSuite, void * aContext) char record[5] = { 0 }; }; TestState testState; - NL_TEST_ASSERT(suite, testState.record[0] == 0); + EXPECT_EQ(testState.record[0], 0); Clock::ClockBase * const savedClock = &SystemClock(); Clock::Internal::MockClock mockClock; @@ -618,7 +603,7 @@ static void ExtendTimerToTest(nlTestSuite * inSuite, void * aContext) systemLayer.ExtendTimerTo(100_ms, TestState::A, &testState); mockClock.AdvanceMonotonic(100_ms); LayerEvents::ServiceEvents(systemLayer); - NL_TEST_ASSERT(suite, strcmp(testState.record, "A") == 0); + EXPECT_EQ(strcmp(testState.record, "A"), 0); // Timer B as 50ms remaining. ExtendTimerTo 25 should have no effect // Timer C as 100ms remaining. ExtendTimerTo 75ms should have no effect @@ -629,32 +614,30 @@ static void ExtendTimerToTest(nlTestSuite * inSuite, void * aContext) mockClock.AdvanceMonotonic(25_ms); LayerEvents::ServiceEvents(systemLayer); - NL_TEST_ASSERT(suite, strcmp(testState.record, "A") == 0); + EXPECT_EQ(strcmp(testState.record, "A"), 0); mockClock.AdvanceMonotonic(25_ms); LayerEvents::ServiceEvents(systemLayer); - NL_TEST_ASSERT(suite, strcmp(testState.record, "AB") == 0); + EXPECT_EQ(strcmp(testState.record, "AB"), 0); // Timer D as 25ms remaining. Timer should be extend to a duration of 75ms systemLayer.ExtendTimerTo(75_ms, TestState::D, &testState); mockClock.AdvanceMonotonic(100_ms); LayerEvents::ServiceEvents(systemLayer); - NL_TEST_ASSERT(suite, strcmp(testState.record, "ABCD") == 0); + EXPECT_EQ(strcmp(testState.record, "ABCD"), 0); Clock::Internal::SetSystemClockForTesting(savedClock); // Extending a timer by 0 ms permitted - NL_TEST_ASSERT(suite, systemLayer.ExtendTimerTo(0_ms, TestState::A, &testState) == CHIP_ERROR_INVALID_ARGUMENT); + EXPECT_EQ(systemLayer.ExtendTimerTo(0_ms, TestState::A, &testState), CHIP_ERROR_INVALID_ARGUMENT); } -static void IsTimerActiveTest(nlTestSuite * inSuite, void * aContext) +TEST_F(TestSystemTimer, IsTimerActiveTest) { if (!LayerEvents::HasServiceEvents()) return; - TestContext & testContext = *static_cast(aContext); - Layer & systemLayer = *testContext.mLayer; - nlTestSuite * const suite = testContext.mTestSuite; + Layer & systemLayer = mLayer; struct TestState { @@ -673,7 +656,7 @@ static void IsTimerActiveTest(nlTestSuite * inSuite, void * aContext) char record[4] = { 0 }; }; TestState testState; - NL_TEST_ASSERT(suite, testState.record[0] == 0); + EXPECT_EQ(testState.record[0], 0); Clock::ClockBase * const savedClock = &SystemClock(); Clock::Internal::MockClock mockClock; @@ -684,113 +667,27 @@ static void IsTimerActiveTest(nlTestSuite * inSuite, void * aContext) systemLayer.StartTimer(200_ms, TestState::B, &testState); systemLayer.StartTimer(300_ms, TestState::C, &testState); - NL_TEST_ASSERT(suite, systemLayer.IsTimerActive(TestState::A, &testState)); - NL_TEST_ASSERT(suite, systemLayer.IsTimerActive(TestState::B, &testState)); - NL_TEST_ASSERT(suite, systemLayer.IsTimerActive(TestState::C, &testState)); + EXPECT_TRUE(systemLayer.IsTimerActive(TestState::A, &testState)); + EXPECT_TRUE(systemLayer.IsTimerActive(TestState::B, &testState)); + EXPECT_TRUE(systemLayer.IsTimerActive(TestState::C, &testState)); mockClock.AdvanceMonotonic(100_ms); LayerEvents::ServiceEvents(systemLayer); - NL_TEST_ASSERT(suite, systemLayer.IsTimerActive(TestState::A, &testState) == false); - NL_TEST_ASSERT(suite, systemLayer.IsTimerActive(TestState::B, &testState)); - NL_TEST_ASSERT(suite, systemLayer.IsTimerActive(TestState::C, &testState)); + EXPECT_FALSE(systemLayer.IsTimerActive(TestState::A, &testState)); + EXPECT_TRUE(systemLayer.IsTimerActive(TestState::B, &testState)); + EXPECT_TRUE(systemLayer.IsTimerActive(TestState::C, &testState)); mockClock.AdvanceMonotonic(100_ms); LayerEvents::ServiceEvents(systemLayer); - NL_TEST_ASSERT(suite, systemLayer.IsTimerActive(TestState::B, &testState) == false); - NL_TEST_ASSERT(suite, systemLayer.IsTimerActive(TestState::C, &testState)); + EXPECT_FALSE(systemLayer.IsTimerActive(TestState::B, &testState)); + EXPECT_TRUE(systemLayer.IsTimerActive(TestState::C, &testState)); mockClock.AdvanceMonotonic(100_ms); LayerEvents::ServiceEvents(systemLayer); - NL_TEST_ASSERT(suite, systemLayer.IsTimerActive(TestState::C, &testState) == false); + EXPECT_FALSE(systemLayer.IsTimerActive(TestState::C, &testState)); Clock::Internal::SetSystemClockForTesting(savedClock); } -// Test Suite - -/** - * Test Suite. It lists all the test functions. - */ -// clang-format off -static const nlTest sTests[] = -{ - NL_TEST_DEF("Timer::TestOverflow", CheckOverflow), - NL_TEST_DEF("Timer::TestTimerStarvation", CheckStarvation), - NL_TEST_DEF("Timer::TestTimerOrder", CheckOrder), - NL_TEST_DEF("Timer::TestTimerCancellation", CheckCancellation), - NL_TEST_DEF("Timer::TestTimerPool", chip::System::TestTimer::CheckTimerPool), - NL_TEST_DEF("Timer::TestCancelTimer", CancelTimerTest::Test), - NL_TEST_DEF("Timer::ExtendTimerTo", ExtendTimerToTest), - NL_TEST_DEF("Timer::TestIsTimerActive", IsTimerActiveTest), - NL_TEST_SENTINEL() -}; -// clang-format on - -static int TestSetup(void * aContext); -static int TestTeardown(void * aContext); - -// clang-format off -static nlTestSuite kTheSuite = -{ - "chip-system-timer", - &sTests[0], - TestSetup, - TestTeardown -}; -// clang-format on - -static LayerImpl sLayer; - -/** - * Set up the test suite. - */ -static int TestSetup(void * aContext) -{ - TestContext & lContext = *reinterpret_cast(aContext); - - if (::chip::Platform::MemoryInit() != CHIP_NO_ERROR) - { - return FAILURE; - } - -#if CHIP_SYSTEM_CONFIG_USE_LWIP && (LWIP_VERSION_MAJOR == 2) && (LWIP_VERSION_MINOR == 0) && !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) - static sys_mbox_t * sLwIPEventQueue = NULL; - - sys_mbox_new(sLwIPEventQueue, 100); - tcpip_init(NULL, NULL); -#endif // CHIP_SYSTEM_CONFIG_USE_LWIP && (LWIP_VERSION_MAJOR == 2) && (LWIP_VERSION_MINOR == 0) && - // !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) - - sLayer.Init(); - - lContext.mLayer = &sLayer; - lContext.mTestSuite = &kTheSuite; - - return (SUCCESS); -} - -/** - * Tear down the test suite. - * Free memory reserved at TestSetup. - */ -static int TestTeardown(void * aContext) -{ - TestContext & lContext = *reinterpret_cast(aContext); - - lContext.mLayer->Shutdown(); - -#if CHIP_SYSTEM_CONFIG_USE_LWIP && (LWIP_VERSION_MAJOR == 2) && (LWIP_VERSION_MINOR == 0) && !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) - tcpip_finish(NULL, NULL); -#endif // CHIP_SYSTEM_CONFIG_USE_LWIP && (LWIP_VERSION_MAJOR == 2) && (LWIP_VERSION_MINOR == 0) && - // !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) - - ::chip::Platform::MemoryShutdown(); - return (SUCCESS); -} - -int TestSystemTimer() -{ - return chip::ExecuteTestsWithContext(&kTheSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestSystemTimer) +} // namespace System +} // namespace chip diff --git a/src/system/tests/TestSystemWakeEvent.cpp b/src/system/tests/TestSystemWakeEvent.cpp index 46ed99dae31656..0978d22131b0e8 100644 --- a/src/system/tests/TestSystemWakeEvent.cpp +++ b/src/system/tests/TestSystemWakeEvent.cpp @@ -21,13 +21,11 @@ * */ -#include +#include #include #include -#include -#include -#include +#include #include #include @@ -51,25 +49,27 @@ class WakeEventTest namespace { -struct TestContext +class TestSystemWakeEvent : public ::testing::Test { - ::chip::System::LayerImpl mSystemLayer; - WakeEvent mWakeEvent; - fd_set mReadSet; - fd_set mWriteSet; - fd_set mErrorSet; - - TestContext() +public: + void SetUp() { mSystemLayer.Init(); mWakeEvent.Open(mSystemLayer); } - ~TestContext() + + void TearDown() { mWakeEvent.Close(mSystemLayer); mSystemLayer.Shutdown(); } + ::chip::System::LayerImpl mSystemLayer; + WakeEvent mWakeEvent; + fd_set mReadSet; + fd_set mWriteSet; + fd_set mErrorSet; + int SelectWakeEvent(timeval timeout = {}) { // NOLINTBEGIN(clang-analyzer-security.insecureAPI.bzero) @@ -85,108 +85,69 @@ struct TestContext } }; -void TestOpen(nlTestSuite * inSuite, void * aContext) +TEST_F(TestSystemWakeEvent, TestOpen) { - TestContext & lContext = *static_cast(aContext); - NL_TEST_ASSERT(inSuite, WakeEventTest::GetReadFD(lContext.mWakeEvent) >= 0); - NL_TEST_ASSERT(inSuite, lContext.SelectWakeEvent() == 0); + EXPECT_GE(WakeEventTest::GetReadFD(mWakeEvent), 0); + EXPECT_EQ(SelectWakeEvent(), 0); } -void TestNotify(nlTestSuite * inSuite, void * aContext) +TEST_F(TestSystemWakeEvent, TestNotify) { - TestContext & lContext = *static_cast(aContext); - NL_TEST_ASSERT(inSuite, lContext.SelectWakeEvent() == 0); + EXPECT_EQ(SelectWakeEvent(), 0); // Check that select() succeeds after Notify() has been called - lContext.mWakeEvent.Notify(); - NL_TEST_ASSERT(inSuite, lContext.SelectWakeEvent() == 1); - NL_TEST_ASSERT(inSuite, FD_ISSET(WakeEventTest::GetReadFD(lContext.mWakeEvent), &lContext.mReadSet)); + mWakeEvent.Notify(); + EXPECT_EQ(SelectWakeEvent(), 1); + EXPECT_TRUE(FD_ISSET(WakeEventTest::GetReadFD(mWakeEvent), &mReadSet)); // ...and state of the event is not cleared automatically - NL_TEST_ASSERT(inSuite, lContext.SelectWakeEvent() == 1); - NL_TEST_ASSERT(inSuite, FD_ISSET(WakeEventTest::GetReadFD(lContext.mWakeEvent), &lContext.mReadSet)); + EXPECT_EQ(SelectWakeEvent(), 1); + EXPECT_TRUE(FD_ISSET(WakeEventTest::GetReadFD(mWakeEvent), &mReadSet)); } -void TestConfirm(nlTestSuite * inSuite, void * aContext) +TEST_F(TestSystemWakeEvent, TestConfirm) { - TestContext & lContext = *static_cast(aContext); - // Check that select() succeeds after Notify() has been called - lContext.mWakeEvent.Notify(); - NL_TEST_ASSERT(inSuite, lContext.SelectWakeEvent() == 1); - NL_TEST_ASSERT(inSuite, FD_ISSET(WakeEventTest::GetReadFD(lContext.mWakeEvent), &lContext.mReadSet)); + mWakeEvent.Notify(); + EXPECT_EQ(SelectWakeEvent(), 1); + EXPECT_TRUE(FD_ISSET(WakeEventTest::GetReadFD(mWakeEvent), &mReadSet)); // Check that Confirm() clears state of the event - lContext.mWakeEvent.Confirm(); - NL_TEST_ASSERT(inSuite, lContext.SelectWakeEvent() == 0); + mWakeEvent.Confirm(); + EXPECT_EQ(SelectWakeEvent(), 0); } #if CHIP_SYSTEM_CONFIG_POSIX_LOCKING void * WaitForEvent(void * aContext) { - TestContext & lContext = *static_cast(aContext); // wait 5 seconds - return reinterpret_cast(lContext.SelectWakeEvent(timeval{ 5, 0 })); + auto * context = static_cast(aContext); + return reinterpret_cast(context->SelectWakeEvent(timeval{ 5, 0 })); } -void TestBlockingSelect(nlTestSuite * inSuite, void * aContext) +TEST_F(TestSystemWakeEvent, TestBlockingSelect) { - TestContext & lContext = *static_cast(aContext); - // Spawn a thread waiting for the event pthread_t tid = 0; - NL_TEST_ASSERT(inSuite, 0 == pthread_create(&tid, nullptr, WaitForEvent, aContext)); + EXPECT_EQ(0, pthread_create(&tid, nullptr, WaitForEvent, this)); - lContext.mWakeEvent.Notify(); + mWakeEvent.Notify(); void * selectResult = nullptr; - NL_TEST_ASSERT(inSuite, 0 == pthread_join(tid, &selectResult)); - NL_TEST_ASSERT(inSuite, selectResult == reinterpret_cast(1)); + EXPECT_EQ(0, pthread_join(tid, &selectResult)); + EXPECT_EQ(selectResult, reinterpret_cast(1)); } -#else // CHIP_SYSTEM_CONFIG_POSIX_LOCKING -void TestBlockingSelect(nlTestSuite *, void *) {} #endif // CHIP_SYSTEM_CONFIG_POSIX_LOCKING -void TestClose(nlTestSuite * inSuite, void * aContext) +TEST_F(TestSystemWakeEvent, TestClose) { - TestContext & lContext = *static_cast(aContext); - lContext.mWakeEvent.Close(lContext.mSystemLayer); + mWakeEvent.Close(mSystemLayer); - const auto notifFD = WakeEventTest::GetReadFD(lContext.mWakeEvent); + const auto notifFD = WakeEventTest::GetReadFD(mWakeEvent); // Check that Close() has cleaned up itself and reopen is possible - NL_TEST_ASSERT(inSuite, lContext.mWakeEvent.Open(lContext.mSystemLayer) == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, notifFD < 0); + EXPECT_EQ(mWakeEvent.Open(mSystemLayer), CHIP_NO_ERROR); + EXPECT_LT(notifFD, 0); } } // namespace -// Test Suite - -/** - * Test Suite. It lists all the test functions. - */ -// clang-format off -static const nlTest sTests[] = -{ - NL_TEST_DEF("WakeEvent::TestOpen", TestOpen), - NL_TEST_DEF("WakeEvent::TestNotify", TestNotify), - NL_TEST_DEF("WakeEvent::TestConfirm", TestConfirm), - NL_TEST_DEF("WakeEvent::TestBlockingSelect", TestBlockingSelect), - NL_TEST_DEF("WakeEvent::TestClose", TestClose), - NL_TEST_SENTINEL() -}; -// clang-format on - -static nlTestSuite kTheSuite = { "chip-system-wake-event", sTests }; - -int TestSystemWakeEvent() -{ - return chip::ExecuteTestsWithContext(&kTheSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestSystemWakeEvent) -#else // CHIP_SYSTEM_CONFIG_USE_SOCKETS -int TestSystemWakeEvent(void) -{ - return SUCCESS; -} #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS diff --git a/src/system/tests/TestTLVPacketBufferBackingStore.cpp b/src/system/tests/TestTLVPacketBufferBackingStore.cpp index e46906d2edbbb3..6dc76fed46745e 100644 --- a/src/system/tests/TestTLVPacketBufferBackingStore.cpp +++ b/src/system/tests/TestTLVPacketBufferBackingStore.cpp @@ -18,14 +18,13 @@ #include #include +#include + #include #include #include -#include #include -#include - using ::chip::Platform::ScopedMemoryBuffer; using ::chip::System::PacketBuffer; using ::chip::System::PacketBufferHandle; @@ -33,49 +32,27 @@ using ::chip::System::PacketBufferTLVReader; using ::chip::System::PacketBufferTLVWriter; using namespace ::chip; -namespace { - -void WriteUntilRemainingLessThan(nlTestSuite * inSuite, PacketBufferTLVWriter & writer, const uint32_t remainingSize) +class TestTLVPacketBufferBackingStore : public ::testing::Test { - uint32_t lengthRemaining = writer.GetRemainingFreeLength(); - while (lengthRemaining >= remainingSize) +public: + static void SetUpTestSuite() { ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); } + static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); } + + void WriteUntilRemainingLessThan(PacketBufferTLVWriter & writer, const uint32_t remainingSize) { - NL_TEST_ASSERT(inSuite, writer.Put(TLV::AnonymousTag(), static_cast(7)) == CHIP_NO_ERROR); - lengthRemaining = writer.GetRemainingFreeLength(); + uint32_t lengthRemaining = writer.GetRemainingFreeLength(); + while (lengthRemaining >= remainingSize) + { + EXPECT_EQ(writer.Put(TLV::AnonymousTag(), static_cast(7)), CHIP_NO_ERROR); + lengthRemaining = writer.GetRemainingFreeLength(); + } } -} - -class TLVPacketBufferBackingStoreTest -{ -public: - static int TestSetup(void * inContext); - static int TestTeardown(void * inContext); - - static void BasicEncodeDecode(nlTestSuite * inSuite, void * inContext); - static void MultiBufferEncode(nlTestSuite * inSuite, void * inContext); - static void NonChainedBufferCanReserve(nlTestSuite * inSuite, void * inContext); - static void TestWriterReserveUnreserveDoesNotOverflow(nlTestSuite * inSuite, void * inContext); - static void TestWriterReserve(nlTestSuite * inSuite, void * inContext); }; -int TLVPacketBufferBackingStoreTest::TestSetup(void * inContext) -{ - chip::Platform::MemoryInit(); - - return SUCCESS; -} - -int TLVPacketBufferBackingStoreTest::TestTeardown(void * inContext) -{ - chip::Platform::MemoryShutdown(); - - return SUCCESS; -} - /** * Test that we can do a basic encode to TLV followed by decode. */ -void TLVPacketBufferBackingStoreTest::BasicEncodeDecode(nlTestSuite * inSuite, void * inContext) +TEST_F(TestTLVPacketBufferBackingStore, BasicEncodeDecode) { auto buffer = PacketBufferHandle::New(PacketBuffer::kMaxSizeWithoutReserve, 0); @@ -84,74 +61,74 @@ void TLVPacketBufferBackingStoreTest::BasicEncodeDecode(nlTestSuite * inSuite, v TLV::TLVType outerContainerType; CHIP_ERROR error = writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Array, outerContainerType); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); error = writer.Put(TLV::AnonymousTag(), static_cast(7)); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); error = writer.Put(TLV::AnonymousTag(), static_cast(8)); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); error = writer.Put(TLV::AnonymousTag(), static_cast(9)); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); error = writer.EndContainer(outerContainerType); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); error = writer.Finalize(&buffer); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); // Array start/end is 2 bytes. Each entry is also 2 bytes: control + // value. So 8 bytes total. - NL_TEST_ASSERT(inSuite, !buffer->HasChainedBuffer()); - NL_TEST_ASSERT(inSuite, buffer->TotalLength() == 8); - NL_TEST_ASSERT(inSuite, buffer->DataLength() == 8); + EXPECT_FALSE(buffer->HasChainedBuffer()); + EXPECT_EQ(buffer->TotalLength(), 8); + EXPECT_EQ(buffer->DataLength(), 8); PacketBufferTLVReader reader; reader.Init(std::move(buffer)); error = reader.Next(TLV::kTLVType_Array, TLV::AnonymousTag()); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); error = reader.EnterContainer(outerContainerType); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); error = reader.Next(TLV::kTLVType_UnsignedInteger, TLV::AnonymousTag()); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); uint8_t value; error = reader.Get(value); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, value == 7); + EXPECT_EQ(error, CHIP_NO_ERROR); + EXPECT_EQ(value, 7); error = reader.Next(TLV::kTLVType_UnsignedInteger, TLV::AnonymousTag()); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); error = reader.Get(value); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, value == 8); + EXPECT_EQ(error, CHIP_NO_ERROR); + EXPECT_EQ(value, 8); error = reader.Next(TLV::kTLVType_UnsignedInteger, TLV::AnonymousTag()); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); error = reader.Get(value); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, value == 9); + EXPECT_EQ(error, CHIP_NO_ERROR); + EXPECT_EQ(value, 9); error = reader.Next(); - NL_TEST_ASSERT(inSuite, error == CHIP_END_OF_TLV); + EXPECT_EQ(error, CHIP_END_OF_TLV); error = reader.ExitContainer(outerContainerType); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); error = reader.Next(); - NL_TEST_ASSERT(inSuite, error == CHIP_END_OF_TLV); + EXPECT_EQ(error, CHIP_END_OF_TLV); } /** * Test that we can do an encode that's going to split across multiple buffers correctly. */ -void TLVPacketBufferBackingStoreTest::MultiBufferEncode(nlTestSuite * inSuite, void * inContext) +TEST_F(TestTLVPacketBufferBackingStore, MultiBufferEncode) { // Start with a too-small buffer. auto buffer = PacketBufferHandle::New(2, 0); @@ -161,97 +138,97 @@ void TLVPacketBufferBackingStoreTest::MultiBufferEncode(nlTestSuite * inSuite, v TLV::TLVType outerContainerType; CHIP_ERROR error = writer.StartContainer(TLV::AnonymousTag(), TLV::kTLVType_Array, outerContainerType); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); error = writer.Put(TLV::AnonymousTag(), static_cast(7)); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); error = writer.Put(TLV::AnonymousTag(), static_cast(8)); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); // Something to make sure we have 3 buffers. uint8_t bytes[2000] = { 0 }; error = writer.Put(TLV::AnonymousTag(), ByteSpan(bytes)); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); error = writer.EndContainer(outerContainerType); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); error = writer.Finalize(&buffer); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); // Array start/end is 2 bytes. First two entries are 2 bytes each. // Third entry is 1 control byte, 2 length bytes, 2000 bytes of data, // for a total of 2009 bytes. constexpr size_t totalSize = 2009; - NL_TEST_ASSERT(inSuite, buffer->HasChainedBuffer()); - NL_TEST_ASSERT(inSuite, buffer->TotalLength() == totalSize); - NL_TEST_ASSERT(inSuite, buffer->DataLength() == 2); + EXPECT_TRUE(buffer->HasChainedBuffer()); + EXPECT_EQ(buffer->TotalLength(), totalSize); + EXPECT_EQ(buffer->DataLength(), 2); auto nextBuffer = buffer->Next(); - NL_TEST_ASSERT(inSuite, nextBuffer->HasChainedBuffer()); - NL_TEST_ASSERT(inSuite, nextBuffer->TotalLength() == totalSize - 2); - NL_TEST_ASSERT(inSuite, nextBuffer->DataLength() == PacketBuffer::kMaxSizeWithoutReserve); + EXPECT_TRUE(nextBuffer->HasChainedBuffer()); + EXPECT_EQ(nextBuffer->TotalLength(), totalSize - 2); + EXPECT_EQ(nextBuffer->DataLength(), PacketBuffer::kMaxSizeWithoutReserve); nextBuffer = nextBuffer->Next(); - NL_TEST_ASSERT(inSuite, !nextBuffer->HasChainedBuffer()); - NL_TEST_ASSERT(inSuite, nextBuffer->TotalLength() == nextBuffer->DataLength()); - NL_TEST_ASSERT(inSuite, nextBuffer->DataLength() == totalSize - 2 - PacketBuffer::kMaxSizeWithoutReserve); + EXPECT_FALSE(nextBuffer->HasChainedBuffer()); + EXPECT_EQ(nextBuffer->TotalLength(), nextBuffer->DataLength()); + EXPECT_EQ(nextBuffer->DataLength(), totalSize - 2 - PacketBuffer::kMaxSizeWithoutReserve); // PacketBufferTLVReader cannot handle non-contiguous buffers, and our // buffers are too big to stick into a single packet buffer. ScopedMemoryBuffer buf; - NL_TEST_ASSERT(inSuite, buf.Calloc(totalSize)); + EXPECT_TRUE(buf.Calloc(totalSize)); size_t offset = 0; while (!buffer.IsNull()) { memcpy(buf.Get() + offset, buffer->Start(), buffer->DataLength()); offset += buffer->DataLength(); buffer.Advance(); - NL_TEST_ASSERT(inSuite, offset < totalSize || (offset == totalSize && buffer.IsNull())); + EXPECT_TRUE(offset < totalSize || (offset == totalSize && buffer.IsNull())); } TLV::TLVReader reader; reader.Init(buf.Get(), totalSize); error = reader.Next(TLV::kTLVType_Array, TLV::AnonymousTag()); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); error = reader.EnterContainer(outerContainerType); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); error = reader.Next(TLV::kTLVType_UnsignedInteger, TLV::AnonymousTag()); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); uint8_t value; error = reader.Get(value); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, value == 7); + EXPECT_EQ(error, CHIP_NO_ERROR); + EXPECT_EQ(value, 7); error = reader.Next(TLV::kTLVType_UnsignedInteger, TLV::AnonymousTag()); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); error = reader.Get(value); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, value == 8); + EXPECT_EQ(error, CHIP_NO_ERROR); + EXPECT_EQ(value, 8); error = reader.Next(TLV::kTLVType_ByteString, TLV::AnonymousTag()); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); ByteSpan byteValue; error = reader.Get(byteValue); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); - NL_TEST_ASSERT(inSuite, byteValue.size() == sizeof(bytes)); + EXPECT_EQ(error, CHIP_NO_ERROR); + EXPECT_EQ(byteValue.size(), sizeof(bytes)); error = reader.Next(); - NL_TEST_ASSERT(inSuite, error == CHIP_END_OF_TLV); + EXPECT_EQ(error, CHIP_END_OF_TLV); error = reader.ExitContainer(outerContainerType); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); error = reader.Next(); - NL_TEST_ASSERT(inSuite, error == CHIP_END_OF_TLV); + EXPECT_EQ(error, CHIP_END_OF_TLV); } -void TLVPacketBufferBackingStoreTest::NonChainedBufferCanReserve(nlTestSuite * inSuite, void * inContext) +TEST_F(TestTLVPacketBufferBackingStore, NonChainedBufferCanReserve) { // Start with a too-small buffer. uint32_t smallSize = 5; @@ -263,12 +240,12 @@ void TLVPacketBufferBackingStoreTest::NonChainedBufferCanReserve(nlTestSuite * i writer.Init(std::move(buffer), /* useChainedBuffers = */ false); CHIP_ERROR error = writer.ReserveBuffer(smallerSizeToReserver); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); } // This test previously was created to show that there was an overflow bug, now this test mainly // just checks that you cannot reserve this type of TLVBackingStorage buffer. -void TLVPacketBufferBackingStoreTest::TestWriterReserveUnreserveDoesNotOverflow(nlTestSuite * inSuite, void * inContext) +TEST_F(TestTLVPacketBufferBackingStore, TestWriterReserveUnreserveDoesNotOverflow) { // Start with a too-small buffer. uint32_t smallSize = 100; @@ -283,29 +260,29 @@ void TLVPacketBufferBackingStoreTest::TestWriterReserveUnreserveDoesNotOverflow( if (error == CHIP_NO_ERROR) { uint32_t lengthRemaining = writer.GetRemainingFreeLength(); - NL_TEST_ASSERT(inSuite, lengthRemaining == 1); + EXPECT_EQ(lengthRemaining, 1U); // Lets try to overflow by getting next buffer in the chain, // unreserving then writing until the end of the current buffer. error = writer.Put(TLV::AnonymousTag(), static_cast(7)); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); lengthRemaining = writer.GetRemainingFreeLength(); - NL_TEST_ASSERT(inSuite, lengthRemaining > smallerSizeToReserver); + EXPECT_GT(lengthRemaining, smallerSizeToReserver); - WriteUntilRemainingLessThan(inSuite, writer, 2); + WriteUntilRemainingLessThan(writer, 2); lengthRemaining = writer.GetRemainingFreeLength(); - NL_TEST_ASSERT(inSuite, lengthRemaining != 0); - NL_TEST_ASSERT(inSuite, lengthRemaining < smallerSizeToReserver); + EXPECT_NE(lengthRemaining, 0U); + EXPECT_LT(lengthRemaining, smallerSizeToReserver); error = writer.UnreserveBuffer(smallerSizeToReserver); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); lengthRemaining = writer.GetRemainingFreeLength(); - NL_TEST_ASSERT(inSuite, lengthRemaining > smallerSizeToReserver); + EXPECT_GT(lengthRemaining, smallerSizeToReserver); // This is where we get overflow. - WriteUntilRemainingLessThan(inSuite, writer, 2); + WriteUntilRemainingLessThan(writer, 2); // If we get here then the overflow condition we were expecting did not happen. If that is the case, // either we have fixed reservation for chained buffers, or expected failure didn't hit on this @@ -314,14 +291,14 @@ void TLVPacketBufferBackingStoreTest::TestWriterReserveUnreserveDoesNotOverflow( // If there is a fix please add reservation for chained buffers, please make sure you account for // what happens if TLVWriter::WriteData fails to get a new buffer but we are not at max size, do // you actually have space for what was supposed to be reserved. - NL_TEST_ASSERT(inSuite, false); + FAIL(); } // We no longer allow non-contigous buffers to be reserved. - NL_TEST_ASSERT(inSuite, error == CHIP_ERROR_INCORRECT_STATE); + EXPECT_EQ(error, CHIP_ERROR_INCORRECT_STATE); } -void TLVPacketBufferBackingStoreTest::TestWriterReserve(nlTestSuite * inSuite, void * inContext) +TEST_F(TestTLVPacketBufferBackingStore, TestWriterReserve) { // Start with a too-small buffer. uint32_t smallSize = 5; @@ -333,51 +310,14 @@ void TLVPacketBufferBackingStoreTest::TestWriterReserve(nlTestSuite * inSuite, v writer.Init(std::move(buffer), /* useChainedBuffers = */ false); CHIP_ERROR error = writer.ReserveBuffer(smallerSizeToReserver); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); error = writer.Put(TLV::AnonymousTag(), static_cast(7)); - NL_TEST_ASSERT(inSuite, error == CHIP_ERROR_NO_MEMORY); + EXPECT_EQ(error, CHIP_ERROR_NO_MEMORY); error = writer.UnreserveBuffer(smallerSizeToReserver); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); error = writer.Put(TLV::AnonymousTag(), static_cast(7)); - NL_TEST_ASSERT(inSuite, error == CHIP_NO_ERROR); + EXPECT_EQ(error, CHIP_NO_ERROR); } - -/** - * Test Suite. It lists all the test functions. - */ -// clang-format off -const nlTest sTests[] = -{ - NL_TEST_DEF("BasicEncodeDecode", TLVPacketBufferBackingStoreTest::BasicEncodeDecode), - NL_TEST_DEF("MultiBufferEncode", TLVPacketBufferBackingStoreTest::MultiBufferEncode), - NL_TEST_DEF("NonChainedBufferCanReserve", TLVPacketBufferBackingStoreTest::NonChainedBufferCanReserve), - NL_TEST_DEF("TestWriterReserveUnreserveDoesNotOverflow", TLVPacketBufferBackingStoreTest::TestWriterReserveUnreserveDoesNotOverflow), - NL_TEST_DEF("TestWriterReserve", TLVPacketBufferBackingStoreTest::TestWriterReserve), - - NL_TEST_SENTINEL() -}; -// clang-format on - -} // anonymous namespace - -int TestTLVPacketBufferBackingStore() -{ - // clang-format off - nlTestSuite theSuite = { - .name ="chip-tlv-packet-buffer-backing-store", - .tests = &sTests[0], - .setup = TLVPacketBufferBackingStoreTest::TestSetup, - .tear_down = TLVPacketBufferBackingStoreTest::TestTeardown, - }; - // clang-format on - - // Run test suite. - nlTestRunner(&theSuite, nullptr); - - return (nlTestRunnerStats(&theSuite)); -} - -CHIP_REGISTER_TEST_SUITE(TestTLVPacketBufferBackingStore) diff --git a/src/system/tests/TestTimeSource.cpp b/src/system/tests/TestTimeSource.cpp index daee4cfb974f8b..e6b8251f0ee63f 100644 --- a/src/system/tests/TestTimeSource.cpp +++ b/src/system/tests/TestTimeSource.cpp @@ -21,29 +21,26 @@ * the ability to compile and use the test implementation of the time source. */ -#include +#include #include #include -#include -#include +#include #include -namespace { - -void TestTimeSourceSetAndGet(nlTestSuite * inSuite, void * inContext) +TEST(TestTimeSource, TestTimeSourceSetAndGet) { chip::Time::TimeSource source; - NL_TEST_ASSERT(inSuite, source.GetMonotonicTimestamp() == chip::System::Clock::kZero); + EXPECT_EQ(source.GetMonotonicTimestamp(), chip::System::Clock::kZero); constexpr chip::System::Clock::Milliseconds64 k1234 = chip::System::Clock::Milliseconds64(1234); source.SetMonotonicTimestamp(k1234); - NL_TEST_ASSERT(inSuite, source.GetMonotonicTimestamp() == k1234); + EXPECT_EQ(source.GetMonotonicTimestamp(), k1234); } -void SystemTimeSourceGet(nlTestSuite * inSuite, void * inContext) +TEST(TestTimeSource, SystemTimeSourceGet) { chip::Time::TimeSource source; @@ -55,35 +52,7 @@ void SystemTimeSourceGet(nlTestSuite * inSuite, void * inContext) for (int i = 0; i < 100; i++) { chip::System::Clock::Timestamp newValue = source.GetMonotonicTimestamp(); - NL_TEST_ASSERT(inSuite, newValue >= oldValue); + EXPECT_GE(newValue, oldValue); oldValue = newValue; } } - -} // namespace - -/** - * Test Suite. It lists all the test functions. - */ -// clang-format off -static const nlTest sTests[] = -{ - NL_TEST_DEF("TimeSource::SetAndGet", TestTimeSourceSetAndGet), - NL_TEST_DEF("TimeSource::SetAndGet", SystemTimeSourceGet), - NL_TEST_SENTINEL() -}; -// clang-format on - -int TestTimeSource() -{ - nlTestSuite theSuite = { - "chip-timesource", &sTests[0], nullptr /* setup */, nullptr /* teardown */ - }; - - // Run test suite against one context. - nlTestRunner(&theSuite, nullptr /* context */); - - return (nlTestRunnerStats(&theSuite)); -} - -CHIP_REGISTER_TEST_SUITE(TestTimeSource) diff --git a/src/test_driver/openiotsdk/unit-tests/test_components.txt b/src/test_driver/openiotsdk/unit-tests/test_components.txt index 11b230af4870a7..eccf4044c24099 100644 --- a/src/test_driver/openiotsdk/unit-tests/test_components.txt +++ b/src/test_driver/openiotsdk/unit-tests/test_components.txt @@ -1,4 +1,5 @@ accesstest +SystemLayerTests ASN1Tests PlatformTests TestShell diff --git a/src/test_driver/openiotsdk/unit-tests/test_components_nl.txt b/src/test_driver/openiotsdk/unit-tests/test_components_nl.txt index cd1ea79e9f8228..f44abf53e654fa 100644 --- a/src/test_driver/openiotsdk/unit-tests/test_components_nl.txt +++ b/src/test_driver/openiotsdk/unit-tests/test_components_nl.txt @@ -15,6 +15,5 @@ RetransmitTests SecureChannelTests SetupPayloadTests SupportTests -SystemLayerTests TransportLayerTests UserDirectedCommissioningTests