add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / test / org / apache / lucene / analysis / tokenattributes / TestTermAttributeImpl.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
22 public class TestTermAttributeImpl extends LuceneTestCase {
23
24   public void testResize() {
25     TermAttributeImpl t = new TermAttributeImpl();
26     char[] content = "hello".toCharArray();
27     t.setTermBuffer(content, 0, content.length);
28     for (int i = 0; i < 2000; i++)
29     {
30       t.resizeTermBuffer(i);
31       assertTrue(i <= t.termBuffer().length);
32       assertEquals("hello", t.term());
33     }
34   }
35
36   public void testGrow() {
37     TermAttributeImpl t = new TermAttributeImpl();
38     StringBuilder buf = new StringBuilder("ab");
39     for (int i = 0; i < 20; i++)
40     {
41       char[] content = buf.toString().toCharArray();
42       t.setTermBuffer(content, 0, content.length);
43       assertEquals(buf.length(), t.termLength());
44       assertEquals(buf.toString(), t.term());
45       buf.append(buf.toString());
46     }
47     assertEquals(1048576, t.termLength());
48
49     // now as a string, first variant
50     t = new TermAttributeImpl();
51     buf = new StringBuilder("ab");
52     for (int i = 0; i < 20; i++)
53     {
54       String content = buf.toString();
55       t.setTermBuffer(content, 0, content.length());
56       assertEquals(content.length(), t.termLength());
57       assertEquals(content, t.term());
58       buf.append(content);
59     }
60     assertEquals(1048576, t.termLength());
61
62     // now as a string, second variant
63     t = new TermAttributeImpl();
64     buf = new StringBuilder("ab");
65     for (int i = 0; i < 20; i++)
66     {
67       String content = buf.toString();
68       t.setTermBuffer(content);
69       assertEquals(content.length(), t.termLength());
70       assertEquals(content, t.term());
71       buf.append(content);
72     }
73     assertEquals(1048576, t.termLength());
74
75     // Test for slow growth to a long term
76     t = new TermAttributeImpl();
77     buf = new StringBuilder("a");
78     for (int i = 0; i < 20000; i++)
79     {
80       String content = buf.toString();
81       t.setTermBuffer(content);
82       assertEquals(content.length(), t.termLength());
83       assertEquals(content, t.term());
84       buf.append("a");
85     }
86     assertEquals(20000, t.termLength());
87
88     // Test for slow growth to a long term
89     t = new TermAttributeImpl();
90     buf = new StringBuilder("a");
91     for (int i = 0; i < 20000; i++)
92     {
93       String content = buf.toString();
94       t.setTermBuffer(content);
95       assertEquals(content.length(), t.termLength());
96       assertEquals(content, t.term());
97       buf.append("a");
98     }
99     assertEquals(20000, t.termLength());
100   }
101
102   public void testToString() throws Exception {
103     char[] b = {'a', 'l', 'o', 'h', 'a'};
104     TermAttributeImpl t = new TermAttributeImpl();
105     t.setTermBuffer(b, 0, 5);
106     assertEquals("aloha", t.toString());
107
108     t.setTermBuffer("hi there");
109     assertEquals("hi there", t.toString());
110   }
111
112   public void testMixedStringArray() throws Exception {
113     TermAttributeImpl t = new TermAttributeImpl();
114     t.setTermBuffer("hello");
115     assertEquals(t.termLength(), 5);
116     assertEquals(t.term(), "hello");
117     t.setTermBuffer("hello2");
118     assertEquals(t.termLength(), 6);
119     assertEquals(t.term(), "hello2");
120     t.setTermBuffer("hello3".toCharArray(), 0, 6);
121     assertEquals(t.term(), "hello3");
122
123     // Make sure if we get the buffer and change a character
124     // that term() reflects the change
125     char[] buffer = t.termBuffer();
126     buffer[1] = 'o';
127     assertEquals(t.term(), "hollo3");
128   }
129   
130   public void testClone() throws Exception {
131     TermAttributeImpl t = new TermAttributeImpl();
132     char[] content = "hello".toCharArray();
133     t.setTermBuffer(content, 0, 5);
134     char[] buf = t.termBuffer();
135     TermAttributeImpl copy = (TermAttributeImpl) TestSimpleAttributeImpls.assertCloneIsEqual(t);
136     assertEquals(t.term(), copy.term());
137     assertNotSame(buf, copy.termBuffer());
138   }
139   
140   public void testEquals() throws Exception {
141     TermAttributeImpl t1a = new TermAttributeImpl();
142     char[] content1a = "hello".toCharArray();
143     t1a.setTermBuffer(content1a, 0, 5);
144     TermAttributeImpl t1b = new TermAttributeImpl();
145     char[] content1b = "hello".toCharArray();
146     t1b.setTermBuffer(content1b, 0, 5);
147     TermAttributeImpl t2 = new TermAttributeImpl();
148     char[] content2 = "hello2".toCharArray();
149     t2.setTermBuffer(content2, 0, 6);
150     assertTrue(t1a.equals(t1b));
151     assertFalse(t1a.equals(t2));
152     assertFalse(t2.equals(t1b));
153   }
154   
155   public void testCopyTo() throws Exception {
156     TermAttributeImpl t = new TermAttributeImpl();
157     TermAttributeImpl copy = (TermAttributeImpl) TestSimpleAttributeImpls.assertCopyIsEqual(t);
158     assertEquals("", t.term());
159     assertEquals("", copy.term());
160
161     t = new TermAttributeImpl();
162     char[] content = "hello".toCharArray();
163     t.setTermBuffer(content, 0, 5);
164     char[] buf = t.termBuffer();
165     copy = (TermAttributeImpl) TestSimpleAttributeImpls.assertCopyIsEqual(t);
166     assertEquals(t.term(), copy.term());
167     assertNotSame(buf, copy.termBuffer());
168   }
169 }