Skip to content

Commit

Permalink
Make linked-list non-circular.
Browse files Browse the repository at this point in the history
This bug was introduced when making the collection library strong-mode clean.

Fixes #26522
BUG= http://dartbug.com/26522
R=lrn@google.com

Review URL: https://codereview.chromium.org/2015513006 .
  • Loading branch information
floitschG committed Jun 1, 2016
1 parent 0ae4ae5 commit e1bf350
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
6 changes: 3 additions & 3 deletions sdk/lib/collection/linked_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,13 @@ abstract class LinkedListEntry<E extends LinkedListEntry<E>> {
}

/**
* Return the succeessor of this element in its linked list.
* Return the successor of this element in its linked list.
*
* Returns `null` if there is no successor in the linked list, or if this
* entry is not currently in any list.
*/
E get next {
if (identical(this, _next)) return null;
if (_list == null || identical(_list.first, _next)) return null;
return _next;
}

Expand All @@ -275,7 +275,7 @@ abstract class LinkedListEntry<E extends LinkedListEntry<E>> {
* entry is not currently in any list.
*/
E get previous {
if (identical(this, _previous)) return null;
if (_list == null || identical(this, _list.first)) return null;
return _previous;
}

Expand Down
51 changes: 51 additions & 0 deletions tests/lib/collection/linked_list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,55 @@ class MyEntry extends LinkedListEntry<MyEntry> {
}


testPreviousNext() {
var list = new LinkedList<MyEntry>();
Expect.throws(() => list.first);
Expect.throws(() => list.last);
Expect.equals(0, list.length);

for (int i = 0; i < 3; i++) {
list.add(new MyEntry(i));
}
Expect.equals(3, list.length);

var entry = list.first;
Expect.isNull(entry.previous);
Expect.equals(0, entry.value);
entry = entry.next;
Expect.equals(1, entry.value);
entry = entry.next;
Expect.equals(2, entry.value);
Expect.isNull(entry.next);
entry = entry.previous;
Expect.equals(1, entry.value);
entry = entry.previous;
Expect.equals(0, entry.value);
Expect.isNull(entry.previous);
}

testUnlinked() {
var unlinked = new MyEntry(0);
Expect.isNull(unlinked.previous);
Expect.isNull(unlinked.next);
var list = new LinkedList<MyEntry>();
list.add(unlinked);
Expect.isNull(unlinked.previous);
Expect.isNull(unlinked.next);
list.remove(unlinked);
Expect.isNull(unlinked.previous);
Expect.isNull(unlinked.next);
list.add(unlinked);
list.add(new MyEntry(1));
Expect.isNull(unlinked.previous);
Expect.equals(1, unlinked.next.value);
list.remove(unlinked);
Expect.isNull(unlinked.previous);
Expect.isNull(unlinked.next);
list.add(unlinked);
Expect.isNull(unlinked.next);
Expect.equals(1, unlinked.previous.value);
}

testInsert() {
// Insert last.
var list = new LinkedList<MyEntry>();
Expand Down Expand Up @@ -168,6 +217,8 @@ testConcurrentModificationError() {
}

main() {
testPreviousNext();
testUnlinked();
testInsert();
testRemove();
testBadAdd();
Expand Down

0 comments on commit e1bf350

Please sign in to comment.