|
| 1 | +// validating/tests/CircularQueueTest.java |
| 2 | +// (c)2016 MindView LLC: see Copyright.txt |
| 3 | +// We make no guarantees that this code is fit for any purpose. |
| 4 | +// Visit http://OnJava8.com for more book information. |
| 5 | +package validating; |
| 6 | +import org.junit.jupiter.api.*; |
| 7 | +import static org.junit.jupiter.api.Assertions.*; |
| 8 | + |
| 9 | +public class CircularQueueTest { |
| 10 | + private CircularQueue queue = new CircularQueue(10); |
| 11 | + private int i = 0; |
| 12 | + @BeforeEach |
| 13 | + public void initialize() { |
| 14 | + while(i < 5) // Preload with some data |
| 15 | + queue.put(Integer.toString(i++)); |
| 16 | + } |
| 17 | + // Support methods: |
| 18 | + private void showFullness() { |
| 19 | + assertTrue(queue.full()); |
| 20 | + assertFalse(queue.empty()); |
| 21 | + System.out.println(queue.dump()); |
| 22 | + } |
| 23 | + private void showEmptiness() { |
| 24 | + assertFalse(queue.full()); |
| 25 | + assertTrue(queue.empty()); |
| 26 | + System.out.println(queue.dump()); |
| 27 | + } |
| 28 | + @Test |
| 29 | + public void full() { |
| 30 | + System.out.println("testFull"); |
| 31 | + System.out.println(queue.dump()); |
| 32 | + System.out.println(queue.get()); |
| 33 | + System.out.println(queue.get()); |
| 34 | + while(!queue.full()) |
| 35 | + queue.put(Integer.toString(i++)); |
| 36 | + String msg = ""; |
| 37 | + try { |
| 38 | + queue.put(""); |
| 39 | + } catch(CircularQueueException e) { |
| 40 | + msg = e.getMessage(); |
| 41 | + System.out.println(msg); |
| 42 | + } |
| 43 | + assertEquals(msg, "put() into full CircularQueue"); |
| 44 | + showFullness(); |
| 45 | + } |
| 46 | + @Test |
| 47 | + public void empty() { |
| 48 | + System.out.println("testEmpty"); |
| 49 | + while(!queue.empty()) |
| 50 | + System.out.println(queue.get()); |
| 51 | + String msg = ""; |
| 52 | + try { |
| 53 | + queue.get(); |
| 54 | + } catch(CircularQueueException e) { |
| 55 | + msg = e.getMessage(); |
| 56 | + System.out.println(msg); |
| 57 | + } |
| 58 | + assertEquals(msg, "get() from empty CircularQueue"); |
| 59 | + showEmptiness(); |
| 60 | + } |
| 61 | + @Test |
| 62 | + public void nullPut() { |
| 63 | + System.out.println("testNullPut"); |
| 64 | + String msg = ""; |
| 65 | + try { |
| 66 | + queue.put(null); |
| 67 | + } catch(CircularQueueException e) { |
| 68 | + msg = e.getMessage(); |
| 69 | + System.out.println(msg); |
| 70 | + } |
| 71 | + assertEquals(msg, "put() null item"); |
| 72 | + } |
| 73 | + @Test |
| 74 | + public void circularity() { |
| 75 | + System.out.println("testCircularity"); |
| 76 | + while(!queue.full()) |
| 77 | + queue.put(Integer.toString(i++)); |
| 78 | + showFullness(); |
| 79 | + assertTrue(queue.isWrapped()); |
| 80 | + while(!queue.empty()) |
| 81 | + System.out.println(queue.get()); |
| 82 | + showEmptiness(); |
| 83 | + while(!queue.full()) |
| 84 | + queue.put(Integer.toString(i++)); |
| 85 | + showFullness(); |
| 86 | + while(!queue.empty()) |
| 87 | + System.out.println(queue.get()); |
| 88 | + showEmptiness(); |
| 89 | + } |
| 90 | +} |
| 91 | +/* Output: |
| 92 | +testNullPut |
| 93 | +put() null item |
| 94 | +testCircularity |
| 95 | +in = 0, out = 0, full() = true, empty() = false, CircularQueue = |
| 96 | +[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 97 | +0 |
| 98 | +1 |
| 99 | +2 |
| 100 | +3 |
| 101 | +4 |
| 102 | +5 |
| 103 | +6 |
| 104 | +7 |
| 105 | +8 |
| 106 | +9 |
| 107 | +in = 0, out = 0, full() = false, empty() = true, CircularQueue = |
| 108 | +[null, null, null, null, null, null, null, null, null, |
| 109 | +null] |
| 110 | +in = 0, out = 0, full() = true, empty() = false, CircularQueue = |
| 111 | +[10, 11, 12, 13, 14, 15, 16, 17, 18, 19] |
| 112 | +10 |
| 113 | +11 |
| 114 | +12 |
| 115 | +13 |
| 116 | +14 |
| 117 | +15 |
| 118 | +16 |
| 119 | +17 |
| 120 | +18 |
| 121 | +19 |
| 122 | +in = 0, out = 0, full() = false, empty() = true, CircularQueue = |
| 123 | +[null, null, null, null, null, null, null, null, null, |
| 124 | +null] |
| 125 | +testFull |
| 126 | +in = 5, out = 0, full() = false, empty() = false, CircularQueue = |
| 127 | +[0, 1, 2, 3, 4, null, null, null, null, null] |
| 128 | +0 |
| 129 | +1 |
| 130 | +put() into full CircularQueue |
| 131 | +in = 2, out = 2, full() = true, empty() = false, CircularQueue = |
| 132 | +[10, 11, 2, 3, 4, 5, 6, 7, 8, 9] |
| 133 | +testEmpty |
| 134 | +0 |
| 135 | +1 |
| 136 | +2 |
| 137 | +3 |
| 138 | +4 |
| 139 | +get() from empty CircularQueue |
| 140 | +in = 5, out = 5, full() = false, empty() = true, CircularQueue = |
| 141 | +[null, null, null, null, null, null, null, null, null, |
| 142 | +null] |
| 143 | +*/ |
0 commit comments