[m-rev.] diff 2/3: Update psqueue.m code style.
Paul Bone
paul at bone.id.au
Sun Dec 7 23:22:37 AEDT 2014
Branches: master
This patch has no semantic changes.
---
Update psqueue.m code style.
library/psqueue.m:
Update coding style.
In min_view/1 there's two variable names are incorrect (they should be
swapped) this doesn't change the program but may confuse people reading
the code.
tests/hard_coded/psqueue_test.exp:
tests/hard_coded/psqueue_test.m:
Also improve the coding style in the test code.
Add lables to all of the test outputs.
---
library/psqueue.m | 519 ++++++++++++++++++++------------------
tests/hard_coded/psqueue_test.exp | 4 +-
tests/hard_coded/psqueue_test.m | 32 ++-
3 files changed, 300 insertions(+), 255 deletions(-)
diff --git a/library/psqueue.m b/library/psqueue.m
index da22f20..481c02d 100644
--- a/library/psqueue.m
+++ b/library/psqueue.m
@@ -183,15 +183,27 @@
% (stored in this node) is considered to be part of one of the two subtrees,
% depending how it's key compares with the sort key.
-:- type psqueue(P, K) --->
- void
- ; winner(K, P, ltree(K, P), K).
+:- type psqueue(P, K)
+ ---> void
+ ; winner(
+ w_key :: K,
+ w_prio :: P,
+ w_loosers :: ltree(K, P),
+ w_max_key :: K
+ ).
:- type t_ltree_size == int.
-:- type ltree(K, P) --->
- start
- ; loser(t_ltree_size, K, P, ltree(K, P), K, ltree(K, P)).
+:- type ltree(K, P)
+ ---> start
+ ; loser(
+ l_size :: t_ltree_size,
+ l_looser_key :: K,
+ l_looser_prio :: P,
+ l_left_tree :: ltree(K, P),
+ l_sort_key :: K,
+ l_right_tree :: ltree(K, P)
+ ).
%---------------------------------------------------------------------------%
@@ -233,7 +245,6 @@ max_key(PSQ, MaxKey) :-
% Play tournament to combine two priority search queues, see Ralf Hinze's
% paper for explanantion.
%
-:- func tournament(psqueue(P, K), psqueue(P, K)) = psqueue(P, K) is det.
:- pred tournament(psqueue(P, K)::in, psqueue(P, K)::in, psqueue(P, K)::out)
is det.
:- pragma type_spec(tournament/3, P = int).
@@ -241,12 +252,17 @@ max_key(PSQ, MaxKey) :-
tournament(PSQ0, PSQ1, PSQ) :-
PSQ = tournament(PSQ0, PSQ1).
+:- func tournament(psqueue(P, K), psqueue(P, K)) = psqueue(P, K).
+:- pragma type_spec(tournament/2, P = int).
+
tournament(PSQ1, PSQ2) = Res :-
- ( PSQ1 = void,
+ (
+ PSQ1 = void,
Res = PSQ2
;
PSQ1 = winner(K1, Prio1, L1, MaxKey1),
- ( PSQ2 = void,
+ (
+ PSQ2 = void,
Res = PSQ1
;
PSQ2 = winner(K2, Prio2, L2, MaxKey2),
@@ -262,23 +278,23 @@ tournament(PSQ1, PSQ2) = Res :-
)
).
-
:- func second_best(ltree(K, P), K) = psqueue(P, K) is det.
+
second_best(LTree, Key) = Res :-
(
- LTree = start,
- Res = void
+ LTree = start,
+ Res = void
;
- LTree = loser(_, LK, LP, T, SplitKey, U),
- ( LK `leq` SplitKey ->
- T1 = winner(LK, LP, T, SplitKey),
- T2 = second_best(U, Key),
- Res = tournament(T1, T2)
- ;
- T1 = second_best(T, SplitKey),
- T2 = winner(LK, LP, U, Key),
- Res = tournament(T1, T2)
- )
+ LTree = loser(_, LK, LP, T, SplitKey, U),
+ ( LK `leq` SplitKey ->
+ T1 = winner(LK, LP, T, SplitKey),
+ T2 = second_best(U, Key),
+ Res = tournament(T1, T2)
+ ;
+ T1 = second_best(T, SplitKey),
+ T2 = winner(LK, LP, U, Key),
+ Res = tournament(T1, T2)
+ )
).
to_ord_assoc_list(PSQ) = Res :-
@@ -373,8 +389,8 @@ min_view(PSQ) = Res :-
(
PSQ = void, Res = empty
;
- PSQ = winner(Prio, Key, LTree, MaxKey),
- Res = min(Prio, Key, second_best(LTree, MaxKey))
+ PSQ = winner(Key, Prio, LTree, MaxKey),
+ Res = min(Key, Prio, second_best(LTree, MaxKey))
).
% get tournament view of priority search queue
@@ -730,28 +746,27 @@ double_right(K1, P1, TVL, S2, T3) = Res :-
is_semi_heap(PSQ) :-
(
- PSQ = void
+ PSQ = void
;
- PSQ = winner(_, Prio, LTree, _),
- all_keys_larger_ltree(Prio, LTree),
- all_nodes_loser_prio(LTree)
+ PSQ = winner(_, Prio, LTree, _),
+ all_keys_larger_ltree(Prio, LTree),
+ all_nodes_loser_prio(LTree)
).
:- pred all_keys_larger_ltree(P::in, ltree(K, P)::in) is semidet.
+
all_keys_larger_ltree(Prio, LTree) :-
(
- LTree = start
+ LTree = start
;
- LTree = loser(_, _, LP, LT, _, RT),
- Prio `leq` LP,
- all_keys_larger_ltree(Prio, LT),
- all_keys_larger_ltree(Prio, RT)
+ LTree = loser(_, _, LP, LT, _, RT),
+ Prio `leq` LP,
+ all_keys_larger_ltree(Prio, LT),
+ all_keys_larger_ltree(Prio, RT)
).
-:- pred min_prio_loser_tree(ltree(K, P)::in, maybe(P)::out) is det.
-:- pred min_prio_loser_tree(ltree(K, P)::in, P::in, maybe(P)::out) is det.
-
:- func min(V, V) = V is det.
+
min(P1, P2) = Res :-
( P1 `leq` P2 ->
Res = P1
@@ -760,6 +775,7 @@ min(P1, P2) = Res :-
).
:- func max(V, V) = V is det.
+
max(P1, P2) = Res :-
( P1 `leq` P2 ->
Res = P2
@@ -767,296 +783,311 @@ max(P1, P2) = Res :-
Res = P1
).
+:- pred min_prio_loser_tree(ltree(K, P)::in, maybe(P)::out) is det.
+
min_prio_loser_tree(LTree, MinPrio) :-
(
- LTree = start,
- MinPrio = no
+ LTree = start,
+ MinPrio = no
;
- LTree = loser(_, _, Prio, TL, _, TR),
- min_prio_loser_tree(TL, Prio, MinPrio1),
- min_prio_loser_tree(TR, Prio, MinPrio2),
- (
- MinPrio1 = no,
- MinPrio2 = no,
- MinPrio = yes(Prio)
- ;
- MinPrio1 = yes(MinPrio1Val),
- MinPrio2 = no,
- MinPrio = yes(min(MinPrio1Val, Prio))
- ;
- MinPrio2 = yes(MinPrio2Val),
- MinPrio1 = no,
- MinPrio = yes(min(MinPrio2Val, Prio))
- ;
- MinPrio1 = yes(MinPrio1Val),
- MinPrio2 = yes(MinPrio2Val),
- MinPrio = yes(min(MinPrio1Val,
+ LTree = loser(_, _, Prio, TL, _, TR),
+ min_prio_loser_tree(TL, Prio, MinPrio1),
+ min_prio_loser_tree(TR, Prio, MinPrio2),
+ (
+ MinPrio1 = no,
+ MinPrio2 = no,
+ MinPrio = yes(Prio)
+ ;
+ MinPrio1 = yes(MinPrio1Val),
+ MinPrio2 = no,
+ MinPrio = yes(min(MinPrio1Val, Prio))
+ ;
+ MinPrio2 = yes(MinPrio2Val),
+ MinPrio1 = no,
+ MinPrio = yes(min(MinPrio2Val, Prio))
+ ;
+ MinPrio1 = yes(MinPrio1Val),
+ MinPrio2 = yes(MinPrio2Val),
+ MinPrio = yes(min(MinPrio1Val,
min(Prio, MinPrio2Val)))
- )
+ )
).
+:- pred min_prio_loser_tree(ltree(K, P)::in, P::in, maybe(P)::out) is det.
+
min_prio_loser_tree(LTree, CurrMin, MinPrio) :-
(
- LTree = start, MinPrio = no
+ LTree = start,
+ MinPrio = no
;
- LTree = loser(_, _, Prio, TL, _, TR),
- ( CurrMin `leq` Prio ->
- NewPrio = CurrMin
- ;
- NewPrio = Prio
- ),
- min_prio_loser_tree(TL, NewPrio, MinPrio1),
- min_prio_loser_tree(TR, NewPrio, MinPrio2),
- (
- MinPrio1 = no,
- MinPrio2 = no,
- MinPrio = yes(NewPrio)
- ;
- MinPrio1 = yes(MinPrio1Val),
- MinPrio2 = no,
- MinPrio = yes(min(MinPrio1Val, NewPrio))
- ;
- MinPrio2 = yes(MinPrio2Val),
- MinPrio1 = no,
- MinPrio = yes(min(MinPrio2Val, NewPrio))
- ;
- MinPrio1 = yes(MinPrio1Val),
- MinPrio2 = yes(MinPrio2Val),
- MinPrio = yes(min(MinPrio1Val,
+ LTree = loser(_, _, Prio, TL, _, TR),
+ ( CurrMin `leq` Prio ->
+ NewPrio = CurrMin
+ ;
+ NewPrio = Prio
+ ),
+ min_prio_loser_tree(TL, NewPrio, MinPrio1),
+ min_prio_loser_tree(TR, NewPrio, MinPrio2),
+ (
+ MinPrio1 = no,
+ MinPrio2 = no,
+ MinPrio = yes(NewPrio)
+ ;
+ MinPrio1 = yes(MinPrio1Val),
+ MinPrio2 = no,
+ MinPrio = yes(min(MinPrio1Val, NewPrio))
+ ;
+ MinPrio2 = yes(MinPrio2Val),
+ MinPrio1 = no,
+ MinPrio = yes(min(MinPrio2Val, NewPrio))
+ ;
+ MinPrio1 = yes(MinPrio1Val),
+ MinPrio2 = yes(MinPrio2Val),
+ MinPrio = yes(min(MinPrio1Val,
min(MinPrio2Val, NewPrio)))
- )
+ )
).
:- pred all_nodes_loser_prio(ltree(K, P)::in) is semidet.
+
all_nodes_loser_prio(LTree) :-
(
- LTree = start
+ LTree = start
;
- LTree = loser(_, K, Prio, TL, SplitKey, TR),
- ( K `leq` SplitKey ->
- min_prio_loser_tree(TL, Prio, MinPrio)
- ;
- min_prio_loser_tree(TR, Prio, MinPrio)
- ),
- ( MinPrio = no ->
- MinPrio0 = Prio
- ;
- MinPrio = yes(MinPrio0)
- ),
- compare(CMP, Prio, MinPrio0),
- CMP = (=),
- all_nodes_loser_prio(TL),
- all_nodes_loser_prio(TR)
+ LTree = loser(_, K, Prio, TL, SplitKey, TR),
+ ( K `leq` SplitKey ->
+ min_prio_loser_tree(TL, Prio, MinPrio)
+ ;
+ min_prio_loser_tree(TR, Prio, MinPrio)
+ ),
+ ( MinPrio = no ->
+ MinPrio0 = Prio
+ ;
+ MinPrio = yes(MinPrio0)
+ ),
+ compare(CMP, Prio, MinPrio0),
+ CMP = (=),
+ all_nodes_loser_prio(TL),
+ all_nodes_loser_prio(TR)
).
+%-----------------------------------------------------------------------%
+
is_search_tree(PSQ) :-
(
- PSQ = void
+ PSQ = void
;
- PSQ = winner(_, _, LTree, _),
- all_search_keys(LTree)
+ PSQ = winner(_, _, LTree, _),
+ all_search_keys(LTree)
).
:- pred all_search_keys(ltree(K, P)::in) is semidet.
+
all_search_keys(LTree) :-
(
- LTree = start
+ LTree = start
;
- LTree = loser(_, _, _, TL, SplitKey, TR),
- max_key_loser_tree(TL, MaxKeyL),
- min_key_loser_tree(TR, MinKeyR),
- (
- MaxKeyL = no
- ;
- MaxKeyL = yes(MaxKey),
- MaxKey `leq` SplitKey,
- all_search_keys(TL)
- ),
- (
- MinKeyR = no
- ;
- MinKeyR = yes(MinKey),
- compare(CMP, MinKey, SplitKey),
- CMP = (>),
- all_search_keys(TR)
- )
+ LTree = loser(_, _, _, TL, SplitKey, TR),
+ max_key_loser_tree(TL, MaxKeyL),
+ min_key_loser_tree(TR, MinKeyR),
+ (
+ MaxKeyL = no
+ ;
+ MaxKeyL = yes(MaxKey),
+ MaxKey `leq` SplitKey,
+ all_search_keys(TL)
+ ),
+ (
+ MinKeyR = no
+ ;
+ MinKeyR = yes(MinKey),
+ compare(CMP, MinKey, SplitKey),
+ CMP = (>),
+ all_search_keys(TR)
+ )
).
:- pred min_key_loser_tree(ltree(K, P)::in, maybe(K)::out) is det.
-:- pred min_key_loser_tree(ltree(K, P)::in, K::in, maybe(K)::out) is det.
min_key_loser_tree(LTree, MinKey) :-
(
- LTree = start,
- MinKey = no
+ LTree = start,
+ MinKey = no
;
- LTree = loser(_, Key, _, TL, _, TR),
- min_key_loser_tree(TL, Key, MinKey1),
- min_key_loser_tree(TR, Key, MinKey2),
- (
- MinKey1 = no,
- MinKey2 = no,
- MinKey = yes(Key)
- ;
- MinKey1 = yes(MinKey1Val),
- MinKey2 = no,
- MinKey = yes(min(MinKey1Val, Key))
- ;
- MinKey2 = yes(MinKey2Val),
- MinKey1 = no,
- MinKey = yes(min(MinKey2Val, Key))
- ;
- MinKey1 = yes(MinKey1Val),
- MinKey2 = yes(MinKey2Val),
- MinKey = yes(min(MinKey1Val,
+ LTree = loser(_, Key, _, TL, _, TR),
+ min_key_loser_tree(TL, Key, MinKey1),
+ min_key_loser_tree(TR, Key, MinKey2),
+ (
+ MinKey1 = no,
+ MinKey2 = no,
+ MinKey = yes(Key)
+ ;
+ MinKey1 = yes(MinKey1Val),
+ MinKey2 = no,
+ MinKey = yes(min(MinKey1Val, Key))
+ ;
+ MinKey2 = yes(MinKey2Val),
+ MinKey1 = no,
+ MinKey = yes(min(MinKey2Val, Key))
+ ;
+ MinKey1 = yes(MinKey1Val),
+ MinKey2 = yes(MinKey2Val),
+ MinKey = yes(min(MinKey1Val,
min(Key, MinKey2Val)))
- )
+ )
).
+:- pred min_key_loser_tree(ltree(K, P)::in, K::in, maybe(K)::out) is det.
+
min_key_loser_tree(LTree, CurrMin, MinKey) :-
(
- LTree = start, MinKey = no
+ LTree = start, MinKey = no
;
- LTree = loser(_, Key, _, TL, _, TR),
- ( CurrMin `leq` Key ->
- NewKey = CurrMin
- ;
- NewKey = Key
- ),
- min_key_loser_tree(TL, NewKey, MinKey1),
- min_key_loser_tree(TR, NewKey, MinKey2),
- (
- MinKey1 = no,
- MinKey2 = no,
- MinKey = yes(NewKey)
- ;
- MinKey1 = yes(MinKey1Val),
- MinKey2 = no,
- MinKey = yes(min(MinKey1Val, NewKey))
- ;
- MinKey2 = yes(MinKey2Val),
- MinKey1 = no,
- MinKey = yes(min(MinKey2Val, NewKey))
- ;
- MinKey1 = yes(MinKey1Val),
- MinKey2 = yes(MinKey2Val),
- MinKey = yes(min(MinKey1Val,
+ LTree = loser(_, Key, _, TL, _, TR),
+ ( CurrMin `leq` Key ->
+ NewKey = CurrMin
+ ;
+ NewKey = Key
+ ),
+ min_key_loser_tree(TL, NewKey, MinKey1),
+ min_key_loser_tree(TR, NewKey, MinKey2),
+ (
+ MinKey1 = no,
+ MinKey2 = no,
+ MinKey = yes(NewKey)
+ ;
+ MinKey1 = yes(MinKey1Val),
+ MinKey2 = no,
+ MinKey = yes(min(MinKey1Val, NewKey))
+ ;
+ MinKey2 = yes(MinKey2Val),
+ MinKey1 = no,
+ MinKey = yes(min(MinKey2Val, NewKey))
+ ;
+ MinKey1 = yes(MinKey1Val),
+ MinKey2 = yes(MinKey2Val),
+ MinKey = yes(min(MinKey1Val,
min(MinKey2Val, NewKey)))
- )
+ )
).
:- pred max_key_loser_tree(ltree(K, P)::in, maybe(K)::out) is det.
-:- pred max_key_loser_tree(ltree(K, P)::in, K::in, maybe(K)::out) is det.
max_key_loser_tree(LTree, MaxKey) :-
(
- LTree = start,
- MaxKey = no
+ LTree = start,
+ MaxKey = no
;
- LTree = loser(_, Key, _, TL, _, TR),
- max_key_loser_tree(TL, Key, MaxKey1),
- max_key_loser_tree(TR, Key, MaxKey2),
- (
- MaxKey1 = no,
- MaxKey2 = no,
- MaxKey = yes(Key)
- ;
- MaxKey1 = yes(MaxKey1Val),
- MaxKey2 = no,
- MaxKey = yes(max(MaxKey1Val, Key))
- ;
- MaxKey2 = yes(MaxKey2Val),
- MaxKey1 = no,
- MaxKey = yes(max(MaxKey2Val, Key))
- ;
- MaxKey1 = yes(MaxKey1Val),
- MaxKey2 = yes(MaxKey2Val),
- MaxKey = yes(max(MaxKey1Val,
+ LTree = loser(_, Key, _, TL, _, TR),
+ max_key_loser_tree(TL, Key, MaxKey1),
+ max_key_loser_tree(TR, Key, MaxKey2),
+ (
+ MaxKey1 = no,
+ MaxKey2 = no,
+ MaxKey = yes(Key)
+ ;
+ MaxKey1 = yes(MaxKey1Val),
+ MaxKey2 = no,
+ MaxKey = yes(max(MaxKey1Val, Key))
+ ;
+ MaxKey2 = yes(MaxKey2Val),
+ MaxKey1 = no,
+ MaxKey = yes(max(MaxKey2Val, Key))
+ ;
+ MaxKey1 = yes(MaxKey1Val),
+ MaxKey2 = yes(MaxKey2Val),
+ MaxKey = yes(max(MaxKey1Val,
max(Key, MaxKey2Val)))
- )
+ )
).
+:- pred max_key_loser_tree(ltree(K, P)::in, K::in, maybe(K)::out) is det.
+
max_key_loser_tree(LTree, CurrMax, MaxKey) :-
(
- LTree = start, MaxKey = no
+ LTree = start, MaxKey = no
;
- LTree = loser(_, Key, _, TL, _, TR),
- compare(CMP, CurrMax, Key),
- (
- ( CMP = (=); CMP = (>) ),
- NewKey = CurrMax
- ;
- CMP = (<),
- NewKey = Key
- ),
- max_key_loser_tree(TL, NewKey, MaxKey1),
- max_key_loser_tree(TR, NewKey, MaxKey2),
- (
- MaxKey1 = no,
- MaxKey2 = no,
- MaxKey = yes(NewKey)
- ;
- MaxKey1 = yes(MaxKey1Val),
- MaxKey2 = no,
- MaxKey = yes(max(MaxKey1Val, NewKey))
- ;
- MaxKey2 = yes(MaxKey2Val),
- MaxKey1 = no,
- MaxKey = yes(max(MaxKey2Val, NewKey))
- ;
- MaxKey1 = yes(MaxKey1Val),
- MaxKey2 = yes(MaxKey2Val),
- MaxKey = yes(max(MaxKey1Val,
+ LTree = loser(_, Key, _, TL, _, TR),
+ compare(CMP, CurrMax, Key),
+ (
+ ( CMP = (=)
+ ; CMP = (>)
+ ),
+ NewKey = CurrMax
+ ;
+ CMP = (<),
+ NewKey = Key
+ ),
+ max_key_loser_tree(TL, NewKey, MaxKey1),
+ max_key_loser_tree(TR, NewKey, MaxKey2),
+ (
+ MaxKey1 = no,
+ MaxKey2 = no,
+ MaxKey = yes(NewKey)
+ ;
+ MaxKey1 = yes(MaxKey1Val),
+ MaxKey2 = no,
+ MaxKey = yes(max(MaxKey1Val, NewKey))
+ ;
+ MaxKey2 = yes(MaxKey2Val),
+ MaxKey1 = no,
+ MaxKey = yes(max(MaxKey2Val, NewKey))
+ ;
+ MaxKey1 = yes(MaxKey1Val),
+ MaxKey2 = yes(MaxKey2Val),
+ MaxKey = yes(max(MaxKey1Val,
max(MaxKey2Val, NewKey)))
- )
+ )
).
+%-----------------------------------------------------------------------%
key_condition(PSQ) :-
(
- PSQ = void
+ PSQ = void
;
- PSQ = winner(_, _, T, MaxKey),
- lookup(MaxKey, PSQ, _),
- key_condition(PSQ, T)
+ PSQ = winner(_, _, T, MaxKey),
+ search(PSQ, MaxKey, _),
+ key_condition(PSQ, T)
).
:- pred key_condition(psqueue(P, K)::in, ltree(K, P)::in) is semidet.
key_condition(PSQ, T) :-
(
- T = start
+ T = start
;
- T = loser(_, _, _, TL, SplitKey, TR),
- lookup(SplitKey, PSQ, _),
- key_condition(PSQ, TL),
- key_condition(PSQ, TR)
+ T = loser(_, _, _, TL, SplitKey, TR),
+ search(PSQ, SplitKey, _),
+ key_condition(PSQ, TL),
+ key_condition(PSQ, TR)
).
+%-----------------------------------------------------------------------%
is_finite_map(PSQ) :-
(
- PSQ = void
+ PSQ = void
;
- PSQ = winner(_, _, T, _),
- KeyList = get_keys(T),
- UniqList = list.sort_and_remove_dups(KeyList),
- length(KeyList, LK),
- length(UniqList, LUK),
- LK = LUK
+ PSQ = winner(_, _, T, _),
+ KeyList = get_keys(T),
+ UniqList = list.sort_and_remove_dups(KeyList),
+ length(KeyList, LK),
+ length(UniqList, LUK),
+ LK = LUK
).
:- func get_keys(ltree(K, P)) = list(K).
get_keys(T) = Res :-
(
- T = start,
- Res = []
+ T = start,
+ Res = []
;
- T = loser(_, K, _, TL, _, TR),
- Res = [K | get_keys(TL) ++ get_keys(TR)]
+ T = loser(_, K, _, TL, _, TR),
+ Res = [K | get_keys(TL) ++ get_keys(TR)]
).
%---------------------------------------------------------------------------%
-
:- end_module psqueue.
+%---------------------------------------------------------------------------%
diff --git a/tests/hard_coded/psqueue_test.exp b/tests/hard_coded/psqueue_test.exp
index e283b5e..548c263 100644
--- a/tests/hard_coded/psqueue_test.exp
+++ b/tests/hard_coded/psqueue_test.exp
@@ -1,7 +1,7 @@
empty test: ok
paper example test: winner("Lennart", 1, loser(7, "Phil", 3, loser(3, "Erik", 2, loser(1, "Charles", 4, start, "Charles", start), "Erik", loser(1, "Mary", 6, start, "Lennart", start)), "Mary", loser(3, "Simon", 5, loser(1, "Richard", 7, start, "Phil", start), "Richard", loser(1, "Warren", 8, start, "Simon", start))), "Warren")
-sol([1 - "Lennart", 2 - "Erik", 3 - "Phil", 4 - "Charles"])
+at_most 4: sol([1 - "Lennart", 2 - "Erik", 3 - "Phil", 4 - "Charles"])
to_ord_assoc_list test: [1 - "Lennart", 2 - "Erik", 3 - "Phil", 4 - "Charles", 5 - "Simon", 6 - "Mary", 7 - "Richard", 8 - "Warren"]
delete and to_or_assoc: [1 - "Lennart", 2 - "Erik", 4 - "Charles", 5 - "Simon", 6 - "Mary", 7 - "Richard", 8 - "Warren"]
from_assoc_list: [0 - "M", 1 - "L", 2 - "B", 3 - "N", 4 - "H"]
-[1 - "L", 2 - "B", 3 - "N", 4 - "H", 10 - "M"]
+Adjust: [1 - "L", 2 - "B", 3 - "N", 4 - "H", 10 - "M"]
diff --git a/tests/hard_coded/psqueue_test.m b/tests/hard_coded/psqueue_test.m
index c020c96..e4f6e2a 100644
--- a/tests/hard_coded/psqueue_test.m
+++ b/tests/hard_coded/psqueue_test.m
@@ -1,3 +1,6 @@
+%
+% Test library/psqueue.m
+%
:- module psqueue_test.
:- interface.
@@ -12,20 +15,12 @@
:- import_module set.
:- pred test_psqueue_empty is semidet.
+
test_psqueue_empty :-
psqueue.init(PSQ),
psqueue.is_empty(PSQ).
-
-:- pred test_all(io::di, io::uo) is det.
-:- pred test_paper_ex(psqueue(int, string)::out, io::di, io::uo) is det.
-:- pred test_psqueue_paper_ex(psqueue(int, string)::in, psqueue(int, string)::out) is det.
:- pred test_empty(io::di, io::uo) is det.
-:- pred test_at_most(psqueue(int, string)::in, io::di, io::uo) is det.
-:- pred test_to_ord_list(psqueue(int, string)::in, io::di, io::uo) is det.
-:- pred test_delete(psqueue(int, string)::in, io::di, io::uo) is det.
-:- pred test_from_assoc_list(psqueue(int, string)::out, io::di, io::uo) is det.
-:- pred test_adjust(psqueue(int, string)::in, io::di, io::uo) is det.
test_empty(!IO) :-
io.print("empty test: ", !IO),
@@ -36,12 +31,17 @@ test_empty(!IO) :-
),
io.nl(!IO).
+:- pred test_paper_ex(psqueue(int, string)::out, io::di, io::uo) is det.
+
test_paper_ex(PSQ_EX, !IO) :-
io.print("paper example test: ", !IO),
test_psqueue_paper_ex(psqueue.init, PSQ_EX),
io.print(PSQ_EX, !IO),
io.nl(!IO).
+:- pred test_psqueue_paper_ex(psqueue(int, string)::in,
+ psqueue(int, string)::out) is det.
+
test_psqueue_paper_ex(!PSQ) :-
psqueue.insert(8, "Warren", !PSQ),
psqueue.insert(2, "Erik", !PSQ),
@@ -52,17 +52,24 @@ test_psqueue_paper_ex(!PSQ) :-
psqueue.insert(3, "Phil", !PSQ),
psqueue.insert(1, "Lennart", !PSQ).
+:- pred test_at_most(psqueue(int, string)::in, io::di, io::uo) is det.
+
test_at_most(PSQ_EX, !IO) :-
+ io.print("at_most 4: ", !IO),
at_most(4, PSQ_EX, AList),
io.print(set.from_list(AList), !IO),
io.nl(!IO).
+:- pred test_to_ord_list(psqueue(int, string)::in, io::di, io::uo) is det.
+
test_to_ord_list(PSQ_EX, !IO) :-
io.print("to_ord_assoc_list test: ", !IO),
to_ord_assoc_list(PSQ_EX, AList),
io.print(AList, !IO),
io.nl(!IO).
+:- pred test_delete(psqueue(int, string)::in, io::di, io::uo) is det.
+
test_delete(PSQ_EX, !IO) :-
io.print("delete and to_or_assoc: ", !IO),
delete("Phil", PSQ_EX, PSQ_DEL),
@@ -70,6 +77,8 @@ test_delete(PSQ_EX, !IO) :-
io.print(AList, !IO),
io.nl(!IO).
+:- pred test_from_assoc_list(psqueue(int, string)::out, io::di, io::uo) is det.
+
test_from_assoc_list(PSQ5, !IO) :-
io.print("from_assoc_list: ", !IO),
init(PSQ0),
@@ -82,11 +91,16 @@ test_from_assoc_list(PSQ5, !IO) :-
io.print(AList, !IO),
io.nl(!IO).
+:- pred test_adjust(psqueue(int, string)::in, io::di, io::uo) is det.
+
test_adjust(PSQ, !IO) :-
+ io.print("Adjust: ", !IO),
adjust(func(_) = 10, "M", PSQ, PSQ0),
to_ord_assoc_list(PSQ0, AList),
io.print(AList, !IO).
+:- pred test_all(io::di, io::uo) is det.
+
test_all(!IO) :-
test_empty(!IO),
test_paper_ex(PSQ_EX, !IO),
--
2.1.3
More information about the reviews
mailing list