11package fj .data .fingertrees ;
22
3+ import fj .Function ;
34import fj .P ;
45import fj .P2 ;
5- import fj .Show ;
66import fj .data .List ;
7- import fj .data .Stream ;
7+ import fj .data .Option ;
88import org .junit .Test ;
99
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 ;
1513import static org .hamcrest .CoreMatchers .equalTo ;
1614import static org .junit .Assert .assertThat ;
15+ import static org .hamcrest .core .Is .is ;
16+
1717
1818/**
1919 * Created by MarkPerry on 10/10/2015.
@@ -24,18 +24,35 @@ public class FingerTreeTest {
2424
2525 @ Test
2626 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 ));
2929 }
3030
31- void validateSize (List <Integer > list ) {
31+ void validateOperations (List <Integer > list ) {
3232 FingerTree <Integer , Integer > ft = list .foldLeft (
3333 (acc , i ) -> acc .snoc (i ), FingerTree .<Integer >emptyIntAddition ()
3434 );
3535 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 ));
3639 assertThat (ft .length (), equalTo (list .length ()));
3740 }
3841
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+
3956 public FingerTree <Integer , Integer > midSeq () {
4057 FingerTree <Integer , Integer > ft = FingerTree .emptyIntAddition ();
4158 return List .range (1 , SIZE ).foldLeft (ft2 -> i -> ft2 .snoc (i ), ft );
0 commit comments