|
| 1 | +package fj.data.properties; |
| 2 | + |
| 3 | +import fj.Equal; |
| 4 | +import fj.P; |
| 5 | +import fj.P2; |
| 6 | +import fj.data.List; |
| 7 | +import fj.runner.PropertyTestRunner; |
| 8 | +import fj.test.Gen; |
| 9 | +import fj.test.Property; |
| 10 | +import org.junit.runner.RunWith; |
| 11 | + |
| 12 | +import static fj.test.Arbitrary.*; |
| 13 | +import static fj.test.Property.implies; |
| 14 | +import static fj.test.Property.prop; |
| 15 | +import static fj.test.Property.property; |
| 16 | +import static fj.Equal.intEqual; |
| 17 | + |
| 18 | +/** |
| 19 | + * Created by Zheka Kozlov on 02.06.2015. |
| 20 | + */ |
| 21 | +@RunWith(PropertyTestRunner.class) |
| 22 | +public class ListProperties { |
| 23 | + |
| 24 | + public Property isPrefixOf() { |
| 25 | + final Gen<P2<List<Integer>, Integer>> gen = arbList(arbInteger).gen.bind(list -> |
| 26 | + Gen.choose(0, list.length()).map(i -> P.p(list, i))); |
| 27 | + |
| 28 | + return property(arbitrary(gen), pair -> prop(pair._1().take(pair._2()).isPrefixOf(intEqual, pair._1()))); |
| 29 | + } |
| 30 | + |
| 31 | + public Property isSuffixOf() { |
| 32 | + final Gen<P2<List<Integer>, Integer>> gen = arbList(arbInteger).gen.bind(list -> |
| 33 | + Gen.choose(0, list.length()).map(i -> P.p(list, i))); |
| 34 | + |
| 35 | + return property(arbitrary(gen), pair -> prop(pair._1().drop(pair._2()).isSuffixOf(intEqual, pair._1()))); |
| 36 | + } |
| 37 | + |
| 38 | + public Property isPrefixOfShorter() { |
| 39 | + return property(arbList(arbInteger), arbList(arbInteger), (list1, list2) -> |
| 40 | + implies(list1.length() > list2.length(), () -> prop(!list1.isPrefixOf(intEqual, list2)))); |
| 41 | + } |
| 42 | + |
| 43 | + public Property isSuffixOfShorter() { |
| 44 | + return property(arbList(arbInteger), arbList(arbInteger), (list1, list2) -> |
| 45 | + implies(list1.length() > list2.length(), () -> prop(!list1.isSuffixOf(intEqual, list2)))); |
| 46 | + } |
| 47 | + |
| 48 | + public Property isPrefixOfDifferentHeads() { |
| 49 | + return property(arbList(arbInteger), arbList(arbInteger), arbInteger, arbInteger, (list1, list2, h1, h2) -> |
| 50 | + implies(intEqual.notEq(h1, h2), () -> prop(!list1.cons(h1).isPrefixOf(intEqual, list2.cons(h2))))); |
| 51 | + } |
| 52 | + |
| 53 | + public Property isSuffixOfDifferentHeads() { |
| 54 | + return property(arbList(arbInteger), arbList(arbInteger), arbInteger, arbInteger, (list1, list2, h1, h2) -> |
| 55 | + implies(intEqual.notEq(h1, h2), () -> prop(!list1.snoc(h1).isSuffixOf(intEqual, list2.snoc(h2))))); |
| 56 | + } |
| 57 | +} |
0 commit comments