1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3
4 #include "unicode/utypes.h"
5
6 #if !UCONFIG_NO_FORMATTING
7
8 #include "string_segment.h"
9 #include "intltest.h"
10
11 class StringSegmentTest : public IntlTest {
12 public:
13 void testOffset();
14 void testLength();
15 void testCharAt();
16 void testGetCodePoint();
17 void testCommonPrefixLength();
18
19 void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = 0);
20 };
21
22 static const char16_t* SAMPLE_STRING = u" radio ";
23
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)24 void StringSegmentTest::runIndexedTest(int32_t index, UBool exec, const char*&name, char*) {
25 if (exec) {
26 logln("TestSuite StringSegmentTest: ");
27 }
28 TESTCASE_AUTO_BEGIN;
29 TESTCASE_AUTO(testOffset);
30 TESTCASE_AUTO(testLength);
31 TESTCASE_AUTO(testCharAt);
32 TESTCASE_AUTO(testGetCodePoint);
33 TESTCASE_AUTO(testCommonPrefixLength);
34 TESTCASE_AUTO_END;
35 }
36
testOffset()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());
46 segment.setOffset(4);
47 assertEquals("Set Offset", 4, segment.getOffset());
48 }
49
testLength()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());
57 segment.setLength(4);
58 assertEquals("Set Length", 4, segment.length());
59 segment.setOffset(5);
60 assertEquals("After adjust offset", 2, segment.length());
61 segment.resetLength();
62 assertEquals("After reset length", 6, segment.length());
63 }
64
testCharAt()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());
74 segment.setLength(5);
75 assertEquals("After adjust-length", UnicodeString(u"radio"), segment.toUnicodeString());
76 assertEquals("After adjust-length", UnicodeString(u"radio"), segment.toTempUnicodeString());
77 }
78
testGetCodePoint()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());
84 segment.setLength(1);
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());
91 }
92
testCommonPrefixLength()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"));
113 }
114
115
createStringSegmentTest()116 extern IntlTest *createStringSegmentTest() {
117 return new StringSegmentTest();
118 }
119
120 #endif
121