Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pw_unit_test migration: lib support batch 2 #33144

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/lib/support/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ chip_test_suite("tests") {
test_sources = [
"TestBitMask.cpp",
"TestBufferReader.cpp",
"TestBytesToHex.cpp",
"TestDefer.cpp",
"TestFixedBufferAllocator.cpp",
"TestFold.cpp",
"TestIniEscaping.cpp",
"TestSafeInt.cpp",
"TestSafeString.cpp",
"TestScoped.cpp",
"TestSpan.cpp",
"TestStaticSupportSmartPtr.cpp",
"TestTestPersistentStorageDelegate.cpp",
"TestUtf8.cpp",
]
sources = []

Expand Down Expand Up @@ -59,7 +66,6 @@ chip_test_suite_using_nltest("tests_nltest") {
test_sources = [
"TestBufferWriter.cpp",
"TestBytesCircularBuffer.cpp",
"TestBytesToHex.cpp",
"TestCHIPCounter.cpp",
"TestCHIPMem.cpp",
"TestCHIPMemString.cpp",
Expand All @@ -70,21 +76,15 @@ chip_test_suite_using_nltest("tests_nltest") {
"TestPersistedCounter.cpp",
"TestPool.cpp",
"TestPrivateHeap.cpp",
"TestSafeString.cpp",
"TestScoped.cpp",
"TestScopedBuffer.cpp",
"TestSorting.cpp",
"TestSpan.cpp",
"TestStateMachine.cpp",
"TestStaticSupportSmartPtr.cpp",
"TestStringBuilder.cpp",
"TestStringSplitter.cpp",
"TestTestPersistentStorageDelegate.cpp",
"TestThreadOperationalDataset.cpp",
"TestTimeUtils.cpp",
"TestTlvJson.cpp",
"TestTlvToJson.cpp",
"TestUtf8.cpp",
"TestVariant.cpp",
"TestZclString.cpp",
]
Expand Down
237 changes: 98 additions & 139 deletions src/lib/support/tests/TestBytesToHex.cpp

Large diffs are not rendered by default.

40 changes: 11 additions & 29 deletions src/lib/support/tests/TestSafeString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,27 @@
*
*/

#include <lib/support/SafeString.h>
#include <lib/support/UnitTestRegistration.h>
#include <gtest/gtest.h>

#include <nlunit-test.h>
#include <lib/support/SafeString.h>

using namespace chip;

static void TestMaxStringLength(nlTestSuite * inSuite, void * inContext)
TEST(TestSafeString, TestMaxStringLength)
{
constexpr size_t len = MaxStringLength("a", "bc", "def");
NL_TEST_ASSERT(inSuite, len == 3);
EXPECT_EQ(len, 3u);

NL_TEST_ASSERT(inSuite, MaxStringLength("bc") == 2);
EXPECT_EQ(MaxStringLength("bc"), 2u);

NL_TEST_ASSERT(inSuite, MaxStringLength("def", "bc", "a") == 3);
EXPECT_EQ(MaxStringLength("def", "bc", "a"), 3u);

NL_TEST_ASSERT(inSuite, MaxStringLength("") == 0);
}

static void TestTotalStringLength(nlTestSuite * inSuite, void * inContext)
{
NL_TEST_ASSERT(inSuite, TotalStringLength("") == 0);
NL_TEST_ASSERT(inSuite, TotalStringLength("a") == 1);
NL_TEST_ASSERT(inSuite, TotalStringLength("def", "bc", "a") == 6);
EXPECT_EQ(MaxStringLength(""), 0u);
}

#define NL_TEST_DEF_FN(fn) NL_TEST_DEF("Test " #fn, fn)
/**
* Test Suite. It lists all the test functions.
*/
static const nlTest sTests[] = { NL_TEST_DEF_FN(TestMaxStringLength), NL_TEST_DEF_FN(TestTotalStringLength), NL_TEST_SENTINEL() };

int TestSafeString()
TEST(TestSafeString, TestTotalStringLength)
{
nlTestSuite theSuite = { "CHIP SafeString tests", &sTests[0], nullptr, nullptr };

// Run test suite against one context.
nlTestRunner(&theSuite, nullptr);
return nlTestRunnerStats(&theSuite);
EXPECT_EQ(TotalStringLength(""), 0u);
EXPECT_EQ(TotalStringLength("a"), 1u);
EXPECT_EQ(TotalStringLength("def", "bc", "a"), 6u);
}

CHIP_REGISTER_TEST_SUITE(TestSafeString)
65 changes: 22 additions & 43 deletions src/lib/support/tests/TestScoped.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,87 +16,66 @@
* limitations under the License.
*/

#include <lib/support/Scoped.h>
#include <lib/support/UnitTestRegistration.h>

#include <nlunit-test.h>
#include <string.h>

#include <gtest/gtest.h>

#include <lib/support/Scoped.h>

namespace {

using namespace chip;

void TestScopedVariableChange(nlTestSuite * inSuite, void * inContext)
TEST(TestScoped, TestScopedVariableChange)
{
int x = 123;

{
ScopedChange change1(x, 10);
NL_TEST_ASSERT(inSuite, x == 10);
EXPECT_EQ(x, 10);

x = 15;
{
ScopedChange change2(x, 20);
NL_TEST_ASSERT(inSuite, x == 20);
EXPECT_EQ(x, 20);
}
NL_TEST_ASSERT(inSuite, x == 15);
EXPECT_EQ(x, 15);
}
NL_TEST_ASSERT(inSuite, x == 123);
EXPECT_EQ(x, 123);
}

void TestScopedChangeOnly(nlTestSuite * inSuite, void * inContext)
TEST(TestScoped, TestScopedChangeOnly)
{
ScopedChangeOnly intValue(123);
ScopedChangeOnly strValue("abc");

NL_TEST_ASSERT(inSuite, intValue == 123);
NL_TEST_ASSERT(inSuite, strcmp(strValue, "abc") == 0);
EXPECT_EQ(intValue, 123);
EXPECT_STREQ(strValue, "abc");

{
ScopedChange change1(intValue, 234);

NL_TEST_ASSERT(inSuite, intValue == 234);
NL_TEST_ASSERT(inSuite, strcmp(strValue, "abc") == 0);
EXPECT_EQ(intValue, 234);
EXPECT_STREQ(strValue, "abc");

ScopedChange change2(strValue, "xyz");
NL_TEST_ASSERT(inSuite, intValue == 234);
NL_TEST_ASSERT(inSuite, strcmp(strValue, "xyz") == 0);
EXPECT_EQ(intValue, 234);
EXPECT_STREQ(strValue, "xyz");

{
ScopedChange change3(intValue, 10);
ScopedChange change4(strValue, "test");

NL_TEST_ASSERT(inSuite, intValue == 10);
NL_TEST_ASSERT(inSuite, strcmp(strValue, "test") == 0);
EXPECT_EQ(intValue, 10);
EXPECT_STREQ(strValue, "test");
}

NL_TEST_ASSERT(inSuite, intValue == 234);
NL_TEST_ASSERT(inSuite, strcmp(strValue, "xyz") == 0);
EXPECT_EQ(intValue, 234);
EXPECT_STREQ(strValue, "xyz");
}

NL_TEST_ASSERT(inSuite, intValue == 123);
NL_TEST_ASSERT(inSuite, strcmp(strValue, "abc") == 0);
EXPECT_EQ(intValue, 123);
EXPECT_STREQ(strValue, "abc");
}

} // namespace

#define NL_TEST_DEF_FN(fn) NL_TEST_DEF("Test " #fn, fn)
/**
* Test Suite. It lists all the test functions.
*/
static const nlTest sTests[] = {
NL_TEST_DEF_FN(TestScopedVariableChange), //
NL_TEST_DEF_FN(TestScopedChangeOnly), //
NL_TEST_SENTINEL() //
};

int TestScoped()
{
nlTestSuite theSuite = { "CHIP Scoped tests", &sTests[0], nullptr, nullptr };

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

CHIP_REGISTER_TEST_SUITE(TestScoped)
Loading
Loading