diff --git a/sdk/lib/collection/linked_list.dart b/sdk/lib/collection/linked_list.dart index 481f04dde02..f1b55bb8774 100644 --- a/sdk/lib/collection/linked_list.dart +++ b/sdk/lib/collection/linked_list.dart @@ -258,13 +258,13 @@ abstract class LinkedListEntry> { } /** - * 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> { * 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; } diff --git a/tests/lib/collection/linked_list_test.dart b/tests/lib/collection/linked_list_test.dart index 3ed06b75cde..785ebe7dd44 100644 --- a/tests/lib/collection/linked_list_test.dart +++ b/tests/lib/collection/linked_list_test.dart @@ -14,6 +14,55 @@ class MyEntry extends LinkedListEntry { } +testPreviousNext() { + var list = new LinkedList(); + 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(); + 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(); @@ -168,6 +217,8 @@ testConcurrentModificationError() { } main() { + testPreviousNext(); + testUnlinked(); testInsert(); testRemove(); testBadAdd();