Skip to content

Commit

Permalink
dylan: Fix return value of push-last
Browse files Browse the repository at this point in the history
According to the DRM, the push-last function is supposed to return the
new value added to a deque rather than the deque.

* sources/dylan/deque.dylan
  (trusted-push-last): Change to return the new element.
  (push-last): Change return signature to reflect returning
   the new element.

* sources/dylan/tests/collections.dylan
  (test test-<deque>-functions): New test (inspired by cmu-test-suite)
   for testing <deque>-specific functions and methods.
  (suite dylan-collections-test-suite): Add test-<deque>-functions.
  • Loading branch information
housel committed Jul 31, 2024
1 parent cfc29c0 commit b75e298
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
8 changes: 4 additions & 4 deletions sources/dylan/deque.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ end method pop;
//

define sealed inline method trusted-push-last
(deque :: <object-deque>, new-element) => (result :: <deque>)
(deque :: <object-deque>, new-element) => (new-element)
let rep = deque.representation;
let rep-last-index = rep.last-index;
while (rep-last-index = (rep.size - 1))
Expand All @@ -529,13 +529,13 @@ define sealed inline method trusted-push-last
end while;
rep.last-index := rep-last-index := rep-last-index + 1;
island-deque-element(rep, rep-last-index) := new-element;
deque
new-element
end method trusted-push-last;

define sealed method push-last
(deque :: <object-deque>, new-element) => (result :: <deque>)
(deque :: <object-deque>, new-element) => (new-element)
check-type(new-element, element-type(deque));
trusted-push-last(deque, new-element);
trusted-push-last(deque, new-element)
end method push-last;


Expand Down
17 changes: 17 additions & 0 deletions sources/dylan/tests/collections.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ define test test-<deque> ()
test-collection-class(<deque>, instantiable?: #t);
end;

define test test-<deque>-functions ()
let d = make-test-instance(<deque>);
for (i from 5 to 1 by -1)
check-equal("push returns new value", i, push(d, i));
end for;
for (i from 6 to 10)
check-equal("push-last returns new value", i, push-last(d, i));
end for;
check-equal("first pop is last item inserted at start", 1, pop(d));
check-equal("first pop-lat is last item inserted at end", 10, pop-last(d));
check-true("add! returns <deque> argument", d == add!(d, 1));
check-equal("add! added new element to front", 1, pop(d));
check-true("remove! returns <deque> argument", d == remove!(d, 9));
check-equal("remove! removed final element", 8, pop-last(d));
end;

define test test-<list> ()
test-collection-class(<list>, instantiable?: #t);
end;
Expand Down Expand Up @@ -108,6 +124,7 @@ define suite dylan-collections-test-suite ()
test test-<simple-object-vector>;
test test-<stretchy-vector>;
test test-<deque>;
test test-<deque>-functions;
test test-<list>;
test test-<pair>;
test test-<empty-list>;
Expand Down

0 comments on commit b75e298

Please sign in to comment.