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

Switched to pw_unit_test in src/lib/dnssd/minimal_mdns/core/ #33069

Merged
merged 3 commits into from
Apr 23, 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
5 changes: 1 addition & 4 deletions src/lib/dnssd/minimal_mdns/core/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

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")

Expand All @@ -27,7 +26,7 @@ source_set("support") {
]
}

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

test_sources = [
Expand All @@ -43,7 +42,5 @@ chip_test_suite_using_nltest("tests") {
":support",
"${chip_root}/src/lib/core",
"${chip_root}/src/lib/dnssd/minimal_mdns/core",
"${chip_root}/src/lib/support:testing_nlunit",
"${nlunit_test_root}:nlunit-test",
]
}
66 changes: 23 additions & 43 deletions src/lib/dnssd/minimal_mdns/core/tests/TestFlatAllocatedQName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <lib/dnssd/minimal_mdns/core/FlatAllocatedQName.h>
#include <lib/support/UnitTestRegistration.h>
#include <gtest/gtest.h>

#include <nlunit-test.h>
#include <lib/dnssd/minimal_mdns/core/FlatAllocatedQName.h>

namespace {

Expand All @@ -38,28 +37,28 @@ class AutoFreeBuffer
void * mBuffer;
};

void TestFlatAllocatedQName(nlTestSuite * inSuite, void * inContext)
TEST(TestFlatAllocatedQName, TestFlatAllocatedQName)
{
AutoFreeBuffer buffer(128);

NL_TEST_ASSERT(inSuite, FlatAllocatedQName::RequiredStorageSize("some", "test") == (sizeof(char * [2]) + 5 + 5));
EXPECT_EQ(FlatAllocatedQName::RequiredStorageSize("some", "test"), (sizeof(char * [2]) + 5 + 5));

{
FullQName built = FlatAllocatedQName::Build(buffer.Buffer(), "some", "test");
const QNamePart expected[] = { "some", "test" };

NL_TEST_ASSERT(inSuite, FullQName(expected) == built);
EXPECT_EQ(FullQName(expected), built);
}

{
FullQName built = FlatAllocatedQName::Build(buffer.Buffer(), "1", "2", "3");
const QNamePart expected[] = { "1", "2", "3" };

NL_TEST_ASSERT(inSuite, FullQName(expected) == built);
EXPECT_EQ(FullQName(expected), built);
}
}

void SizeCompare(nlTestSuite * inSuite, void * inContext)
TEST(TestFlatAllocatedQName, SizeCompare)
{
static const char kThis[] = "this";
static const char kIs[] = "is";
Expand All @@ -79,23 +78,22 @@ void SizeCompare(nlTestSuite * inSuite, void * inContext)

const size_t kTestStorageSize = FlatAllocatedQName::RequiredStorageSize(kThis, kIs, kA, kTest);

NL_TEST_ASSERT(inSuite, kTestStorageSize == FlatAllocatedQName::RequiredStorageSizeFromArray(kSameArraySameSize, 4));
NL_TEST_ASSERT(inSuite, kTestStorageSize == FlatAllocatedQName::RequiredStorageSizeFromArray(kDifferentArraySameSize, 4));
NL_TEST_ASSERT(inSuite, kTestStorageSize < FlatAllocatedQName::RequiredStorageSizeFromArray(kDifferenArrayLongerWord, 4));
NL_TEST_ASSERT(inSuite, kTestStorageSize > FlatAllocatedQName::RequiredStorageSizeFromArray(kDifferenArrayShorterWord, 4));
EXPECT_EQ(kTestStorageSize, FlatAllocatedQName::RequiredStorageSizeFromArray(kSameArraySameSize, 4));
EXPECT_EQ(kTestStorageSize, FlatAllocatedQName::RequiredStorageSizeFromArray(kDifferentArraySameSize, 4));
EXPECT_LT(kTestStorageSize, FlatAllocatedQName::RequiredStorageSizeFromArray(kDifferenArrayLongerWord, 4));
EXPECT_GT(kTestStorageSize, FlatAllocatedQName::RequiredStorageSizeFromArray(kDifferenArrayShorterWord, 4));

// Although the size of the array is larger, if we tell the function there are only 4 words, it should still work.
NL_TEST_ASSERT(inSuite, kTestStorageSize == FlatAllocatedQName::RequiredStorageSizeFromArray(kSameArrayExtraWord, 4));
EXPECT_EQ(kTestStorageSize, FlatAllocatedQName::RequiredStorageSizeFromArray(kSameArrayExtraWord, 4));
// If we add the extra word, the sizes should not match
NL_TEST_ASSERT(inSuite, kTestStorageSize < FlatAllocatedQName::RequiredStorageSizeFromArray(kSameArrayExtraWord, 5));
EXPECT_LT(kTestStorageSize, FlatAllocatedQName::RequiredStorageSizeFromArray(kSameArrayExtraWord, 5));

NL_TEST_ASSERT(inSuite, kTestStorageSize > FlatAllocatedQName::RequiredStorageSizeFromArray(kShorterArray, 3));
NL_TEST_ASSERT(inSuite,
FlatAllocatedQName::RequiredStorageSizeFromArray(kSameArraySameSize, 3) ==
FlatAllocatedQName::RequiredStorageSizeFromArray(kShorterArray, 3));
EXPECT_GT(kTestStorageSize, FlatAllocatedQName::RequiredStorageSizeFromArray(kShorterArray, 3));
EXPECT_EQ(FlatAllocatedQName::RequiredStorageSizeFromArray(kSameArraySameSize, 3),
FlatAllocatedQName::RequiredStorageSizeFromArray(kShorterArray, 3));
}

void BuildCompare(nlTestSuite * inSuite, void * inContext)
TEST(TestFlatAllocatedQName, BuildCompare)
{
static const char kThis[] = "this";
static const char kIs[] = "is";
Expand All @@ -111,33 +109,15 @@ void BuildCompare(nlTestSuite * inSuite, void * inContext)

const FullQName kTestQName = FlatAllocatedQName::Build(storage, kThis, kIs, kA, kTest);

NL_TEST_ASSERT(inSuite, kTestQName == FlatAllocatedQName::BuildFromArray(storage, kSameArraySameSize, 4));
EXPECT_EQ(kTestQName, FlatAllocatedQName::BuildFromArray(storage, kSameArraySameSize, 4));

// Although the size of the array is larger, if we tell the function there are only 4 words, it should still work.
NL_TEST_ASSERT(inSuite, kTestQName == FlatAllocatedQName::BuildFromArray(storage, kSameArrayExtraWord, 4));
EXPECT_EQ(kTestQName, FlatAllocatedQName::BuildFromArray(storage, kSameArrayExtraWord, 4));
// If we add the extra word, the names
NL_TEST_ASSERT(inSuite, kTestQName != FlatAllocatedQName::BuildFromArray(storage, kSameArrayExtraWord, 5));
EXPECT_NE(kTestQName, FlatAllocatedQName::BuildFromArray(storage, kSameArrayExtraWord, 5));

NL_TEST_ASSERT(inSuite, kTestQName != FlatAllocatedQName::BuildFromArray(storage, kShorterArray, 3));
NL_TEST_ASSERT(inSuite,
FlatAllocatedQName::BuildFromArray(storage, kSameArraySameSize, 3) ==
FlatAllocatedQName::BuildFromArray(storage, kShorterArray, 3));
EXPECT_NE(kTestQName, FlatAllocatedQName::BuildFromArray(storage, kShorterArray, 3));
EXPECT_EQ(FlatAllocatedQName::BuildFromArray(storage, kSameArraySameSize, 3),
FlatAllocatedQName::BuildFromArray(storage, kShorterArray, 3));
}

const nlTest sTests[] = {
NL_TEST_DEF("TestFlatAllocatedQName", TestFlatAllocatedQName), //
NL_TEST_DEF("TestFlatAllocatedQNameRequiredSizes", SizeCompare), //
NL_TEST_DEF("TestFlatAllocatedQNameBuild", BuildCompare), //
NL_TEST_SENTINEL() //
};

} // namespace

int TestFlatAllocatedQName()
{
nlTestSuite theSuite = { "FlatAllocatedQName", sTests, nullptr, nullptr };
nlTestRunner(&theSuite, nullptr);
return nlTestRunnerStats(&theSuite);
}

