001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.lang;
018:
019: import junit.framework.Test;
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022: import junit.textui.TestRunner;
023:
024: /**
025: * Tests for the IntHashMap class.
026: *
027: * @author Steven Caswell
028: * @version $Id: IntHashMapTest.java 437554 2006-08-28 06:21:41Z bayard $
029: */
030: public class IntHashMapTest extends TestCase {
031:
032: public static void main(String[] args) {
033: TestRunner.run(suite());
034: }
035:
036: public static Test suite() {
037: TestSuite suite = new TestSuite(IntHashMapTest.class);
038: suite.setName("IntHashMapTest Tests");
039: return suite;
040: }
041:
042: public void testConstructor() {
043: try {
044: new IntHashMap(-1, 0.0f);
045: fail();
046: } catch (IllegalArgumentException e) {
047: assertEquals("Illegal Capacity: -1", e.getMessage());
048: }
049: try {
050: new IntHashMap(1, 0.0f);
051: fail();
052: } catch (IllegalArgumentException e) {
053: assertEquals("Illegal Load: 0.0", e.getMessage());
054: }
055: new IntHashMap(0, 1.0f);
056:
057: try {
058: new IntHashMap(-1);
059: fail();
060: } catch (IllegalArgumentException e) {
061: assertEquals("Illegal Capacity: -1", e.getMessage());
062: }
063: IntHashMap map1 = new IntHashMap(0);
064: assertEquals(0, map1.size());
065: }
066:
067: public void testClear() {
068: IntHashMap map = new IntHashMap();
069: assertNull(map.put(1, "hello"));
070: assertNull(map.put(2, "world"));
071: assertEquals(2, map.size());
072: map.clear();
073: assertEquals(0, map.size());
074: }
075:
076: public void testContainsKey() {
077: IntHashMap map = new IntHashMap();
078: assertNull(map.put(1, "hello"));
079: assertNull(map.put(2, "world"));
080: assertEquals(2, map.size());
081: assertTrue(map.containsKey(1));
082: assertTrue(map.containsKey(2));
083: assertFalse(map.containsKey(3));
084: }
085:
086: public void testContains() {
087: IntHashMap map = new IntHashMap();
088: assertNull(map.put(1, "hello"));
089: assertNull(map.put(2, "world"));
090: assertEquals(2, map.size());
091: assertTrue(map.containsValue("hello"));
092: assertTrue(map.containsValue("world"));
093: assertFalse(map.containsValue("goodbye"));
094: try {
095: map.containsValue(null);
096: fail();
097: } catch (NullPointerException e) {
098: }
099: }
100:
101: public void testContainsValue() {
102: IntHashMap map = new IntHashMap();
103: assertNull(map.put(1, "hello"));
104: assertNull(map.put(2, "world"));
105: assertEquals(2, map.size());
106: assertTrue(map.containsValue("hello"));
107: assertTrue(map.containsValue("world"));
108: assertFalse(map.containsValue("goodbye"));
109: try {
110: map.containsValue(null);
111: fail();
112: } catch (NullPointerException e) {
113: }
114: }
115:
116: public void testIsEmpty() {
117: IntHashMap map = new IntHashMap();
118: assertTrue(map.isEmpty());
119: assertNull(map.put(1, "hello"));
120: assertEquals(1, map.size());
121: assertFalse(map.isEmpty());
122: }
123:
124: public void testPut() {
125: IntHashMap map = new IntHashMap();
126: assertNull(map.put(1, "hello"));
127: assertNull(map.put(2, "world"));
128: assertEquals(2, map.size());
129: assertEquals("hello", map.put(1, "hellooooo"));
130: }
131:
132: public void testRemove() {
133: IntHashMap map = new IntHashMap();
134: assertNull(map.put(1, "hello"));
135: assertNull(map.put(2, "world"));
136: assertEquals(2, map.size());
137: assertEquals("hello", map.remove(1));
138: assertEquals(1, map.size());
139: assertNull(map.remove(3));
140: }
141:
142: }
|