summaryrefslogtreecommitdiff
path: root/src/test/java/au/id/zancanaro/javacheck/SetFunctorRulesTest.java
blob: 04fa674a34976c8eacdc1901a014f610d719f54c (about) (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package au.id.zancanaro.javacheck;

import au.id.zancanaro.javacheck.annotations.DataSource;
import au.id.zancanaro.javacheck.annotations.Property;
import au.id.zancanaro.javacheck.junit.Properties;
import org.junit.runner.RunWith;

import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import static au.id.zancanaro.javacheck.Generators.longInteger;
import static au.id.zancanaro.javacheck.Generators.oneOf;
import static org.junit.Assert.assertEquals;

@RunWith(Properties.class)
public class SetFunctorRulesTest {

    @DataSource
    public static Generator<Function<Long, Long>> integerFunction =
            oneOf(
                    longInteger().map(SetFunctorRulesTest::plusI),
                    longInteger().map(SetFunctorRulesTest::timesI),
                    longInteger().map(SetFunctorRulesTest::constantlyI));

    @Property
    public void mappingCompositionsWithStreams(
            Set<Long> list,
            Function<Long, Long> f,
            Function<Long, Long> g) {
        Set<Long> left = list.stream()
                .map(g)
                .map(f)
                .collect(Collectors.toSet());

        Set<Long> right = list.stream()
                .map(f.compose(g))
                .collect(Collectors.toSet());

        assertEquals(left, right);
    }

    @Property
    public void mappingCompositionsWithIntermediateList(
            Set<Long> list,
            Function<Long, Long> f,
            Function<Long, Long> g) {
        Set<Long> intermediate = list.stream().map(g).collect(Collectors.toSet());
        Set<Long> left = intermediate.stream().map(f).collect(Collectors.toSet());

        Set<Long> right = list.stream()
                .map(f.compose(g))
                .collect(Collectors.toSet());

        assertEquals(left, right);
    }

    @Property
    public void mapIdentityIsIdentity(Set<Long> list) {
        Set<Long> mapped = list.stream().map(x -> x).collect(Collectors.toSet());

        assertEquals(list, mapped);
    }

    private static Function<Long, Long> plusI(final long i) {
        return new Function<Long, Long>() {
            @Override
            public Long apply(Long integer) {
                return i + integer;
            }

            @Override
            public String toString() {
                return "x -> x + " + i;
            }
        };
    }

    private static Function<Long, Long> timesI(final long i) {
        return new Function<Long, Long>() {
            @Override
            public Long apply(Long integer) {
                return i * integer;
            }

            @Override
            public String toString() {
                return "x -> x * " + i;
            }
        };
    }

    private static Function<Long, Long> constantlyI(final long i) {
        return new Function<Long, Long>() {
            @Override
            public Long apply(Long integer) {
                return i;
            }

            @Override
            public String toString() {
                return "x -> " + i;
            }
        };
    }
}