Skip to content

Commit 2a2b8ea

Browse files
committed
Added priority queue properties
1 parent 6bd372b commit 2a2b8ea

2 files changed

Lines changed: 100 additions & 37 deletions

File tree

core/src/test/java/fj/data/PriorityQueueTest.java

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,5 @@
1414
*/
1515
public class PriorityQueueTest {
1616

17-
@Test
18-
public void test1() {
19-
PriorityQueue<Integer, Integer> pq = PriorityQueue.<Integer>emptyInt();
20-
PriorityQueue<Integer, Integer> pq2 = pq.enqueue(List.list(p(1, 1)));
21-
out.println(pq2.toString());
22-
}
23-
24-
@Test
25-
public void empty() {
26-
PriorityQueue<Integer, Integer> pq = PriorityQueue.<Integer>emptyInt();
27-
assertThat(pq.isEmpty(), is(true));
28-
}
29-
30-
@Test
31-
public void top() {
32-
PriorityQueue<Integer, Integer> pq1 = PriorityQueue.emptyInt();
33-
Option<P2<Integer, Integer>> o = pq1.top();
34-
assertThat(o, is(Option.none()));
35-
}
36-
37-
@Test
38-
public void dequeue() {
39-
PriorityQueue<Integer, Integer> pq1 = PriorityQueue.<Integer>emptyInt().enqueue(List.list(
40-
p(3, 6), p(10, 20), p(4, 8), p(1, 2)
41-
));
42-
out.println(pq1);
43-
assertThat(pq1.toString(), equalTo("PriorityQueue((3: 6), (10: 20), (4: 8), (1: 2))"));
44-
45-
PriorityQueue<Integer, Integer> pq2 = PriorityQueue.emptyInt();
46-
PriorityQueue<Integer, Integer> pq3 = pq2.dequeue();
47-
out.println(pq3);
48-
assertThat(pq3.toString(), equalTo("PriorityQueue()"));
49-
50-
PriorityQueue<Integer, Integer> pq4 = pq1.dequeue();
51-
out.println(pq4);
52-
assertThat(pq4.toString(), equalTo("PriorityQueue((3: 6), (4: 8), (1: 2))"));
53-
}
5417

5518
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)