]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/numbertest_stringsegment.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 "numbertest.h"
9 #include "numparse_stringsegment.h"
11 static const char16_t* SAMPLE_STRING
= u
"📻 radio 📻";
13 void StringSegmentTest::runIndexedTest(int32_t index
, UBool exec
, const char*&name
, char*) {
15 logln("TestSuite StringSegmentTest: ");
18 TESTCASE_AUTO(testOffset
);
19 TESTCASE_AUTO(testLength
);
20 TESTCASE_AUTO(testCharAt
);
21 TESTCASE_AUTO(testGetCodePoint
);
22 TESTCASE_AUTO(testCommonPrefixLength
);
26 void StringSegmentTest::testOffset() {
27 StringSegment
segment(SAMPLE_STRING
, false);
28 assertEquals("Initial Offset", 0, segment
.getOffset());
29 segment
.adjustOffset(3);
30 assertEquals("Adjust A", 3, segment
.getOffset());
31 segment
.adjustOffset(2);
32 assertEquals("Adjust B", 5, segment
.getOffset());
34 assertEquals("Set Offset", 4, segment
.getOffset());
37 void StringSegmentTest::testLength() {
38 StringSegment
segment(SAMPLE_STRING
, false);
39 assertEquals("Initial length", 11, segment
.length());
40 segment
.adjustOffset(3);
41 assertEquals("Adjust", 8, segment
.length());
43 assertEquals("Set Length", 4, segment
.length());
45 assertEquals("After adjust offset", 2, segment
.length());
46 segment
.resetLength();
47 assertEquals("After reset length", 6, segment
.length());
50 void StringSegmentTest::testCharAt() {
51 StringSegment
segment(SAMPLE_STRING
, false);
52 assertEquals("Initial", SAMPLE_STRING
, segment
.toUnicodeString());
53 assertEquals("Initial", SAMPLE_STRING
, segment
.toTempUnicodeString());
54 segment
.adjustOffset(3);
55 assertEquals("After adjust-offset", UnicodeString(u
"radio 📻"), segment
.toUnicodeString());
56 assertEquals("After adjust-offset", UnicodeString(u
"radio 📻"), segment
.toTempUnicodeString());
58 assertEquals("After adjust-length", UnicodeString(u
"radio"), segment
.toUnicodeString());
59 assertEquals("After adjust-length", UnicodeString(u
"radio"), segment
.toTempUnicodeString());
62 void StringSegmentTest::testGetCodePoint() {
63 StringSegment
segment(SAMPLE_STRING
, false);
64 assertEquals("Double-width code point", 0x1F4FB, segment
.getCodePoint());
66 assertEquals("Inalid A", -1, segment
.getCodePoint());
67 segment
.resetLength();
68 segment
.adjustOffset(1);
69 assertEquals("Invalid B", -1, segment
.getCodePoint());
70 segment
.adjustOffset(1);
71 assertEquals("Valid again", 0x20, segment
.getCodePoint());
74 void StringSegmentTest::testCommonPrefixLength() {
75 StringSegment
segment(SAMPLE_STRING
, false);
76 assertEquals("", 11, segment
.getCommonPrefixLength(SAMPLE_STRING
));
77 assertEquals("", 4, segment
.getCommonPrefixLength(u
"📻 r"));
78 assertEquals("", 3, segment
.getCommonPrefixLength(u
"📻 x"));
79 assertEquals("", 0, segment
.getCommonPrefixLength(u
"x"));
80 segment
.adjustOffset(3);
81 assertEquals("", 0, segment
.getCommonPrefixLength(u
"RADiO"));
82 assertEquals("", 5, segment
.getCommonPrefixLength(u
"radio"));
83 assertEquals("", 2, segment
.getCommonPrefixLength(u
"rafio"));
84 assertEquals("", 0, segment
.getCommonPrefixLength(u
"fadio"));
86 assertEquals("", 3, segment
.getCommonPrefixLength(u
"radio"));
87 assertEquals("", 2, segment
.getCommonPrefixLength(u
"rafio"));
88 assertEquals("", 0, segment
.getCommonPrefixLength(u
"fadio"));
89 segment
.resetLength();
90 segment
.setOffset(11); // end of string
91 assertEquals("", 0, segment
.getCommonPrefixLength(u
"foo"));