Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.junit.jupiter.api.*;
- import static org.junit.jupiter.api.Assertions.*;
- class GenericStackTest {
- // integer and string test queues
- private GenericStack<Integer> s1;
- private GenericStack<String> s2;
- private Integer init1 = 1;
- private String init2 = "test";
- @BeforeEach
- public void setup() {
- s1 = new GenericStack<Integer>(init1);
- s2 = new GenericStack<String>(init2);
- }
- @Test
- // 1. test the constructor to ensure the expected value was placed in the list
- public void constructorTest() {
- assertEquals(init1, s1.pop(), "Failure: constructor did not add item to the correct place.");
- assertEquals(init2, s2.pop(), "Failure: constructor did not add item to the correct place.");
- }
- @Test
- // 2. in a list of three values, ensure that when adding a fourth, it adds it to the front of the stack
- public void addFrontTest() {
- int val = 2;
- for (int i=0; i<3; i++) {
- s1.add(i);
- }
- assertEquals(val, s1.pop(), "Failure: value not added to the top of the stack for push.");
- }
- @Test
- // 3. in an empty list, ensure that null is returned when attempting to pop
- public void emptyList() {
- s1.pop();
- assertNull(s1.pop(), "Failure: null not returned from pop in an empty list");
- }
- @Test
- // 4. ensure that the length will not be negative
- public void lengthTest1() {
- s2.pop();
- s2.pop();
- assertEquals(0, s2.getLength(), "Failure: length is negative after popping from an empty list.");
- }
- @Test
- // 5. ensure that the length value properly increments when creating a new list
- public void lengthTest2() {
- for (int i=0; i<5; i++) {
- s1.push(i + 1);
- }
- assertEquals(6, s1.getLength(), "Failure: length is not properly incremented on push.");
- }
- @Test
- // 6. add to a stack that has been emptied out, ensuring that the object may be reused
- public void pushEmptyTest( ) {
- String str = "txt";
- s2.pop();
- s2.push(str);
- assertEquals(str, s2.pop(), "Failure: unable to push to an empty list.");
- }
- @Test
- // 7. ensure that when pop is preformed on a non-empty list, the correct value is returned
- public void deleteTest() {
- for (int i=0; i<10; i++) {
- s1.push(i);
- }
- assertEquals(9, s1.pop(), "Failure: incorrect value returned from pop.");
- }
- @Test
- // 8. Ensure that push works properly when adding a second element to the constructor - value added to top of stack
- public void pushTest() {
- Integer val = 0;
- s1.push(2);
- assertEquals(2, s1.pop(), "Failure: push does not add to the top of the stack.");
- }
- @Test
- // 9. check that the length decrements appropriately when deleting from a list
- public void lengthTest3() {
- // add 10 items to the stack
- for (int i=0; i<9; i++) {
- s1.push(i+1);
- }
- // remove 7 items from the stack
- for (int i=0; i<7; i++) {
- s1.pop();
- }
- assertEquals(3, s1.getLength(), "Failure: error in updating length upon pop.");
- }
- @Test
- // 10. check that popping 4 items from a stack, and then adding 4 items results in length 4
- public void t10() {
- // stack will have 4 items after this loop
- for (int i=0; i<3; i++) {
- s1.push(i);
- }
- // pop 4 from the stack
- for (int j=0; j<4; j++) {
- s1.pop();
- }
- for (int i=0; i<4; i++) {
- s1.push(i);
- }
- assertEquals(4, s1.getLength(), "Failure: stack does not have the appropriate length after both push and pop operations have been completed.");
- }
- @Test
- // 11. check that
- public void lengthEmpty() {
- s2.pop();
- assertEquals(0, s2.getLength(), "Failure: the length should be 0 for an empty list.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement