[Arakhnę-Dev] [397] * Fixing code according to failing unit tests. |
[ Thread Index |
Date Index
| More arakhne.org/dev Archives
]
Revision: 397
Author: galland
Date: 2013-03-21 17:52:14 +0100 (Thu, 21 Mar 2013)
Log Message:
-----------
* Fixing code according to failing unit tests.
Modified Paths:
--------------
trunk/util/src/main/java/org/arakhne/afc/sizediterator/CollectionSizedIterator.java
trunk/util/src/main/java/org/arakhne/afc/util/ListUtil.java
trunk/util/src/test/java/org/arakhne/afc/util/ListUtilTest.java
Modified: trunk/util/src/main/java/org/arakhne/afc/sizediterator/CollectionSizedIterator.java
===================================================================
--- trunk/util/src/main/java/org/arakhne/afc/sizediterator/CollectionSizedIterator.java 2013-03-21 16:23:02 UTC (rev 396)
+++ trunk/util/src/main/java/org/arakhne/afc/sizediterator/CollectionSizedIterator.java 2013-03-21 16:52:14 UTC (rev 397)
@@ -79,7 +79,7 @@
*/
@Override
public int index() {
- return this.index - 1;
+ return this.index;
}
/**
Modified: trunk/util/src/main/java/org/arakhne/afc/util/ListUtil.java
===================================================================
--- trunk/util/src/main/java/org/arakhne/afc/util/ListUtil.java 2013-03-21 16:23:02 UTC (rev 396)
+++ trunk/util/src/main/java/org/arakhne/afc/util/ListUtil.java 2013-03-21 16:52:14 UTC (rev 397)
@@ -48,7 +48,7 @@
*/
@Deprecated
public static <E> boolean dichotomicRemove(List<E> list, Comparator<? super E> comparator, E data) {
- return remove(list, comparator, data);
+ return remove(list, comparator, data)>=0;
}
/** Remove the given element from the list using a dichotomic algorithm.
@@ -59,9 +59,10 @@
* @param list is the list to change.
* @param comparator is the comparator of elements.
* @param data is the data to remove.
- * @return <code>true</code> if the data was removed, otherwise <code>false</code>
+ * @return the index at which the element was removed in the list; or
+ * <code>-1</code> if the element was not removed.
*/
- public static <E> boolean remove(List<E> list, Comparator<? super E> comparator, E data) {
+ public static <E> int remove(List<E> list, Comparator<? super E> comparator, E data) {
assert(list!=null);
assert(comparator!=null);
assert(data!=null);
@@ -76,7 +77,7 @@
cmpR = comparator.compare(data, d);
if (cmpR==0) {
list.remove(c);
- return true;
+ return c;
}
else if (cmpR<0) {
l = c-1;
@@ -85,7 +86,7 @@
f = c+1;
}
}
- return false;
+ return -1;
}
/** Add the given element in the main list using a dichotomic algorithm.
@@ -105,7 +106,7 @@
*/
@Deprecated
public static <E> boolean dichotomicAdd(List<E> list, Comparator<? super E> comparator, E data, boolean allowMultipleOccurencesOfSameValue) {
- return add(list, comparator, data, allowMultipleOccurencesOfSameValue, false);
+ return add(list, comparator, data, allowMultipleOccurencesOfSameValue, false)>=0;
}
/** Add the given element in the main list using a dichotomic algorithm.
@@ -122,9 +123,10 @@
* occurrences of the same value are allowed in the list.
* @param allowReplacement indicates if the given <vaz>elt</var> may replace
* the found element.
- * @return <code>true</code> if the data was added, otherwise <code>false</code>
+ * @return the index where the element was inserted, or <code>-1</code>
+ * if the element was not inserted.
*/
- public static <E> boolean add(List<E> list, Comparator<? super E> comparator, E data, boolean allowMultipleOccurencesOfSameValue, boolean allowReplacement) {
+ public static <E> int add(List<E> list, Comparator<? super E> comparator, E data, boolean allowMultipleOccurencesOfSameValue, boolean allowReplacement) {
assert(list!=null);
assert(comparator!=null);
assert(data!=null);
@@ -137,7 +139,13 @@
c = (f+l)/2;
d = list.get(c);
cmpR = comparator.compare(data, d);
- if (cmpR==0 && !allowMultipleOccurencesOfSameValue) return false;
+ if (cmpR==0 && !allowMultipleOccurencesOfSameValue) {
+ if (allowReplacement) {
+ list.set(c, data);
+ return c;
+ }
+ return -1;
+ }
if (cmpR<0) {
l = c-1;
}
@@ -146,7 +154,7 @@
}
}
list.add(f, data);
- return true;
+ return f;
}
/** Replies if the given element is inside the list, using a dichotomic algorithm.
Modified: trunk/util/src/test/java/org/arakhne/afc/util/ListUtilTest.java
===================================================================
--- trunk/util/src/test/java/org/arakhne/afc/util/ListUtilTest.java 2013-03-21 16:23:02 UTC (rev 396)
+++ trunk/util/src/test/java/org/arakhne/afc/util/ListUtilTest.java 2013-03-21 16:52:14 UTC (rev 397)
@@ -53,13 +53,13 @@
/**
*/
- public static void testAddComparatorTList() {
+ public static void testAddComparatorTListBoolean_true_false() {
List<Integer> list = new ArrayList<Integer>();
list.addAll(Arrays.asList(1, 4, 6, 12, 30));
- assertEquals(0, ListUtil.add(list, new NaturalOrderComparator<Integer>(), -2, false, false));
- assertEquals(3, ListUtil.add(list, new NaturalOrderComparator<Integer>(), 4, false, false));
- assertEquals(6, ListUtil.add(list, new NaturalOrderComparator<Integer>(), 16, false, false));
+ assertEquals(0, ListUtil.add(list, new NaturalOrderComparator<Integer>(), -2, true, false));
+ assertEquals(3, ListUtil.add(list, new NaturalOrderComparator<Integer>(), 4, true, false));
+ assertEquals(6, ListUtil.add(list, new NaturalOrderComparator<Integer>(), 16, true, false));
Iterator<Integer> iterator = list.iterator();
assertEquals(Integer.valueOf(-2), iterator.next());
@@ -72,22 +72,43 @@
assertEquals(Integer.valueOf(30), iterator.next());
assertFalse(iterator.hasNext());
}
-
+
/**
*/
- public static void testAddComparatorTListBoolean_true() {
+ public static void testAddComparatorTListBoolean_false_false() {
List<Integer> list = new ArrayList<Integer>();
list.addAll(Arrays.asList(1, 4, 6, 12, 30));
- assertEquals(0, ListUtil.add(list, new NaturalOrderComparator<Integer>(), -2, true, false));
- assertEquals(3, ListUtil.add(list, new NaturalOrderComparator<Integer>(), 4, true, false));
- assertEquals(6, ListUtil.add(list, new NaturalOrderComparator<Integer>(), 16, true, false));
+ assertEquals(0, ListUtil.add(list, new NaturalOrderComparator<Integer>(), -2, false, false));
+ assertEquals(-1, ListUtil.add(list, new NaturalOrderComparator<Integer>(), 4, false, false));
+ assertEquals(5, ListUtil.add(list, new NaturalOrderComparator<Integer>(), 16, false, false));
Iterator<Integer> iterator = list.iterator();
assertEquals(Integer.valueOf(-2), iterator.next());
assertEquals(Integer.valueOf(1), iterator.next());
assertEquals(Integer.valueOf(4), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(12), iterator.next());
+ assertEquals(Integer.valueOf(16), iterator.next());
+ assertEquals(Integer.valueOf(30), iterator.next());
+ assertFalse(iterator.hasNext());
+ }
+
+ /**
+ */
+ public static void testAddComparatorTListBoolean_true_true() {
+ List<Integer> list = new ArrayList<Integer>();
+ list.addAll(Arrays.asList(1, 4, 6, 12, 30));
+
+ assertEquals(0, ListUtil.add(list, new NaturalOrderComparator<Integer>(), -2, true, true));
+ assertEquals(3, ListUtil.add(list, new NaturalOrderComparator<Integer>(), 4, true, true));
+ assertEquals(6, ListUtil.add(list, new NaturalOrderComparator<Integer>(), 16, true, true));
+
+ Iterator<Integer> iterator = list.iterator();
+ assertEquals(Integer.valueOf(-2), iterator.next());
+ assertEquals(Integer.valueOf(1), iterator.next());
assertEquals(Integer.valueOf(4), iterator.next());
+ assertEquals(Integer.valueOf(4), iterator.next());
assertEquals(Integer.valueOf(6), iterator.next());
assertEquals(Integer.valueOf(12), iterator.next());
assertEquals(Integer.valueOf(16), iterator.next());
@@ -97,13 +118,13 @@
/**
*/
- public static void testAddComparatorTListBoolean_false() {
+ public static void testAddComparatorTListBoolean_false_true() {
List<Integer> list = new ArrayList<Integer>();
list.addAll(Arrays.asList(1, 4, 6, 12, 30));
- assertEquals(0, ListUtil.add(list, new NaturalOrderComparator<Integer>(), -2, false, false));
- assertEquals(-1, ListUtil.add(list, new NaturalOrderComparator<Integer>(), 4, false, false));
- assertEquals(5, ListUtil.add(list, new NaturalOrderComparator<Integer>(), 16, false, false));
+ assertEquals(0, ListUtil.add(list, new NaturalOrderComparator<Integer>(), -2, false, true));
+ assertEquals(2, ListUtil.add(list, new NaturalOrderComparator<Integer>(), 4, false, true));
+ assertEquals(5, ListUtil.add(list, new NaturalOrderComparator<Integer>(), 16, false, true));
Iterator<Integer> iterator = list.iterator();
assertEquals(Integer.valueOf(-2), iterator.next());