|
4 | 4 | import fj.Ord; |
5 | 5 | import fj.P3; |
6 | 6 | import fj.Show; |
| 7 | +import fj.P; |
7 | 8 | import org.junit.Assert; |
8 | 9 | import org.junit.Test; |
9 | 10 |
|
| 11 | +import static fj.data.Option.some; |
| 12 | +import static org.junit.Assert.assertTrue; |
| 13 | + |
10 | 14 | /** |
11 | 15 | * Created by MarkPerry on 11/01/2015. |
12 | 16 | */ |
13 | 17 | public class TreeMapTest { |
14 | 18 |
|
15 | 19 | @Test |
16 | 20 | public void split() { |
| 21 | + // do the split |
17 | 22 | int pivot = 4; |
18 | | - List<Integer> l = List.range(1, 6); |
| 23 | + int max = 5; |
| 24 | + List<Integer> l = List.range(1, max + 1); |
19 | 25 | TreeMap<Integer, String> m2 = TreeMap.map(Ord.intOrd, l.zip(l.map(i -> i.toString()))); |
20 | | - P3<Set<String>, Option<String>, Set<String>> p = m2.split(pivot); |
| 26 | + P3<Set<String>, Option<String>, Set<String>> p = m2.split(Ord.stringOrd, pivot); |
| 27 | + |
| 28 | + // print debug info |
21 | 29 | Show<TreeMap<Integer, String>> st = Show.treeMapShow(Show.intShow, Show.stringShow); |
22 | 30 | Show<Set<String>> ss = Show.setShow(Show.stringShow); |
23 | 31 | Show<Option<String>> so = Show.optionShow(Show.stringShow); |
24 | 32 | Show<P3<Set<String>, Option<String>, Set<String>>> sp3 = Show.p3Show(ss, so, ss); |
| 33 | + if (true) { |
| 34 | + st.println(m2); |
| 35 | + sp3.println(p); |
| 36 | + } |
25 | 37 |
|
26 | | - st.println(m2); |
27 | | - sp3.println(p); |
28 | | - |
29 | | - Equal<Set<String>> eq = Equal.setEqual(Equal.stringEqual); |
30 | | - Set<String> left = toSetString(List.list(1, 2, 3)); |
31 | | - Set<String> right = toSetString(List.list(5)); |
32 | | - Assert.assertTrue("Left side of split unexpected", eq.eq(left, p._1())); |
33 | | - Assert.assertTrue(eq.eq(right, p._1())); |
34 | | - Assert.assertTrue(Equal.optionEqual(Equal.stringEqual).eq(p._2(), Option.some(Integer.toString(pivot)))); |
| 38 | + // assert equals |
| 39 | + Equal<Set<String>> seq = Equal.setEqual(Equal.stringEqual); |
| 40 | + Set<String> left = toSetString(List.range(1, pivot)); |
| 41 | + Set<String> right = toSetString(List.range(pivot + 1, max + 1)); |
| 42 | + P3<Set<String>, Option<String>, Set<String>> expected = P.p(left, some(Integer.toString(pivot)), right); |
| 43 | + assertTrue(Equal.p3Equal(seq, Equal.optionEqual(Equal.stringEqual), seq).eq(p, expected)); |
35 | 44 | } |
36 | 45 |
|
37 | 46 | private static Set<String> toSetString(List<Integer> list) { |
38 | 47 | return Set.set(Ord.stringOrd, list.map(i -> i.toString())); |
39 | 48 | } |
40 | 49 |
|
| 50 | + @Test |
| 51 | + public void splitLookup() { |
| 52 | + // do the split |
| 53 | + int pivot = 4; |
| 54 | + int max = 5; |
| 55 | + List<Integer> l = List.range(1, max + 1); |
| 56 | + TreeMap<Integer, String> m2 = TreeMap.map(Ord.intOrd, l.zip(l.map(i -> i.toString()))); |
| 57 | + P3<TreeMap<Integer, String>, Option<String>, TreeMap<Integer, String>> p3 = m2.splitLookup(pivot); |
| 58 | + |
| 59 | + // create expected output |
| 60 | + List<Integer> leftList = List.range(1, pivot); |
| 61 | + TreeMap<Integer, String> leftMap = TreeMap.map(Ord.intOrd, leftList.zip(leftList.map(i -> i.toString()))); |
| 62 | + List<Integer> rightList = List.range(pivot + 1, max + 1); |
| 63 | + TreeMap<Integer, String> rightMap = TreeMap.map(Ord.intOrd, rightList.zip(rightList.map(i -> i.toString()))); |
| 64 | + |
| 65 | + // debug info |
| 66 | + if (true) { |
| 67 | + Show<TreeMap<Integer, String>> st = Show.treeMapShow(Show.intShow, Show.stringShow); |
| 68 | + Show<P3<TreeMap<Integer, String>, Option<String>, TreeMap<Integer, String>>> sp3 = Show.p3Show(st, Show.optionShow(Show.stringShow), st); |
| 69 | + sp3.println(p3); |
| 70 | + } |
| 71 | + |
| 72 | + // do the assert |
| 73 | + Equal<TreeMap<Integer, String>> tme = Equal.treeMapEqual(Equal.intEqual, Equal.stringEqual); |
| 74 | + Equal<P3<TreeMap<Integer, String>, Option<String>, TreeMap<Integer, String>>> eq = Equal.p3Equal(tme, Equal.optionEqual(Equal.stringEqual), tme); |
| 75 | + assertTrue(eq.eq(p3, P.p(leftMap, some(Integer.toString(pivot)), rightMap))); |
| 76 | + } |
| 77 | + |
41 | 78 | } |
0 commit comments