diff options
author | 2021-08-15 16:07:40 +0200 | |
---|---|---|
committer | 2021-08-15 16:07:40 +0200 | |
commit | 6fae62f537b29ad38f76c487b3e7c6014bd87acf (patch) | |
tree | 2eb159a539ebf578d7ad318ffa0fb0e7c400bbc5 | |
parent | Added filter (diff) | |
download | refinery-6fae62f537b29ad38f76c487b3e7c6014bd87acf.tar.gz refinery-6fae62f537b29ad38f76c487b3e7c6014bd87acf.tar.zst refinery-6fae62f537b29ad38f76c487b3e7c6014bd87acf.zip |
Tests for CollectionsUtil
-rw-r--r-- | model-data/src/test/java/org/eclipse/viatra/solver/data/util/CollectionsUtilTests.java | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/model-data/src/test/java/org/eclipse/viatra/solver/data/util/CollectionsUtilTests.java b/model-data/src/test/java/org/eclipse/viatra/solver/data/util/CollectionsUtilTests.java new file mode 100644 index 00000000..ccc5cbeb --- /dev/null +++ b/model-data/src/test/java/org/eclipse/viatra/solver/data/util/CollectionsUtilTests.java | |||
@@ -0,0 +1,73 @@ | |||
1 | package org.eclipse.viatra.solver.data.util; | ||
2 | |||
3 | import java.util.ArrayList; | ||
4 | import java.util.Iterator; | ||
5 | import java.util.List; | ||
6 | import java.util.NoSuchElementException; | ||
7 | |||
8 | import org.junit.jupiter.api.Assertions; | ||
9 | import org.junit.jupiter.api.Test; | ||
10 | |||
11 | import static org.eclipse.viatra.solver.data.util.CollectionsUtil.*; | ||
12 | import static org.junit.jupiter.api.Assertions.assertEquals; | ||
13 | |||
14 | class CollectionsUtilTests { | ||
15 | List<Integer> list10 = List.of(1,2,3,4,5,6,7,8,9,10); | ||
16 | List<String> listTen = List.of("1","2","3","4","5","6","7","8","9","10"); | ||
17 | |||
18 | private static <T> void compare(Iterable<T> a, Iterable<T> b) { | ||
19 | List<T> listA = toList(a); | ||
20 | List<T> listB = toList(b); | ||
21 | assertEquals(listA, listB); | ||
22 | } | ||
23 | private static <T> List<T> toList(Iterable<T> a) { | ||
24 | List<T> result = new ArrayList<T>(); | ||
25 | Iterator<T> iterator = a.iterator(); | ||
26 | while(iterator.hasNext()) { | ||
27 | result.add(iterator.next()); | ||
28 | } | ||
29 | return result; | ||
30 | } | ||
31 | |||
32 | @Test | ||
33 | void testFilterEven() { | ||
34 | compare(List.of(2,4,6,8,10), filter(list10, (x->x%2==0))); | ||
35 | } | ||
36 | |||
37 | @Test | ||
38 | void testFilterOdd() { | ||
39 | compare(List.of(1,3,5,7,9), filter(list10, (x->x%2==1))); | ||
40 | } | ||
41 | |||
42 | @Test | ||
43 | void testFilterFalse() { | ||
44 | compare(List.of(), filter(list10, (x->false))); | ||
45 | } | ||
46 | |||
47 | @Test | ||
48 | void testFilterTrue() { | ||
49 | compare(list10, filter(list10, (x->true))); | ||
50 | } | ||
51 | |||
52 | @Test | ||
53 | void testFilterEmpty() { | ||
54 | compare(List.of(), filter(List.of(), (x->true))); | ||
55 | } | ||
56 | |||
57 | @Test() | ||
58 | void testNoSuchElement() { | ||
59 | Iterable<Integer> iterable = filter(list10, (x->x%2==0)); | ||
60 | Iterator<Integer> iterator = iterable.iterator(); | ||
61 | while(iterator.hasNext()) { | ||
62 | iterator.next(); | ||
63 | } | ||
64 | Assertions.assertThrows(NoSuchElementException.class, ()->iterator.next()); | ||
65 | } | ||
66 | |||
67 | void mapTest() { | ||
68 | compare(listTen,map(list10, x->x.toString())); | ||
69 | } | ||
70 | void mapEmtyTest() { | ||
71 | compare(List.of(),map(List.of(), x->x.toString())); | ||
72 | } | ||
73 | } | ||