pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / analysis / tokenattributes / TestCharTermAttributeImpl.java
1 package org.apache.lucene.analysis.tokenattributes;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
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;
27
28 public class TestCharTermAttributeImpl extends LuceneTestCase {
29
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++)
35     {
36       t.resizeBuffer(i);
37       assertTrue(i <= t.buffer().length);
38       assertEquals("hello", t.toString());
39     }
40   }
41
42   public void testGrow() {
43     CharTermAttributeImpl t = new CharTermAttributeImpl();
44     StringBuilder buf = new StringBuilder("ab");
45     for (int i = 0; i < 20; i++)
46     {
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());
52     }
53     assertEquals(1048576, t.length());
54
55     // now as a StringBuilder, first variant
56     t = new CharTermAttributeImpl();
57     buf = new StringBuilder("ab");
58     for (int i = 0; i < 20; i++)
59     {
60       t.setEmpty().append(buf);
61       assertEquals(buf.length(), t.length());
62       assertEquals(buf.toString(), t.toString());
63       buf.append(t);
64     }
65     assertEquals(1048576, t.length());
66
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++)
71     {
72       t.setEmpty().append(buf);
73       assertEquals(buf.length(), t.length());
74       assertEquals(buf.toString(), t.toString());
75       buf.append("a");
76     }
77     assertEquals(20000, t.length());
78   }
79
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());
85
86     t.setEmpty().append("hi there");
87     assertEquals("hi there", t.toString());
88   }
89
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());
98   }
99   
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));
113   }
114   
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());
120
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());
128   }
129   
130   public void testAttributeReflection() throws Exception {
131     CharTermAttributeImpl t = new CharTermAttributeImpl();
132     t.append("foobar");
133     _TestUtil.assertAttributeReflection(t,
134       Collections.singletonMap(CharTermAttribute.class.getName() + "#term", "foobar"));
135   }
136   
137   public void testCharSequenceInterface() {
138     final String s = "0123456789"; 
139     final CharTermAttributeImpl t = new CharTermAttributeImpl();
140     t.append(s);
141     
142     assertEquals(s.length(), t.length());
143     assertEquals("12", t.subSequence(1,3).toString());
144     assertEquals(s, t.subSequence(0,s.length()).toString());
145     
146     assertTrue(Pattern.matches("01\\d+", t));
147     assertTrue(Pattern.matches("34", t.subSequence(3,5)));
148     
149     assertEquals(s.subSequence(3,7).toString(), t.subSequence(3,7).toString());
150     
151     for (int i = 0; i < s.length(); i++) {
152       assertTrue(t.charAt(i) == s.charAt(i));
153     }
154   }
155
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());
163     t.append('9');
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();
183     t2.append("test");
184     t.append((CharSequence) t2);
185     assertEquals("4test", t.toString());
186     t.append((CharSequence) t2, 1, 2);
187     assertEquals("4teste", t.toString());
188     
189     try {
190       t.append((CharSequence) t2, 1, 5);
191       fail("Should throw IndexOutOfBoundsException");
192     } catch(IndexOutOfBoundsException iobe) {
193     }
194     
195     try {
196       t.append((CharSequence) t2, 1, 0);
197       fail("Should throw IndexOutOfBoundsException");
198     } catch(IndexOutOfBoundsException iobe) {
199     }
200     
201     t.append((CharSequence) null);
202     assertEquals("4testenull", t.toString());
203   }
204   
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());
219     
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); }
226       @Override
227       public String toString() { return longTestString; }
228     });
229     assertEquals("4567890123456"+longTestString, t.toString());
230   }
231   
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();
240     t2.append("test");
241     t.append(t2);
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());
247   }
248   
249   public void testExceptions() {
250     CharTermAttributeImpl t = new CharTermAttributeImpl();
251     t.append("test");
252     assertEquals("test", t.toString());
253
254     try {
255       t.charAt(-1);
256       fail("Should throw IndexOutOfBoundsException");
257     } catch(IndexOutOfBoundsException iobe) {
258     }
259
260     try {
261       t.charAt(4);
262       fail("Should throw IndexOutOfBoundsException");
263     } catch(IndexOutOfBoundsException iobe) {
264     }
265
266     try {
267       t.subSequence(0, 5);
268       fail("Should throw IndexOutOfBoundsException");
269     } catch(IndexOutOfBoundsException iobe) {
270     }
271
272     try {
273       t.subSequence(5, 0);
274       fail("Should throw IndexOutOfBoundsException");
275     } catch(IndexOutOfBoundsException iobe) {
276     }
277   }
278
279   /*
280   
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++) {
289       sb.append(i%10);
290       final String testString = sb.toString();
291       CharTermAttribute cta = new CharTermAttributeImpl();
292       cta.append(testString);
293       csq[j++] = cta;
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; }
303       };
304     }
305
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)]);
310     }
311     long endTime = System.currentTimeMillis();
312     System.out.println("Time: " + (endTime-startTime)/1000.0 + " s");
313   }
314   
315   */
316
317 }