Skip to content

Commit

Permalink
Replace nl-unit-test with pigweed for system (project-chip#33046)
Browse files Browse the repository at this point in the history
* Use pigweed for system tests

* Restyled by clang-format

* Friend class fix

---------

Co-authored-by: Restyled.io <[email protected]>
Co-authored-by: Andrei Litvin <[email protected]>
  • Loading branch information
3 people authored Apr 19, 2024
1 parent 69d8585 commit 72e7554
Show file tree
Hide file tree
Showing 14 changed files with 863 additions and 1,552 deletions.
6 changes: 2 additions & 4 deletions src/system/SystemPacketBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@
#include <lwip/pbuf.h>
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

class PacketBufferTest;

namespace chip {
namespace System {

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions src/system/SystemTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ namespace chip {
namespace System {

class Layer;
class TestTimer;

/**
* Basic Timer information: time and callback.
Expand Down Expand Up @@ -238,7 +237,7 @@ class TimerPool
}

private:
friend class TestTimer;
friend class TestSystemTimer_CheckTimerPool_Test;
ObjectPool<Timer, CHIP_SYSTEM_CONFIG_NUM_TIMERS> mTimerPool;
};

Expand Down
5 changes: 1 addition & 4 deletions src/system/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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",
]
}
60 changes: 16 additions & 44 deletions src/system/tests/TestSystemClock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
* limitations under the License.
*/

#include <system/SystemConfig.h>
#include <gtest/gtest.h>

#include <lib/core/ErrorStr.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/TimeUtils.h>
#include <lib/support/UnitTestRegistration.h>
#include <nlunit-test.h>
#include <system/SystemClock.h>
#include <system/SystemConfig.h>

#if !CHIP_SYSTEM_CONFIG_PLATFORM_PROVIDES_TIME

Expand All @@ -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)
Expand All @@ -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)
53 changes: 7 additions & 46 deletions src/system/tests/TestSystemErrorStr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,28 @@
#include <stdint.h>
#include <string.h>

#include <gtest/gtest.h>

#include <inet/InetError.h>
#include <lib/core/ErrorStr.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/UnitTestRegistration.h>

#include <nlunit-test.h>

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...
Expand All @@ -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)
Loading

0 comments on commit 72e7554

Please sign in to comment.