2 *******************************************************************************
4 * Copyright (C) 2016 and later: Unicode, Inc. and others.
5 * License & terms of use: http://www.unicode.org/copyright.html#License
7 *******************************************************************************
8 *******************************************************************************
10 * Copyright (C) 2002-2011, International Business Machines
11 * Corporation and others. All Rights Reserved.
13 *******************************************************************************
16 #include "unicode/uchriter.h"
17 #include "unicode/schriter.h"
18 #include "unicode/ustring.h"
20 #include <unicode/brkiter.h>
21 #include <unicode/ustdio.h>
26 void printUnicodeString(const UnicodeString &s)
28 u_fprintf(out, "%S", &s);
31 void printUChar(UChar32 ch)
34 u_fprintf(out, "%C", (UChar) ch);
35 } else if (ch == CharacterIterator::DONE) {
36 u_fprintf(out, "[CharacterIterator::DONE = 0xFFFF]");
38 u_fprintf(out, "[%X]", ch);
46 void TestStringiter();
49 void Test::TestUChariter() {
50 const char testChars[] = "Now is the time for all good men to come "
51 "to the aid of their country.";
53 UnicodeString testString(testChars,"");
54 const UChar *testText = testString.getTerminatedBuffer();
56 UCharCharacterIterator iter(testText, u_strlen(testText));
57 UCharCharacterIterator* test2 = (UCharCharacterIterator*)iter.clone();
59 u_fprintf(out, "testText = %s", testChars);
61 if (iter != *test2 ) {
62 u_fprintf(out, "clone() or equals() failed: Two clones tested unequal\n");
65 UnicodeString result1, result2;
66 // getting and comparing the text within the iterators
67 iter.getText(result1);
68 test2->getText(result2);
69 if (result1 != result2) {
70 u_fprintf(out, "iter.getText() != clone.getText()\n");
75 // Demonstrates seeking forward using the iterator.
76 u_fprintf(out, "Forward = ");
78 UChar c = iter.first();
79 printUChar(c); // The first char
82 if (iter.startIndex() != 0 || iter.endIndex() != u_strlen(testText)) {
83 u_fprintf(out, "startIndex() or endIndex() failed\n");
87 // Testing forward iteration...
89 if (c == CharacterIterator::DONE && i != u_strlen(testText)) {
90 u_fprintf(out, "Iterator reached end prematurely");
92 else if (c != testText[i]) {
93 u_fprintf(out, "Character mismatch at position %d\n" + i);
95 if (iter.current() != c) {
96 u_fprintf(out, "current() isn't working right");
98 if (iter.getIndex() != i) {
99 u_fprintf(out, "getIndex() isn't working right\n");
101 if (c != CharacterIterator::DONE) {
109 } while (c != CharacterIterator::DONE);
112 u_fprintf(out, "\n");
116 void Test::TestStringiter() {
117 const char testChars[] = "Now is the time for all good men to come "
118 "to the aid of their country.";
120 UnicodeString testString(testChars,"");
121 const UChar *testText = testString.getTerminatedBuffer();
123 StringCharacterIterator iter(testText, u_strlen(testText));
124 StringCharacterIterator* test2 = (StringCharacterIterator*)iter.clone();
126 if (iter != *test2 ) {
127 u_fprintf(out, "clone() or equals() failed: Two clones tested unequal\n");
130 UnicodeString result1, result2;
131 // getting and comparing the text within the iterators
132 iter.getText(result1);
133 test2->getText(result2);
134 if (result1 != result2) {
135 u_fprintf(out, "getText() failed\n");
138 u_fprintf(out, "Backwards: ");
140 UChar c = iter.last();
141 int32_t i = iter.endIndex();
144 i--; // already printed out the last char
146 if (iter.startIndex() != 0 || iter.endIndex() != u_strlen(testText)) {
147 u_fprintf(out, "startIndex() or endIndex() failed\n");
150 // Testing backward iteration over a range...
152 if (c == CharacterIterator::DONE) {
153 u_fprintf(out, "Iterator reached end prematurely\n");
155 else if (c != testText[i]) {
156 u_fprintf(out, "Character mismatch at position %d\n", i);
158 if (iter.current() != c) {
159 u_fprintf(out, "current() isn't working right\n");
161 if (iter.getIndex() != i) {
162 u_fprintf(out, "getIndex() isn't working right [%d should be %d]\n", iter.getIndex(), i);
164 if (c != CharacterIterator::DONE) {
171 } while (c != CharacterIterator::DONE);
173 u_fprintf(out, "\n");
177 /* Creating and using text boundaries */
180 UErrorCode status = U_ZERO_ERROR;
182 out = u_finit(stdout, NULL, NULL);
184 u_fprintf(out, "ICU Iteration Sample Program (C++)\n\n");
188 u_fprintf(out, "\n");
189 u_fprintf(out, "Test::TestUCharIter()\n");
193 u_fprintf(out, "-----\n");
194 u_fprintf(out, "Test::TestStringchariter()\n");
198 u_fprintf(out, "-----\n");