CHIP_REGISTER_TEST_SUITE(TestFlatAllocatedQName)
77 changes: 22 additions & 55 deletions src/lib/dnssd/minimal_mdns/core/tests/TestHeapQName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,33 @@
* limitations under the License.
*/

#include <gtest/gtest.h>

#include <lib/dnssd/minimal_mdns/core/HeapQName.h>
#include <lib/dnssd/minimal_mdns/core/tests/QNameStrings.h>
#include <lib/support/UnitTestRegistration.h>

#include <nlunit-test.h>

namespace {

using namespace mdns::Minimal;

void Construction(nlTestSuite * inSuite, void * inContext)
class TestHeapQName : public ::testing::Test
{
public:
static void SetUpTestSuite() { ASSERT_EQ(chip::Platform::MemoryInit(), CHIP_NO_ERROR); }
static void TearDownTestSuite() { chip::Platform::MemoryShutdown(); }
};

TEST_F(TestHeapQName, Construction)
{
{

const testing::TestQName<2> kShort({ "some", "test" });

HeapQName heapQName(kShort.Serialized());

NL_TEST_ASSERT(inSuite, heapQName.IsOk());
NL_TEST_ASSERT(inSuite, heapQName.Content() == kShort.Full());
NL_TEST_ASSERT(inSuite, kShort.Serialized() == heapQName.Content());
EXPECT_TRUE(heapQName.IsOk());
EXPECT_EQ(heapQName.Content(), kShort.Full());
EXPECT_EQ(kShort.Serialized(), heapQName.Content());
}

{
Expand All @@ -45,13 +51,13 @@ void Construction(nlTestSuite * inSuite, void * inContext)

HeapQName heapQName(kLonger.Serialized());

NL_TEST_ASSERT(inSuite, heapQName.IsOk());
NL_TEST_ASSERT(inSuite, heapQName.Content() == kLonger.Full());
NL_TEST_ASSERT(inSuite, kLonger.Serialized() == heapQName.Content());
EXPECT_TRUE(heapQName.IsOk());
EXPECT_EQ(heapQName.Content(), kLonger.Full());
EXPECT_EQ(kLonger.Serialized(), heapQName.Content());
}
}

void Copying(nlTestSuite * inSuite, void * inContext)
TEST_F(TestHeapQName, Copying)
{
const testing::TestQName<2> kShort({ "some", "test" });

Expand All @@ -61,50 +67,11 @@ void Copying(nlTestSuite * inSuite, void * inContext)

name3 = name2;

NL_TEST_ASSERT(inSuite, name1.IsOk());
NL_TEST_ASSERT(inSuite, name2.IsOk());
NL_TEST_ASSERT(inSuite, name3.IsOk());
NL_TEST_ASSERT(inSuite, name1.Content() == name2.Content());
NL_TEST_ASSERT(inSuite, name1.Content() == name3.Content());
}

static const nlTest sTests[] = { //
NL_TEST_DEF("Construction", Construction), //
NL_TEST_DEF("Copying", Copying), //
NL_TEST_SENTINEL()
};

int Setup(void * inContext)
{
CHIP_ERROR error = chip::Platform::MemoryInit();
if (error != CHIP_NO_ERROR)
return FAILURE;
return SUCCESS;
}

/**
* Tear down the test suite.
*/
int Teardown(void * inContext)
{
chip::Platform::MemoryShutdown();
return SUCCESS;
EXPECT_TRUE(name1.IsOk());
EXPECT_TRUE(name2.IsOk());
EXPECT_TRUE(name3.IsOk());
EXPECT_EQ(name1.Content(), name2.Content());
EXPECT_EQ(name1.Content(), name3.Content());
}

} // namespace

int TestHeapQName()
{
nlTestSuite theSuite = {
"HeapQName",
&sTests[0],
&Setup,
&Teardown,
};

nlTestRunner(&theSuite, nullptr);

return (nlTestRunnerStats(&theSuite));
}

CHIP_REGISTER_TEST_SUITE(TestHeapQName)
Loading
Loading