common-utils/src/test/java/org/warp/commonutils/type/TestStackSet.java

145 lines
4.3 KiB
Java

package org.warp.commonutils.type;
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
import java.util.LinkedHashSet;
import java.util.NoSuchElementException;
import java.util.Set;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.warp.commonutils.error.IndexOutOfBoundsException;
public class TestStackSet {
@Test
public void testStackSetEmptyTop() {
for (StackSet<String> implementation : getImplementations()) {
Assertions.assertThrows(NoSuchElementException.class, implementation::top);
}
}
@Test
public void testStackSetTop() {
for (StackSet<String> implementation : getImplementations()) {
implementation.push("testBottom");
implementation.push("testMiddle");
implementation.push("testTop");
Assertions.assertEquals("testTop", implementation.top());
}
}
@Test
public void testStackSetItemPeekBottom() {
for (StackSet<String> implementation : getImplementations()) {
implementation.push("testBottom");
implementation.push("testMiddle");
implementation.push("testTop");
Assertions.assertEquals("testBottom", implementation.peek(2));
}
}
@Test
public void testStackSetItemPeekMiddle() {
for (StackSet<String> implementation : getImplementations()) {
implementation.push("testBottom");
implementation.push("testMiddle");
implementation.push("testTop");
Assertions.assertEquals("testMiddle", implementation.peek(1));
}
}
@Test
public void testStackSetItemPeekTop() {
for (StackSet<String> implementation : getImplementations()) {
implementation.push("testBottom");
implementation.push("testMiddle");
implementation.push("testTop");
Assertions.assertEquals("testTop", implementation.peek(0));
}
}
@Test
public void testStackSetItemPeekTopSingle() {
for (StackSet<String> implementation : getImplementations()) {
implementation.push("testTop");
Assertions.assertEquals("testTop", implementation.peek(0));
}
}
@Test
public void testStackSetEmptyIsEmpty() {
for (StackSet<String> implementation : getImplementations()) {
Assertions.assertTrue(implementation.isEmpty());
}
}
@Test
public void testStackSetFullIsEmpty() {
for (StackSet<String> implementation : getImplementations()) {
implementation.push("testTop");
Assertions.assertFalse(implementation.isEmpty());
}
}
@Test
public void testStackSetEmptyPeekTop() {
for (StackSet<String> implementation : getImplementations()) {
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> implementation.peek(0));
}
}
@Test
public void testStackSetPeekOverRange() {
for (StackSet<String> implementation : getImplementations()) {
implementation.push("testTop");
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> implementation.peek(10));
}
}
@Test
public void testStackSetPeekUnderRange() {
for (StackSet<String> implementation : getImplementations()) {
implementation.push("testTop");
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> implementation.peek(-10));
}
}
@Test
public void testStackSetItemPop() {
for (StackSet<String> implementation : getImplementations()) {
implementation.push("testBottom");
implementation.push("testMiddle");
implementation.push("testTop");
implementation.push("testExtra");
Assertions.assertEquals("testTop", implementation.peek(1));
implementation.pop();
Assertions.assertEquals("testTop", implementation.peek(0));
Assertions.assertEquals("testTop", implementation.top());
}
}
@Test
public void testStackSetOneItemOnePop() {
for (StackSet<String> implementation : getImplementations()) {
implementation.push("testExtra");
implementation.pop();
Assertions.assertThrows(IndexOutOfBoundsException.class, () -> implementation.peek(0));
Assertions.assertThrows(NoSuchElementException.class, implementation::top);
Assertions.assertTrue(implementation.isEmpty());
}
}
@Test
public void testStackSetItemEmptyPop() {
for (StackSet<String> implementation : getImplementations()) {
Assertions.assertThrows(NoSuchElementException.class, implementation::pop);
}
}
private Set<StackSet<String>> getImplementations() {
return Set.of(new HashStackSet<>(),
new JavaStackSetWrapper<>(new LinkedHashSet<>()),
new FastUtilStackSetWrapper<>(new ObjectLinkedOpenHashSet<>())
);
}
}