Guest User

Untitled

a guest
Jul 13th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. package testqueue;
  2.  
  3. import static org.junit.Assert.*;
  4. import org.junit.After;
  5. import org.junit.Before;
  6. import org.junit.Test;
  7.  
  8. import java.util.NoSuchElementException;
  9. import java.util.Iterator;
  10.  
  11. import queue.FifoQueue;
  12.  
  13. public class TestFifoQueue {
  14. private FifoQueue<Integer> myIntQueue;
  15. private FifoQueue<String> myStringQueue;
  16.  
  17. @Before
  18. public void setUp() throws Exception {
  19. myIntQueue = new FifoQueue<Integer>();
  20. myStringQueue = new FifoQueue<String>();
  21. }
  22.  
  23. @After
  24. public void tearDown() throws Exception {
  25. myIntQueue = null;
  26. myStringQueue = null;
  27. }
  28.  
  29. /**
  30. * Test if a newly created queue is empty.
  31. */
  32. @Test
  33. public final void testNewFifoQueue() {
  34. assertTrue(myIntQueue.isEmpty());
  35. assertTrue(myIntQueue.size() == 0);
  36. }
  37.  
  38. /** Test a single offer followed by a single peek. */
  39. @Test
  40. public final void testPeek() {
  41. myIntQueue.offer(1);
  42. int i = myIntQueue.peek();
  43. assertEquals("peek on queue of size 1", 1, i);
  44. assertTrue(myIntQueue.size() == 1);
  45. }
  46.  
  47. /**
  48. * Test a single offer followed by a single poll.
  49. */
  50. @Test
  51. public final void testPoll() {
  52. myIntQueue.offer(1);
  53. int i = myIntQueue.poll();
  54. assertEquals("poll on queue of size 1", 1, i);
  55. assertTrue("Wrong size after poll", myIntQueue.size() == 0);
  56. }
  57.  
  58. /**
  59. * Test peek of empty queue.
  60. */
  61. @Test
  62. public final void testPeekOfEmpty() {
  63. assertEquals("Front of empty queue not null", null, myIntQueue.peek());
  64. }
  65.  
  66. /**
  67. * Test poll of empty queue.
  68. */
  69. @Test
  70. public final void testPollOfEmpty() {
  71. assertEquals("Poll of empty queue should return null", null, myIntQueue
  72. .poll());
  73. }
  74.  
  75. /**
  76. * Test that implementation works for a queue of strings.
  77. */
  78. @Test
  79. public final void testStringQueue() {
  80. myStringQueue.offer("First");
  81. myStringQueue.offer("Second");
  82. myStringQueue.offer("Third");
  83. assertTrue("Wrong size of queue", myStringQueue.size() == 3);
  84. assertEquals("peek on queue of strings", "First", myStringQueue.peek());
  85. assertEquals("String First expected", "First", myStringQueue.poll());
  86. assertEquals("String Second expected", "Second", myStringQueue.poll());
  87. assertEquals("String Third expected", "Third", myStringQueue.poll());
  88. assertTrue("Queue of strings should be empty", myStringQueue.isEmpty());
  89. }
  90.  
  91. /**
  92. * Test that polling gives elements in right order.
  93. */
  94. @Test
  95. public final void testOrder() {
  96. myIntQueue.offer(1);
  97. myIntQueue.offer(2);
  98. myIntQueue.offer(3);
  99. myIntQueue.offer(4);
  100. myIntQueue.offer(5);
  101. for (int i = 1; i <= 5; i++) {
  102. int k = myIntQueue.poll();
  103. assertEquals("poll returns incorrect element", i, k);
  104. }
  105. assertTrue("Queue not empty", myIntQueue.isEmpty());
  106. }
  107.  
  108. /**
  109. * Test that polling all elements gives an empty queue.
  110. */
  111. @Test
  112. public final void testMakeQueueEmpty() {
  113. myIntQueue.offer(1);
  114. myIntQueue.offer(2);
  115. myIntQueue.poll();
  116. myIntQueue.poll();
  117. assertTrue("Wrong size after poll", myIntQueue.size() == 0);
  118. assertTrue("Queue not empty after poll", myIntQueue.isEmpty());
  119. myIntQueue.offer(3);
  120. myIntQueue.offer(4);
  121. assertTrue("Wrong size after offer", myIntQueue.size() == 2);
  122. for (int i = 3; i <= 4; i++) {
  123. int k = myIntQueue.poll();
  124. assertEquals("poll returns incorrect element", i, k);
  125. }
  126. assertTrue("Wrong size after poll", myIntQueue.size() == 0);
  127. assertTrue("Queue not empty after poll", myIntQueue.isEmpty());
  128. }
  129.  
  130. }
Add Comment
Please, Sign In to add comment