aboutsummaryrefslogtreecommitdiffstats
path: root/store/src/test/java/tools/refinery/data/util/CollectionsUtilTests.java
diff options
context:
space:
mode:
Diffstat (limited to 'store/src/test/java/tools/refinery/data/util/CollectionsUtilTests.java')
-rw-r--r--store/src/test/java/tools/refinery/data/util/CollectionsUtilTests.java78
1 files changed, 78 insertions, 0 deletions
diff --git a/store/src/test/java/tools/refinery/data/util/CollectionsUtilTests.java b/store/src/test/java/tools/refinery/data/util/CollectionsUtilTests.java
new file mode 100644
index 00000000..39ff4aca
--- /dev/null
+++ b/store/src/test/java/tools/refinery/data/util/CollectionsUtilTests.java
@@ -0,0 +1,78 @@
1package tools.refinery.data.util;
2
3import static org.junit.jupiter.api.Assertions.assertEquals;
4import static tools.refinery.data.util.CollectionsUtil.filter;
5import static tools.refinery.data.util.CollectionsUtil.map;
6
7import java.util.ArrayList;
8import java.util.Iterator;
9import java.util.List;
10import java.util.NoSuchElementException;
11
12import org.junit.jupiter.api.Assertions;
13import org.junit.jupiter.api.Test;
14
15class CollectionsUtilTests {
16 List<Integer> list10 = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
17 List<String> listTen = List.of("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
18
19 private static <T> void compare(Iterable<T> a, Iterable<T> b) {
20 List<T> listA = toList(a);
21 List<T> listB = toList(b);
22 assertEquals(listA, listB);
23 }
24
25 private static <T> List<T> toList(Iterable<T> a) {
26 List<T> result = new ArrayList<T>();
27 Iterator<T> iterator = a.iterator();
28 while (iterator.hasNext()) {
29 result.add(iterator.next());
30 }
31 return result;
32 }
33
34 @Test
35 void testFilterEven() {
36 compare(List.of(2, 4, 6, 8, 10), filter(list10, (x -> x % 2 == 0)));
37 }
38
39 @Test
40 void testFilterOdd() {
41 compare(List.of(1, 3, 5, 7, 9), filter(list10, (x -> x % 2 == 1)));
42 }
43
44 @Test
45 void testFilterFalse() {
46 compare(List.of(), filter(list10, (x -> false)));
47 }
48
49 @Test
50 void testFilterTrue() {
51 compare(list10, filter(list10, (x -> true)));
52 }
53
54 @Test
55 void testFilterEmpty() {
56 compare(List.of(), filter(List.of(), (x -> true)));
57 }
58
59 @Test()
60 void testNoSuchElement() {
61 Iterable<Integer> iterable = filter(list10, (x -> x % 2 == 0));
62 Iterator<Integer> iterator = iterable.iterator();
63 while (iterator.hasNext()) {
64 iterator.next();
65 }
66 Assertions.assertThrows(NoSuchElementException.class, () -> iterator.next());
67 }
68
69 @Test()
70 void mapTest() {
71 compare(listTen, map(list10, x -> x.toString()));
72 }
73
74 @Test()
75 void mapEmtyTest() {
76 compare(List.of(), map(List.of(), x -> x.toString()));
77 }
78}