]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/string_segment_test.cpp
1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #include "unicode/utypes.h"
6 #if !UCONFIG_NO_FORMATTING
8 #include "string_segment.h"
11 class StringSegmentTest
: public IntlTest
{
16 void testGetCodePoint();
17 void testCommonPrefixLength();
19 void runIndexedTest(int32_t index
, UBool exec
, const char *&name
, char *par
= 0);
22 static const char16_t* SAMPLE_STRING
= u
"📻 radio 📻";
24 void StringSegmentTest::runIndexedTest(int32_t index
, UBool exec
, const char*&name
, char*) {
26 logln("TestSuite StringSegmentTest: ");
29 TESTCASE_AUTO(testOffset
);
30 TESTCASE_AUTO(testLength
);
31 TESTCASE_AUTO(testCharAt
);
32 TESTCASE_AUTO(testGetCodePoint
);
33 TESTCASE_AUTO(testCommonPrefixLength
);
37 void StringSegmentTest::testOffset() {
38 // Note: sampleString needs function scope so it is valid while the StringSegment is valid
39 UnicodeString
sampleString(SAMPLE_STRING
);
40 StringSegment
segment(sampleString
, false);
41 assertEquals("Initial Offset", 0, segment
.getOffset());
42 segment
.adjustOffset(3);
43 assertEquals("Adjust A", 3, segment
.getOffset());
44 segment
.adjustOffset(2);
45 assertEquals("Adjust B", 5, segment
.getOffset());
47 assertEquals("Set Offset", 4, segment
.getOffset());
50 void StringSegmentTest::testLength() {
51 // Note: sampleString needs function scope so it is valid while the StringSegment is valid
52 UnicodeString
sampleString(SAMPLE_STRING
);
53 StringSegment
segment(sampleString
, false);
54 assertEquals("Initial length", 11, segment
.length());
55 segment
.adjustOffset(3);
56 assertEquals("Adjust", 8, segment
.length());
58 assertEquals("Set Length", 4, segment
.length());
60 assertEquals("After adjust offset", 2, segment
.length());
61 segment
.resetLength();
62 assertEquals("After reset length", 6, segment
.length());
65 void StringSegmentTest::testCharAt() {
66 // Note: sampleString needs function scope so it is valid while the StringSegment is valid
67 UnicodeString
sampleString(SAMPLE_STRING
);
68 StringSegment
segment(sampleString
, false);
69 assertEquals("Initial", SAMPLE_STRING
, segment
.toUnicodeString());
70 assertEquals("Initial", SAMPLE_STRING
, segment
.toTempUnicodeString());
71 segment
.adjustOffset(3);
72 assertEquals("After adjust-offset", UnicodeString(u
"radio 📻"), segment
.toUnicodeString());
73 assertEquals("After adjust-offset", UnicodeString(u
"radio 📻"), segment
.toTempUnicodeString());
75 assertEquals("After adjust-length", UnicodeString(u
"radio"), segment
.toUnicodeString());
76 assertEquals("After adjust-length", UnicodeString(u
"radio"), segment
.toTempUnicodeString());
79 void StringSegmentTest::testGetCodePoint() {
80 // Note: sampleString needs function scope so it is valid while the StringSegment is valid
81 UnicodeString
sampleString(SAMPLE_STRING
);
82 StringSegment
segment(sampleString
, false);
83 assertEquals("Double-width code point", 0x1F4FB, segment
.getCodePoint());
85 assertEquals("Inalid A", -1, segment
.getCodePoint());
86 segment
.resetLength();
87 segment
.adjustOffset(1);
88 assertEquals("Invalid B", -1, segment
.getCodePoint());
89 segment
.adjustOffset(1);
90 assertEquals("Valid again", 0x20, segment
.getCodePoint());
93 void StringSegmentTest::testCommonPrefixLength() {
94 // Note: sampleString needs function scope so it is valid while the StringSegment is valid
95 UnicodeString
sampleString(SAMPLE_STRING
);
96 StringSegment
segment(sampleString
, false);
97 assertEquals("", 11, segment
.getCommonPrefixLength(SAMPLE_STRING
));
98 assertEquals("", 4, segment
.getCommonPrefixLength(u
"📻 r"));
99 assertEquals("", 3, segment
.getCommonPrefixLength(u
"📻 x"));
100 assertEquals("", 0, segment
.getCommonPrefixLength(u
"x"));
101 segment
.adjustOffset(3);
102 assertEquals("", 0, segment
.getCommonPrefixLength(u
"RADiO"));
103 assertEquals("", 5, segment
.getCommonPrefixLength(u
"radio"));
104 assertEquals("", 2, segment
.getCommonPrefixLength(u
"rafio"));
105 assertEquals("", 0, segment
.getCommonPrefixLength(u
"fadio"));
106 segment
.setLength(3);
107 assertEquals("", 3, segment
.getCommonPrefixLength(u
"radio"));
108 assertEquals("", 2, segment
.getCommonPrefixLength(u
"rafio"));
109 assertEquals("", 0, segment
.getCommonPrefixLength(u
"fadio"));
110 segment
.resetLength();
111 segment
.setOffset(11); // end of string
112 assertEquals("", 0, segment
.getCommonPrefixLength(u
"foo"));
116 extern IntlTest
*createStringSegmentTest() {
117 return new StringSegmentTest();