Skip to content

Commit

Permalink
ICU-22954 header-only-test USet C++ iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
markusicu committed Dec 20, 2024
1 parent 320220e commit e3bc073
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 173 deletions.
2 changes: 1 addition & 1 deletion icu4c/source/test/intltest/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ numbertest_parse.o numbertest_doubleconversion.o numbertest_skeletons.o \
static_unisets_test.o numfmtdatadriventest.o numbertest_range.o erarulestest.o \
formattedvaluetest.o formatted_string_builder_test.o numbertest_permutation.o \
units_data_test.o units_router_test.o units_test.o displayoptions_test.o \
numbertest_simple.o uchar_type_build_test.o
numbertest_simple.o uchar_type_build_test.o usetheaderonlytest.o

DEPS = $(OBJECTS:.o=.d)

Expand Down
1 change: 1 addition & 0 deletions icu4c/source/test/intltest/intltest.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@
<ClCompile Include="units_router_test.cpp" />
<ClCompile Include="units_test.cpp" />
<ClCompile Include="uchar_type_build_test.cpp" />
<ClCompile Include="usetheaderonlytest.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="colldata.h" />
Expand Down
15 changes: 12 additions & 3 deletions icu4c/source/test/intltest/intltest.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,18 @@
<ClCompile Include="uchar_type_build_test.cpp">
<Filter>configuration</Filter>
</ClCompile>
<ClCompile Include="messageformat2test.cpp" />
<ClCompile Include="messageformat2test_custom.cpp" />
<ClCompile Include="messageformat2test_read_json.cpp" />
<ClCompile Include="messageformat2test.cpp">
<Filter>formatting</Filter>
</ClCompile>
<ClCompile Include="messageformat2test_custom.cpp">
<Filter>formatting</Filter>
</ClCompile>
<ClCompile Include="messageformat2test_read_json.cpp">
<Filter>formatting</Filter>
</ClCompile>
<ClCompile Include="usetheaderonlytest.cpp">
<Filter>misc</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="itrbbi.h">
Expand Down
2 changes: 2 additions & 0 deletions icu4c/source/test/intltest/itutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "usettest.h"

extern IntlTest *createBytesTrieTest();
extern IntlTest *createUSetHeaderOnlyTest();
extern IntlTest *createLocaleMatcherTest();
static IntlTest *createLocalPointerTest();
extern IntlTest *createUCharsTrieTest();
Expand Down Expand Up @@ -82,6 +83,7 @@ void IntlTestUtilities::runIndexedTest( int32_t index, UBool exec, const char* &
TESTCASE_AUTO_CLASS(LocaleBuilderTest);
TESTCASE_AUTO_CREATE_CLASS(LocaleMatcherTest);
TESTCASE_AUTO_CREATE_CLASS(UHashTest);
TESTCASE_AUTO_CREATE_CLASS(USetHeaderOnlyTest);
TESTCASE_AUTO_END;
}

Expand Down
219 changes: 219 additions & 0 deletions icu4c/source/test/intltest/usetheaderonlytest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
// © 2024 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html

// usetheaderonlytest.cpp
// created: 2024dec11 Markus W. Scherer

#include <string>

// Test header-only ICU C++ APIs. Do not use other ICU C++ APIs.
// Non-default configuration:
#define U_SHOW_CPLUSPLUS_API 0
// Default configuration:
// #define U_SHOW_CPLUSPLUS_HEADER_API 1

#include "unicode/utypes.h"
#include "unicode/uset.h"
#include "unicode/utf.h"
#include "unicode/utf16.h"
#include "intltest.h"

class USetHeaderOnlyTest : public IntlTest {
public:
USetHeaderOnlyTest() = default;

void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=nullptr) override;

void TestUSetCodePointIterator();
void TestUSetRangeIterator();
void TestUSetStringIterator();
void TestUSetElementIterator();
};

extern IntlTest *createUSetHeaderOnlyTest() {
return new USetHeaderOnlyTest();
}

void USetHeaderOnlyTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
if(exec) {
logln("TestSuite USetHeaderOnlyTest: ");
}
TESTCASE_AUTO_BEGIN;
TESTCASE_AUTO(TestUSetCodePointIterator);
TESTCASE_AUTO(TestUSetRangeIterator);
TESTCASE_AUTO(TestUSetStringIterator);
TESTCASE_AUTO(TestUSetElementIterator);
TESTCASE_AUTO_END;
}

std::u16string cpString(UChar32 c) {
if (U_IS_BMP(c)) {
return {static_cast<char16_t>(c)};
} else {
return {U16_LEAD(c), U16_TRAIL(c)};
}
}

void USetHeaderOnlyTest::TestUSetCodePointIterator() {
IcuTestErrorCode errorCode(*this, "TestUSetCodePointIterator");
using U_HEADER_NESTED_NAMESPACE::USetCodePoints;
LocalUSetPointer uset(uset_openPattern(u"[abcçカ🚴]", -1, errorCode));
std::u16string result;
for (UChar32 c : USetCodePoints(uset.getAlias())) {
// Commented-out sample code for pasting into the API docs.
// printf("uset.codePoint U+%04lx\n", (long)c);
result.append(u" ").append(cpString(c));
}
assertEquals(WHERE, u" a b c ç カ 🚴", result);

USetCodePoints range1(uset.getAlias());
auto range2(range1); // copy constructor
auto iter = range1.begin();
auto limit = range2.end();
// operator* with pre- and post-increment
assertEquals(WHERE, u'a', *iter);
++iter;
assertEquals(WHERE, u'b', *iter);
assertEquals(WHERE, u'c', *++iter);
auto iter2(iter); // copy constructor
assertEquals(WHERE, u'c', *iter2++);
assertEquals(WHERE, u'ç', *iter2++);
assertEquals(WHERE, u'', *iter2);
assertTrue(WHERE, ++iter2 != limit);
auto iter3(iter2++);
assertEquals(WHERE, U'🚴', *iter3);
assertTrue(WHERE, iter2 == limit);
}

void USetHeaderOnlyTest::TestUSetRangeIterator() {
IcuTestErrorCode errorCode(*this, "TestUSetRangeIterator");
using U_HEADER_NESTED_NAMESPACE::USetRanges;
using U_HEADER_NESTED_NAMESPACE::CodePointRange;
LocalUSetPointer uset(uset_openPattern(u"[abcçカ🚴]", -1, errorCode));
std::u16string result;
for (auto [start, end] : USetRanges(uset.getAlias())) {
// Commented-out sample code for pasting into the API docs.
// printf("uset.range U+%04lx..U+%04lx\n", (long)start, (long)end);
result.append(u" ").append(cpString(start)).append(u"-").append(cpString(end));
}
assertEquals(WHERE, u" a-c ç-ç カ-カ 🚴-🚴", result);
result.clear();
for (auto range : USetRanges(uset.getAlias())) {
for (UChar32 c : range) {
// Commented-out sample code for pasting into the API docs.
// printf("uset.range.c U+%04lx\n", (long)c);
result.append(u" ").append(cpString(c));
}
result.append(u" |");
}
assertEquals(WHERE, u" a b c | ç | カ | 🚴 |", result);

USetRanges range1(uset.getAlias());
auto range2(range1); // copy constructor
auto iter = range1.begin();
auto limit = range2.end();
// operator* with pre- and post-increment
{
auto cpRange = *iter;
assertEquals(WHERE, u'a', cpRange.rangeStart);
assertEquals(WHERE, u'c', cpRange.rangeEnd);
assertEquals(WHERE, 3, cpRange.size());
auto cpRange2(cpRange);
auto cpIter = cpRange.begin();
auto cpLimit = cpRange2.end();
assertEquals(WHERE, u'a', *cpIter++);
assertEquals(WHERE, u'b', *cpIter);
assertTrue(WHERE, cpIter != cpLimit);
CodePointRange::iterator cpIter2(u'b'); // public constructor
assertTrue(WHERE, cpIter == cpIter2);
assertEquals(WHERE, u'c', *++cpIter);
assertTrue(WHERE, cpIter != cpIter2);
assertTrue(WHERE, ++cpIter == cpLimit);
}
++iter;
auto iter2(iter); // copy constructor
assertEquals(WHERE, u'ç', (*iter2).rangeStart);
assertEquals(WHERE, u'ç', (*iter2).rangeEnd);
assertEquals(WHERE, 1, (*iter2).size());
assertEquals(WHERE, u'ç', (*iter2++).rangeStart);
assertEquals(WHERE, u'', (*iter2).rangeStart);
assertTrue(WHERE, ++iter2 != limit);
auto iter3(iter2++);
assertEquals(WHERE, U'🚴', (*iter3).rangeStart);
assertTrue(WHERE, iter2 == limit);

{
CodePointRange cpRange(u'h', u'k'); // public constructor
// FYI: currently no operator==
assertEquals(WHERE, u'h', cpRange.rangeStart);
assertEquals(WHERE, u'k', cpRange.rangeEnd);
assertEquals(WHERE, 4, cpRange.size());
assertEquals(WHERE, u'i', *++(cpRange.begin()));
}
}

void USetHeaderOnlyTest::TestUSetStringIterator() {
IcuTestErrorCode errorCode(*this, "TestUSetStringIterator");
using U_HEADER_NESTED_NAMESPACE::USetStrings;
LocalUSetPointer uset(uset_openPattern(u"[abcçカ🚴{}{abc}{de}]", -1, errorCode));
std::u16string result;
for (auto s : USetStrings(uset.getAlias())) {
// Commented-out sample code for pasting into the API docs.
// Needs U_SHOW_CPLUSPLUS_API=1 for UnicodeString.
// UnicodeString us(s);
// std::string u8;
// printf("uset.string length %ld \"%s\"\n", (long)s.length(), us.toUTF8String(u8).c_str());
result.append(u" \"").append(s).append(u"\"");
}
assertEquals(WHERE, uR"( "" "abc" "de")", result);

USetStrings range1(uset.getAlias());
auto range2(range1); // copy constructor
auto iter = range1.begin();
auto limit = range2.end();
// operator* with pre- and post-increment
assertEquals(WHERE, u"", *iter);
assertEquals(WHERE, u"abc", *++iter);
auto iter2(iter); // copy constructor
assertEquals(WHERE, u"abc", *iter2++);
assertTrue(WHERE, iter2 != limit);
auto iter3(iter2++);
assertEquals(WHERE, u"de", *iter3);
assertTrue(WHERE, iter2 == limit);
}

void USetHeaderOnlyTest::TestUSetElementIterator() {
IcuTestErrorCode errorCode(*this, "TestUSetElementIterator");
using U_HEADER_NESTED_NAMESPACE::USetElements;
LocalUSetPointer uset(uset_openPattern(u"[abcçカ🚴{}{abc}{de}]", -1, errorCode));
std::u16string result;
for (auto el : USetElements(uset.getAlias())) {
// Commented-out sample code for pasting into the API docs.
// Needs U_SHOW_CPLUSPLUS_API=1 for UnicodeString.
// UnicodeString us(el);
// std::string u8;
// printf("uset.string length %ld \"%s\"\n", (long)us.length(), us.toUTF8String(u8).c_str());
result.append(u" \"").append(el).append(u"\"");
}
assertEquals(WHERE, uR"( "a" "b" "c" "ç" "カ" "🚴" "" "abc" "de")", result);

USetElements range1(uset.getAlias());
auto range2(range1); // copy constructor
auto iter = range1.begin();
auto limit = range2.end();
// operator* with pre- and post-increment
assertEquals(WHERE, u"a", *iter);
++iter;
assertEquals(WHERE, u"b", *iter);
assertEquals(WHERE, u"c", *++iter);
auto iter2(iter); // copy constructor
assertEquals(WHERE, u"c", *iter2++);
// skip çカ🚴
++++++iter2;
assertEquals(WHERE, u"", *iter2++);
assertEquals(WHERE, u"abc", *iter2);
assertTrue(WHERE, ++iter2 != limit);
auto iter3(iter2++);
assertEquals(WHERE, u"de", *iter3);
assertTrue(WHERE, iter2 == limit);
}
Loading

0 comments on commit e3bc073

Please sign in to comment.