mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
Add the retrieval of minimum and maximum element from stack at O(1) (#5714)
This commit is contained in:
committed by
GitHub
parent
87030aff1e
commit
0f8cda987d
@@ -0,0 +1,69 @@
|
||||
package com.thealgorithms.stacks;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SmallestElementConstantTimeTest {
|
||||
|
||||
private SmallestElementConstantTime sect;
|
||||
|
||||
@BeforeEach
|
||||
public void setSect() {
|
||||
sect = new SmallestElementConstantTime();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMinAtFirst() {
|
||||
sect.push(1);
|
||||
sect.push(10);
|
||||
sect.push(20);
|
||||
sect.push(5);
|
||||
assertEquals(1, sect.getMinimumElement());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMinTwo() {
|
||||
sect.push(5);
|
||||
sect.push(10);
|
||||
sect.push(20);
|
||||
sect.push(1);
|
||||
assertEquals(1, sect.getMinimumElement());
|
||||
sect.pop();
|
||||
assertEquals(5, sect.getMinimumElement());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullMin() {
|
||||
sect.push(10);
|
||||
sect.push(20);
|
||||
sect.pop();
|
||||
sect.pop();
|
||||
assertNull(sect.getMinimumElement());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBlankHandle() {
|
||||
sect.push(10);
|
||||
sect.push(1);
|
||||
sect.pop();
|
||||
sect.pop();
|
||||
assertThrows(NoSuchElementException.class, () -> sect.pop());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPushPopAfterEmpty() {
|
||||
sect.push(10);
|
||||
sect.push(1);
|
||||
sect.pop();
|
||||
sect.pop();
|
||||
sect.push(5);
|
||||
assertEquals(5, sect.getMinimumElement());
|
||||
sect.push(1);
|
||||
assertEquals(1, sect.getMinimumElement());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user