Skip to content

Commit 157e6ab

Browse files
committed
add tests + fixes
1 parent 8d257a6 commit 157e6ab

File tree

11 files changed

+142
-15
lines changed

11 files changed

+142
-15
lines changed

net.lecousin.core/src/main/java/net/lecousin/framework/collections/ArrayUtil.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ public static <T> boolean equals(T[] a1, T[] a2) {
149149

150150
/** Return true if the portions of the arrays are equal. */
151151
public static <T> boolean equals(T[] a1, int off1, T[] a2, int off2, int len) {
152+
if (a1 == null) return a2 == null;
153+
if (a2 == null) return false;
152154
for (int i = 0; i < len; ++i)
153155
if (!ObjectUtil.equalsOrNull(a1[off1 + i], a2[off2 + i]))
154156
return false;

net.lecousin.core/src/main/java/net/lecousin/framework/collections/TurnArray.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import java.util.List;
1010
import java.util.NoSuchElementException;
1111

12-
import net.lecousin.framework.application.LCCore;
1312
import net.lecousin.framework.concurrent.CancelException;
1413
import net.lecousin.framework.concurrent.Task;
14+
import net.lecousin.framework.concurrent.Threading;
1515
import net.lecousin.framework.exception.NoException;
1616

1717
/**
@@ -323,7 +323,7 @@ private void increase(int newSize) {
323323

324324
private void checkDecrease() {
325325
if (decreaseTask != null) return;
326-
if (!LCCore.isStarted()) return;
326+
if (!Threading.isInitialized()) return;
327327
if (array.length > minSize && size() < array.length - (array.length >> 1))
328328
decreaseTask = new DecreaseTask();
329329
}

net.lecousin.core/src/main/java/net/lecousin/framework/collections/sort/OldestList.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,16 @@ public void add(long date, T element) {
4848
size++;
4949
return;
5050
}
51-
if (newestIndex >= 0 && dates[newestIndex] < date) return; // newer than newest
52-
if (oldestIndex >= 0 && dates[oldestIndex] > date) {
51+
if (dates[oldestIndex] > date) {
5352
// older than oldest
54-
if (newestIndex < 0) refreshIndexes();
53+
if (newestIndex < 0) refreshNewestIndex();
5554
elements[newestIndex] = element;
5655
dates[newestIndex] = date;
5756
oldestIndex = newestIndex;
5857
newestIndex = -1;
5958
return;
6059
}
61-
if (newestIndex < 0 || oldestIndex < 0) refreshIndexes();
60+
if (newestIndex < 0) refreshNewestIndex();
6261
if (dates[newestIndex] < date) return; // newer than newest
6362
// replace the newest
6463
elements[newestIndex] = element;
@@ -68,11 +67,9 @@ public void add(long date, T element) {
6867
return;
6968
}
7069

71-
private void refreshIndexes() {
72-
for (int i = elements.length - 1; i >= 0; --i) {
73-
if (oldestIndex == -1 || dates[i] < dates[oldestIndex]) oldestIndex = i;
70+
private void refreshNewestIndex() {
71+
for (int i = elements.length - 1; i >= 0; --i)
7472
if (newestIndex == -1 || dates[i] > dates[newestIndex]) newestIndex = i;
75-
}
7673
}
7774

7875
@SuppressWarnings("unchecked")

net.lecousin.core/src/main/java/net/lecousin/framework/collections/sort/RedBlackTreeInteger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ private Node<T> searchNearestLower(Node<T> node, int value) {
286286
return n;
287287
}
288288
Node<T> n = searchNearestLower(node.right, value);
289-
if (n == null || n.getValue() > node.value) return node;
289+
if (n == null) return node;
290290
return n;
291291
}
292292
if (node.left == null) return null;

net.lecousin.core/src/main/java/net/lecousin/framework/collections/sort/RedBlackTreeLong.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ private Node<T> searchNearestLower(Node<T> node, long value, boolean acceptEqual
290290
}
291291
Node<T> n = searchNearestLower(node.right, value, acceptEquals);
292292
if (n == null) return node;
293-
if (n.getValue() > node.value) return node;
294293
return n;
295294
}
296295
if (acceptEquals) return node;

net.lecousin.core/src/test/java/net/lecousin/framework/core/test/collections/sort/TestSortedAssociatedWithInteger.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ public abstract class TestSortedAssociatedWithInteger extends LCCoreAbstractTest
1515

1616
protected abstract Sorted.AssociatedWithInteger<Object> createSorted();
1717

18+
@Test(timeout=120000)
19+
public void simpleTests() {
20+
Sorted.AssociatedWithInteger<Object> list = createSorted();
21+
Integer elem1 = Integer.valueOf(10);
22+
list.add(10, elem1);
23+
Integer elem2 = Integer.valueOf(12);
24+
list.add(12, elem2);
25+
Assert.assertTrue(list.containsInstance(10, elem1));
26+
Assert.assertTrue(list.containsInstance(12, elem2));
27+
Assert.assertFalse(list.containsInstance(9, elem1));
28+
Assert.assertFalse(list.containsInstance(13, elem1));
29+
Assert.assertFalse(list.containsInstance(10, Integer.valueOf(9)));
30+
Assert.assertFalse(list.containsInstance(10, Integer.valueOf(11)));
31+
Assert.assertFalse(list.containsInstance(12, Integer.valueOf(9)));
32+
Assert.assertFalse(list.containsInstance(12, Integer.valueOf(10)));
33+
Assert.assertFalse(list.containsInstance(11, Integer.valueOf(11)));
34+
}
35+
1836
@Test(timeout=120000)
1937
public void testAddRemoveIncrement() {
2038
Sorted.AssociatedWithInteger<Object> list = createSorted();

net.lecousin.core/src/test/java/net/lecousin/framework/core/test/collections/sort/TestSortedAssociatedWithLong.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ public abstract class TestSortedAssociatedWithLong extends LCCoreAbstractTest {
1515

1616
protected abstract Sorted.AssociatedWithLong<Object> createSorted();
1717

18+
@Test(timeout=120000)
19+
public void simpleTests() {
20+
Sorted.AssociatedWithLong<Object> list = createSorted();
21+
Integer elem1 = Integer.valueOf(10);
22+
list.add(10, elem1);
23+
Integer elem2 = Integer.valueOf(12);
24+
list.add(12, elem2);
25+
Assert.assertTrue(list.containsInstance(10, elem1));
26+
Assert.assertTrue(list.containsInstance(12, elem2));
27+
Assert.assertFalse(list.containsInstance(9, elem1));
28+
Assert.assertFalse(list.containsInstance(13, elem1));
29+
Assert.assertFalse(list.containsInstance(10, Integer.valueOf(9)));
30+
Assert.assertFalse(list.containsInstance(10, Integer.valueOf(11)));
31+
Assert.assertFalse(list.containsInstance(12, Integer.valueOf(9)));
32+
Assert.assertFalse(list.containsInstance(12, Integer.valueOf(10)));
33+
Assert.assertFalse(list.containsInstance(11, Integer.valueOf(11)));
34+
}
35+
1836
@Test(timeout=120000)
1937
public void testAddRemoveIncrement() {
2038
Sorted.AssociatedWithLong<Object> list = createSorted();

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/collections/TestArrayIterator.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ public void test() {
2020
} catch (NoSuchElementException e) {
2121
// ok
2222
}
23+
24+
it = new ArrayIterator.Generic(new Integer[] { Integer.valueOf(10), Integer.valueOf(20) });
25+
Assert.assertTrue(it.hasNext());
26+
Assert.assertEquals(10, ((Integer)it.next()).intValue());
27+
Assert.assertTrue(it.hasNext());
28+
Assert.assertEquals(20, ((Integer)it.next()).intValue());
29+
Assert.assertFalse(it.hasNext());
30+
try {
31+
it.next();
32+
throw new AssertionError("Iterator must throw NoSuchElementException");
33+
} catch (NoSuchElementException e) {
34+
// ok
35+
}
2336
}
2437

2538
}

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/collections/TestArrayUtil.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,52 +124,72 @@ public void testEquals() {
124124
Assert.assertTrue(ArrayUtil.equals(new byte[] { 1, 10, 22 }, new byte[] { 1, 10, 22 }));
125125
Assert.assertFalse(ArrayUtil.equals(new byte[] { 2, 10, 22 }, new byte[] { 1, 10, 22 }));
126126
Assert.assertFalse(ArrayUtil.equals(new byte[] { 1, 10, 22 }, new byte[] { 1, 10, 21 }));
127+
Assert.assertFalse(ArrayUtil.equals(new byte[] { 1, 10, 22 }, new byte[] { 1, 10, 22, 30 }));
127128
Assert.assertTrue(ArrayUtil.equals(new byte[] { 2, 10, 22 }, 1, new byte[] { 1, 10, 22 }, 1, 2));
128129
Assert.assertTrue(ArrayUtil.equals(new byte[] { 2, 10, 22 }, 1, new byte[] { 10, 22, 33 }, 0, 2));
129130
Assert.assertFalse(ArrayUtil.equals(new byte[] { 2, 10, 22 }, 0, new byte[] { 1, 10, 22 }, 0, 2));
130131
Assert.assertTrue(ArrayUtil.equals((byte[])null, (byte[])null));
132+
Assert.assertTrue(ArrayUtil.equals((byte[])null, 0, (byte[])null, 0, 0));
131133
Assert.assertFalse(ArrayUtil.equals(new byte[] {}, (byte[])null));
134+
Assert.assertFalse(ArrayUtil.equals(new byte[] {}, 0, (byte[])null, 0, 0));
132135
Assert.assertFalse(ArrayUtil.equals((byte[])null, new byte[] {}));
136+
Assert.assertFalse(ArrayUtil.equals((byte[])null, 0, new byte[] {}, 0, 0));
133137

134138
Assert.assertTrue(ArrayUtil.equals(new short[] { 1, 10, 22 }, new short[] { 1, 10, 22 }));
135139
Assert.assertFalse(ArrayUtil.equals(new short[] { 2, 10, 22 }, new short[] { 1, 10, 22 }));
136140
Assert.assertFalse(ArrayUtil.equals(new short[] { 1, 10, 22 }, new short[] { 1, 10, 21 }));
141+
Assert.assertFalse(ArrayUtil.equals(new short[] { 1, 10, 22 }, new short[] { 1, 10, 22, 30 }));
137142
Assert.assertTrue(ArrayUtil.equals(new short[] { 2, 10, 22 }, 1, new short[] { 1, 10, 22 }, 1, 2));
138143
Assert.assertTrue(ArrayUtil.equals(new short[] { 2, 10, 22 }, 1, new short[] { 10, 22, 33 }, 0, 2));
139144
Assert.assertFalse(ArrayUtil.equals(new short[] { 2, 10, 22 }, 0, new short[] { 1, 10, 22 }, 0, 2));
140145
Assert.assertTrue(ArrayUtil.equals((short[])null, (short[])null));
146+
Assert.assertTrue(ArrayUtil.equals((short[])null, 0, (short[])null, 0, 0));
141147
Assert.assertFalse(ArrayUtil.equals(new short[] {}, (short[])null));
148+
Assert.assertFalse(ArrayUtil.equals(new short[] {}, 0, (short[])null, 0, 0));
142149
Assert.assertFalse(ArrayUtil.equals((short[])null, new short[] {}));
150+
Assert.assertFalse(ArrayUtil.equals((short[])null, 0, new short[] {}, 0, 0));
143151

144152
Assert.assertTrue(ArrayUtil.equals(new int[] { 1, 10, 22 }, new int[] { 1, 10, 22 }));
145153
Assert.assertFalse(ArrayUtil.equals(new int[] { 2, 10, 22 }, new int[] { 1, 10, 22 }));
146154
Assert.assertFalse(ArrayUtil.equals(new int[] { 1, 10, 22 }, new int[] { 1, 10, 21 }));
155+
Assert.assertFalse(ArrayUtil.equals(new int[] { 1, 10, 22 }, new int[] { 1, 10, 22, 30 }));
147156
Assert.assertTrue(ArrayUtil.equals(new int[] { 2, 10, 22 }, 1, new int[] { 1, 10, 22 }, 1, 2));
148157
Assert.assertTrue(ArrayUtil.equals(new int[] { 2, 10, 22 }, 1, new int[] { 10, 22, 33 }, 0, 2));
149158
Assert.assertFalse(ArrayUtil.equals(new int[] { 2, 10, 22 }, 0, new int[] { 1, 10, 22 }, 0, 2));
150159
Assert.assertTrue(ArrayUtil.equals((int[])null, (int[])null));
160+
Assert.assertTrue(ArrayUtil.equals((int[])null, 0, (int[])null, 0, 0));
151161
Assert.assertFalse(ArrayUtil.equals(new int[] {}, (int[])null));
162+
Assert.assertFalse(ArrayUtil.equals(new int[] {}, 0, (int[])null, 0, 0));
152163
Assert.assertFalse(ArrayUtil.equals((int[])null, new int[] {}));
164+
Assert.assertFalse(ArrayUtil.equals((int[])null, 0, new int[] {}, 0, 0));
153165

154166
Assert.assertTrue(ArrayUtil.equals(new long[] { 1, 10, 22 }, new long[] { 1, 10, 22 }));
155167
Assert.assertFalse(ArrayUtil.equals(new long[] { 2, 10, 22 }, new long[] { 1, 10, 22 }));
156168
Assert.assertFalse(ArrayUtil.equals(new long[] { 1, 10, 22 }, new long[] { 1, 10, 21 }));
169+
Assert.assertFalse(ArrayUtil.equals(new long[] { 1, 10, 22 }, new long[] { 1, 10, 22, 30 }));
157170
Assert.assertTrue(ArrayUtil.equals(new long[] { 2, 10, 22 }, 1, new long[] { 1, 10, 22 }, 1, 2));
158171
Assert.assertTrue(ArrayUtil.equals(new long[] { 2, 10, 22 }, 1, new long[] { 10, 22, 33 }, 0, 2));
159172
Assert.assertFalse(ArrayUtil.equals(new long[] { 2, 10, 22 }, 0, new long[] { 1, 10, 22 }, 0, 2));
160173
Assert.assertTrue(ArrayUtil.equals((long[])null, (long[])null));
174+
Assert.assertTrue(ArrayUtil.equals((long[])null, 0, (long[])null, 0, 0));
161175
Assert.assertFalse(ArrayUtil.equals(new long[] {}, (long[])null));
176+
Assert.assertFalse(ArrayUtil.equals(new long[] {}, 0, (long[])null, 0, 0));
162177
Assert.assertFalse(ArrayUtil.equals((long[])null, new long[] {}));
178+
Assert.assertFalse(ArrayUtil.equals((long[])null, 0, new long[] {}, 0, 0));
163179

164180
Assert.assertTrue(ArrayUtil.equals(new char[] { 1, 10, 22 }, new char[] { 1, 10, 22 }));
165181
Assert.assertFalse(ArrayUtil.equals(new char[] { 2, 10, 22 }, new char[] { 1, 10, 22 }));
166182
Assert.assertFalse(ArrayUtil.equals(new char[] { 1, 10, 22 }, new char[] { 1, 10, 21 }));
183+
Assert.assertFalse(ArrayUtil.equals(new char[] { 1, 10, 22 }, new char[] { 1, 10, 22, 30 }));
167184
Assert.assertTrue(ArrayUtil.equals(new char[] { 2, 10, 22 }, 1, new char[] { 1, 10, 22 }, 1, 2));
168185
Assert.assertTrue(ArrayUtil.equals(new char[] { 2, 10, 22 }, 1, new char[] { 10, 22, 33 }, 0, 2));
169186
Assert.assertFalse(ArrayUtil.equals(new char[] { 2, 10, 22 }, 0, new char[] { 1, 10, 22 }, 0, 2));
170187
Assert.assertTrue(ArrayUtil.equals((char[])null, (char[])null));
188+
Assert.assertTrue(ArrayUtil.equals((char[])null, 0, (char[])null, 0, 0));
171189
Assert.assertFalse(ArrayUtil.equals(new char[] {}, (char[])null));
190+
Assert.assertFalse(ArrayUtil.equals(new char[] {}, 0, (char[])null, 0, 0));
172191
Assert.assertFalse(ArrayUtil.equals((char[])null, new char[] {}));
192+
Assert.assertFalse(ArrayUtil.equals((char[])null, 0, new char[] {}, 0, 0));
173193

174194
Object o1 = new Object();
175195
Object o2 = new Object();
@@ -185,8 +205,11 @@ public void testEquals() {
185205
Assert.assertFalse(ArrayUtil.equals(new Object[] { o1, o2, o3 }, 1, new Object[] { o1, o2, o3 }, 0, 2));
186206
Assert.assertTrue(ArrayUtil.equals(new Object[] { o1, o2, o3 }, 1, new Object[] { o4, o2, o3 }, 1, 2));
187207
Assert.assertTrue(ArrayUtil.equals((Object[])null, (Object[])null));
208+
Assert.assertTrue(ArrayUtil.equals((Object[])null, 0, (Object[])null, 0, 0));
188209
Assert.assertFalse(ArrayUtil.equals(new Object[] {}, (Object[])null));
210+
Assert.assertFalse(ArrayUtil.equals(new Object[] {}, 0, (Object[])null, 0, 0));
189211
Assert.assertFalse(ArrayUtil.equals((Object[])null, new Object[] {}));
212+
Assert.assertFalse(ArrayUtil.equals((Object[])null, 0, new Object[] {}, 0, 0));
190213
}
191214

192215
@Test

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/collections/sort/TestRedBlackTreeInteger.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,30 @@ public void tests() {
109109
Assert.assertEquals(89, tree.size());
110110
tree.removeInstance(79, o);
111111
Assert.assertEquals(88, tree.size());
112+
113+
tree = new RedBlackTreeInteger<Object>();
114+
Assert.assertNull(tree.searchNearestLower(22));
115+
for (int i = 0; i < 100; i += 10)
116+
tree.add(i, Integer.valueOf(-i));
117+
Assert.assertEquals(0, tree.searchNearestLower(1).getValue());
118+
Assert.assertEquals(0, tree.searchNearestLower(10).getValue());
119+
Assert.assertEquals(10, tree.searchNearestLower(11).getValue());
120+
Assert.assertEquals(10, tree.searchNearestLower(20).getValue());
121+
Assert.assertEquals(20, tree.searchNearestLower(22).getValue());
122+
Assert.assertEquals(20, tree.searchNearestLower(30).getValue());
123+
Assert.assertEquals(30, tree.searchNearestLower(33).getValue());
124+
Assert.assertEquals(30, tree.searchNearestLower(40).getValue());
125+
Assert.assertEquals(40, tree.searchNearestLower(44).getValue());
126+
Assert.assertEquals(40, tree.searchNearestLower(50).getValue());
127+
Assert.assertEquals(50, tree.searchNearestLower(55).getValue());
128+
Assert.assertEquals(50, tree.searchNearestLower(60).getValue());
129+
Assert.assertEquals(60, tree.searchNearestLower(66).getValue());
130+
Assert.assertEquals(60, tree.searchNearestLower(70).getValue());
131+
Assert.assertEquals(70, tree.searchNearestLower(77).getValue());
132+
Assert.assertEquals(70, tree.searchNearestLower(80).getValue());
133+
Assert.assertEquals(80, tree.searchNearestLower(88).getValue());
134+
Assert.assertEquals(80, tree.searchNearestLower(90).getValue());
135+
Assert.assertEquals(90, tree.searchNearestLower(99).getValue());
112136
}
113137

114138
}

0 commit comments

Comments
 (0)