From 2813e6da664cf001277d84f695051dc489912713 Mon Sep 17 00:00:00 2001 From: nbauma109 Date: Fri, 12 May 2023 07:58:22 +0200 Subject: [PATCH 1/2] added tests for DefaultStack and DefaultListTest --- .../java/org/jd/core/v1/DefaultListTest.java | 138 ++++++++++++++++++ .../java/org/jd/core/v1/DefaultStackTest.java | 109 ++++++++++++++ 2 files changed, 247 insertions(+) create mode 100644 src/test/java/org/jd/core/v1/DefaultListTest.java create mode 100644 src/test/java/org/jd/core/v1/DefaultStackTest.java diff --git a/src/test/java/org/jd/core/v1/DefaultListTest.java b/src/test/java/org/jd/core/v1/DefaultListTest.java new file mode 100644 index 0000000..3b7784f --- /dev/null +++ b/src/test/java/org/jd/core/v1/DefaultListTest.java @@ -0,0 +1,138 @@ +package org.jd.core.v1; + +import org.jd.core.v1.util.DefaultList; +import org.junit.Test; + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class DefaultListTest { + + @Test + public void testDefaultConstructor() { + DefaultList list = new DefaultList<>(); + assertTrue(list.isList()); + assertEquals(0, list.size()); + } + + @Test + public void testConstructorWithCapacity() { + DefaultList list = new DefaultList<>(10); + assertTrue(list.isList()); + assertEquals(0, list.size()); + } + + @Test + public void testAddAndRemove() { + DefaultList list = new DefaultList<>(); + list.add(10); + list.add(20); + assertEquals(Integer.valueOf(10), list.getFirst()); + assertEquals(Integer.valueOf(20), list.getLast()); + + assertEquals(Integer.valueOf(10), list.removeFirst()); + assertEquals(Integer.valueOf(20), list.removeLast()); + assertTrue(list.isEmpty()); + } + + @Test(expected = NoSuchElementException.class) + public void testEmptyIterator() { + DefaultList list = new DefaultList<>(); + list.iterator().next(); + } + + @Test + public void testConstructorWithCollection() { + List initialList = Arrays.asList(10, 20, 30); + DefaultList list = new DefaultList<>(initialList); + assertEquals(3, list.size()); + assertEquals(Integer.valueOf(10), list.getFirst()); + assertEquals(Integer.valueOf(30), list.getLast()); + } + + @Test + public void testConstructorWithElementAndVarargs() { + DefaultList list = new DefaultList<>(10, 20, 30); + assertEquals(3, list.size()); + assertEquals(Integer.valueOf(10), list.getFirst()); + assertEquals(Integer.valueOf(30), list.getLast()); + } + + @Test + public void testConstructorWithArray() { + Integer[] initialArray = { 10, 20, 30 }; + DefaultList list = new DefaultList<>(initialArray); + assertEquals(3, list.size()); + assertEquals(Integer.valueOf(10), list.getFirst()); + assertEquals(Integer.valueOf(30), list.getLast()); + } + + @Test + public void testConstructorWithNullArray() { + Integer[] initialArray = null; + DefaultList list = new DefaultList<>(initialArray); + assertEquals(0, list.size()); + } + + @Test + public void testConstructorWithEmptyArray() { + Integer[] initialArray = {}; + DefaultList list = new DefaultList<>(initialArray); + assertEquals(0, list.size()); + } + + @Test + public void testGetList() { + DefaultList list = new DefaultList<>(); + assertEquals(list, list.getList()); + } + + @Test + public void testEmptyList() { + DefaultList list = DefaultList.emptyList(); + assertEquals(0, list.size()); + } + + @Test(expected = UnsupportedOperationException.class) + public void testEmptyListAdd() { + DefaultList list = DefaultList.emptyList(); + list.add(10, 0); + } + + @Test(expected = UnsupportedOperationException.class) + public void testEmptyListSet() { + DefaultList list = DefaultList.emptyList(); + list.set(0, 10); + } + + @Test(expected = UnsupportedOperationException.class) + public void testEmptyListRemove() { + DefaultList list = DefaultList.emptyList(); + list.remove(0); + } + + @Test + public void testEmptyIteratorHasNext() { + DefaultList list = DefaultList.emptyList(); + Iterator iterator = list.iterator(); + assertFalse(iterator.hasNext()); + } + + @Test(expected = NoSuchElementException.class) + public void testEmptyIteratorNext() { + DefaultList list = DefaultList.emptyList(); + list.iterator().next(); + } + + @Test(expected = UnsupportedOperationException.class) + public void testEmptyIteratorRemove() { + DefaultList list = DefaultList.emptyList(); + list.iterator().remove(); + } +} diff --git a/src/test/java/org/jd/core/v1/DefaultStackTest.java b/src/test/java/org/jd/core/v1/DefaultStackTest.java new file mode 100644 index 0000000..ed76653 --- /dev/null +++ b/src/test/java/org/jd/core/v1/DefaultStackTest.java @@ -0,0 +1,109 @@ +package org.jd.core.v1; + +import static org.junit.Assert.*; + +import org.jd.core.v1.util.DefaultStack; +import org.junit.Before; +import org.junit.Test; + +public class DefaultStackTest { + + private DefaultStack stack; + + @Before + public void setUp() { + stack = new DefaultStack<>(); + } + + @Test + public void testPush() { + stack.push(1); + assertEquals(1, stack.size()); + assertEquals(1, stack.peek().intValue()); + } + + @Test + public void testPushAndResize() { + for(int i = 0; i < 20; i++) { + stack.push(i); + } + assertEquals(20, stack.size()); + assertEquals(19, stack.peek().intValue()); + } + + @Test + public void testPop() { + stack.push(1); + Integer popped = stack.pop(); + assertEquals(1, popped.intValue()); + assertTrue(stack.isEmpty()); + } + + @Test + public void testIsEmpty() { + assertTrue(stack.isEmpty()); + stack.push(2); + assertFalse(stack.isEmpty()); + } + + @Test + public void testSize() { + stack.push(1); + stack.push(2); + assertEquals(2, stack.size()); + } + + @Test + public void testPeek() { + stack.push(1); + stack.push(2); + assertEquals(2, stack.peek().intValue()); + } + + @Test + public void testReplace() { + stack.push(1); + stack.push(1); + stack.replace(1, 2); + assertEquals(2, stack.peek().intValue()); + } + + @Test + public void testCopy() { + DefaultStack other = new DefaultStack<>(); + other.push(1); + other.push(2); + stack.copy(other); + assertEquals(2, stack.size()); + assertEquals(2, stack.peek().intValue()); + } + + @Test + public void testCopyAndResize() { + DefaultStack other = new DefaultStack<>(); + for(int i = 0; i < 20; i++) { + other.push(i); + } + stack.copy(other); + assertEquals(20, stack.size()); + assertEquals(19, stack.peek().intValue()); + } + + @Test + public void testCopyConstructor() { + stack.push(1); + DefaultStack copiedStack = new DefaultStack<>(stack); + assertNotNull(copiedStack); + assertFalse(copiedStack.isEmpty()); + assertEquals(stack.size(), copiedStack.size()); + assertEquals(stack.peek().intValue(), copiedStack.peek().intValue()); + } + + @Test + public void testToString() { + stack.push(1); + stack.push(2); + String expected = "Stack{head=2, elements=[1, 2]}"; + assertEquals(expected, stack.toString()); + } +} From b5f7f7705a35fe1bd6268b87fec697cd5ef12c36 Mon Sep 17 00:00:00 2001 From: nbauma109 Date: Fri, 12 May 2023 08:16:15 +0200 Subject: [PATCH 2/2] DefaultStackTest: added testReplaceNonExistingElement --- src/test/java/org/jd/core/v1/DefaultStackTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/test/java/org/jd/core/v1/DefaultStackTest.java b/src/test/java/org/jd/core/v1/DefaultStackTest.java index ed76653..9373eb7 100644 --- a/src/test/java/org/jd/core/v1/DefaultStackTest.java +++ b/src/test/java/org/jd/core/v1/DefaultStackTest.java @@ -68,6 +68,16 @@ public void testReplace() { assertEquals(2, stack.peek().intValue()); } + @Test + public void testReplaceNonExistingElement() { + // push a non-matching element + stack.push(1); + // attempt to replace a non-existing element + stack.replace(2, 3); + // verify that the original element hasn't changed + assertEquals(1, stack.peek().intValue()); + } + @Test public void testCopy() { DefaultStack other = new DefaultStack<>(); @@ -101,6 +111,7 @@ public void testCopyConstructor() { @Test public void testToString() { + assertEquals("Stack{head=0, elements=[]}", stack.toString()); stack.push(1); stack.push(2); String expected = "Stack{head=2, elements=[1, 2]}";