Skip to content

Commit

Permalink
ICU-22879 Test the collator predicates in maps
Browse files Browse the repository at this point in the history
  • Loading branch information
eggrobin authored and roubert committed Sep 17, 2024
1 parent b160fea commit bfc5354
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions icu4c/source/test/intltest/collationtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* created by: Markus W. Scherer
*/

#include <map>
#include <string>
#include <vector>

Expand Down Expand Up @@ -88,6 +89,7 @@ class CollationTest : public IntlTest {
void TestUCollatorPredicates();
void TestCollatorPredicateTypes();
void TestUCollatorPredicateTypes();
void TestCollatorMap();

private:
void checkFCD(const char *name, CollationIterator &ci, CodePointIterator &cpi);
Expand Down Expand Up @@ -165,6 +167,7 @@ void CollationTest::runIndexedTest(int32_t index, UBool exec, const char *&name,
TESTCASE_AUTO(TestUCollatorPredicates);
TESTCASE_AUTO(TestCollatorPredicateTypes);
TESTCASE_AUTO(TestUCollatorPredicateTypes);
TESTCASE_AUTO(TestCollatorMap);
TESTCASE_AUTO_END;
}

Expand Down Expand Up @@ -2080,4 +2083,43 @@ void CollationTest::TestUCollatorPredicateTypes() {
assertTrue("string", equal_to(std::string{TEXT_CHAR}, TEXT_CHAR));
}

void CollationTest::TestCollatorMap() {
using namespace U_HEADER_NESTED_NAMESPACE;
IcuTestErrorCode status(*this, "TestCollatorMap");
setRootCollator(status);
status.assertSuccess();
coll->setStrength(Collator::PRIMARY);
std::map<std::string, int, decltype(coll->less())> m{coll->less()};
++m["a"];
++m["b"];
++m["A"];
assertEquals("m.size()", 2, m.size());
assertEquals(R"(m["a"])", 2, m["a"]);

std::map<std::u16string, int, collator::less> u16m{collator::less(coll->toUCollator())};
++u16m[u"a"];
++u16m[u"b"];
++u16m[u"A"];
assertEquals("u16m.size()", 2, u16m.size());
assertEquals(R"(u16m["a"])", 2, u16m[u"a"]);

#if defined(__cpp_char8_t)
std::map<std::u8string, int, collator::less> u8m{collator::less(coll->toUCollator())};
++u8m[u8"a"];
++u8m[u8"b"];
++u8m[u8"A"];
assertEquals("u8m.size()", 2, u8m.size());
assertEquals(R"(u8m["a"])", 2, u8m[u8"a"]);
#endif

std::map<UnicodeString, int, collator::less> um{collator::less(coll->toUCollator())};
// Only UnicodeString allows heterogeneous lookup across encodings:
++um[u"a"];
++um["b"];
// ++um[u8"A"]; // TODO(egg): Should this be legal?
++um["A"];
assertEquals("u16m.size()", 2, um.size());
assertEquals(R"(u16m["a"])", 2, um[u"a"]);
}

#endif // !UCONFIG_NO_COLLATION

0 comments on commit bfc5354

Please sign in to comment.