Skip to content

Commit 10886ac

Browse files
authored
Add equality operator to StatusIB (project-chip#33156)
1 parent 29cfb7f commit 10886ac

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/app/MessageDef/StatusIB.h

+10
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,15 @@ struct StatusIB
117117

118118
}; // struct StatusIB
119119

120+
constexpr bool operator==(const StatusIB & one, const StatusIB & two)
121+
{
122+
return one.mStatus == two.mStatus && one.mClusterStatus == two.mClusterStatus;
123+
}
124+
125+
constexpr bool operator!=(const StatusIB & one, const StatusIB & two)
126+
{
127+
return !(one == two);
128+
}
129+
120130
}; // namespace app
121131
}; // namespace chip

src/app/tests/TestStatusIB.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,52 @@ void TestStatusIBErrorToString(nlTestSuite * aSuite, void * aContext)
113113
}
114114
#endif // !CHIP_CONFIG_SHORT_ERROR_STR
115115

116+
void TestStatusIBEqualityOperator(nlTestSuite * aSuite, void * /*aContext*/)
117+
{
118+
// Equality against self is true.
119+
StatusIB one;
120+
NL_TEST_ASSERT(aSuite, one == one);
121+
122+
// Default constructors are equal.
123+
NL_TEST_ASSERT(aSuite, one == StatusIB());
124+
125+
// Different imStatus is not equal.
126+
StatusIB with_imstatus(Status::Failure);
127+
NL_TEST_ASSERT(aSuite, one != with_imstatus);
128+
129+
// Same imStatus are equal.
130+
NL_TEST_ASSERT(aSuite, with_imstatus == StatusIB(Status::Failure));
131+
132+
// Same imStatus but different clusterStatus are not equal.
133+
StatusIB with_cluster_status(Status::Failure, /*clusterStatus=*/2);
134+
NL_TEST_ASSERT(aSuite, with_imstatus != with_cluster_status);
135+
136+
// Different imStatus but same clusterStatus are not equal.
137+
NL_TEST_ASSERT(aSuite, with_cluster_status != StatusIB(Status::Success, /*clusterStatus=*/2));
138+
139+
// Same imStatus and clusterStatus are equal.
140+
NL_TEST_ASSERT(aSuite, with_cluster_status == StatusIB(Status::Failure, /*clusterStatus=*/2));
141+
142+
// From same CHIP_ERROR are equal.
143+
StatusIB invalid_argument(CHIP_ERROR_INVALID_ARGUMENT);
144+
NL_TEST_ASSERT(aSuite, invalid_argument == StatusIB(CHIP_ERROR_INVALID_ARGUMENT));
145+
146+
// Different CHIP_ERROR are equal if they are not from kIMClusterStatus or
147+
// kIMGlobalStatus.
148+
NL_TEST_ASSERT(aSuite, invalid_argument == StatusIB(CHIP_ERROR_INCORRECT_STATE));
149+
150+
// Error never equals NO_ERROR
151+
NL_TEST_ASSERT(aSuite, invalid_argument != StatusIB(CHIP_NO_ERROR));
152+
}
153+
116154
// clang-format off
117155
const nlTest sTests[] =
118156
{
119157
NL_TEST_DEF("StatusIBToFromChipError", TestStatusIBToFromChipError),
120158
#if !CHIP_CONFIG_SHORT_ERROR_STR
121159
NL_TEST_DEF("StatusIBErrorToString", TestStatusIBErrorToString),
122160
#endif // !CHIP_CONFIG_SHORT_ERROR_STR
161+
NL_TEST_DEF("StatusIBEqualityOperator", TestStatusIBEqualityOperator),
123162
NL_TEST_SENTINEL()
124163
};
125164
// clang-format on

0 commit comments

Comments
 (0)