Skip to content

Commit

Permalink
Add equality operator to StatusIB (#33156)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwswartwout authored Apr 26, 2024
1 parent 29cfb7f commit 10886ac
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/app/MessageDef/StatusIB.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,15 @@ struct StatusIB

}; // struct StatusIB

constexpr bool operator==(const StatusIB & one, const StatusIB & two)
{
return one.mStatus == two.mStatus && one.mClusterStatus == two.mClusterStatus;
}

constexpr bool operator!=(const StatusIB & one, const StatusIB & two)
{
return !(one == two);
}

}; // namespace app
}; // namespace chip
39 changes: 39 additions & 0 deletions src/app/tests/TestStatusIB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,52 @@ void TestStatusIBErrorToString(nlTestSuite * aSuite, void * aContext)
}
#endif // !CHIP_CONFIG_SHORT_ERROR_STR

void TestStatusIBEqualityOperator(nlTestSuite * aSuite, void * /*aContext*/)
{
// Equality against self is true.
StatusIB one;
NL_TEST_ASSERT(aSuite, one == one);

// Default constructors are equal.
NL_TEST_ASSERT(aSuite, one == StatusIB());

// Different imStatus is not equal.
StatusIB with_imstatus(Status::Failure);
NL_TEST_ASSERT(aSuite, one != with_imstatus);

// Same imStatus are equal.
NL_TEST_ASSERT(aSuite, with_imstatus == StatusIB(Status::Failure));

// Same imStatus but different clusterStatus are not equal.
StatusIB with_cluster_status(Status::Failure, /*clusterStatus=*/2);
NL_TEST_ASSERT(aSuite, with_imstatus != with_cluster_status);

// Different imStatus but same clusterStatus are not equal.
NL_TEST_ASSERT(aSuite, with_cluster_status != StatusIB(Status::Success, /*clusterStatus=*/2));

// Same imStatus and clusterStatus are equal.
NL_TEST_ASSERT(aSuite, with_cluster_status == StatusIB(Status::Failure, /*clusterStatus=*/2));

// From same CHIP_ERROR are equal.
StatusIB invalid_argument(CHIP_ERROR_INVALID_ARGUMENT);
NL_TEST_ASSERT(aSuite, invalid_argument == StatusIB(CHIP_ERROR_INVALID_ARGUMENT));

// Different CHIP_ERROR are equal if they are not from kIMClusterStatus or
// kIMGlobalStatus.
NL_TEST_ASSERT(aSuite, invalid_argument == StatusIB(CHIP_ERROR_INCORRECT_STATE));

// Error never equals NO_ERROR
NL_TEST_ASSERT(aSuite, invalid_argument != StatusIB(CHIP_NO_ERROR));
}

// clang-format off
const nlTest sTests[] =
{
NL_TEST_DEF("StatusIBToFromChipError", TestStatusIBToFromChipError),
#if !CHIP_CONFIG_SHORT_ERROR_STR
NL_TEST_DEF("StatusIBErrorToString", TestStatusIBErrorToString),
#endif // !CHIP_CONFIG_SHORT_ERROR_STR
NL_TEST_DEF("StatusIBEqualityOperator", TestStatusIBEqualityOperator),
NL_TEST_SENTINEL()
};
// clang-format on
Expand Down

0 comments on commit 10886ac

Please sign in to comment.