summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/libcore/java/text/BreakIteratorTest.java
blob: de2ae529b369062d3d0878993a837bb63a42383d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package libcore.java.text;

import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.Locale;

public class BreakIteratorTest extends junit.framework.TestCase {
    BreakIterator iterator;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        iterator = BreakIterator.getCharacterInstance(Locale.US);
    }

    public void testGetAvailableLocales() {
        Locale[] locales = BreakIterator.getAvailableLocales();
        assertTrue("Array available locales is null", locales != null);
        assertTrue("Array available locales is 0-length",
                (locales != null && locales.length != 0));
        boolean found = false;
        for (Locale l : locales) {
            if (l.equals(Locale.US)) {
                // expected
                found = true;
            }
        }
        assertTrue("At least locale " + Locale.US + " must be presented", found);
    }

    public void testGetWordInstanceLocale() {
        BreakIterator it1 = BreakIterator.getWordInstance(Locale.CANADA_FRENCH);
        assertTrue("Incorrect BreakIterator", it1 != BreakIterator.getWordInstance());
        BreakIterator it2 = BreakIterator.getWordInstance(new Locale("bad locale"));
        assertTrue("Incorrect BreakIterator", it2 != BreakIterator.getWordInstance());
    }

    // http://b/7307154 - we used to pin an unbounded number of char[]s, relying on finalization.
    public void testStress() throws Exception {
        char[] cs = { 'a' };
        for (int i = 0; i < 4096; ++i) {
            BreakIterator it = BreakIterator.getWordInstance(Locale.US);
            it.setText(new String(cs));
        }
    }

    public void testWordBoundaries() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 1024; ++i) {
            if (i > 0) {
                sb.append(' ');
            }
            sb.append("12345");
        }
        String s = sb.toString();

        BreakIterator it = BreakIterator.getWordInstance(Locale.US);
        it.setText(s);

        // Check we're not leaking global references. 2048 would bust the VM's hard-coded limit.
        for (int i = 0; i < 2048; ++i) {
            it.setText(s);
        }

        BreakIterator clone = (BreakIterator) it.clone();

        assertExpectedWordBoundaries(it, s);
        assertExpectedWordBoundaries(clone, s);
    }

    private void assertExpectedWordBoundaries(BreakIterator it, String s) {
        int expectedPos = 0;
        int pos = it.first();
        assertEquals(expectedPos, pos);
        while (pos != BreakIterator.DONE) {
            expectedPos += 5; // The five characters until the end of this word.
            pos = it.next();
            assertEquals(expectedPos, pos);

            expectedPos += 1; // The space before the start of the next word...
            if (expectedPos > s.length()) {
                expectedPos = BreakIterator.DONE; // ...unless we're done.
            }
            pos = it.next();
            assertEquals(expectedPos, pos);
        }
    }

    public void testIsBoundary() {
        BreakIterator it = BreakIterator.getCharacterInstance(Locale.US);
        it.setText("hello");

        try {
            it.isBoundary(-1);
            fail();
        } catch (IllegalArgumentException expected) {
            // Note that this exception is not listed in the Java API documentation
        }

        assertTrue(it.isBoundary(0));
        assertTrue(it.isBoundary(1));
        assertTrue(it.isBoundary(4));
        assertTrue(it.isBoundary(5));

        try {
            it.isBoundary(6);
            fail();
        } catch (IllegalArgumentException expected) {
            // Note that this exception is not listed in the Java API documentation
        }
    }

    public void testFollowing() {
        BreakIterator it = BreakIterator.getCharacterInstance(Locale.US);
        it.setText("hello");

        try {
            it.following(-1);
            fail();
        } catch (IllegalArgumentException expected) {
            // Expected exception
        }

        assertEquals(1, it.following(0));
        assertEquals(2, it.following(1));
        assertEquals(5, it.following(4));
        assertEquals(BreakIterator.DONE, it.following(5));

        try {
            it.following(6);
            fail();
        } catch (IllegalArgumentException expected) {
            // Expected exception
        }
    }

    public void testPreceding() {
        BreakIterator it = BreakIterator.getCharacterInstance(Locale.US);
        it.setText("hello");

        try {
            it.preceding(-1);
            fail();
        } catch (IllegalArgumentException expected) {
            // Expected exception
        }

        assertEquals(BreakIterator.DONE, it.preceding(0));
        assertEquals(0, it.preceding(1));
        assertEquals(4, it.preceding(5));

        try {
            it.preceding(6);
            fail();
        } catch (IllegalArgumentException expected) {
            // Expected exception
        }
    }
}