forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHamtTest.java
More file actions
66 lines (55 loc) · 1.9 KB
/
HamtTest.java
File metadata and controls
66 lines (55 loc) · 1.9 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
package fj.data.hamt;
import fj.Ord;
import fj.P2;
import fj.data.List;
import fj.data.Option;
import org.junit.Test;
import static fj.Equal.intEqual;
import static fj.Equal.optionEqual;
import static fj.Hash.intHash;
import static fj.P.p;
import static fj.data.List.list;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* @author Mark Perry
*/
public class HamtTest {
public static final HashArrayMappedTrie<Integer, Integer> empty = HashArrayMappedTrie.emptyKeyInteger();
@Test
public void empty() {
assertThat(empty.length(), equalTo(0));
}
@Test
public void lengthOne() {
assertThat(empty.set(3, 6).length(), equalTo(1));
}
@Test
public void updateLength() {
HashArrayMappedTrie<Integer, Integer> h1 = empty.set(3, 3).set(3, 5);
assertThat(h1.length(), equalTo(1));
}
@Test
public void streamLength() {
List<P2<Integer, Integer>> list = list(p(0, 1), p(31, 1), p(32, 1), p(33, 1));
HashArrayMappedTrie<Integer, Integer> h2 = empty.set(list);
assertThat(h2.toStream().length(), equalTo(list.length()));
}
@Test
public void allIn() {
List<P2<Integer, Integer>> list = List.list(p(-5, 0), p(-1, -5), p(2, 4), p(4, -2));
HashArrayMappedTrie<Integer, Integer> h = empty.set(list);
Boolean b = list.foldLeft((acc, p) -> h.find(p._1()).option(false, i -> true && acc), true);
assertThat(b, equalTo(true));
}
@Test
public void sampleInts() {
List<P2<Integer, Integer>> ps = List.list(p(-3, 0), p(1, 2));
int key = -3;
HashArrayMappedTrie<Integer, Integer> h = empty.set(ps);
Option<Integer> o1 = ps.find(p -> intEqual.eq(p._1(), key)).map(p -> p._2());
Option<Integer> o2 = h.find(key);
boolean b = optionEqual(intEqual).eq(o1, o2);
assertThat(b, equalTo(true));
}
}