Skip to content

Commit

Permalink
pigweed unit_test migration: App 3rd batch (#33757)
Browse files Browse the repository at this point in the history
* pigweed unit_test migration: App 3rd batch

* Integrating Comments

* moving buffer in TestDataModelSerialization to Teardown
  • Loading branch information
Alami-Amine authored and pull[bot] committed Jun 26, 2024
1 parent 7788f00 commit 8260996
Show file tree
Hide file tree
Showing 11 changed files with 1,478 additions and 1,914 deletions.
36 changes: 18 additions & 18 deletions src/app/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,18 @@ chip_test_suite("tests") {
test_sources = [
"TestAttributeAccessInterfaceCache.cpp",
"TestAttributePathExpandIterator.cpp",
"TestAttributePathParams.cpp",
"TestAttributePersistenceProvider.cpp",
"TestAttributeValueDecoder.cpp",
"TestAttributeValueEncoder.cpp",
"TestBasicCommandPathRegistry.cpp",
"TestBindingTable.cpp",
"TestBuilderParser.cpp",
"TestCommandPathParams.cpp",
"TestConcreteAttributePath.cpp",
"TestDataModelSerialization.cpp",
"TestDefaultOTARequestorStorage.cpp",
"TestEventPathParams.cpp",
"TestMessageDef.cpp",
"TestNullable.cpp",
"TestNumericAttributeTraits.cpp",
Expand Down Expand Up @@ -193,6 +200,17 @@ chip_test_suite("tests") {
"${chip_root}/src/lib/support:test_utils",
"${chip_root}/src/lib/support:testing",
]

if (chip_device_platform != "android") {
test_sources += [
"TestExtensionFieldSets.cpp",
"TestSceneTable.cpp",
]
public_deps += [
":power-cluster-test-srcs",
":scenes-table-test-srcs",
]
}
}

chip_test_suite_using_nltest("tests_nltest") {
Expand All @@ -201,16 +219,9 @@ chip_test_suite_using_nltest("tests_nltest") {
test_sources = [
"TestAclAttribute.cpp",
"TestAclEvent.cpp",
"TestBasicCommandPathRegistry.cpp",
"TestClusterInfo.cpp",
"TestCommandInteraction.cpp",
"TestCommandPathParams.cpp",
"TestConcreteAttributePath.cpp",
"TestDataModelSerialization.cpp",
"TestDefaultOTARequestorStorage.cpp",
"TestEventLoggingNoUTCTime.cpp",
"TestEventOverflow.cpp",
"TestEventPathParams.cpp",
"TestFabricScopedEventLogging.cpp",
"TestInteractionModelEngine.cpp",
"TestReadInteraction.cpp",
Expand Down Expand Up @@ -258,17 +269,6 @@ chip_test_suite_using_nltest("tests_nltest") {
"${nlunit_test_root}:nlunit-test",
]

if (chip_device_platform != "android") {
test_sources += [
"TestExtensionFieldSets.cpp",
"TestSceneTable.cpp",
]
public_deps += [
":power-cluster-test-srcs",
":scenes-table-test-srcs",
]
}

if (chip_config_network_layer_ble &&
(chip_device_platform == "linux" || chip_device_platform == "darwin")) {
test_sources += [ "TestCommissionManager.cpp" ]
Expand Down
156 changes: 156 additions & 0 deletions src/app/tests/TestAttributePathParams.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <app/AttributePathParams.h>
#include <app/DataVersionFilter.h>

#include <lib/core/StringBuilderAdapters.h>
#include <pw_unit_test/framework.h>

namespace chip {
namespace app {
namespace TestPath {

TEST(TestAttributePathParams, TestAttributePathIntersect)
{
EndpointId endpointIdArray[2] = { 1, kInvalidEndpointId };
ClusterId clusterIdArray[2] = { 2, kInvalidClusterId };
AttributeId attributeIdArray[2] = { 3, kInvalidAttributeId };

for (auto endpointId1 : endpointIdArray)
{
for (auto clusterId1 : clusterIdArray)
{
for (auto attributeId1 : attributeIdArray)
{
for (auto endpointId2 : endpointIdArray)
{
for (auto clusterId2 : clusterIdArray)
{
for (auto attributeId2 : attributeIdArray)
{
AttributePathParams path1;
path1.mEndpointId = endpointId1;
path1.mClusterId = clusterId1;
path1.mAttributeId = attributeId1;
AttributePathParams path2;
path2.mEndpointId = endpointId2;
path2.mClusterId = clusterId2;
path2.mAttributeId = attributeId2;
EXPECT_TRUE(path1.Intersects(path2));
}
}
}
}
}
}

{
AttributePathParams path1;
path1.mEndpointId = 1;
AttributePathParams path2;
path2.mEndpointId = 2;
EXPECT_FALSE(path1.Intersects(path2));
}

{
AttributePathParams path1;
path1.mClusterId = 1;
AttributePathParams path2;
path2.mClusterId = 2;
EXPECT_FALSE(path1.Intersects(path2));
}

{
AttributePathParams path1;
path1.mAttributeId = 1;
AttributePathParams path2;
path2.mAttributeId = 2;
EXPECT_FALSE(path1.Intersects(path2));
}
}

TEST(TestAttributePathParams, TestAttributePathIncludedSameFieldId)
{
AttributePathParams path1;
AttributePathParams path2;
AttributePathParams path3;
path1.mAttributeId = 1;
path2.mAttributeId = 1;
path3.mAttributeId = 1;
EXPECT_TRUE(path1.IsAttributePathSupersetOf(path2));
path2.mListIndex = 1;
EXPECT_TRUE(path1.IsAttributePathSupersetOf(path2));
path1.mListIndex = 0;
EXPECT_FALSE(path1.IsAttributePathSupersetOf(path3));
path3.mListIndex = 0;
EXPECT_TRUE(path1.IsAttributePathSupersetOf(path3));
path3.mListIndex = 1;
EXPECT_FALSE(path1.IsAttributePathSupersetOf(path3));
}

TEST(TestAttributePathParams, TestAttributePathIncludedDifferentFieldId)
{
{
AttributePathParams path1;
AttributePathParams path2;
path1.mAttributeId = 1;
path2.mAttributeId = 2;
EXPECT_FALSE(path1.IsAttributePathSupersetOf(path2));
}
{
AttributePathParams path1;
AttributePathParams path2;
path2.mAttributeId = 2;
EXPECT_TRUE(path1.IsAttributePathSupersetOf(path2));
}
{
AttributePathParams path1;
AttributePathParams path2;
EXPECT_TRUE(path1.IsAttributePathSupersetOf(path2));
}
{
AttributePathParams path1;
AttributePathParams path2;

path1.mAttributeId = 1;
EXPECT_FALSE(path1.IsAttributePathSupersetOf(path2));
}
}

TEST(TestAttributePathParams, TestAttributePathIncludedDifferentEndpointId)
{
AttributePathParams path1;
AttributePathParams path2;
path1.mEndpointId = 1;
path2.mEndpointId = 2;
EXPECT_FALSE(path1.IsAttributePathSupersetOf(path2));
}

TEST(TestAttributePathParams, TestAttributePathIncludedDifferentClusterId)
{
AttributePathParams path1;
AttributePathParams path2;
path1.mClusterId = 1;
path2.mClusterId = 2;
EXPECT_FALSE(path1.IsAttributePathSupersetOf(path2));
}

} // namespace TestPath
} // namespace app
} // namespace chip
58 changes: 15 additions & 43 deletions src/app/tests/TestBasicCommandPathRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
*/

#include <app/CommandPathRegistry.h>
#include <lib/support/UnitTestRegistration.h>

#include <nlunit-test.h>
#include <lib/core/StringBuilderAdapters.h>
#include <pw_unit_test/framework.h>

namespace chip {
namespace app {
Expand All @@ -30,7 +30,7 @@ size_t constexpr kQuickTestSize = 10;

} // namespace

void TestAddingSameConcretePath(nlTestSuite * apSuite, void * apContext)
TEST(TestBasicCommandPathRegistry, TestAddingSameConcretePath)
{
CHIP_ERROR err = CHIP_NO_ERROR;
BasicCommandPathRegistry<kQuickTestSize> basicCommandPathRegistry;
Expand All @@ -47,11 +47,11 @@ void TestAddingSameConcretePath(nlTestSuite * apSuite, void * apContext)
err = basicCommandPathRegistry.Add(concretePath, commandRef);
}

NL_TEST_ASSERT(apSuite, err == CHIP_ERROR_DUPLICATE_KEY_ID);
NL_TEST_ASSERT(apSuite, basicCommandPathRegistry.Count() == 1);
EXPECT_EQ(err, CHIP_ERROR_DUPLICATE_KEY_ID);
EXPECT_EQ(basicCommandPathRegistry.Count(), 1u);
}

void TestAddingSameCommandRef(nlTestSuite * apSuite, void * apContext)
TEST(TestBasicCommandPathRegistry, TestAddingSameCommandRef)
{
CHIP_ERROR err = CHIP_NO_ERROR;
BasicCommandPathRegistry<kQuickTestSize> basicCommandPathRegistry;
Expand All @@ -69,32 +69,30 @@ void TestAddingSameCommandRef(nlTestSuite * apSuite, void * apContext)
err = basicCommandPathRegistry.Add(concretePath, commandRef);
}

NL_TEST_ASSERT(apSuite, err == CHIP_ERROR_DUPLICATE_KEY_ID);
NL_TEST_ASSERT(apSuite, basicCommandPathRegistry.Count() == 1);
EXPECT_EQ(err, CHIP_ERROR_DUPLICATE_KEY_ID);
EXPECT_EQ(basicCommandPathRegistry.Count(), 1u);
}

void TestAddingMaxNumberOfEntries(nlTestSuite * apSuite, void * apContext)
TEST(TestBasicCommandPathRegistry, TestAddingMaxNumberOfEntries)
{
CHIP_ERROR err = CHIP_NO_ERROR;
BasicCommandPathRegistry<kQuickTestSize> basicCommandPathRegistry;

std::optional<uint16_t> commandRef;
uint16_t commandRefAndEndpointValue = 0;

size_t idx = 0;
for (idx = 0; idx < kQuickTestSize && err == CHIP_NO_ERROR; idx++)
for (idx = 0; idx < kQuickTestSize; idx++)
{
ConcreteCommandPath concretePath(commandRefAndEndpointValue, 0, 0);
commandRef.emplace(commandRefAndEndpointValue);
commandRefAndEndpointValue++;
err = basicCommandPathRegistry.Add(concretePath, commandRef);
ASSERT_EQ(basicCommandPathRegistry.Add(concretePath, commandRef), CHIP_NO_ERROR);
}

NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR);
NL_TEST_ASSERT(apSuite, basicCommandPathRegistry.Count() == kQuickTestSize);
EXPECT_EQ(basicCommandPathRegistry.Count(), kQuickTestSize);
}

void TestAddingTooManyEntries(nlTestSuite * apSuite, void * apContext)
TEST(TestBasicCommandPathRegistry, TestAddingTooManyEntries)
{
CHIP_ERROR err = CHIP_NO_ERROR;
BasicCommandPathRegistry<kQuickTestSize> basicCommandPathRegistry;
Expand All @@ -112,36 +110,10 @@ void TestAddingTooManyEntries(nlTestSuite * apSuite, void * apContext)
err = basicCommandPathRegistry.Add(concretePath, commandRef);
}

NL_TEST_ASSERT(apSuite, err == CHIP_ERROR_NO_MEMORY);
NL_TEST_ASSERT(apSuite, basicCommandPathRegistry.Count() == kQuickTestSize);
EXPECT_EQ(err, CHIP_ERROR_NO_MEMORY);
EXPECT_EQ(basicCommandPathRegistry.Count(), kQuickTestSize);
}

} // namespace TestBasicCommandPathRegistry
} // namespace app
} // namespace chip

namespace {
// clang-format off
const nlTest sTests[] =
{
NL_TEST_DEF("TestAddingSameConcretePath", chip::app::TestBasicCommandPathRegistry::TestAddingSameConcretePath),
NL_TEST_DEF("TestAddingSameCommandRef", chip::app::TestBasicCommandPathRegistry::TestAddingSameCommandRef),
NL_TEST_DEF("TestAddingMaxNumberOfEntries", chip::app::TestBasicCommandPathRegistry::TestAddingMaxNumberOfEntries),
NL_TEST_DEF("TestAddingTooManyEntries", chip::app::TestBasicCommandPathRegistry::TestAddingTooManyEntries),

NL_TEST_SENTINEL()
};
// clang-format on

} // namespace

int TestBasicCommandPathRegistry()
{
nlTestSuite theSuite = { "CommandPathRegistry", &sTests[0], nullptr, nullptr };

nlTestRunner(&theSuite, nullptr);

return (nlTestRunnerStats(&theSuite));
}

CHIP_REGISTER_TEST_SUITE(TestBasicCommandPathRegistry)
Loading

0 comments on commit 8260996

Please sign in to comment.