|
| 1 | +package fj.data.properties; |
| 2 | + |
| 3 | + |
| 4 | +import fj.Function; |
| 5 | +import fj.P; |
| 6 | +import fj.P2; |
| 7 | +import fj.data.Option; |
| 8 | +import fj.data.Seq; |
| 9 | +import fj.runner.PropertyTestRunner; |
| 10 | +import fj.test.Arbitrary; |
| 11 | +import fj.test.Gen; |
| 12 | +import fj.test.Property; |
| 13 | +import org.junit.Assert; |
| 14 | +import org.junit.Test; |
| 15 | +import org.junit.runner.RunWith; |
| 16 | +import org.junit.runners.BlockJUnit4ClassRunner; |
| 17 | + |
| 18 | +import static fj.Function.identity; |
| 19 | +import static fj.data.Option.none; |
| 20 | +import static fj.data.Option.some; |
| 21 | +import static fj.test.Arbitrary.arbInteger; |
| 22 | +import static fj.test.Arbitrary.arbSeq; |
| 23 | +import static fj.test.Property.implies; |
| 24 | +import static fj.test.Property.prop; |
| 25 | +import static fj.test.Property.property; |
| 26 | + |
| 27 | +/** |
| 28 | + * Created by Zheka Kozlov on 01.06.2015. |
| 29 | + */ |
| 30 | +@RunWith(PropertyTestRunner.class) |
| 31 | +public class SeqProperties { |
| 32 | + |
| 33 | + public Property consHead() { |
| 34 | + return property(arbSeq(arbInteger), arbInteger, (seq, n) -> prop(seq.cons(n).head().equals(n))); |
| 35 | + } |
| 36 | + |
| 37 | + public Property consLength() { |
| 38 | + return property(arbSeq(arbInteger), arbInteger, (seq, n) -> prop(seq.cons(n).length() == seq.length() + 1)); |
| 39 | + } |
| 40 | + |
| 41 | + public Property snocLast() { |
| 42 | + return property(arbSeq(arbInteger), arbInteger, (seq, n) -> prop(seq.snoc(n).last().equals(n))); |
| 43 | + } |
| 44 | + |
| 45 | + public Property snocLength() { |
| 46 | + return property(arbSeq(arbInteger), arbInteger, (seq, n) -> prop(seq.snoc(n).length() == seq.length() + 1)); |
| 47 | + } |
| 48 | + |
| 49 | + public Property appendEmptyLeft() { |
| 50 | + return property(arbSeq(arbInteger), seq -> prop(Seq.<Integer>empty().append(seq).equals(seq))); |
| 51 | + } |
| 52 | + |
| 53 | + public Property appendEmptyRight() { |
| 54 | + return property(arbSeq(arbInteger), seq -> prop(seq.append(Seq.empty()).equals(seq))); |
| 55 | + } |
| 56 | + |
| 57 | + public Property appendLength() { |
| 58 | + return property(arbSeq(arbInteger), arbSeq(arbInteger), (seq1, seq2) -> |
| 59 | + prop(seq1.append(seq2).length() == seq1.length() + seq2.length())); |
| 60 | + } |
| 61 | + |
| 62 | + public Property consNotEmpty() { |
| 63 | + return property(arbSeq(arbInteger), arbInteger, (seq, n) -> prop(!seq.cons(n).isEmpty())); |
| 64 | + } |
| 65 | + |
| 66 | + public Property snocNotEmpty() { |
| 67 | + return property(arbSeq(arbInteger), arbInteger, (seq, n) -> prop(!seq.snoc(n).isEmpty())); |
| 68 | + } |
| 69 | + |
| 70 | + public Property appendSingleLeft() { |
| 71 | + return property(arbSeq(arbInteger), arbInteger, (seq, n) -> prop(Seq.single(n).append(seq).equals(seq.cons(n)))); |
| 72 | + } |
| 73 | + |
| 74 | + public Property appendSingleRight() { |
| 75 | + return property(arbSeq(arbInteger), arbInteger, (seq, n) -> prop(seq.append(Seq.single(n)).equals(seq.snoc(n)))); |
| 76 | + } |
| 77 | + |
| 78 | + public Property splitLength() { |
| 79 | + return property(arbSeq(arbInteger), arbInteger, (seq, i) -> prop(seq.length() == seq.split(i)._1().length() + seq.split(i)._2().length())); |
| 80 | + } |
| 81 | + |
| 82 | + public Property tailLength() { |
| 83 | + return property(arbSeq(arbInteger), seq -> |
| 84 | + Property.implies(!seq.isEmpty(), P.lazy(() -> prop(seq.length() == 1 + seq.tail().length())))); |
| 85 | + } |
| 86 | + |
| 87 | + public Property initLength() { |
| 88 | + return property(arbSeq(arbInteger), seq -> |
| 89 | + Property.implies(!seq.isEmpty(), P.lazy(() -> prop(seq.length() == seq.init().length() + 1)))); |
| 90 | + } |
| 91 | + |
| 92 | + public Property mapId() { |
| 93 | + return property(arbSeq(arbInteger), seq -> prop(seq.map(identity()).equals(seq))); |
| 94 | + } |
| 95 | + |
| 96 | + public Property updateAndIndex() { |
| 97 | + final Gen<P2<Seq<Integer>, Option<Integer>>> gen = arbSeq(arbInteger).gen.bind(seq -> { |
| 98 | + if (seq.isEmpty()) { |
| 99 | + return Gen.value(P.p(seq, none())); |
| 100 | + } else { |
| 101 | + return Gen.choose(0, seq.length() - 1).map(i -> P.p(seq, some(i))); |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + return property(Arbitrary.arbitrary(gen), arbInteger, (pair, n) -> |
| 106 | + implies(pair._2().isSome(), P.lazy(() -> { |
| 107 | + final Seq<Integer> seq = pair._1(); |
| 108 | + final int index = pair._2().some(); |
| 109 | + return prop(seq.update(index, n).index(index).equals(n)); |
| 110 | + }))); |
| 111 | + } |
| 112 | + |
| 113 | + public Property foldLeft() { |
| 114 | + return property(arbSeq(Arbitrary.arbitrary(Gen.value(1))), seq -> |
| 115 | + prop(seq.foldLeft((acc, i) -> acc + i, 0) == seq.length())); |
| 116 | + } |
| 117 | + |
| 118 | + public Property foldRight() { |
| 119 | + return property(arbSeq(Arbitrary.arbitrary(Gen.value(1))), seq -> |
| 120 | + prop(seq.foldRight((i, acc) -> acc + i, 0) == seq.length())); |
| 121 | + } |
| 122 | +} |
0 commit comments