1
1
package fj .data .fingertrees ;
2
2
3
+ import fj .Function ;
3
4
import fj .P ;
4
5
import fj .P2 ;
5
- import fj .Show ;
6
6
import fj .data .List ;
7
- import fj .data .Stream ;
7
+ import fj .data .Option ;
8
8
import org .junit .Test ;
9
9
10
- import static fj .P .p ;
11
- import static fj .Show .intShow ;
12
- import static fj .test .Property .prop ;
13
- import static fj .test .Property .property ;
14
- import static java .lang .System .out ;
10
+ import static fj .Monoid .intAdditionMonoid ;
11
+ import static fj .Monoid .intMinMonoid ;
12
+ import static fj .data .fingertrees .FingerTree .measured ;
15
13
import static org .hamcrest .CoreMatchers .equalTo ;
16
14
import static org .junit .Assert .assertThat ;
15
+ import static org .hamcrest .core .Is .is ;
16
+
17
17
18
18
/**
19
19
* Created by MarkPerry on 10/10/2015.
@@ -24,18 +24,35 @@ public class FingerTreeTest {
24
24
25
25
@ Test
26
26
public void size () {
27
- validateSize (List .list (-92 , 68 , 54 , -77 , -18 , 67 ));
28
- validateSize (List .list (-92 , 68 , 54 , -77 , -18 , 67 , -60 , 23 , -70 , 99 , 66 , -79 , -5 ));
27
+ validateOperations (List .list (-92 , 68 , 54 , -77 , -18 , 67 ));
28
+ validateOperations (List .list (-92 , 68 , 54 , -77 , -18 , 67 , -60 , 23 , -70 , 99 , 66 , -79 , -5 ));
29
29
}
30
30
31
- void validateSize (List <Integer > list ) {
31
+ void validateOperations (List <Integer > list ) {
32
32
FingerTree <Integer , Integer > ft = list .foldLeft (
33
33
(acc , i ) -> acc .snoc (i ), FingerTree .<Integer >emptyIntAddition ()
34
34
);
35
35
assertThat (ft .measure (), equalTo (list .length ()));
36
+ assertThat (ft .foldLeft ((s , i ) -> s + 1 , 0 ), equalTo (list .length ()));
37
+ assertThat (ft .foldRight ((i , s ) -> 1 + s , 0 ), equalTo (list .length ()));
38
+ assertThat (ft .filter (e -> e .equals (-77 )).head (), equalTo (-77 ));
36
39
assertThat (ft .length (), equalTo (list .length ()));
37
40
}
38
41
42
+ @ Test
43
+ public void testHeadOption () {
44
+ assertThat (Empty .emptyIntAddition ().headOption (), is (Option .none ()));
45
+ FingerTree <Integer , Integer > ft = new MakeTree (measured (intAdditionMonoid , Function .constant (1 ))).single (1 );
46
+ assertThat (ft .headOption (), is (Option .some (1 )));
47
+ }
48
+
49
+ @ Test
50
+ public void testUncons () {
51
+ assertThat (Empty .emptyIntAddition ().uncons (0 , (h , t ) -> h ), is (0 ));
52
+ FingerTree <Integer , Integer > ft = new MakeTree (measured (intAdditionMonoid , Function .constant (1 ))).single (1 );
53
+ assertThat (ft .uncons (0 , (h , t ) -> h ), is (1 ));
54
+ }
55
+
39
56
public FingerTree <Integer , Integer > midSeq () {
40
57
FingerTree <Integer , Integer > ft = FingerTree .emptyIntAddition ();
41
58
return List .range (1 , SIZE ).foldLeft (ft2 -> i -> ft2 .snoc (i ), ft );
0 commit comments