|
| 1 | +package com.baeldung.circularfifoqueue; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.apache.commons.collections4.queue.CircularFifoQueue; |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +public class CircularFifoQueueTest { |
| 11 | + |
| 12 | + private static final int DEFAULT_SIZE = 32; |
| 13 | + |
| 14 | + private static final int FIXED_SIZE = 5; |
| 15 | + |
| 16 | + private static final int COLLECTION_SIZE = 7; |
| 17 | + |
| 18 | + private static final String TEST_COLOR = "Red"; |
| 19 | + |
| 20 | + private static final String TEST_COLOR_BY_INDEX = "Blue"; |
| 21 | + |
| 22 | + @Test |
| 23 | + public void whenUsingDefualtConstructor_correctSizeQueue() { |
| 24 | + CircularFifoQueue<String> bits = new CircularFifoQueue<>(); |
| 25 | + |
| 26 | + Assert.assertEquals(DEFAULT_SIZE, bits.maxSize()); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void givenAddElements_whenUsingIntConstructor_correctSizeQueue() { |
| 31 | + CircularFifoQueue<String> colors = new CircularFifoQueue<>(5); |
| 32 | + colors.add("Red"); |
| 33 | + colors.add("Blue"); |
| 34 | + colors.add("Green"); |
| 35 | + colors.offer("White"); |
| 36 | + colors.offer("Black"); |
| 37 | + |
| 38 | + Assert.assertEquals(FIXED_SIZE, colors.maxSize()); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void whenUsingCollectionConstructor_correctSizeQueue() { |
| 43 | + List<String> days = new ArrayList<>(); |
| 44 | + days.add("Monday"); |
| 45 | + days.add("Tuesday"); |
| 46 | + days.add("Wednesday"); |
| 47 | + days.add("Thursday"); |
| 48 | + days.add("Friday"); |
| 49 | + days.add("Saturday"); |
| 50 | + days.add("Sunday"); |
| 51 | + |
| 52 | + CircularFifoQueue<String> daysOfWeek = new CircularFifoQueue<>(days); |
| 53 | + |
| 54 | + Assert.assertEquals(COLLECTION_SIZE, daysOfWeek.maxSize()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void givenAddElements_whenGetElement_correctElement() { |
| 59 | + CircularFifoQueue<String> colors = new CircularFifoQueue<>(5); |
| 60 | + colors.add("Red"); |
| 61 | + colors.add("Blue"); |
| 62 | + colors.add("Green"); |
| 63 | + colors.offer("White"); |
| 64 | + colors.offer("Black"); |
| 65 | + |
| 66 | + Assert.assertEquals(TEST_COLOR_BY_INDEX, colors.get(1)); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void givenAddElements_whenPollElement_correctElement() { |
| 71 | + CircularFifoQueue<String> colors = new CircularFifoQueue<>(5); |
| 72 | + colors.add("Red"); |
| 73 | + colors.add("Blue"); |
| 74 | + colors.add("Green"); |
| 75 | + colors.offer("White"); |
| 76 | + colors.offer("Black"); |
| 77 | + |
| 78 | + Assert.assertEquals(TEST_COLOR, colors.poll()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void givenAddElements_whenPeekQueue_correctElement() { |
| 83 | + CircularFifoQueue<String> colors = new CircularFifoQueue<>(5); |
| 84 | + colors.add("Red"); |
| 85 | + colors.add("Blue"); |
| 86 | + colors.add("Green"); |
| 87 | + colors.offer("White"); |
| 88 | + colors.offer("Black"); |
| 89 | + |
| 90 | + Assert.assertEquals(TEST_COLOR, colors.peek()); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void givenAddElements_whenElementQueue_correctElement() { |
| 95 | + CircularFifoQueue<String> colors = new CircularFifoQueue<>(5); |
| 96 | + colors.add("Red"); |
| 97 | + colors.add("Blue"); |
| 98 | + colors.add("Green"); |
| 99 | + colors.offer("White"); |
| 100 | + colors.offer("Black"); |
| 101 | + |
| 102 | + Assert.assertEquals(TEST_COLOR, colors.element()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void givenAddElements_whenRemoveElement_correctElement() { |
| 107 | + CircularFifoQueue<String> colors = new CircularFifoQueue<>(5); |
| 108 | + colors.add("Red"); |
| 109 | + colors.add("Blue"); |
| 110 | + colors.add("Green"); |
| 111 | + colors.offer("White"); |
| 112 | + colors.offer("Black"); |
| 113 | + |
| 114 | + Assert.assertEquals(TEST_COLOR, colors.remove()); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void givenFullQueue_whenClearQueue_getIsEmpty() { |
| 119 | + CircularFifoQueue<String> colors = new CircularFifoQueue<>(5); |
| 120 | + colors.add("Red"); |
| 121 | + colors.add("Blue"); |
| 122 | + colors.add("Green"); |
| 123 | + colors.offer("White"); |
| 124 | + colors.offer("Black"); |
| 125 | + |
| 126 | + colors.clear(); |
| 127 | + |
| 128 | + Assert.assertEquals(true, colors.isEmpty()); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void givenFullQueue_whenCheckFull_getIsFull() { |
| 133 | + CircularFifoQueue<String> colors = new CircularFifoQueue<>(5); |
| 134 | + colors.add("Red"); |
| 135 | + colors.add("Blue"); |
| 136 | + colors.add("Green"); |
| 137 | + colors.offer("White"); |
| 138 | + colors.offer("Black"); |
| 139 | + |
| 140 | + Assert.assertEquals(false, colors.isFull()); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void givenFullQueue_whenAddMoreElements_getIsAtFullCapacity() { |
| 145 | + CircularFifoQueue<String> colors = new CircularFifoQueue<>(5); |
| 146 | + colors.add("Red"); |
| 147 | + colors.add("Blue"); |
| 148 | + colors.add("Green"); |
| 149 | + colors.offer("White"); |
| 150 | + colors.offer("Black"); |
| 151 | + |
| 152 | + colors.add("Orange"); |
| 153 | + colors.add("Violet"); |
| 154 | + colors.add("Pink"); |
| 155 | + |
| 156 | + Assert.assertEquals(true, colors.isAtFullCapacity()); |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments