Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. import org.junit.jupiter.api.*;
  2. import static org.junit.jupiter.api.Assertions.*;
  3.  
  4. class GenericStackTest {
  5.  
  6. // integer and string test queues
  7. private GenericStack<Integer> s1;
  8. private GenericStack<String> s2;
  9.  
  10. private Integer init1 = 1;
  11. private String init2 = "test";
  12.  
  13. @BeforeEach
  14. public void setup() {
  15. s1 = new GenericStack<Integer>(init1);
  16. s2 = new GenericStack<String>(init2);
  17. }
  18.  
  19. @Test
  20. // 1. test the constructor to ensure the expected value was placed in the list
  21. public void constructorTest() {
  22. assertEquals(init1, s1.pop(), "Failure: constructor did not add item to the correct place.");
  23. assertEquals(init2, s2.pop(), "Failure: constructor did not add item to the correct place.");
  24. }
  25.  
  26. @Test
  27. // 2. in a list of three values, ensure that when adding a fourth, it adds it to the front of the stack
  28. public void addFrontTest() {
  29. int val = 2;
  30. for (int i=0; i<3; i++) {
  31. s1.add(i);
  32. }
  33. assertEquals(val, s1.pop(), "Failure: value not added to the top of the stack for push.");
  34. }
  35.  
  36. @Test
  37. // 3. in an empty list, ensure that null is returned when attempting to pop
  38. public void emptyList() {
  39. s1.pop();
  40. assertNull(s1.pop(), "Failure: null not returned from pop in an empty list");
  41. }
  42.  
  43. @Test
  44. // 4. ensure that the length will not be negative
  45. public void lengthTest1() {
  46. s2.pop();
  47. s2.pop();
  48. assertEquals(0, s2.getLength(), "Failure: length is negative after popping from an empty list.");
  49. }
  50.  
  51. @Test
  52. // 5. ensure that the length value properly increments when creating a new list
  53. public void lengthTest2() {
  54. for (int i=0; i<5; i++) {
  55. s1.push(i + 1);
  56. }
  57. assertEquals(6, s1.getLength(), "Failure: length is not properly incremented on push.");
  58. }
  59.  
  60. @Test
  61. // 6. add to a stack that has been emptied out, ensuring that the object may be reused
  62. public void pushEmptyTest( ) {
  63. String str = "txt";
  64. s2.pop();
  65. s2.push(str);
  66. assertEquals(str, s2.pop(), "Failure: unable to push to an empty list.");
  67. }
  68.  
  69. @Test
  70. // 7. ensure that when pop is preformed on a non-empty list, the correct value is returned
  71. public void deleteTest() {
  72. for (int i=0; i<10; i++) {
  73. s1.push(i);
  74. }
  75. assertEquals(9, s1.pop(), "Failure: incorrect value returned from pop.");
  76. }
  77.  
  78. @Test
  79. // 8. Ensure that push works properly when adding a second element to the constructor - value added to top of stack
  80. public void pushTest() {
  81. Integer val = 0;
  82. s1.push(2);
  83. assertEquals(2, s1.pop(), "Failure: push does not add to the top of the stack.");
  84. }
  85.  
  86. @Test
  87. // 9. check that the length decrements appropriately when deleting from a list
  88. public void lengthTest3() {
  89. // add 10 items to the stack
  90. for (int i=0; i<9; i++) {
  91. s1.push(i+1);
  92. }
  93. // remove 7 items from the stack
  94. for (int i=0; i<7; i++) {
  95. s1.pop();
  96. }
  97. assertEquals(3, s1.getLength(), "Failure: error in updating length upon pop.");
  98. }
  99.  
  100. @Test
  101. // 10. check that popping 4 items from a stack, and then adding 4 items results in length 4
  102. public void t10() {
  103. // stack will have 4 items after this loop
  104. for (int i=0; i<3; i++) {
  105. s1.push(i);
  106. }
  107. // pop 4 from the stack
  108. for (int j=0; j<4; j++) {
  109. s1.pop();
  110. }
  111. for (int i=0; i<4; i++) {
  112. s1.push(i);
  113. }
  114. assertEquals(4, s1.getLength(), "Failure: stack does not have the appropriate length after both push and pop operations have been completed.");
  115. }
  116.  
  117. @Test
  118. // 11. check that
  119. public void lengthEmpty() {
  120. s2.pop();
  121. assertEquals(0, s2.getLength(), "Failure: the length should be 0 for an empty list.");
  122. }
  123.  
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement