From 244a12906306c7424fbb182cfec5dd3896e55536 Mon Sep 17 00:00:00 2001 From: Tommy Ettinger Date: Thu, 28 Dec 2023 22:16:24 -0800 Subject: [PATCH] Step 1: resize() should work for capacity 0. --- src/main/java/com/github/tommyettinger/ds/BooleanDeque.java | 2 +- src/main/java/com/github/tommyettinger/ds/ByteDeque.java | 2 +- src/main/java/com/github/tommyettinger/ds/CharDeque.java | 2 +- src/main/java/com/github/tommyettinger/ds/DoubleDeque.java | 2 +- src/main/java/com/github/tommyettinger/ds/FloatDeque.java | 2 +- src/main/java/com/github/tommyettinger/ds/IntDeque.java | 2 +- src/main/java/com/github/tommyettinger/ds/LongDeque.java | 2 +- src/main/java/com/github/tommyettinger/ds/ShortDeque.java | 2 +- .../com/github/tommyettinger/ds/test/ConstructionTest.java | 3 +-- 9 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/github/tommyettinger/ds/BooleanDeque.java b/src/main/java/com/github/tommyettinger/ds/BooleanDeque.java index b880307e1..c6bffb6be 100644 --- a/src/main/java/com/github/tommyettinger/ds/BooleanDeque.java +++ b/src/main/java/com/github/tommyettinger/ds/BooleanDeque.java @@ -202,7 +202,7 @@ protected void resize (int newSize) { final int head = this.head; final int tail = this.tail; - final boolean[] newArray = new boolean[newSize]; + final boolean[] newArray = new boolean[Math.max(1, newSize)]; if (head < tail) { // Continuous System.arraycopy(values, head, newArray, 0, tail - head); diff --git a/src/main/java/com/github/tommyettinger/ds/ByteDeque.java b/src/main/java/com/github/tommyettinger/ds/ByteDeque.java index 08e1ee611..34bcab15f 100644 --- a/src/main/java/com/github/tommyettinger/ds/ByteDeque.java +++ b/src/main/java/com/github/tommyettinger/ds/ByteDeque.java @@ -202,7 +202,7 @@ protected void resize (int newSize) { final int head = this.head; final int tail = this.tail; - final byte[] newArray = new byte[newSize]; + final byte[] newArray = new byte[Math.max(1, newSize)]; if (head < tail) { // Continuous System.arraycopy(values, head, newArray, 0, tail - head); diff --git a/src/main/java/com/github/tommyettinger/ds/CharDeque.java b/src/main/java/com/github/tommyettinger/ds/CharDeque.java index c87419c53..fdf9953e4 100644 --- a/src/main/java/com/github/tommyettinger/ds/CharDeque.java +++ b/src/main/java/com/github/tommyettinger/ds/CharDeque.java @@ -202,7 +202,7 @@ protected void resize (int newSize) { final int head = this.head; final int tail = this.tail; - final char[] newArray = new char[newSize]; + final char[] newArray = new char[Math.max(1, newSize)]; if (head < tail) { // Continuous System.arraycopy(values, head, newArray, 0, tail - head); diff --git a/src/main/java/com/github/tommyettinger/ds/DoubleDeque.java b/src/main/java/com/github/tommyettinger/ds/DoubleDeque.java index b86d3de4b..ad5982c2a 100644 --- a/src/main/java/com/github/tommyettinger/ds/DoubleDeque.java +++ b/src/main/java/com/github/tommyettinger/ds/DoubleDeque.java @@ -203,7 +203,7 @@ protected void resize (int newSize) { final int head = this.head; final int tail = this.tail; - final double[] newArray = new double[newSize]; + final double[] newArray = new double[Math.max(1, newSize)]; if (head < tail) { // Continuous System.arraycopy(values, head, newArray, 0, tail - head); diff --git a/src/main/java/com/github/tommyettinger/ds/FloatDeque.java b/src/main/java/com/github/tommyettinger/ds/FloatDeque.java index a754ac375..bc9223c21 100644 --- a/src/main/java/com/github/tommyettinger/ds/FloatDeque.java +++ b/src/main/java/com/github/tommyettinger/ds/FloatDeque.java @@ -203,7 +203,7 @@ protected void resize (int newSize) { final int head = this.head; final int tail = this.tail; - final float[] newArray = new float[newSize]; + final float[] newArray = new float[Math.max(1, newSize)]; if (head < tail) { // Continuous System.arraycopy(values, head, newArray, 0, tail - head); diff --git a/src/main/java/com/github/tommyettinger/ds/IntDeque.java b/src/main/java/com/github/tommyettinger/ds/IntDeque.java index ab711b10d..568c8ac95 100644 --- a/src/main/java/com/github/tommyettinger/ds/IntDeque.java +++ b/src/main/java/com/github/tommyettinger/ds/IntDeque.java @@ -202,7 +202,7 @@ protected void resize (int newSize) { final int head = this.head; final int tail = this.tail; - final int[] newArray = new int[newSize]; + final int[] newArray = new int[Math.max(1, newSize)]; if (head < tail) { // Continuous System.arraycopy(values, head, newArray, 0, tail - head); diff --git a/src/main/java/com/github/tommyettinger/ds/LongDeque.java b/src/main/java/com/github/tommyettinger/ds/LongDeque.java index 72359f8f5..41e043316 100644 --- a/src/main/java/com/github/tommyettinger/ds/LongDeque.java +++ b/src/main/java/com/github/tommyettinger/ds/LongDeque.java @@ -202,7 +202,7 @@ protected void resize (int newSize) { final int head = this.head; final int tail = this.tail; - final long[] newArray = new long[newSize]; + final long[] newArray = new long[Math.max(1, newSize)]; if (head < tail) { // Continuous System.arraycopy(values, head, newArray, 0, tail - head); diff --git a/src/main/java/com/github/tommyettinger/ds/ShortDeque.java b/src/main/java/com/github/tommyettinger/ds/ShortDeque.java index 835291902..3eee85dcd 100644 --- a/src/main/java/com/github/tommyettinger/ds/ShortDeque.java +++ b/src/main/java/com/github/tommyettinger/ds/ShortDeque.java @@ -202,7 +202,7 @@ protected void resize (int newSize) { final int head = this.head; final int tail = this.tail; - final short[] newArray = new short[newSize]; + final short[] newArray = new short[Math.max(1, newSize)]; if (head < tail) { // Continuous System.arraycopy(values, head, newArray, 0, tail - head); diff --git a/src/test/java/com/github/tommyettinger/ds/test/ConstructionTest.java b/src/test/java/com/github/tommyettinger/ds/test/ConstructionTest.java index 93ac97886..9af6e9472 100644 --- a/src/test/java/com/github/tommyettinger/ds/test/ConstructionTest.java +++ b/src/test/java/com/github/tommyettinger/ds/test/ConstructionTest.java @@ -40,8 +40,7 @@ public void testObjectEmpty() { ObjectDeque deque = new ObjectDeque<>(0); deque.add("START"); - deque.remove(); - deque.addAll(list); + deque.addAll(names); Assert.assertEquals(targetLength, deque.size()); ObjectSet set = new ObjectSet<>(0);