Skip to content

Commit 8d257a6

Browse files
committed
add tests
1 parent 8ab5820 commit 8d257a6

File tree

9 files changed

+287
-95
lines changed

9 files changed

+287
-95
lines changed
Lines changed: 84 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,84 @@
1-
package net.lecousin.framework.collections.sort;
2-
3-
import java.util.Iterator;
4-
5-
import net.lecousin.framework.collections.ArrayIterator;
6-
7-
/**
8-
* Sorted list of elements, each being associated with a date (timestamp).
9-
* The list is sorted such as the oldest comes first.
10-
* The list has a maximum number of elements, given in the constructor: when already full,
11-
* if a new element comes with a timestamp more recent than the last element, it is ignored,
12-
* else it is inserted and the more recent element is removed, such as this list keeps the
13-
* oldest elements.
14-
* An example of usage can be a cache, where we want to quickly access to the oldest
15-
* items to free some space.
16-
* @param <T> type of elements
17-
*/
18-
public class OldestList<T> implements Iterable<T> {
19-
20-
/** Initialize with the given capacity. */
21-
public OldestList(int nb) {
22-
elements = new Object[nb];
23-
dates = new long[nb];
24-
size = 0;
25-
}
26-
27-
private Object[] elements;
28-
private long[] dates;
29-
private int size;
30-
private int oldestIndex = -1;
31-
private int newestIndex = -1;
32-
33-
/** Add an elements with the given timestamp. */
34-
public void add(long date, T element) {
35-
if (size == 0) {
36-
elements[0] = element;
37-
dates[0] = date;
38-
size = 1;
39-
oldestIndex = 0;
40-
newestIndex = 0;
41-
return;
42-
}
43-
if (size < elements.length) {
44-
elements[size] = element;
45-
dates[size] = date;
46-
if (date < oldestIndex) oldestIndex = size;
47-
if (date > newestIndex) newestIndex = size;
48-
size++;
49-
return;
50-
}
51-
if (newestIndex >= 0 && dates[newestIndex] < date) return; // newer than newest
52-
if (oldestIndex >= 0 && dates[oldestIndex] > date) {
53-
// older than oldest
54-
if (newestIndex < 0) refreshIndexes();
55-
elements[newestIndex] = element;
56-
dates[newestIndex] = date;
57-
oldestIndex = newestIndex;
58-
newestIndex = -1;
59-
return;
60-
}
61-
if (newestIndex < 0 || oldestIndex < 0) refreshIndexes();
62-
if (dates[newestIndex] < date) return; // newer than newest
63-
// replace the newest
64-
elements[newestIndex] = element;
65-
dates[newestIndex] = date;
66-
oldestIndex = newestIndex;
67-
newestIndex = -1;
68-
return;
69-
}
70-
71-
private void refreshIndexes() {
72-
for (int i = elements.length - 1; i >= 0; --i) {
73-
if (oldestIndex == -1 || dates[i] < dates[oldestIndex]) oldestIndex = i;
74-
if (newestIndex == -1 || dates[i] > dates[newestIndex]) newestIndex = i;
75-
}
76-
}
77-
78-
@SuppressWarnings("unchecked")
79-
@Override
80-
public Iterator<T> iterator() {
81-
return new ArrayIterator<T>((T[])elements, size);
82-
}
83-
84-
}
1+
package net.lecousin.framework.collections.sort;
2+
3+
import java.util.Iterator;
4+
5+
import net.lecousin.framework.collections.ArrayIterator;
6+
7+
/**
8+
* Sorted list of elements, each being associated with a date (timestamp).
9+
* The list is sorted such as the oldest comes first.
10+
* The list has a maximum number of elements, given in the constructor: when already full,
11+
* if a new element comes with a timestamp more recent than the last element, it is ignored,
12+
* else it is inserted and the more recent element is removed, such as this list keeps the
13+
* oldest elements.
14+
* An example of usage can be a cache, where we want to quickly access to the oldest
15+
* items to free some space.
16+
* @param <T> type of elements
17+
*/
18+
public class OldestList<T> implements Iterable<T> {
19+
20+
/** Initialize with the given capacity. */
21+
public OldestList(int nb) {
22+
elements = new Object[nb];
23+
dates = new long[nb];
24+
size = 0;
25+
}
26+
27+
private Object[] elements;
28+
private long[] dates;
29+
private int size;
30+
private int oldestIndex = -1;
31+
private int newestIndex = -1;
32+
33+
/** Add an elements with the given timestamp. */
34+
public void add(long date, T element) {
35+
if (size == 0) {
36+
elements[0] = element;
37+
dates[0] = date;
38+
size = 1;
39+
oldestIndex = 0;
40+
newestIndex = 0;
41+
return;
42+
}
43+
if (size < elements.length) {
44+
elements[size] = element;
45+
dates[size] = date;
46+
if (date < dates[oldestIndex]) oldestIndex = size;
47+
if (date > dates[newestIndex]) newestIndex = size;
48+
size++;
49+
return;
50+
}
51+
if (newestIndex >= 0 && dates[newestIndex] < date) return; // newer than newest
52+
if (oldestIndex >= 0 && dates[oldestIndex] > date) {
53+
// older than oldest
54+
if (newestIndex < 0) refreshIndexes();
55+
elements[newestIndex] = element;
56+
dates[newestIndex] = date;
57+
oldestIndex = newestIndex;
58+
newestIndex = -1;
59+
return;
60+
}
61+
if (newestIndex < 0 || oldestIndex < 0) refreshIndexes();
62+
if (dates[newestIndex] < date) return; // newer than newest
63+
// replace the newest
64+
elements[newestIndex] = element;
65+
dates[newestIndex] = date;
66+
oldestIndex = newestIndex;
67+
newestIndex = -1;
68+
return;
69+
}
70+
71+
private void refreshIndexes() {
72+
for (int i = elements.length - 1; i >= 0; --i) {
73+
if (oldestIndex == -1 || dates[i] < dates[oldestIndex]) oldestIndex = i;
74+
if (newestIndex == -1 || dates[i] > dates[newestIndex]) newestIndex = i;
75+
}
76+
}
77+
78+
@SuppressWarnings("unchecked")
79+
@Override
80+
public Iterator<T> iterator() {
81+
return new ArrayIterator<T>((T[])elements, size);
82+
}
83+
84+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ protected void check(Sorted.AssociatedWithInteger<Object> sorted, TreeSet<Intege
107107
for (Integer i : order) {
108108
int value = i.intValue();
109109
Assert.assertTrue("contains(" + value + ") returned false", sorted.contains(value, Integer.valueOf(-value)));
110+
if (sorted.size() < 100) {
111+
Integer instance = null;
112+
for (Object o : sorted)
113+
if (((Integer)o).intValue() == -value) {
114+
instance = (Integer)o;
115+
break;
116+
}
117+
Assert.assertTrue(sorted.containsInstance(value, instance));
118+
}
110119
}
111120
if (!order.isEmpty()) {
112121
int val = order.last().intValue() + 1;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ protected void check(Sorted.AssociatedWithLong<Object> sorted, TreeSet<Long> ord
107107
for (Long i : order) {
108108
long value = i.longValue();
109109
Assert.assertTrue("contains(" + value + ") returned false", sorted.contains(value, Long.valueOf(-value)));
110+
if (sorted.size() < 100) {
111+
Long instance = null;
112+
for (Object o : sorted)
113+
if (((Long)o).longValue() == -value) {
114+
instance = (Long)o;
115+
break;
116+
}
117+
Assert.assertTrue(sorted.containsInstance(value, instance));
118+
}
110119
}
111120
if (!order.isEmpty()) {
112121
long val = order.last().longValue() + 1;

net.lecousin.core/src/test/java/net/lecousin/framework/core/tests/adapter/TestAdapter.java

Lines changed: 94 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,26 @@
1515
import net.lecousin.framework.io.util.FileInfo;
1616

1717
import org.junit.Assert;
18+
import org.junit.BeforeClass;
1819
import org.junit.Test;
1920

2021
public class TestAdapter extends LCCoreAbstractTest {
2122

23+
@BeforeClass
24+
public static void register() {
25+
AdapterRegistry.get().addPlugin(new Adapter1());
26+
AdapterRegistry.get().addPlugin(new Adapter2_1());
27+
AdapterRegistry.get().addPlugin(new Adapter2_2());
28+
AdapterRegistry.get().addPlugin(new Adapter2_3());
29+
AdapterRegistry.get().addPlugin(new Adapter3());
30+
AdapterRegistry.get().addPlugin(new Adapter4bis());
31+
AdapterRegistry.get().addPlugin(new Adapter4ter());
32+
AdapterRegistry.get().addPlugin(new Source1ToSource2());
33+
AdapterRegistry.get().addPlugin(new Source1ToSource3());
34+
AdapterRegistry.get().addPlugin(new Target2ToTarget4());
35+
AdapterRegistry.get().addPlugin(new Target3ToTarget4());
36+
}
37+
2238
public static class Source1 {
2339
public int value = 0;
2440
}
@@ -43,11 +59,10 @@ public Target1 adapt(Source1 input) {
4359

4460
@Test(timeout=120000)
4561
public void testDirectAdapter() throws Exception {
46-
AdapterRegistry.get().addPlugin(new Adapter1());
4762
Source1 src = new Source1();
4863
src.value = 51;
4964
Assert.assertTrue(AdapterRegistry.get().canAdapt(src, Target1.class));
50-
Assert.assertFalse(AdapterRegistry.get().canAdapt(src, Target2.class));
65+
Assert.assertFalse(AdapterRegistry.get().canAdapt(src, Target4ter.class));
5166
Target1 t = AdapterRegistry.get().adapt(src, Target1.class);
5267
Assert.assertEquals(t.value, src.value);
5368
}
@@ -110,9 +125,6 @@ public Target2 adapt(Intermediate2_2 input) {
110125

111126
@Test(timeout=120000)
112127
public void testIntermediates() throws Exception {
113-
AdapterRegistry.get().addPlugin(new Adapter2_1());
114-
AdapterRegistry.get().addPlugin(new Adapter2_2());
115-
AdapterRegistry.get().addPlugin(new Adapter2_3());
116128
Source2 src = new Source2();
117129
src.value = 51;
118130
Target2 t = AdapterRegistry.get().adapt(src, Target2.class);
@@ -217,17 +229,91 @@ public Target4ter adapt(Source3 input) {
217229

218230
@Test
219231
public void test3() throws Exception {
220-
AdapterRegistry.get().addPlugin(new Adapter3());
221232
Source3 src = new Source3();
222233
src.value = 1;
223234
Assert.assertNull(AdapterRegistry.get().adapt(src, Target3.class));
224235
src.value = 111;
225236
Assert.assertEquals(11, AdapterRegistry.get().adapt(src, Target3.class).value);
226237

227-
AdapterRegistry.get().addPlugin(new Adapter4bis());
228-
AdapterRegistry.get().addPlugin(new Adapter4ter());
229238
src.value = 111;
230239
Assert.assertEquals(110, AdapterRegistry.get().adapt(src, Target4.class).value);
231240
}
241+
242+
public static class Source1ToSource2 implements Adapter<Source1, Source2> {
243+
@Override
244+
public Class<Source1> getInputType() { return Source1.class; }
245+
@Override
246+
public Class<Source2> getOutputType() { return Source2.class; }
247+
@Override
248+
public boolean canAdapt(Source1 input) {
249+
return input.value > 0;
250+
}
251+
@Override
252+
public Source2 adapt(Source1 input) {
253+
Source2 t = new Source2();
254+
t.value = input.value;
255+
return t;
256+
}
257+
}
258+
259+
public static class Source1ToSource3 implements Adapter<Source1, Source3> {
260+
@Override
261+
public Class<Source1> getInputType() { return Source1.class; }
262+
@Override
263+
public Class<Source3> getOutputType() { return Source3.class; }
264+
@Override
265+
public boolean canAdapt(Source1 input) {
266+
return input.value > 0;
267+
}
268+
@Override
269+
public Source3 adapt(Source1 input) {
270+
Source3 t = new Source3();
271+
t.value = input.value;
272+
return t;
273+
}
274+
}
275+
276+
public static class Target2ToTarget4 implements Adapter<Target2, Target4> {
277+
@Override
278+
public Class<Target2> getInputType() { return Target2.class; }
279+
@Override
280+
public Class<Target4> getOutputType() { return Target4.class; }
281+
@Override
282+
public boolean canAdapt(Target2 input) {
283+
return input.value > 0;
284+
}
285+
@Override
286+
public Target4 adapt(Target2 input) {
287+
Target4 t = new Target4();
288+
t.value = input.value;
289+
return t;
290+
}
291+
}
292+
293+
public static class Target3ToTarget4 implements Adapter<Target3, Target4> {
294+
@Override
295+
public Class<Target3> getInputType() { return Target3.class; }
296+
@Override
297+
public Class<Target4> getOutputType() { return Target4.class; }
298+
@Override
299+
public boolean canAdapt(Target3 input) {
300+
return input.value > 0;
301+
}
302+
@Override
303+
public Target4 adapt(Target3 input) {
304+
Target4 t = new Target4();
305+
t.value = input.value;
306+
return t;
307+
}
308+
}
309+
310+
@Test
311+
public void testDifferentIntermediates() throws Exception {
312+
Source1 src = new Source1();
313+
src.value = 10;
314+
AdapterRegistry.get().adapt(src, Target4.class);
315+
src.value = 110;
316+
AdapterRegistry.get().adapt(src, Target4.class);
317+
}
232318

233319
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package net.lecousin.framework.core.tests.collections;
2+
3+
import java.util.NoSuchElementException;
4+
5+
import net.lecousin.framework.collections.ArrayIterator;
6+
import net.lecousin.framework.core.test.LCCoreAbstractTest;
7+
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
11+
public class TestArrayIterator extends LCCoreAbstractTest {
12+
13+
@Test
14+
public void test() {
15+
ArrayIterator.Generic it = new ArrayIterator.Generic(null);
16+
Assert.assertFalse(it.hasNext());
17+
try {
18+
it.next();
19+
throw new AssertionError("Iterator must throw NoSuchElementException");
20+
} catch (NoSuchElementException e) {
21+
// ok
22+
}
23+
}
24+
25+
}

0 commit comments

Comments
 (0)