From: <tho...@us...> - 2010-08-09 12:17:11
|
Revision: 3435 http://bigdata.svn.sourceforge.net/bigdata/?rev=3435&view=rev Author: thompsonbry Date: 2010-08-09 12:17:05 +0000 (Mon, 09 Aug 2010) Log Message: ----------- Commented out the logic in remove(int index) which was conditionally invoking remove(). The rest of remove(int index) appears to handle remove at an index correctly. This appears to clear up the failing tests in TestRingBuffer. Added test_contains_null(), which checks for a thrown NPE when passing a null to RingBuffer#contains(E). Cleaned up the imports and various warnings in TestRingBuffer. Modified test_contains_all_this() and RingBuffer#containsAll() to respect the contract for Collections#containsAll(Collection), which is that this always returns true when the argument is the same collection. This resolves https://sourceforge.net/apps/trac/bigdata/ticket/101 Modified Paths: -------------- trunk/bigdata/src/java/com/bigdata/cache/RingBuffer.java trunk/bigdata/src/test/com/bigdata/cache/TestRingBuffer.java Modified: trunk/bigdata/src/java/com/bigdata/cache/RingBuffer.java =================================================================== --- trunk/bigdata/src/java/com/bigdata/cache/RingBuffer.java 2010-08-09 10:55:20 UTC (rev 3434) +++ trunk/bigdata/src/java/com/bigdata/cache/RingBuffer.java 2010-08-09 12:17:05 UTC (rev 3435) @@ -387,12 +387,12 @@ if (index < 0 || index >= size) throw new IllegalArgumentException(); - if (index + 1 == size) { - - // remove the LRU position. - return remove(); - - } +// if (index + 1 == size) { +// +// // remove the LRU position. +// return remove(); +// +// } /* * Otherwise we are removing some non-LRU element. @@ -409,7 +409,7 @@ for (;;) { - int nexti = (i + 1) % capacity; // update index. + final int nexti = (i + 1) % capacity; // update index. if (nexti != head) { @@ -581,6 +581,9 @@ public boolean contains(final Object ref) { + if (ref == null) + throw new NullPointerException(); + // MRU to LRU scan. for (int n = 0, i = tail; n < size; n++) { @@ -601,7 +604,8 @@ throw new NullPointerException(); if (c == this) - throw new IllegalArgumentException(); + return true; +// throw new IllegalArgumentException(); for( Object e : c ) { Modified: trunk/bigdata/src/test/com/bigdata/cache/TestRingBuffer.java =================================================================== --- trunk/bigdata/src/test/com/bigdata/cache/TestRingBuffer.java 2010-08-09 10:55:20 UTC (rev 3434) +++ trunk/bigdata/src/test/com/bigdata/cache/TestRingBuffer.java 2010-08-09 12:17:05 UTC (rev 3435) @@ -28,9 +28,7 @@ package com.bigdata.cache; -import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -65,7 +63,7 @@ public void test_ctor() { try { - new RingBuffer(0); + new RingBuffer<String>(0); fail("Expecting: " + IllegalArgumentException.class); } catch (IllegalArgumentException ex) { if (log.isInfoEnabled()) @@ -73,14 +71,14 @@ } try { - new RingBuffer(-1); + new RingBuffer<String>(-1); fail("Expecting: " + IllegalArgumentException.class); } catch (IllegalArgumentException ex) { if (log.isInfoEnabled()) log.info("Ignoring excepted exception: " + ex); } - final RingBuffer b = new RingBuffer(1); + final RingBuffer<String> b = new RingBuffer<String>(1); assertEquals("capacity", 1, b.capacity()); assertEquals("size", 0, b.size()); @@ -304,8 +302,6 @@ * remove(0) : [ _, _, _ ] : head=0; tail=0; size=0, returns [c] (empty, head==tail) * </pre> * - * @todo must also test when remove not at the tail! - * * When removing the tail, head := (head-1) % capacity. */ public void test_removeNth() { @@ -313,7 +309,7 @@ final String a = "a"; final String b = "b"; final String c = "c"; - final String d = "d"; +// final String d = "d"; final RingBuffer<String> buffer = new RingBuffer<String>(3); @@ -619,9 +615,9 @@ public void test_toArray1_nonempty() { Object [] intArr = new Object[] { - new Integer(1), - new Integer(2), - new Integer(3) + Integer.valueOf(1), + Integer.valueOf(2), + Integer.valueOf(3) }; final RingBuffer<Object> buffer = new RingBuffer<Object>(intArr.length); buffer.addAll(Arrays.asList(intArr)); @@ -631,9 +627,9 @@ public void test_toArray1_nonempty_oversized() { Object [] intArr = new Object[] { - new Integer(1), - new Integer(2), - new Integer(3) + Integer.valueOf(1), + Integer.valueOf(2), + Integer.valueOf(3) }; final RingBuffer<Object> buffer = new RingBuffer<Object>(intArr.length); buffer.addAll(Arrays.asList(intArr)); @@ -685,7 +681,7 @@ // see https://sourceforge.net/apps/trac/bigdata/ticket/101 public void test_remove_get_order() { - String[] expected = new String[] { + final String[] expected = new String[] { "a", "b", "c", "d" }; final RingBuffer<String> b = new RingBuffer<String>(expected.length); @@ -698,8 +694,8 @@ //Remove entries in MRU to LRU order -- differs from javadoc order for (int i=(expected.length-1); i >= 0; i--) { - String getString = b.get(i); - String removeString = b.remove(i); + final String getString = b.get(i); + final String removeString = b.remove(i); assertSame(getString, removeString); } assertTrue(b.isEmpty()); @@ -973,13 +969,10 @@ assertTrue(b.contains("c")); } - //TODO - check for exception on contains(null) once implemented - - - public void test_contains_all_null() { - final RingBuffer<String> b = new RingBuffer<String>(1); + public void test_contains_null() { + final RingBuffer<String> b = new RingBuffer<String>(1); try { - b.containsAll(null); + b.contains(null); fail("Expecting: " + NullPointerException.class); } catch (NullPointerException ex) { if (log.isInfoEnabled()) @@ -987,16 +980,29 @@ } } - public void test_contains_all_this() { + public void test_contains_all_null() { final RingBuffer<String> b = new RingBuffer<String>(1); try { - b.containsAll(b); - fail("Expecting: " + IllegalArgumentException.class); - } catch (IllegalArgumentException ex) { + b.containsAll(null); + fail("Expecting: " + NullPointerException.class); + } catch (NullPointerException ex) { if (log.isInfoEnabled()) log.info("Ignoring excepted exception: " + ex); } } + + public void test_contains_all_this() { + final RingBuffer<String> b = new RingBuffer<String>(1); + // Note: This is a tautology. + assertTrue(b.containsAll(b)); +// try { +// b.containsAll(b); +// fail("Expecting: " + IllegalArgumentException.class); +// } catch (IllegalArgumentException ex) { +// if (log.isInfoEnabled()) +// log.info("Ignoring excepted exception: " + ex); +// } + } public void test_contains_all_empty() { final RingBuffer<String> b = new RingBuffer<String>(1); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |