From 4a816157493478cdcf0328076dab69f3aa2c5bbf Mon Sep 17 00:00:00 2001 From: Ludovic Dubost Date: Sun, 1 Oct 2023 19:38:34 +0200 Subject: [PATCH] class: fix LinkedList.offer method adding item incorrectly at the beginning Fix #772 --- .../teavm/classlib/java/util/TLinkedList.java | 24 +++++++++---------- .../classlib/java/util/LinkedListTest.java | 9 +++++++ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TLinkedList.java b/classlib/src/main/java/org/teavm/classlib/java/util/TLinkedList.java index 27ab9d7d6..3eafb601c 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TLinkedList.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TLinkedList.java @@ -95,17 +95,7 @@ public class TLinkedList extends TAbstractSequentialList implements TDeque @Override public boolean offer(E e) { - Entry entry = new Entry<>(); - entry.item = e; - entry.next = firstEntry; - if (firstEntry != null) { - firstEntry.previous = entry; - } else { - lastEntry = entry; - } - firstEntry = entry; - ++modCount; - ++size; + addLast(e); return true; } @@ -149,7 +139,17 @@ public class TLinkedList extends TAbstractSequentialList implements TDeque @Override public void addFirst(E e) { - offer(e); + Entry entry = new Entry<>(); + entry.item = e; + entry.next = firstEntry; + if (firstEntry != null) { + firstEntry.previous = entry; + } else { + lastEntry = entry; + } + firstEntry = entry; + ++modCount; + ++size; } @Override diff --git a/tests/src/test/java/org/teavm/classlib/java/util/LinkedListTest.java b/tests/src/test/java/org/teavm/classlib/java/util/LinkedListTest.java index 72dbd3131..83dbbbf3f 100644 --- a/tests/src/test/java/org/teavm/classlib/java/util/LinkedListTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/util/LinkedListTest.java @@ -308,4 +308,13 @@ public class LinkedListTest { lit.add("x"); assertEquals(List.of("d", "x", "a"), list); } + + @Test + public void offerOrder() { + var list = new LinkedList(); + list.offer("1"); + list.offer("2"); + assertEquals("1", list.getFirst()); + assertEquals("2", list.getLast()); + } }