ICU-22527 add Measure::operator!=()

This commit is contained in:
Markus Scherer 2023-10-02 14:15:23 -07:00
parent e73d9736c9
commit f6d09d514d
2 changed files with 45 additions and 0 deletions

View file

@ -89,6 +89,16 @@ class U_I18N_API Measure: public UObject {
*/
bool operator==(const UObject& other) const;
#ifndef U_HIDE_DRAFT_API
/**
* Inequality operator. Returns true if this object is not equal to the other object.
* @param other the object to compare with
* @return true if the objects are not equal
* @draft ICU 74
*/
inline bool operator!=(const UObject& other) const { return !operator==(other); }
#endif // U_HIDE_DRAFT_API
/**
* Return a reference to the numeric value of this object. The
* numeric value may be of any numeric type supported by

View file

@ -99,6 +99,7 @@ private:
void Test21060_AddressSanitizerProblem();
void Test21223_FrenchDuration();
void TestInternalMeasureUnitImpl();
void TestMeasureEquality();
void verifyFormat(
const char *description,
@ -236,6 +237,7 @@ void MeasureFormatTest::runIndexedTest(
TESTCASE_AUTO(Test21060_AddressSanitizerProblem);
TESTCASE_AUTO(Test21223_FrenchDuration);
TESTCASE_AUTO(TestInternalMeasureUnitImpl);
TESTCASE_AUTO(TestMeasureEquality);
TESTCASE_AUTO_END;
}
@ -6228,6 +6230,39 @@ void MeasureFormatTest::TestInternalMeasureUnitImpl() {
std::move(m2m).build(status).getIdentifier());
}
void MeasureFormatTest::TestMeasureEquality() {
IcuTestErrorCode errorCode(*this, "TestMeasureEquality");
Measure measures[] = {
{ 1., MeasureUnit::createLiter(errorCode), errorCode },
{ 1., MeasureUnit::createLiter(errorCode), errorCode },
{ 2., MeasureUnit::createLiter(errorCode), errorCode },
{ 1., MeasureUnit::createGram(errorCode), errorCode }
};
static const char *const names[] = { "1 liter", "another liter", "2 liters", "1 gram" };
for (int32_t i = 0; i < UPRV_LENGTHOF(measures); ++i) {
for (int32_t j = 0; j < UPRV_LENGTHOF(measures); ++j) {
const Measure &a = measures[i];
const UObject &b = measures[j]; // UObject for "other"
std::string eq = std::string(names[i]) + std::string(" == ") + std::string(names[j]);
std::string ne = std::string(names[i]) + std::string(" != ") + std::string(names[j]);
// 1l = 1l
bool expectedEquals = i == j || (i <= 1 && j <= 1);
assertEquals(eq.c_str(), expectedEquals, a == b);
assertEquals(ne.c_str(), !expectedEquals, a != b);
}
}
UnicodeString s(u"?");
for (int32_t i = 0; i < UPRV_LENGTHOF(measures); ++i) {
const Measure &a = measures[i];
std::string eq = std::string(names[i]) + std::string(" == UnicodeString");
std::string ne = std::string(names[i]) + std::string(" != UnicodeString");
assertEquals(eq.c_str(), false, a == s);
assertEquals(ne.c_str(), true, a != s);
}
}
void MeasureFormatTest::verifyFieldPosition(
const char *description,