forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCollectionsTest.java
More file actions
100 lines (71 loc) · 2.6 KB
/
CollectionsTest.java
File metadata and controls
100 lines (71 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) 2014-2016 K Team. All Rights Reserved.
package org.kframework;
import java.util.stream.Stream;
import org.junit.Test;
import scala.collection.immutable.List;
import scala.collection.Set;
import static org.kframework.Collections.*;
import static org.junit.Assert.*;
public class CollectionsTest {
@Test
public void testList() {
// creating a List
List<Integer> aList = List(1, 2, 3);
// getting a Stream from a list
Stream<Integer> s = stream(aList);
// usual Java 8 manipulation
Stream<String> l = s.map(x -> x.toString());
// and back to an immutable List
List<String> collectedList = l.collect(toList());
// which has the expected value
assertEquals(List("1", "2", "3"), collectedList);
}
@Test
public void testSet() {
// creating a Set
Set<Integer> aList = Set(1, 2, 3);
// getting a Stream from a Set
Stream<Integer> s = stream(aList);
// usual Java 8 manipulation
Stream<Integer> l = s.map(x -> x / 2);
// and back to an immutable Set
Set<Integer> collectedList = l.collect(toSet());
// which has the expected value
assertEquals(Set(0, 1), collectedList);
}
@Test
public void testAssociativeList() {
Stream<Integer> s = stream(List(1, 2, 3));
// splitting 3 into a list
Stream<Object> l = s.map(x -> {
if (x == 3)
return List(1, 2);
else
return x;
});
// and now... converting it to an Sssociative List
List<Object> collectedList = l.collect(toAssociativeList());
// check out the result
assertEquals(List(1, 2, 1, 2), collectedList);
// yes, the types are not perfect, some casting is needed,
// but you cannot really ask too much from Java
}
@Test
public void moreNestingJustToBeSure() {
Stream<Object> s = stream(List(1, List(2, List(3, 4)), List(5)));
// and now... converting it to an assoc List
List<Object> collectedList = s.collect(toAssociativeList());
// check out the result
assertEquals(List(1, 2, 3, 4, 5), collectedList);
}
@Test
public void testAssociativeSet() {
Stream<Integer> s = stream(Set(1, 2, 3));
// usual Java 8 manipulation
Stream<Object> l = s.map(x -> Set(x / 2));
// and back to an *assoc* Set
Set<Object> collectedList = l.collect(toAssociativeSet());
// which has the expected value
assertEquals(Set(0, 1), collectedList);
}
}