Make linked-list non-circular.

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 .
This commit is contained in:
Florian Loitsch 2016-06-01 14:19:37 +02:00
parent 0ae4ae5033
commit e1bf3506d1
2 changed files with 54 additions and 3 deletions

View file

@ -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;
}
@ -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;
}

View file

@ -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>();
@ -168,6 +217,8 @@ testConcurrentModificationError() {
}
main() {
testPreviousNext();
testUnlinked();
testInsert();
testRemove();
testBadAdd();