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 "numbertest.h"
9 #include "numparse_stringsegment.h"
10 
11 static const char16_t* SAMPLE_STRING = u"�� radio ��";
12 
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)13 void StringSegmentTest::runIndexedTest(int32_t index, UBool exec, const char*&name, char*) {
14     if (exec) {
15         logln("TestSuite StringSegmentTest: ");
16     }
17     TESTCASE_AUTO_BEGIN;
18         TESTCASE_AUTO(testOffset);
19         TESTCASE_AUTO(testLength);
20         TESTCASE_AUTO(testCharAt);
21         TESTCASE_AUTO(testGetCodePoint);
22         TESTCASE_AUTO(testCommonPrefixLength);
23     TESTCASE_AUTO_END;
24 }
25 
testOffset()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());
33     segment.setOffset(4);
34     assertEquals("Set Offset", 4, segment.getOffset());
35 }
36 
testLength()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());
42     segment.setLength(4);
43     assertEquals("Set Length", 4, segment.length());
44     segment.setOffset(5);
45     assertEquals("After adjust offset", 2, segment.length());
46     segment.resetLength();
47     assertEquals("After reset length", 6, segment.length());
48 }
49 
testCharAt()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());
57     segment.setLength(5);
58     assertEquals("After adjust-length", UnicodeString(u"radio"), segment.toUnicodeString());
59     assertEquals("After adjust-length", UnicodeString(u"radio"), segment.toTempUnicodeString());
60 }
61 
testGetCodePoint()62 void StringSegmentTest::testGetCodePoint() {
63     StringSegment segment(SAMPLE_STRING, false);
64     assertEquals("Double-width code point", 0x1F4FB, segment.getCodePoint());
65     segment.setLength(1);
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());
72 }
73 
testCommonPrefixLength()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"));
85     segment.setLength(3);
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"));
92 }
93 
94 #endif
95