From 84913af2f3a2f98a7a67a7c2a88c23d6cd1fba01 Mon Sep 17 00:00:00 2001 From: Matthew Swartwout Date: Thu, 21 Mar 2024 07:43:16 -0700 Subject: [PATCH] Add equality operator for ConcreteDataAttributePath (#32309) * Add equality operator for ConcreteDataAttributePath * Restyled by clang-format * Add inequality tests --------- Co-authored-by: Restyled.io --- src/app/ConcreteAttributePath.h | 12 ++- src/app/tests/TestConcreteAttributePath.cpp | 99 +++++++++++++++++++++ 2 files changed, 108 insertions(+), 3 deletions(-) diff --git a/src/app/ConcreteAttributePath.h b/src/app/ConcreteAttributePath.h index 253b590ef90739..cfde88ab9bd374 100644 --- a/src/app/ConcreteAttributePath.h +++ b/src/app/ConcreteAttributePath.h @@ -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 diff --git a/src/app/tests/TestConcreteAttributePath.cpp b/src/app/tests/TestConcreteAttributePath.cpp index 6e3451be26a78f..a598c2f23c121f 100644 --- a/src/app/tests/TestConcreteAttributePath.cpp +++ b/src/app/tests/TestConcreteAttributePath.cpp @@ -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), @@ -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() };