add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / src / test / org / apache / lucene / analysis / TestCharArrayMap.java
1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.apache.lucene.analysis;
19
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.Locale;
24 import java.util.Map;
25
26 import org.apache.lucene.util.LuceneTestCase;
27
28 public class TestCharArrayMap extends LuceneTestCase {
29
30   public void doRandom(int iter, boolean ignoreCase) {
31     CharArrayMap<Integer> map = new CharArrayMap<Integer>(TEST_VERSION_CURRENT, 1, ignoreCase);
32     HashMap<String,Integer> hmap = new HashMap<String,Integer>();
33
34     char[] key;
35     for (int i=0; i<iter; i++) {
36       int len = random.nextInt(5);
37       key = new char[len];
38       for (int j=0; j<key.length; j++) {
39         key[j] = (char)random.nextInt(127);
40       }
41       String keyStr = new String(key);
42       String hmapKey = ignoreCase ? keyStr.toLowerCase(Locale.ENGLISH) : keyStr; 
43
44       int val = random.nextInt();
45
46       Object o1 = map.put(key, val);
47       Object o2 = hmap.put(hmapKey,val);
48       assertEquals(o1,o2);
49
50       // add it again with the string method
51       assertEquals(val, map.put(keyStr,val).intValue());
52
53       assertEquals(val, map.get(key,0,key.length).intValue());
54       assertEquals(val, map.get(key).intValue());
55       assertEquals(val, map.get(keyStr).intValue());
56
57       assertEquals(hmap.size(), map.size());
58     }
59   }
60
61   public void testCharArrayMap() {
62     int num = 5 * RANDOM_MULTIPLIER;
63     for (int i = 0; i < num; i++) { // pump this up for more random testing
64       doRandom(1000,false);
65       doRandom(1000,true);      
66     }
67   }
68
69   public void testMethods() {
70     CharArrayMap<Integer> cm = new CharArrayMap<Integer>(TEST_VERSION_CURRENT, 2, false);
71     HashMap<String,Integer> hm = new HashMap<String,Integer>();
72     hm.put("foo",1);
73     hm.put("bar",2);
74     cm.putAll(hm);
75     assertEquals(hm.size(), cm.size());
76     hm.put("baz", 3);
77     cm.putAll(hm);
78     assertEquals(hm.size(), cm.size());
79
80     CharArraySet cs = cm.keySet();
81     int n=0;
82     for (Object o : cs) {
83       assertTrue(cm.containsKey(o));
84       char[] co = (char[]) o;
85       assertTrue(cm.containsKey(co, 0, co.length));
86       n++;
87     }
88     assertEquals(hm.size(), n);
89     assertEquals(hm.size(), cs.size());
90     assertEquals(cm.size(), cs.size());
91     cs.clear();
92     assertEquals(0, cs.size());
93     assertEquals(0, cm.size());
94     try {
95       cs.add("test");
96       fail("keySet() allows adding new keys");
97     } catch (UnsupportedOperationException ue) {
98       // pass
99     }
100     cm.putAll(hm);
101     assertEquals(hm.size(), cs.size());
102     assertEquals(cm.size(), cs.size());
103
104     Iterator<Map.Entry<Object,Integer>> iter1 = cm.entrySet().iterator();
105     n=0;
106     while (iter1.hasNext()) {
107       Map.Entry<Object,Integer> entry = iter1.next();
108       Object key = entry.getKey();
109       Integer val = entry.getValue();
110       assertEquals(cm.get(key), val);
111       entry.setValue(val*100);
112       assertEquals(val*100, (int)cm.get(key));
113       n++;
114     }
115     assertEquals(hm.size(), n);
116     cm.clear();
117     cm.putAll(hm);
118     assertEquals(cm.size(), n);
119
120     CharArrayMap<Integer>.EntryIterator iter2 = cm.entrySet().iterator();
121     n=0;
122     while (iter2.hasNext()) {
123       char[] keyc = iter2.nextKey();
124       Integer val = iter2.currentValue();
125       assertEquals(hm.get(new String(keyc)), val);
126       iter2.setValue(val*100);
127       assertEquals(val*100, (int)cm.get(keyc));
128       n++;
129     }
130     assertEquals(hm.size(), n);
131
132     cm.entrySet().clear();
133     assertEquals(0, cm.size());
134     assertEquals(0, cm.entrySet().size());
135     assertTrue(cm.isEmpty());
136   }
137
138   public void testModifyOnUnmodifiable(){
139     CharArrayMap<Integer> map = new CharArrayMap<Integer>(TEST_VERSION_CURRENT, 2, false);
140     map.put("foo",1);
141     map.put("bar",2);
142     final int size = map.size();
143     assertEquals(2, size);
144     assertTrue(map.containsKey("foo"));  
145     assertEquals(1, map.get("foo").intValue());  
146     assertTrue(map.containsKey("bar"));  
147     assertEquals(2, map.get("bar").intValue());  
148
149     map = CharArrayMap.unmodifiableMap(map);
150     assertEquals("Map size changed due to unmodifiableMap call" , size, map.size());
151     String NOT_IN_MAP = "SirGallahad";
152     assertFalse("Test String already exists in map", map.containsKey(NOT_IN_MAP));
153     assertNull("Test String already exists in map", map.get(NOT_IN_MAP));
154     
155     try{
156       map.put(NOT_IN_MAP.toCharArray(), 3);  
157       fail("Modified unmodifiable map");
158     }catch (UnsupportedOperationException e) {
159       // expected
160       assertFalse("Test String has been added to unmodifiable map", map.containsKey(NOT_IN_MAP));
161       assertNull("Test String has been added to unmodifiable map", map.get(NOT_IN_MAP));
162       assertEquals("Size of unmodifiable map has changed", size, map.size());
163     }
164     
165     try{
166       map.put(NOT_IN_MAP, 3);  
167       fail("Modified unmodifiable map");
168     }catch (UnsupportedOperationException e) {
169       // expected
170       assertFalse("Test String has been added to unmodifiable map", map.containsKey(NOT_IN_MAP));
171       assertNull("Test String has been added to unmodifiable map", map.get(NOT_IN_MAP));
172       assertEquals("Size of unmodifiable map has changed", size, map.size());
173     }
174     
175     try{
176       map.put(new StringBuilder(NOT_IN_MAP), 3);  
177       fail("Modified unmodifiable map");
178     }catch (UnsupportedOperationException e) {
179       // expected
180       assertFalse("Test String has been added to unmodifiable map", map.containsKey(NOT_IN_MAP));
181       assertNull("Test String has been added to unmodifiable map", map.get(NOT_IN_MAP));
182       assertEquals("Size of unmodifiable map has changed", size, map.size());
183     }
184     
185     try{
186       map.clear();  
187       fail("Modified unmodifiable map");
188     }catch (UnsupportedOperationException e) {
189       // expected
190       assertEquals("Size of unmodifiable map has changed", size, map.size());
191     }
192     
193     try{
194       map.entrySet().clear();  
195       fail("Modified unmodifiable map");
196     }catch (UnsupportedOperationException e) {
197       // expected
198       assertEquals("Size of unmodifiable map has changed", size, map.size());
199     }
200     
201     try{
202       map.keySet().clear();  
203       fail("Modified unmodifiable map");
204     }catch (UnsupportedOperationException e) {
205       // expected
206       assertEquals("Size of unmodifiable map has changed", size, map.size());
207     }
208     
209     try{
210       map.put((Object) NOT_IN_MAP, 3);  
211       fail("Modified unmodifiable map");
212     }catch (UnsupportedOperationException e) {
213       // expected
214       assertFalse("Test String has been added to unmodifiable map", map.containsKey(NOT_IN_MAP));
215       assertNull("Test String has been added to unmodifiable map", map.get(NOT_IN_MAP));
216       assertEquals("Size of unmodifiable map has changed", size, map.size());
217     }
218     
219     try{
220       map.putAll(Collections.singletonMap(NOT_IN_MAP, 3));  
221       fail("Modified unmodifiable map");
222     }catch (UnsupportedOperationException e) {
223       // expected
224       assertFalse("Test String has been added to unmodifiable map", map.containsKey(NOT_IN_MAP));
225       assertNull("Test String has been added to unmodifiable map", map.get(NOT_IN_MAP));
226       assertEquals("Size of unmodifiable map has changed", size, map.size());
227     }
228     
229     assertTrue(map.containsKey("foo"));  
230     assertEquals(1, map.get("foo").intValue());  
231     assertTrue(map.containsKey("bar"));  
232     assertEquals(2, map.get("bar").intValue());  
233   }
234   
235   public void testToString() {
236     CharArrayMap<Integer> cm = new CharArrayMap<Integer>(TEST_VERSION_CURRENT, Collections.singletonMap("test",1), false);
237     assertEquals("[test]",cm.keySet().toString());
238     assertEquals("[1]",cm.values().toString());
239     assertEquals("[test=1]",cm.entrySet().toString());
240     assertEquals("{test=1}",cm.toString());
241     cm.put("test2", 2);
242     assertTrue(cm.keySet().toString().contains(", "));
243     assertTrue(cm.values().toString().contains(", "));
244     assertTrue(cm.entrySet().toString().contains(", "));
245     assertTrue(cm.toString().contains(", "));
246   }
247 }
248