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 implementation : getImplementations()) { Assertions.assertThrows(NoSuchElementException.class, implementation::top); } } @Test public void testStackSetTop() { for (StackSet implementation : getImplementations()) { implementation.push("testBottom"); implementation.push("testMiddle"); implementation.push("testTop"); Assertions.assertEquals("testTop", implementation.top()); } } @Test public void testStackSetItemPeekBottom() { for (StackSet implementation : getImplementations()) { implementation.push("testBottom"); implementation.push("testMiddle"); implementation.push("testTop"); Assertions.assertEquals("testBottom", implementation.peek(2)); } } @Test public void testStackSetItemPeekMiddle() { for (StackSet implementation : getImplementations()) { implementation.push("testBottom"); implementation.push("testMiddle"); implementation.push("testTop"); Assertions.assertEquals("testMiddle", implementation.peek(1)); } } @Test public void testStackSetItemPeekTop() { for (StackSet implementation : getImplementations()) { implementation.push("testBottom"); implementation.push("testMiddle"); implementation.push("testTop"); Assertions.assertEquals("testTop", implementation.peek(0)); } } @Test public void testStackSetItemPeekTopSingle() { for (StackSet implementation : getImplementations()) { implementation.push("testTop"); Assertions.assertEquals("testTop", implementation.peek(0)); } } @Test public void testStackSetEmptyIsEmpty() { for (StackSet implementation : getImplementations()) { Assertions.assertTrue(implementation.isEmpty()); } } @Test public void testStackSetFullIsEmpty() { for (StackSet implementation : getImplementations()) { implementation.push("testTop"); Assertions.assertFalse(implementation.isEmpty()); } } @Test public void testStackSetEmptyPeekTop() { for (StackSet implementation : getImplementations()) { Assertions.assertThrows(IndexOutOfBoundsException.class, () -> implementation.peek(0)); } } @Test public void testStackSetPeekOverRange() { for (StackSet implementation : getImplementations()) { implementation.push("testTop"); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> implementation.peek(10)); } } @Test public void testStackSetPeekUnderRange() { for (StackSet implementation : getImplementations()) { implementation.push("testTop"); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> implementation.peek(-10)); } } @Test public void testStackSetItemPop() { for (StackSet 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 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 implementation : getImplementations()) { Assertions.assertThrows(NoSuchElementException.class, implementation::pop); } } private Set> getImplementations() { return Set.of(new HashStackSet<>(), new JavaStackSetWrapper<>(new LinkedHashSet<>()), new FastUtilStackSetWrapper<>(new ObjectLinkedOpenHashSet<>()) ); } }