|
| 1 | +package fj.data.properties; |
| 2 | + |
| 3 | +import fj.Ord; |
| 4 | +import fj.P; |
| 5 | +import fj.P2; |
| 6 | +import fj.data.List; |
| 7 | +import fj.data.Option; |
| 8 | +import fj.data.PriorityQueue; |
| 9 | +import fj.data.Set; |
| 10 | +import fj.test.Arbitrary; |
| 11 | +import fj.test.Property; |
| 12 | +import fj.test.reflect.CheckParams; |
| 13 | +import fj.test.runner.PropertyTestRunner; |
| 14 | +import org.junit.runner.RunWith; |
| 15 | + |
| 16 | +import static fj.data.Option.some; |
| 17 | +import static fj.data.PriorityQueue.emptyInt; |
| 18 | +import static fj.test.Arbitrary.arbAlphaNumString; |
| 19 | +import static fj.test.Arbitrary.arbInteger; |
| 20 | +import static fj.test.Arbitrary.arbSet; |
| 21 | +import static fj.test.Arbitrary.arbitrary; |
| 22 | +import static fj.test.Property.impliesBoolean; |
| 23 | +import static fj.test.Property.prop; |
| 24 | +import static fj.test.Property.property; |
| 25 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 26 | +import static org.junit.Assert.assertThat; |
| 27 | + |
| 28 | +/** |
| 29 | + * Created by MarkPerry on 18 Jun 16. |
| 30 | + */ |
| 31 | +@RunWith(PropertyTestRunner.class) |
| 32 | +@CheckParams(maxSize = 100) |
| 33 | +public class PriorityQueueProperties { |
| 34 | + |
| 35 | + public static Arbitrary<PriorityQueue<Integer, String>> arbPriorityQueueIntegerString = arbUniqueQueue(arbAlphaNumString); |
| 36 | + |
| 37 | + public static <A> Arbitrary<PriorityQueue<Integer, A>> arbUniqueQueue(Arbitrary<A> aa) { |
| 38 | + Arbitrary<Set<Integer>> as = arbSet(Ord.intOrd, arbInteger); |
| 39 | + Arbitrary<List<Integer>> ints = arbitrary(as.gen.map(si -> si.toList())); |
| 40 | + Arbitrary<List<P2<Integer, A>>> alp = arbitrary( |
| 41 | + ints.gen.bind(li -> aa.gen.map(s -> li.map(i -> P.p(i, s)))) |
| 42 | + ); |
| 43 | + return arbitrary(alp.gen.map(l -> PriorityQueue.<A>emptyInt().enqueue(l))); |
| 44 | + } |
| 45 | + |
| 46 | + Property empty() { |
| 47 | + PriorityQueue<Integer, Object> pq = emptyInt(); |
| 48 | + return prop(pq.isEmpty()); |
| 49 | + } |
| 50 | + |
| 51 | + Property addRemove() { |
| 52 | + return property(arbPriorityQueueIntegerString, arbInteger, arbAlphaNumString, (q, i, s) -> { |
| 53 | + Option<P2<Integer, String>> o = q.top(); |
| 54 | + Option<P2<Integer, String>> o2 = q.enqueue(i, s).dequeue().top(); |
| 55 | + return Property.impliesBoolean( |
| 56 | + q.isGreaterThan(Ord.intOrd, i), |
| 57 | +// o.map(p -> i > p._1()).orSome(true), |
| 58 | + () -> o.equals(o2) |
| 59 | + ); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + Property emptyTop() { |
| 64 | + return prop(emptyInt().top().isNone()); |
| 65 | + } |
| 66 | + |
| 67 | + Property addTop() { |
| 68 | + return property(arbPriorityQueueIntegerString, arbInteger, arbAlphaNumString, (q, i, s) -> { |
| 69 | + Option<P2<Integer, String>> actual = q.enqueue(i, s).top(); |
| 70 | + return impliesBoolean( |
| 71 | + q.isGreaterThan(Ord.intOrd, i), |
| 72 | +// q.top().map(p -> p._1() < i).orSome(true), |
| 73 | + actual.equals(some(P.p(i, s)))); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + Property sorted() { |
| 78 | + Arbitrary<Set<Integer>> as = arbSet(Ord.intOrd, arbInteger); |
| 79 | + Arbitrary<List<Integer>> ints = arbitrary(as.gen.map(si -> si.toList())); |
| 80 | + Arbitrary<List<P2<Integer, String>>> alp = arbitrary( |
| 81 | + ints.gen.bind(li -> arbAlphaNumString.gen.map(s -> li.map(i -> P.p(i, s)))) |
| 82 | + ); |
| 83 | + return property(alp, list -> { |
| 84 | + PriorityQueue<Integer, String> q = PriorityQueue.<String>emptyInt().enqueue(list); |
| 85 | + List<P2<Integer, String>> expected = list.sort(Ord.p2Ord1(Ord.intOrd.reverse())); |
| 86 | + List<P2<Integer, String>> actual = q.toStream().toList(); |
| 87 | + assertThat(actual, equalTo(expected)); |
| 88 | + System.out.println(actual); |
| 89 | + return prop(actual.equals(expected)); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + public Property sorted2() { |
| 94 | + return property(arbPriorityQueueIntegerString, pq -> { |
| 95 | + List<P2<Integer, String>> expected = pq.toList().sort(Ord.p2Ord1(Ord.intOrd.reverse())); |
| 96 | + return prop(expected.equals(pq.toList())); |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments