X-Git-Url: https://git.mdrn.pl/pylucene.git/blobdiff_plain/a2e61f0c04805cfcb8706176758d1283c7e3a55c..aaeed5504b982cf3545252ab528713250aa33eed:/lucene-java-3.5.0/lucene/src/test/org/apache/lucene/analysis/TestCharArrayMap.java diff --git a/lucene-java-3.5.0/lucene/src/test/org/apache/lucene/analysis/TestCharArrayMap.java b/lucene-java-3.5.0/lucene/src/test/org/apache/lucene/analysis/TestCharArrayMap.java new file mode 100644 index 0000000..c18830d --- /dev/null +++ b/lucene-java-3.5.0/lucene/src/test/org/apache/lucene/analysis/TestCharArrayMap.java @@ -0,0 +1,248 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.lucene.analysis; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Locale; +import java.util.Map; + +import org.apache.lucene.util.LuceneTestCase; + +public class TestCharArrayMap extends LuceneTestCase { + + public void doRandom(int iter, boolean ignoreCase) { + CharArrayMap map = new CharArrayMap(TEST_VERSION_CURRENT, 1, ignoreCase); + HashMap hmap = new HashMap(); + + char[] key; + for (int i=0; i cm = new CharArrayMap(TEST_VERSION_CURRENT, 2, false); + HashMap hm = new HashMap(); + hm.put("foo",1); + hm.put("bar",2); + cm.putAll(hm); + assertEquals(hm.size(), cm.size()); + hm.put("baz", 3); + cm.putAll(hm); + assertEquals(hm.size(), cm.size()); + + CharArraySet cs = cm.keySet(); + int n=0; + for (Object o : cs) { + assertTrue(cm.containsKey(o)); + char[] co = (char[]) o; + assertTrue(cm.containsKey(co, 0, co.length)); + n++; + } + assertEquals(hm.size(), n); + assertEquals(hm.size(), cs.size()); + assertEquals(cm.size(), cs.size()); + cs.clear(); + assertEquals(0, cs.size()); + assertEquals(0, cm.size()); + try { + cs.add("test"); + fail("keySet() allows adding new keys"); + } catch (UnsupportedOperationException ue) { + // pass + } + cm.putAll(hm); + assertEquals(hm.size(), cs.size()); + assertEquals(cm.size(), cs.size()); + + Iterator> iter1 = cm.entrySet().iterator(); + n=0; + while (iter1.hasNext()) { + Map.Entry entry = iter1.next(); + Object key = entry.getKey(); + Integer val = entry.getValue(); + assertEquals(cm.get(key), val); + entry.setValue(val*100); + assertEquals(val*100, (int)cm.get(key)); + n++; + } + assertEquals(hm.size(), n); + cm.clear(); + cm.putAll(hm); + assertEquals(cm.size(), n); + + CharArrayMap.EntryIterator iter2 = cm.entrySet().iterator(); + n=0; + while (iter2.hasNext()) { + char[] keyc = iter2.nextKey(); + Integer val = iter2.currentValue(); + assertEquals(hm.get(new String(keyc)), val); + iter2.setValue(val*100); + assertEquals(val*100, (int)cm.get(keyc)); + n++; + } + assertEquals(hm.size(), n); + + cm.entrySet().clear(); + assertEquals(0, cm.size()); + assertEquals(0, cm.entrySet().size()); + assertTrue(cm.isEmpty()); + } + + public void testModifyOnUnmodifiable(){ + CharArrayMap map = new CharArrayMap(TEST_VERSION_CURRENT, 2, false); + map.put("foo",1); + map.put("bar",2); + final int size = map.size(); + assertEquals(2, size); + assertTrue(map.containsKey("foo")); + assertEquals(1, map.get("foo").intValue()); + assertTrue(map.containsKey("bar")); + assertEquals(2, map.get("bar").intValue()); + + map = CharArrayMap.unmodifiableMap(map); + assertEquals("Map size changed due to unmodifiableMap call" , size, map.size()); + String NOT_IN_MAP = "SirGallahad"; + assertFalse("Test String already exists in map", map.containsKey(NOT_IN_MAP)); + assertNull("Test String already exists in map", map.get(NOT_IN_MAP)); + + try{ + map.put(NOT_IN_MAP.toCharArray(), 3); + fail("Modified unmodifiable map"); + }catch (UnsupportedOperationException e) { + // expected + assertFalse("Test String has been added to unmodifiable map", map.containsKey(NOT_IN_MAP)); + assertNull("Test String has been added to unmodifiable map", map.get(NOT_IN_MAP)); + assertEquals("Size of unmodifiable map has changed", size, map.size()); + } + + try{ + map.put(NOT_IN_MAP, 3); + fail("Modified unmodifiable map"); + }catch (UnsupportedOperationException e) { + // expected + assertFalse("Test String has been added to unmodifiable map", map.containsKey(NOT_IN_MAP)); + assertNull("Test String has been added to unmodifiable map", map.get(NOT_IN_MAP)); + assertEquals("Size of unmodifiable map has changed", size, map.size()); + } + + try{ + map.put(new StringBuilder(NOT_IN_MAP), 3); + fail("Modified unmodifiable map"); + }catch (UnsupportedOperationException e) { + // expected + assertFalse("Test String has been added to unmodifiable map", map.containsKey(NOT_IN_MAP)); + assertNull("Test String has been added to unmodifiable map", map.get(NOT_IN_MAP)); + assertEquals("Size of unmodifiable map has changed", size, map.size()); + } + + try{ + map.clear(); + fail("Modified unmodifiable map"); + }catch (UnsupportedOperationException e) { + // expected + assertEquals("Size of unmodifiable map has changed", size, map.size()); + } + + try{ + map.entrySet().clear(); + fail("Modified unmodifiable map"); + }catch (UnsupportedOperationException e) { + // expected + assertEquals("Size of unmodifiable map has changed", size, map.size()); + } + + try{ + map.keySet().clear(); + fail("Modified unmodifiable map"); + }catch (UnsupportedOperationException e) { + // expected + assertEquals("Size of unmodifiable map has changed", size, map.size()); + } + + try{ + map.put((Object) NOT_IN_MAP, 3); + fail("Modified unmodifiable map"); + }catch (UnsupportedOperationException e) { + // expected + assertFalse("Test String has been added to unmodifiable map", map.containsKey(NOT_IN_MAP)); + assertNull("Test String has been added to unmodifiable map", map.get(NOT_IN_MAP)); + assertEquals("Size of unmodifiable map has changed", size, map.size()); + } + + try{ + map.putAll(Collections.singletonMap(NOT_IN_MAP, 3)); + fail("Modified unmodifiable map"); + }catch (UnsupportedOperationException e) { + // expected + assertFalse("Test String has been added to unmodifiable map", map.containsKey(NOT_IN_MAP)); + assertNull("Test String has been added to unmodifiable map", map.get(NOT_IN_MAP)); + assertEquals("Size of unmodifiable map has changed", size, map.size()); + } + + assertTrue(map.containsKey("foo")); + assertEquals(1, map.get("foo").intValue()); + assertTrue(map.containsKey("bar")); + assertEquals(2, map.get("bar").intValue()); + } + + public void testToString() { + CharArrayMap cm = new CharArrayMap(TEST_VERSION_CURRENT, Collections.singletonMap("test",1), false); + assertEquals("[test]",cm.keySet().toString()); + assertEquals("[1]",cm.values().toString()); + assertEquals("[test=1]",cm.entrySet().toString()); + assertEquals("{test=1}",cm.toString()); + cm.put("test2", 2); + assertTrue(cm.keySet().toString().contains(", ")); + assertTrue(cm.values().toString().contains(", ")); + assertTrue(cm.entrySet().toString().contains(", ")); + assertTrue(cm.toString().contains(", ")); + } +} +