View Javadoc

1   /*
2      Copyright 2011 Atlassian
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15   */
16  package io.atlassian.fugue;
17  
18  import org.junit.Test;
19  
20  import java.util.function.BiFunction;
21  import java.util.function.Function;
22  
23  import static io.atlassian.fugue.Functions.fold;
24  import static java.util.Arrays.asList;
25  import static org.hamcrest.Matchers.is;
26  import static org.junit.Assert.assertThat;
27  
28  public class FunctionsFoldTest {
29    @Test public void f2FoldSum() {
30      final BiFunction<Integer, Integer, Integer> add = (arg1, arg2) -> arg1 + arg2;
31      assertThat(fold(add, 0, asList(1, 2, 3, 4, 5)), is(15));
32    }
33  
34    @Test public void f2FoldMultiply() {
35      final BiFunction<Integer, Integer, Integer> mult = (arg1, arg2) -> arg1 * arg2;
36      assertThat(fold(mult, 1, asList(1, 2, 3, 4)), is(24));
37    }
38  
39    @Test public void f2FoldTypes() {
40      final BiFunction<String, Integer, String> append = (s, i) -> s + "  " + i;
41      assertThat(fold(append, "Iterable:", asList(12, 15, 20)), is("Iterable:  12  15  20"));
42    }
43  
44    @Test public void f1FoldSum() {
45      final Function<Pair<Integer, Integer>, Integer> add = arg -> arg.left() + arg.right();
46      assertThat(fold(add, 0, asList(1, 2, 3, 4, 5, 6)), is(21));
47    }
48  
49    @Test public void f1FoldMultiply() {
50      final Function<Pair<Integer, Integer>, Integer> mult = arg -> arg.left() * arg.right();
51      assertThat(fold(mult, 1, asList(1, 2, 3, 4, 5)), is(120));
52    }
53  
54    @Test public void f1FoldTypes() {
55      final Function<Pair<String, Integer>, String> append = t -> t.left() + "  " + t.right();
56      assertThat(fold(append, "Iterable:", asList(12, 15, 20)), is("Iterable:  12  15  20"));
57    }
58  
59    @Test public void f1FoldVariance() {
60      final Function<Pair<Number, Number>, Number> mult = arg -> arg.left().intValue() * arg.right().intValue();
61      assertThat(fold(mult, 1, asList(1, 2, 3, 4, 5)), is((Number) 120));
62    }
63  
64    @Test public void f2FoldVariance() {
65      final BiFunction<Number, Number, Number> mult = (arg1, arg2) -> arg1.intValue() * arg2.intValue();
66      assertThat(fold(mult, 1, asList(1, 2, 3, 4, 5)), is((Number) 120));
67    }
68  
69    @Test public void f1FoldVarianceFirstArg() {
70      final Function<Pair<Number, Integer>, Integer> mult = arg -> arg.left().intValue() * arg.right();
71      assertThat(fold(mult, 1, asList(1, 2, 3, 4, 5)), is(120));
72    }
73  
74    @Test public void f2FoldVarianceFirstArg() {
75      final BiFunction<Number, Integer, Integer> mult = (arg1, arg2) -> arg1.intValue() * arg2;
76      assertThat(fold(mult, 1, asList(1, 2, 3, 4, 5)), is(120));
77    }
78  }