From c12bb74ad74503bda8470af654b2a314477402d2 Mon Sep 17 00:00:00 2001 From: Green Lightning Date: Fri, 7 Aug 2015 23:15:12 +0100 Subject: [PATCH 1/2] Add size() method to PooledLinkedList. --- gdx/src/com/badlogic/gdx/utils/PooledLinkedList.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/gdx/src/com/badlogic/gdx/utils/PooledLinkedList.java b/gdx/src/com/badlogic/gdx/utils/PooledLinkedList.java index 0accc3a18d3..130431621ce 100644 --- a/gdx/src/com/badlogic/gdx/utils/PooledLinkedList.java +++ b/gdx/src/com/badlogic/gdx/utils/PooledLinkedList.java @@ -35,12 +35,14 @@ static final class Item { public PooledLinkedList (int maxPoolSize) { this.pool = new Pool>(16, maxPoolSize) { + @Override protected Item newObject () { return new Item(); } }; } + /** Adds the specified object to the end of the list regardless of iteration status */ public void add (T object) { Item item = pool.obtain(); item.payload = object; @@ -60,6 +62,11 @@ public void add (T object) { size++; } + /** Returns the number of items in the list */ + public int size () { + return size; + } + /** Starts iterating over the list's items from the head of the list */ public void iter () { iter = head; @@ -163,6 +170,6 @@ public void clear () { T v = null; while ((v = next()) != null) remove(); - } + } From b1a6b85285dd2fcdb2ee5031826d1c2cf4150bdf Mon Sep 17 00:00:00 2001 From: Green Lightning Date: Sat, 8 Aug 2015 11:17:49 +0100 Subject: [PATCH 2/2] Add PooledLinkedListTest. --- .../badlogic/gdx/utils/PooledLinkedList.java | 30 -------- .../gdx/utils/PooledLinkedListTest.java | 68 +++++++++++++++++++ 2 files changed, 68 insertions(+), 30 deletions(-) create mode 100644 gdx/test/com/badlogic/gdx/utils/PooledLinkedListTest.java diff --git a/gdx/src/com/badlogic/gdx/utils/PooledLinkedList.java b/gdx/src/com/badlogic/gdx/utils/PooledLinkedList.java index 130431621ce..7e6003e08ef 100644 --- a/gdx/src/com/badlogic/gdx/utils/PooledLinkedList.java +++ b/gdx/src/com/badlogic/gdx/utils/PooledLinkedList.java @@ -135,36 +135,6 @@ public void remove () { n.prev = p; } -// public static void main (String[] argv) { -// PooledLinkedList list = new PooledLinkedList(10); -// -// list.add(1); -// list.add(2); -// list.add(3); -// list.add(4); -// list.iter(); -// list.next(); -// list.next(); -// list.remove(); -// list.next(); -// list.next(); -// list.remove(); -// -// list.iter(); -// Integer v = null; -// while ((v = list.next()) != null) -// System.out.println(v); -// -// list.iter(); -// list.next(); -// list.next(); -// list.remove(); -// -// list.iter(); -// list.next(); -// list.remove(); -// } - public void clear () { iter(); T v = null; diff --git a/gdx/test/com/badlogic/gdx/utils/PooledLinkedListTest.java b/gdx/test/com/badlogic/gdx/utils/PooledLinkedListTest.java new file mode 100644 index 00000000000..17861143d82 --- /dev/null +++ b/gdx/test/com/badlogic/gdx/utils/PooledLinkedListTest.java @@ -0,0 +1,68 @@ +package com.badlogic.gdx.utils; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class PooledLinkedListTest { + + private PooledLinkedList list; + + @Before + public void setUp () { + list = new PooledLinkedList(10); + list.add(1); + list.add(2); + list.add(3); + } + + @Test + public void size () { + assertEquals(3, list.size()); + list.iter(); + list.next(); + list.remove(); + assertEquals(2, list.size()); + } + + @Test + public void iteration () { + list.iter(); + assertEquals(Integer.valueOf(1), list.next()); + assertEquals(Integer.valueOf(2), list.next()); + assertEquals(Integer.valueOf(3), list.next()); + assertNull(list.next()); + } + + @Test + public void reverseIteration () { + list.iterReverse(); + assertEquals(Integer.valueOf(3), list.previous()); + assertEquals(Integer.valueOf(2), list.previous()); + assertEquals(Integer.valueOf(1), list.previous()); + assertNull(list.previous()); + } + + @Test + public void remove () { + list.iter(); + list.next(); // 1 + list.remove(); + list.next(); // 2 + list.next(); // 3 + list.remove(); + list.iter(); + assertEquals(Integer.valueOf(2), list.next()); + assertNull(list.next()); + } + + @Test + public void clear () { + list.clear(); + assertEquals(0, list.size()); + list.iter(); + assertNull(list.next()); + } + +}