1
package org.apache.lucene.analysis.tokenattributes;
4
* Licensed to the Apache Software Foundation (ASF) under one or more
5
* contributor license agreements. See the NOTICE file distributed with
6
* this work for additional information regarding copyright ownership.
7
* The ASF licenses this file to You under the Apache License, Version 2.0
8
* (the "License"); you may not use this file except in compliance with
9
* the License. You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
20
import org.apache.lucene.util.LuceneTestCase;
21
import org.apache.lucene.util._TestUtil;
22
import java.nio.CharBuffer;
23
import java.util.Collections;
24
import java.util.Formatter;
25
import java.util.Locale;
26
import java.util.regex.Pattern;
28
public class TestCharTermAttributeImpl extends LuceneTestCase {
30
public void testResize() {
31
CharTermAttributeImpl t = new CharTermAttributeImpl();
32
char[] content = "hello".toCharArray();
33
t.copyBuffer(content, 0, content.length);
34
for (int i = 0; i < 2000; i++)
37
assertTrue(i <= t.buffer().length);
38
assertEquals("hello", t.toString());
42
public void testGrow() {
43
CharTermAttributeImpl t = new CharTermAttributeImpl();
44
StringBuilder buf = new StringBuilder("ab");
45
for (int i = 0; i < 20; i++)
47
char[] content = buf.toString().toCharArray();
48
t.copyBuffer(content, 0, content.length);
49
assertEquals(buf.length(), t.length());
50
assertEquals(buf.toString(), t.toString());
51
buf.append(buf.toString());
53
assertEquals(1048576, t.length());
55
// now as a StringBuilder, first variant
56
t = new CharTermAttributeImpl();
57
buf = new StringBuilder("ab");
58
for (int i = 0; i < 20; i++)
60
t.setEmpty().append(buf);
61
assertEquals(buf.length(), t.length());
62
assertEquals(buf.toString(), t.toString());
65
assertEquals(1048576, t.length());
67
// Test for slow growth to a long term
68
t = new CharTermAttributeImpl();
69
buf = new StringBuilder("a");
70
for (int i = 0; i < 20000; i++)
72
t.setEmpty().append(buf);
73
assertEquals(buf.length(), t.length());
74
assertEquals(buf.toString(), t.toString());
77
assertEquals(20000, t.length());
80
public void testToString() throws Exception {
81
char[] b = {'a', 'l', 'o', 'h', 'a'};
82
CharTermAttributeImpl t = new CharTermAttributeImpl();
83
t.copyBuffer(b, 0, 5);
84
assertEquals("aloha", t.toString());
86
t.setEmpty().append("hi there");
87
assertEquals("hi there", t.toString());
90
public void testClone() throws Exception {
91
CharTermAttributeImpl t = new CharTermAttributeImpl();
92
char[] content = "hello".toCharArray();
93
t.copyBuffer(content, 0, 5);
94
char[] buf = t.buffer();
95
CharTermAttributeImpl copy = (CharTermAttributeImpl) TestSimpleAttributeImpls.assertCloneIsEqual(t);
96
assertEquals(t.toString(), copy.toString());
97
assertNotSame(buf, copy.buffer());
100
public void testEquals() throws Exception {
101
CharTermAttributeImpl t1a = new CharTermAttributeImpl();
102
char[] content1a = "hello".toCharArray();
103
t1a.copyBuffer(content1a, 0, 5);
104
CharTermAttributeImpl t1b = new CharTermAttributeImpl();
105
char[] content1b = "hello".toCharArray();
106
t1b.copyBuffer(content1b, 0, 5);
107
CharTermAttributeImpl t2 = new CharTermAttributeImpl();
108
char[] content2 = "hello2".toCharArray();
109
t2.copyBuffer(content2, 0, 6);
110
assertTrue(t1a.equals(t1b));
111
assertFalse(t1a.equals(t2));
112
assertFalse(t2.equals(t1b));
115
public void testCopyTo() throws Exception {
116
CharTermAttributeImpl t = new CharTermAttributeImpl();
117
CharTermAttributeImpl copy = (CharTermAttributeImpl) TestSimpleAttributeImpls.assertCopyIsEqual(t);
118
assertEquals("", t.toString());
119
assertEquals("", copy.toString());
121
t = new CharTermAttributeImpl();
122
char[] content = "hello".toCharArray();
123
t.copyBuffer(content, 0, 5);
124
char[] buf = t.buffer();
125
copy = (CharTermAttributeImpl) TestSimpleAttributeImpls.assertCopyIsEqual(t);
126
assertEquals(t.toString(), copy.toString());
127
assertNotSame(buf, copy.buffer());
130
public void testAttributeReflection() throws Exception {
131
CharTermAttributeImpl t = new CharTermAttributeImpl();
133
_TestUtil.assertAttributeReflection(t,
134
Collections.singletonMap(CharTermAttribute.class.getName() + "#term", "foobar"));
137
public void testCharSequenceInterface() {
138
final String s = "0123456789";
139
final CharTermAttributeImpl t = new CharTermAttributeImpl();
142
assertEquals(s.length(), t.length());
143
assertEquals("12", t.subSequence(1,3).toString());
144
assertEquals(s, t.subSequence(0,s.length()).toString());
146
assertTrue(Pattern.matches("01\\d+", t));
147
assertTrue(Pattern.matches("34", t.subSequence(3,5)));
149
assertEquals(s.subSequence(3,7).toString(), t.subSequence(3,7).toString());
151
for (int i = 0; i < s.length(); i++) {
152
assertTrue(t.charAt(i) == s.charAt(i));
156
public void testAppendableInterface() {
157
CharTermAttributeImpl t = new CharTermAttributeImpl();
158
Formatter formatter = new Formatter(t, Locale.US);
159
formatter.format("%d", 1234);
160
assertEquals("1234", t.toString());
161
formatter.format("%d", 5678);
162
assertEquals("12345678", t.toString());
164
assertEquals("123456789", t.toString());
165
t.append((CharSequence) "0");
166
assertEquals("1234567890", t.toString());
167
t.append((CharSequence) "0123456789", 1, 3);
168
assertEquals("123456789012", t.toString());
169
t.append((CharSequence) CharBuffer.wrap("0123456789".toCharArray()), 3, 5);
170
assertEquals("12345678901234", t.toString());
171
t.append((CharSequence) t);
172
assertEquals("1234567890123412345678901234", t.toString());
173
t.append((CharSequence) new StringBuilder("0123456789"), 5, 7);
174
assertEquals("123456789012341234567890123456", t.toString());
175
t.append((CharSequence) new StringBuffer(t));
176
assertEquals("123456789012341234567890123456123456789012341234567890123456", t.toString());
177
// very wierd, to test if a subSlice is wrapped correct :)
178
CharBuffer buf = CharBuffer.wrap("0123456789".toCharArray(), 3, 5);
179
assertEquals("34567", buf.toString());
180
t.setEmpty().append((CharSequence) buf, 1, 2);
181
assertEquals("4", t.toString());
182
CharTermAttribute t2 = new CharTermAttributeImpl();
184
t.append((CharSequence) t2);
185
assertEquals("4test", t.toString());
186
t.append((CharSequence) t2, 1, 2);
187
assertEquals("4teste", t.toString());
190
t.append((CharSequence) t2, 1, 5);
191
fail("Should throw IndexOutOfBoundsException");
192
} catch(IndexOutOfBoundsException iobe) {
196
t.append((CharSequence) t2, 1, 0);
197
fail("Should throw IndexOutOfBoundsException");
198
} catch(IndexOutOfBoundsException iobe) {
201
t.append((CharSequence) null);
202
assertEquals("4testenull", t.toString());
205
public void testAppendableInterfaceWithLongSequences() {
206
CharTermAttributeImpl t = new CharTermAttributeImpl();
207
t.append((CharSequence) "01234567890123456789012345678901234567890123456789");
208
t.append((CharSequence) CharBuffer.wrap("01234567890123456789012345678901234567890123456789".toCharArray()), 3, 50);
209
assertEquals("0123456789012345678901234567890123456789012345678934567890123456789012345678901234567890123456789", t.toString());
210
t.setEmpty().append((CharSequence) new StringBuilder("01234567890123456789"), 5, 17);
211
assertEquals((CharSequence) "567890123456", t.toString());
212
t.append(new StringBuffer(t));
213
assertEquals((CharSequence) "567890123456567890123456", t.toString());
214
// very wierd, to test if a subSlice is wrapped correct :)
215
CharBuffer buf = CharBuffer.wrap("012345678901234567890123456789".toCharArray(), 3, 15);
216
assertEquals("345678901234567", buf.toString());
217
t.setEmpty().append(buf, 1, 14);
218
assertEquals("4567890123456", t.toString());
220
// finally use a completely custom CharSequence that is not catched by instanceof checks
221
final String longTestString = "012345678901234567890123456789";
222
t.append(new CharSequence() {
223
public char charAt(int i) { return longTestString.charAt(i); }
224
public int length() { return longTestString.length(); }
225
public CharSequence subSequence(int start, int end) { return longTestString.subSequence(start, end); }
227
public String toString() { return longTestString; }
229
assertEquals("4567890123456"+longTestString, t.toString());
232
public void testNonCharSequenceAppend() {
233
CharTermAttributeImpl t = new CharTermAttributeImpl();
234
t.append("0123456789");
235
t.append("0123456789");
236
assertEquals("01234567890123456789", t.toString());
237
t.append(new StringBuilder("0123456789"));
238
assertEquals("012345678901234567890123456789", t.toString());
239
CharTermAttribute t2 = new CharTermAttributeImpl();
242
assertEquals("012345678901234567890123456789test", t.toString());
243
t.append((String) null);
244
t.append((StringBuilder) null);
245
t.append((CharTermAttribute) null);
246
assertEquals("012345678901234567890123456789testnullnullnull", t.toString());
249
public void testExceptions() {
250
CharTermAttributeImpl t = new CharTermAttributeImpl();
252
assertEquals("test", t.toString());
256
fail("Should throw IndexOutOfBoundsException");
257
} catch(IndexOutOfBoundsException iobe) {
262
fail("Should throw IndexOutOfBoundsException");
263
} catch(IndexOutOfBoundsException iobe) {
268
fail("Should throw IndexOutOfBoundsException");
269
} catch(IndexOutOfBoundsException iobe) {
274
fail("Should throw IndexOutOfBoundsException");
275
} catch(IndexOutOfBoundsException iobe) {
281
// test speed of the dynamic instanceof checks in append(CharSequence),
282
// to find the best max length for the generic while (start<end) loop:
283
public void testAppendPerf() {
284
CharTermAttributeImpl t = new CharTermAttributeImpl();
285
final int count = 32;
286
CharSequence[] csq = new CharSequence[count * 6];
287
final StringBuilder sb = new StringBuilder();
288
for (int i=0,j=0; i<count; i++) {
290
final String testString = sb.toString();
291
CharTermAttribute cta = new CharTermAttributeImpl();
292
cta.append(testString);
294
csq[j++] = testString;
295
csq[j++] = new StringBuilder(sb);
296
csq[j++] = new StringBuffer(sb);
297
csq[j++] = CharBuffer.wrap(testString.toCharArray());
298
csq[j++] = new CharSequence() {
299
public char charAt(int i) { return testString.charAt(i); }
300
public int length() { return testString.length(); }
301
public CharSequence subSequence(int start, int end) { return testString.subSequence(start, end); }
302
public String toString() { return testString; }
306
Random rnd = newRandom();
307
long startTime = System.currentTimeMillis();
308
for (int i=0; i<100000000; i++) {
309
t.setEmpty().append(csq[rnd.nextInt(csq.length)]);
311
long endTime = System.currentTimeMillis();
312
System.out.println("Time: " + (endTime-startTime)/1000.0 + " s");