]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/intltest/numbertest_stringsegment.cpp
ICU-64232.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / numbertest_stringsegment.cpp
CommitLineData
0f5d89e8
A
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
11static const char16_t* SAMPLE_STRING = u"📻 radio 📻";
12
13void 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
26void StringSegmentTest::testOffset() {
3d1f044b
A
27 // Note: sampleString needs function scope so it is valid while the StringSegment is valid
28 UnicodeString sampleString(SAMPLE_STRING);
29 StringSegment segment(sampleString, false);
0f5d89e8
A
30 assertEquals("Initial Offset", 0, segment.getOffset());
31 segment.adjustOffset(3);
32 assertEquals("Adjust A", 3, segment.getOffset());
33 segment.adjustOffset(2);
34 assertEquals("Adjust B", 5, segment.getOffset());
35 segment.setOffset(4);
36 assertEquals("Set Offset", 4, segment.getOffset());
37}
38
39void StringSegmentTest::testLength() {
3d1f044b
A
40 // Note: sampleString needs function scope so it is valid while the StringSegment is valid
41 UnicodeString sampleString(SAMPLE_STRING);
42 StringSegment segment(sampleString, false);
0f5d89e8
A
43 assertEquals("Initial length", 11, segment.length());
44 segment.adjustOffset(3);
45 assertEquals("Adjust", 8, segment.length());
46 segment.setLength(4);
47 assertEquals("Set Length", 4, segment.length());
48 segment.setOffset(5);
49 assertEquals("After adjust offset", 2, segment.length());
50 segment.resetLength();
51 assertEquals("After reset length", 6, segment.length());
52}
53
54void StringSegmentTest::testCharAt() {
3d1f044b
A
55 // Note: sampleString needs function scope so it is valid while the StringSegment is valid
56 UnicodeString sampleString(SAMPLE_STRING);
57 StringSegment segment(sampleString, false);
0f5d89e8
A
58 assertEquals("Initial", SAMPLE_STRING, segment.toUnicodeString());
59 assertEquals("Initial", SAMPLE_STRING, segment.toTempUnicodeString());
60 segment.adjustOffset(3);
61 assertEquals("After adjust-offset", UnicodeString(u"radio 📻"), segment.toUnicodeString());
62 assertEquals("After adjust-offset", UnicodeString(u"radio 📻"), segment.toTempUnicodeString());
63 segment.setLength(5);
64 assertEquals("After adjust-length", UnicodeString(u"radio"), segment.toUnicodeString());
65 assertEquals("After adjust-length", UnicodeString(u"radio"), segment.toTempUnicodeString());
66}
67
68void StringSegmentTest::testGetCodePoint() {
3d1f044b
A
69 // Note: sampleString needs function scope so it is valid while the StringSegment is valid
70 UnicodeString sampleString(SAMPLE_STRING);
71 StringSegment segment(sampleString, false);
0f5d89e8
A
72 assertEquals("Double-width code point", 0x1F4FB, segment.getCodePoint());
73 segment.setLength(1);
74 assertEquals("Inalid A", -1, segment.getCodePoint());
75 segment.resetLength();
76 segment.adjustOffset(1);
77 assertEquals("Invalid B", -1, segment.getCodePoint());
78 segment.adjustOffset(1);
79 assertEquals("Valid again", 0x20, segment.getCodePoint());
80}
81
82void StringSegmentTest::testCommonPrefixLength() {
3d1f044b
A
83 // Note: sampleString needs function scope so it is valid while the StringSegment is valid
84 UnicodeString sampleString(SAMPLE_STRING);
85 StringSegment segment(sampleString, false);
0f5d89e8
A
86 assertEquals("", 11, segment.getCommonPrefixLength(SAMPLE_STRING));
87 assertEquals("", 4, segment.getCommonPrefixLength(u"📻 r"));
88 assertEquals("", 3, segment.getCommonPrefixLength(u"📻 x"));
89 assertEquals("", 0, segment.getCommonPrefixLength(u"x"));
90 segment.adjustOffset(3);
91 assertEquals("", 0, segment.getCommonPrefixLength(u"RADiO"));
92 assertEquals("", 5, segment.getCommonPrefixLength(u"radio"));
93 assertEquals("", 2, segment.getCommonPrefixLength(u"rafio"));
94 assertEquals("", 0, segment.getCommonPrefixLength(u"fadio"));
95 segment.setLength(3);
96 assertEquals("", 3, segment.getCommonPrefixLength(u"radio"));
97 assertEquals("", 2, segment.getCommonPrefixLength(u"rafio"));
98 assertEquals("", 0, segment.getCommonPrefixLength(u"fadio"));
99 segment.resetLength();
100 segment.setOffset(11); // end of string
101 assertEquals("", 0, segment.getCommonPrefixLength(u"foo"));
102}
103
104#endif