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

Add equality operator to StatusIB #33156

Merged
merged 1 commit into from
Apr 26, 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
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;
bzbarsky-apple marked this conversation as resolved.
Show resolved Hide resolved
}

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
Loading