Skip to content

Commit

Permalink
Add equality operator for ConcreteDataAttributePath (project-chip#32309)
Browse files Browse the repository at this point in the history
* Add equality operator for ConcreteDataAttributePath

* Restyled by clang-format

* Add inequality tests

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
mwswartwout and restyled-commits authored Mar 21, 2024
1 parent 7f14133 commit 84913af
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/app/ConcreteAttributePath.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,15 @@ struct ConcreteDataAttributePath : public ConcreteAttributePath

bool MatchesConcreteAttributePath(const ConcreteAttributePath & aOther) { return ConcreteAttributePath::operator==(aOther); }

bool operator==(const ConcreteDataAttributePath & aOther) const = delete;
bool operator!=(const ConcreteDataAttributePath & aOther) const = delete;
bool operator<(const ConcreteDataAttributePath & aOther) const = delete;
bool operator==(const ConcreteDataAttributePath & aOther) const
{
return ConcreteAttributePath::operator==(aOther) && (mListIndex == aOther.mListIndex) && (mListOp == aOther.mListOp) &&
(mDataVersion == aOther.mDataVersion);
}

bool operator!=(const ConcreteDataAttributePath & aOther) const { return !(*this == aOther); }

bool operator<(const ConcreteDataAttributePath & aOther) const = delete;

//
// This index is only valid if `mListOp` is set to a list item operation, i.e
Expand Down
99 changes: 99 additions & 0 deletions src/app/tests/TestConcreteAttributePath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,87 @@ void TestConcreteDataAttributePathMatchesConcreteAttributePathInequality(nlTestS
NL_TEST_ASSERT(aSuite, !data_path.MatchesConcreteAttributePath(path));
}

void TestConcreteDataAttributePathEqualityDefaultConstructor(nlTestSuite * aSuite, void * aContext)
{
ConcreteDataAttributePath one;
ConcreteDataAttributePath two;
NL_TEST_ASSERT(aSuite, one == two);
}

void TestConcreteDataAttributePathEqualityConcreteAttributePathConstructor(nlTestSuite * aSuite, void * aContext)
{
ConcreteAttributePath path(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3);
ConcreteDataAttributePath one(path);
ConcreteDataAttributePath two(path);
NL_TEST_ASSERT(aSuite, one == two);
}

void TestConcreteDataAttributePathInequalityConcreteAttributePathConstructorDifferentAttributeId(nlTestSuite * aSuite,
void * aContext)
{
ConcreteAttributePath path_one(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3);
ConcreteAttributePath path_two(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/4);
ConcreteDataAttributePath one(path_one);
ConcreteDataAttributePath two(path_two);
NL_TEST_ASSERT(aSuite, one != two);
}

void TestConcreteDataAttributePathEqualityConcreteAttributePathArgsConstructor(nlTestSuite * aSuite, void * aContext)
{
ConcreteDataAttributePath one(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3);
ConcreteDataAttributePath two(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3);
NL_TEST_ASSERT(aSuite, one == two);
}

void TestConcreteDataAttributePathInequalityConcreteAttributePathArgsConstructorDifferentAttributeId(nlTestSuite * aSuite,
void * aContext)
{
ConcreteDataAttributePath one(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3);
ConcreteDataAttributePath two(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/4);
NL_TEST_ASSERT(aSuite, one != two);
}

void TestConcreteDataAttributePathEqualityDataVersionConstructor(nlTestSuite * aSuite, void * aContext)
{
ConcreteDataAttributePath one(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3, /*aDataVersion=*/MakeOptional(4U));
ConcreteDataAttributePath two(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3, /*aDataVersion=*/MakeOptional(4U));
NL_TEST_ASSERT(aSuite, one == two);
}

void TestConcreteDataAttributePathInequalityDataVersionConstructorDifferentDataVersion(nlTestSuite * aSuite, void * aContext)
{
ConcreteDataAttributePath one(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3, /*aDataVersion=*/MakeOptional(4U));
ConcreteDataAttributePath two(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3, /*aDataVersion=*/MakeOptional(5U));
NL_TEST_ASSERT(aSuite, one != two);
}

void TestConcreteDataAttributePathEqualityListConstructor(nlTestSuite * aSuite, void * aContext)
{
ConcreteDataAttributePath one(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3,
ConcreteDataAttributePath::ListOperation::ReplaceAll, /*aListIndex=*/5);
ConcreteDataAttributePath two(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3,
ConcreteDataAttributePath::ListOperation::ReplaceAll, /*aListIndex=*/5);
NL_TEST_ASSERT(aSuite, one == two);
}

void TestConcreteDataAttributePathInequalityListConstructorDifferentListOp(nlTestSuite * aSuite, void * aContext)
{
ConcreteDataAttributePath one(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3,
ConcreteDataAttributePath::ListOperation::ReplaceAll, /*aListIndex=*/5);
ConcreteDataAttributePath two(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3,
ConcreteDataAttributePath::ListOperation::ReplaceItem, /*aListIndex=*/5);
NL_TEST_ASSERT(aSuite, one != two);
}

void TestConcreteDataAttributePathInequalityListConstructorDifferentListIndex(nlTestSuite * aSuite, void * aContext)
{
ConcreteDataAttributePath one(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3,
ConcreteDataAttributePath::ListOperation::ReplaceAll, /*aListIndex=*/5);
ConcreteDataAttributePath two(/*aEndpointId=*/1, /*aClusterId=*/2, /*aAttributeId=*/3,
ConcreteDataAttributePath::ListOperation::ReplaceAll, /*aListIndex=*/6);
NL_TEST_ASSERT(aSuite, one != two);
}

const nlTest sTests[] = {
NL_TEST_DEF("TestConcreteAttributePathEqualityDefaultConstructor", TestConcreteAttributePathEqualityDefaultConstructor),
NL_TEST_DEF("TestConcreteAttributePathEquality", TestConcreteAttributePathEquality),
Expand All @@ -77,6 +158,24 @@ const nlTest sTests[] = {
TestConcreteDataAttributePathMatchesConcreteAttributePathEquality),
NL_TEST_DEF("TestConcreteDataAttributePathMatchesConcreteAttributePathInequality",
TestConcreteDataAttributePathMatchesConcreteAttributePathInequality),
NL_TEST_DEF("TestConcreteDataAttributePathEqualityDefaultConstructor", TestConcreteDataAttributePathEqualityDefaultConstructor),
NL_TEST_DEF("TestConcreteDataAttributePathEqualityConcreteAttributePathConstructor",
TestConcreteDataAttributePathEqualityConcreteAttributePathConstructor),
NL_TEST_DEF("TestConcreteDataAttributePathInequalityConcreteAttributePathConstructorDifferentAttributeId",
TestConcreteDataAttributePathInequalityConcreteAttributePathConstructorDifferentAttributeId),
NL_TEST_DEF("TestConcreteDataAttributePathEqualityConcreteAttributePathArgsConstructor",
TestConcreteDataAttributePathEqualityConcreteAttributePathArgsConstructor),
NL_TEST_DEF("TestConcreteDataAttributePathInequalityConcreteAttributePathArgsConstructorDifferentAttributeId",
TestConcreteDataAttributePathInequalityConcreteAttributePathArgsConstructorDifferentAttributeId),
NL_TEST_DEF("TestConcreteDataAttributePathEqualityDataVersionConstructor",
TestConcreteDataAttributePathEqualityDataVersionConstructor),
NL_TEST_DEF("TestConcreteDataAttributePathInequalityDataVersionConstructorDifferentDataVersion",
TestConcreteDataAttributePathInequalityDataVersionConstructorDifferentDataVersion),
NL_TEST_DEF("TestConcreteDataAttributePathEqualityListConstructor", TestConcreteDataAttributePathEqualityListConstructor),
NL_TEST_DEF("TestConcreteDataAttributePathInequalityListConstructorDifferentListOp",
TestConcreteDataAttributePathInequalityListConstructorDifferentListOp),
NL_TEST_DEF("TestConcreteDataAttributePathInequalityListConstructorDifferentListIndex",
TestConcreteDataAttributePathInequalityListConstructorDifferentListIndex),
NL_TEST_SENTINEL()
};

Expand Down

0 comments on commit 84913af

Please sign in to comment.