001: package org.drools.util;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.Collection;
020:
021: import junit.framework.TestCase;
022:
023: public class PrimitiveLongMapTest extends TestCase {
024: public void testValues() {
025: final PrimitiveLongMap map = new PrimitiveLongMap();
026: assertNotNull("MapNotNullTest ", map);
027:
028: final Collection values = map.values();
029: assertNotNull("ValuesNotNullTest ", values);
030: assertEquals("ValuesZeroSizeTest ", 0, values.size());
031: }
032:
033: public void testPaging() {
034: final PrimitiveLongMap map = new PrimitiveLongMap(32, 8);
035:
036: for (int i = 0; i < 512; i++) {
037: final Integer value = new Integer(i);
038:
039: final Object oldValue = map.put(i, value);
040: assertNull("OldValueNullTest ", oldValue);
041: assertEquals("OldValueNullTest ", value, map.get(i));
042: }
043:
044: }
045:
046: public void testGetWithNegativeKeyReturnsNull() {
047: final PrimitiveLongMap map = new PrimitiveLongMap(2, 1);
048:
049: assertNull(map.get(-1));
050: }
051:
052: public void testRemoveWithNegativeReturnsNull() {
053: final PrimitiveLongMap map = new PrimitiveLongMap(2, 1);
054:
055: assertNull(map.remove(-1));
056: }
057:
058: public void testPutWithNegativeKeyThrowsIllegalArgumentException() {
059: final PrimitiveLongMap map = new PrimitiveLongMap(2, 1);
060:
061: try {
062: map.put(-1, new Object());
063: fail();
064: } catch (final IllegalArgumentException e) {
065: // expected
066: }
067: }
068:
069: /**
070: * this tests maxKey for gets and removes if ( key > this.maxKey || key < 0 ) {
071: * return null; }
072: *
073: */
074: public void testMaxKey() {
075:
076: final PrimitiveLongMap map = new PrimitiveLongMap(8, 4);
077:
078: // Test maxKey for key 0
079: map.put(0, new Integer(0));
080:
081: assertEquals(new Integer(0), map.get(0));
082: assertNull(map.remove(1));
083: assertEquals(new Integer(0), map.get(0));
084: assertNotNull(map.remove(0));
085: assertNull(map.get(0));
086:
087: // Test maxKey for key 1
088: map.put(1, new Integer(1));
089: assertEquals(new Integer(1), map.get(1));
090: assertNull(map.remove(2));
091: assertEquals(new Integer(1), map.get(1));
092: assertNotNull(map.remove(1));
093: assertNull(map.get(1));
094:
095: // Test maxKey for key 127, an end to a page border
096: map.put(127, new Integer(127));
097: assertEquals(new Integer(127), map.get(127));
098: assertNull(map.remove(128));
099: assertEquals(new Integer(127), map.get(127));
100: assertNotNull(map.remove(127));
101: assertNull(map.get(127));
102:
103: // Test maxKey for key 128, a start to a new page
104: map.put(128, new Integer(128));
105: assertEquals(new Integer(128), map.get(128));
106: assertNull(map.remove(129));
107: assertEquals(new Integer(128), map.get(128));
108: assertNotNull(map.remove(128));
109: assertNull(map.get(128));
110: }
111:
112: public void testLastIndexBoundary() {
113: final PrimitiveLongMap map = new PrimitiveLongMap(32, 8);
114: map.put(8192, new Object());
115: map.remove(8192);
116: map.put(8192, new Object());
117: map.put(8191, new Object());
118: }
119:
120: public void testSize() {
121: final PrimitiveLongMap map = new PrimitiveLongMap(32, 8);
122:
123: final Object object = new Object();
124: map.put(231, object);
125: final Object string = new Object();
126: map.put(211, string);
127: map.put(99822, null);
128: assertEquals(3, map.size());
129:
130: map.put(211, null);
131: map.put(99822, string);
132: assertEquals(3, map.size());
133:
134: map.remove(211);
135: assertEquals(2, map.size());
136: map.remove(99822);
137: assertEquals(1, map.size());
138:
139: map.remove(231);
140: assertEquals(0, map.size());
141: }
142: }
|