Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added tests for DefaultStack and DefaultListTest #9

Merged
merged 2 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions src/test/java/org/jd/core/v1/DefaultListTest.java
Original file line number Diff line number Diff line change
@@ -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<Integer> list = new DefaultList<>();
assertTrue(list.isList());
assertEquals(0, list.size());
}

@Test
public void testConstructorWithCapacity() {
DefaultList<Integer> list = new DefaultList<>(10);
assertTrue(list.isList());
assertEquals(0, list.size());
}

@Test
public void testAddAndRemove() {
DefaultList<Integer> 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<Integer> list = new DefaultList<>();
list.iterator().next();
}

@Test
public void testConstructorWithCollection() {
List<Integer> initialList = Arrays.asList(10, 20, 30);
DefaultList<Integer> 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<Integer> 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<Integer> 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<Integer> list = new DefaultList<>(initialArray);
assertEquals(0, list.size());
}

@Test
public void testConstructorWithEmptyArray() {
Integer[] initialArray = {};
DefaultList<Integer> list = new DefaultList<>(initialArray);
assertEquals(0, list.size());
}

@Test
public void testGetList() {
DefaultList<Integer> list = new DefaultList<>();
assertEquals(list, list.getList());
}

@Test
public void testEmptyList() {
DefaultList<Integer> list = DefaultList.emptyList();
assertEquals(0, list.size());
}

@Test(expected = UnsupportedOperationException.class)
public void testEmptyListAdd() {
DefaultList<Integer> list = DefaultList.emptyList();
list.add(10, 0);
}

@Test(expected = UnsupportedOperationException.class)
public void testEmptyListSet() {
DefaultList<Integer> list = DefaultList.emptyList();
list.set(0, 10);
}

@Test(expected = UnsupportedOperationException.class)
public void testEmptyListRemove() {
DefaultList<Integer> list = DefaultList.emptyList();
list.remove(0);
}

@Test
public void testEmptyIteratorHasNext() {
DefaultList<Integer> list = DefaultList.emptyList();
Iterator<Integer> iterator = list.iterator();
assertFalse(iterator.hasNext());
}

@Test(expected = NoSuchElementException.class)
public void testEmptyIteratorNext() {
DefaultList<Integer> list = DefaultList.emptyList();
list.iterator().next();
}

@Test(expected = UnsupportedOperationException.class)
public void testEmptyIteratorRemove() {
DefaultList<Integer> list = DefaultList.emptyList();
list.iterator().remove();
}
}
120 changes: 120 additions & 0 deletions src/test/java/org/jd/core/v1/DefaultStackTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
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<Integer> 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 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<Integer> 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<Integer> 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<Integer> 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() {
assertEquals("Stack{head=0, elements=[]}", stack.toString());
stack.push(1);
stack.push(2);
String expected = "Stack{head=2, elements=[1, 2]}";
assertEquals(expected, stack.toString());
}
}