Author: sback
Date: 2007-07-11 19:57:43 +0000 (Wed, 11 Jul 2007)
New Revision: 14029
Added:
trunk/freenet/test/freenet/support/LRUHashtableTest.java
Log:
LRUHashtable completely tested
Added: trunk/freenet/test/freenet/support/LRUHashtableTest.java
===================================================================
--- trunk/freenet/test/freenet/support/LRUHashtableTest.java
(rev 0)
+++ trunk/freenet/test/freenet/support/LRUHashtableTest.java 2007-07-11
19:57:43 UTC (rev 14029)
@@ -0,0 +1,330 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+package freenet.support;
+
+import java.util.Enumeration;
+
+import junit.framework.TestCase;
+
+/**
+ * Test case for {@link freenet.support.LRUHashtable} class.
+ *
+ * @author Alberto Bacchelli <sback at freenetproject.org>
+ */
+public class LRUHashtableTest extends TestCase {
+
+ private final int sampleElemsNumber = 100;
+
+ /**
+ * Creates a double array of objects with a specified size
+ * where Object[i][0] is the key, and is an Integer,
+ * and Object[i][1] is the value
+ * @param size the array size
+ * @return the objects double array
+ */
+ private Object[][] createSampleKeyVal(int size) {
+ Object[][] sampleObjects = new Object[size][2];
+ for (int i=0; i<sampleObjects.length;i++) {
+ sampleObjects[i][0] = new Integer(i); //key
+ sampleObjects[i][1] = new Object(); } //value
+ return sampleObjects;
+ }
+
+ /**
+ * Creates a LRUHashtable filled with the specified objects number
+ * @param size HashTable size
+ * @return the created LRUHashtable
+ */
+ private LRUHashtable createSampleHashTable(int size) {
+ LRUHashtable methodLRUht = new LRUHashtable();
+ Object[][] sampleObjects = createSampleKeyVal(size);
+ for (int i=0;i<sampleObjects.length;i++)
+
methodLRUht.push(sampleObjects[i][0],sampleObjects[i][1]);
+ return methodLRUht;
+ }
+
+ /**
+ * It verifies if a key-value pair is present in
+ * a LRUHashtable
+ * @param aLRUht a LRUHashtable to check in
+ * @param aKey a key to find
+ * @param aValue the correspondent value
+ * @return true if the key is present and returned value is the same as
in the argument
+ */
+ private boolean verifyKeyValPresence(LRUHashtable aLRUht, Object aKey,
Object aValue) {
+ if (aLRUht.containsKey(aKey))
+ return aLRUht.get(aKey).equals(aValue);
+ return false;
+ }
+
+ /**
+ * Tests push(Object,Object) method
+ * providing null object as arguments
+ * (after setting up a sample HashTable)
+ * and verifying if the correct exception
+ * is raised
+ */
+ public void testPushNull() {
+ LRUHashtable methodLRUht =
createSampleHashTable(sampleElemsNumber);
+ try {
+ methodLRUht.push(new Object(),null);} //a
null value is admitted
+ catch (NullPointerException anException) { fail("Not expected
exception thrown : " + anException.getMessage()); }
+ try {
+ methodLRUht.push(null,null);
+ fail("Expected Exception Error Not Thrown!"); }
+ catch (NullPointerException anException) {
assertNotNull(anException); }
+ try {
+ methodLRUht.push(null,new Object());
+ fail("Expected Exception Error Not Thrown!"); }
+ catch (NullPointerException anException) {
assertNotNull(anException); }
+
+ }
+
+ /**
+ * Tests push(Object,Object) method
+ * and verifies the behaviour when
+ * pushing the same object more than one
+ * time.
+ */
+ public void testPushSameObjTwice() {
+ LRUHashtable methodLRUht =
createSampleHashTable(sampleElemsNumber);
+ Object[][] sampleObj = {
+ { new Integer(sampleElemsNumber), new Object()
},
+ { new Integer(sampleElemsNumber+1), new
Object() } };
+
+ methodLRUht.push(sampleObj[0][0],sampleObj[0][1]);
+ methodLRUht.push(sampleObj[1][0],sampleObj[1][1]);
+
+
assertTrue(verifyKeyValPresence(methodLRUht,sampleObj[0][0],sampleObj[0][1]));
//check presence
+
assertTrue(verifyKeyValPresence(methodLRUht,sampleObj[1][0],sampleObj[1][1]));
//check presence
+ assertTrue(methodLRUht.size()==sampleElemsNumber+2);
//check size
+
+ methodLRUht.push(sampleObj[0][0],sampleObj[0][1]);
//push the same object another time
+
assertTrue(verifyKeyValPresence(methodLRUht,sampleObj[0][0],sampleObj[0][1]));
//check presence
+
assertTrue(verifyKeyValPresence(methodLRUht,sampleObj[1][0],sampleObj[1][1]));
//check presence
+ assertTrue(methodLRUht.size()==sampleElemsNumber+2);
//check size
+ }
+
+ /**
+ * Tests push(Object,Object) method
+ * and verifies the behaviour when
+ * pushing the same key with two different
+ * values.
+ */
+ public void testPushSameKey() {
+ LRUHashtable methodLRUht =
createSampleHashTable(sampleElemsNumber);
+ Object[][] sampleObj = {
+ { new Integer(sampleElemsNumber), new Object()
},
+ { new Integer(sampleElemsNumber+1), new
Object() } };
+
+ methodLRUht.push(sampleObj[0][0],sampleObj[0][1]);
+ methodLRUht.push(sampleObj[1][0],sampleObj[1][1]);
+
+
assertTrue(verifyKeyValPresence(methodLRUht,sampleObj[0][0],sampleObj[0][1]));
//check presence
+
assertTrue(verifyKeyValPresence(methodLRUht,sampleObj[1][0],sampleObj[1][1]));
//check presence
+ assertTrue(methodLRUht.size()==sampleElemsNumber+2);
//check size
+
+ sampleObj[0][1] = new Object(); //creating a different
value
+ methodLRUht.push(sampleObj[0][0],sampleObj[0][1]);
+
assertTrue(verifyKeyValPresence(methodLRUht,sampleObj[0][0],sampleObj[0][1]));
//check presence
+
assertTrue(verifyKeyValPresence(methodLRUht,sampleObj[1][0],sampleObj[1][1]));
//check presence
+ assertTrue(methodLRUht.size()==sampleElemsNumber+2);
//check size
+ }
+
+ /**
+ * Tests popKey() method pushing
+ * and popping objects and
+ * verifying if their keys are correctly
+ * (in a FIFO manner) fetched and the
+ * HashTable entry deleted
+ */
+ public void testPopKey() {
+ LRUHashtable methodLRUht = new LRUHashtable();
+ Object[][] sampleObjects =
createSampleKeyVal(sampleElemsNumber);
+ for (int i=0; i<sampleObjects.length; i++)
//pushing objects
+
methodLRUht.push(sampleObjects[i][0],sampleObjects[i][1]);
+ for (int i=0; i<sampleObjects.length; i++)
//getting keys
+ assertEquals(sampleObjects[i][0],methodLRUht.popKey());
+ assertNull(methodLRUht.popKey());
//the HashTable must be empty
+ }
+
+ /**
+ * Tests popKey() method pushing
+ * and popping objects and
+ * verifying if their values are correctly
+ * (in a FIFO manner) fetched and the
+ * HashTable entry deleted
+ */
+ public void testPopValue() {
+ LRUHashtable methodLRUht = new LRUHashtable();
+ Object[][] sampleObjects =
createSampleKeyVal(sampleElemsNumber);
+ for (int i=0; i<sampleObjects.length; i++)
//pushing objects
+
methodLRUht.push(sampleObjects[i][0],sampleObjects[i][1]);
+ for (int i=0; i<sampleObjects.length; i++)
//getting values
+
assertEquals(sampleObjects[i][1],methodLRUht.popValue());
+ assertNull(methodLRUht.popKey());
//the HashTable must be empty
+ }
+
+ /**
+ * Tests peekValue() method pushing
+ * and popping objects and
+ * verifying if their peekValue is correct
+ */
+ public void testPeekValue() {
+ LRUHashtable methodLRUht = new LRUHashtable();
+ Object[][] sampleObjects =
createSampleKeyVal(sampleElemsNumber);
+ for (int i=0; i<sampleObjects.length; i++)
//pushing objects
+
methodLRUht.push(sampleObjects[i][0],sampleObjects[i][1]);
+ for (int i=0; i<sampleObjects.length; i++) {
//getting values
+
assertEquals(sampleObjects[i][1],methodLRUht.peekValue());
+ methodLRUht.popKey(); }
+ assertNull(methodLRUht.peekValue());
//the HashTable must be empty
+ methodLRUht.push(new Object(),null);
+ assertNull(methodLRUht.peekValue());
//the value inserted was null
+ }
+
+ /**
+ * Tests size() method
+ * pushing and popping elements into
+ * the LRUHashTable
+ */
+ public void testSize() {
+ LRUHashtable methodLRUht = new LRUHashtable();
+ Object[][] sampleObjects =
createSampleKeyVal(sampleElemsNumber);
+ assertTrue(methodLRUht.size()==0);
+ for (int i=0; i<sampleObjects.length; i++) {
//pushing objects
+
methodLRUht.push(sampleObjects[i][0],sampleObjects[i][1]);
+ assertTrue(methodLRUht.size()==i+1); }
+ for (int i=sampleObjects.length-1; i>=0; i--) {
//popping keys
+ methodLRUht.popKey();
+ assertTrue(methodLRUht.size()==i); }
+ }
+
+ /**
+ * Tests removeKey(Object) method
+ * verifies if all elements are correctly
+ * removed checking the method return value,
+ * if the element is still contained and
+ * the HashTable size.
+ */
+ public void testRemoveKey() {
+ LRUHashtable methodLRUht = new LRUHashtable();
+ Object[][] sampleObjects =
createSampleKeyVal(sampleElemsNumber);
+ for (int i=0; i<sampleObjects.length; i++)
//pushing objects
+
methodLRUht.push(sampleObjects[i][0],sampleObjects[i][1]);
+ for (int i=sampleObjects.length-1; i>=0; i--) {
//popping keys
+ assertTrue(methodLRUht.removeKey(sampleObjects[i][0]));
+
assertFalse(methodLRUht.containsKey(sampleObjects[i][0]));
+ assertTrue(methodLRUht.size()==i); }
+ }
+
+ /**
+ * Tests removeKey(Object) providing a null
+ * key and trying to remove it after
+ * setting up a sample queue.
+ */
+ public void testRemoveNullKey() {
+ LRUHashtable methodLRUht =
createSampleHashTable(sampleElemsNumber);
+ try {
+ methodLRUht.removeKey(null);
+ fail("Expected Exception Error Not Thrown!"); }
+ catch (NullPointerException anException) {
assertNotNull(anException); }
+ }
+
+ /**
+ * Tests removeKey(Object) method
+ * trying to remove a not present key after
+ * setting up a sample LRUHashtable.
+ */
+ public void testRemoveNotPresent() {
+ LRUHashtable methodLRUht =
createSampleHashTable(sampleElemsNumber);
+ assertFalse(methodLRUht.removeKey(new Object()));
+ }
+
+ /**
+ * Tests containsKey(Object) method
+ * trying to find a not present key after
+ * setting up a sample queue.
+ * Then it search for a present one.
+ */
+ public void testContainsKey() {
+ LRUHashtable methodLRUht =
createSampleHashTable(sampleElemsNumber);
+ assertFalse(methodLRUht.containsKey(new Object()));
+ Object methodSampleObj = new Object();
+ methodLRUht.push(methodSampleObj,null);
+ assertTrue(methodLRUht.containsKey(methodSampleObj));
+ }
+
+ /**
+ * Tests get(Object) method
+ * trying to find a not present key after
+ * setting up a sample HashTable,
+ * then it search a present key.
+ */
+ public void testGet() {
+ LRUHashtable methodLRUht =
createSampleHashTable(sampleElemsNumber);
+ assertNull(methodLRUht.get(new Object()));
+ Object methodSampleKey = new Object();
+ Object methodSampleValue = new Object();
+ methodLRUht.push(methodSampleKey,methodSampleValue);
+
assertEquals(methodLRUht.get(methodSampleKey),methodSampleValue);
+ }
+
+ /**
+ * Tests get(Object) trying to fetch
+ * a null key.
+ */
+ public void testGetNullKey() {
+ LRUHashtable methodLRUht =
createSampleHashTable(sampleElemsNumber);
+ try {
+ methodLRUht.get(null);
+ fail("Expected Exception Error Not Thrown!"); }
+ catch (NullPointerException anException) {
assertNotNull(anException); }
+ }
+
+ /**
+ * Tests keys() method
+ * verifying if the Enumeration provided
+ * is correct
+ */
+ public void testKeys() {
+ LRUHashtable methodLRUht = new LRUHashtable();
+ Object[][] sampleObjects =
createSampleKeyVal(sampleElemsNumber);
+ for (int i=0; i<sampleObjects.length; i++)
//pushing objects
+
methodLRUht.push(sampleObjects[i][0],sampleObjects[i][1]);
+ Enumeration methodEnumeration = methodLRUht.keys();
+ int j=0;
+ while(methodEnumeration.hasMoreElements()) {
+
assertEquals(methodEnumeration.nextElement(),sampleObjects[j][0]);
+ j++; }
+ }
+
+ /**
+ * Tests isEmpty() method
+ * trying it with a new generated
+ * HashTable and after popping
+ * out all keys in a sample LRUHashTable
+ */
+ public void testIsEmpty() {
+ LRUHashtable methodLRUht = new LRUHashtable();
+ assertTrue(methodLRUht.isEmpty());
+ methodLRUht = createSampleHashTable(sampleElemsNumber);
+ for (int i=0; i<sampleElemsNumber;i++) //popping keys
+ methodLRUht.popKey();
+ assertTrue(methodLRUht.isEmpty());
+ }
+
+}