From 828e5143d133188d46e8570638543b172d611965 Mon Sep 17 00:00:00 2001 From: dimitarsazdovski Date: Fri, 28 Jul 2017 14:33:00 +0200 Subject: [PATCH] BAEL-907: Code example and tests (#2295) * BAEL-907: Code example and tests * Moved files to libraries module * Moved example to test class * Moved files. Formatted code. Changed object declaring. * Code fixes according to comments --- .../CircularFifoQueueTest.java | 159 ++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 libraries/src/test/java/com/baeldung/circularfifoqueue/CircularFifoQueueTest.java diff --git a/libraries/src/test/java/com/baeldung/circularfifoqueue/CircularFifoQueueTest.java b/libraries/src/test/java/com/baeldung/circularfifoqueue/CircularFifoQueueTest.java new file mode 100644 index 0000000000..9f670af03c --- /dev/null +++ b/libraries/src/test/java/com/baeldung/circularfifoqueue/CircularFifoQueueTest.java @@ -0,0 +1,159 @@ +package com.baeldung.circularfifoqueue; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.collections4.queue.CircularFifoQueue; +import org.junit.Assert; +import org.junit.Test; + +public class CircularFifoQueueTest { + + private static final int DEFAULT_SIZE = 32; + + private static final int FIXED_SIZE = 5; + + private static final int COLLECTION_SIZE = 7; + + private static final String TEST_COLOR = "Red"; + + private static final String TEST_COLOR_BY_INDEX = "Blue"; + + @Test + public void whenUsingDefualtConstructor_correctSizeQueue() { + CircularFifoQueue bits = new CircularFifoQueue<>(); + + Assert.assertEquals(DEFAULT_SIZE, bits.maxSize()); + } + + @Test + public void givenAddElements_whenUsingIntConstructor_correctSizeQueue() { + CircularFifoQueue colors = new CircularFifoQueue<>(5); + colors.add("Red"); + colors.add("Blue"); + colors.add("Green"); + colors.offer("White"); + colors.offer("Black"); + + Assert.assertEquals(FIXED_SIZE, colors.maxSize()); + } + + @Test + public void whenUsingCollectionConstructor_correctSizeQueue() { + List days = new ArrayList<>(); + days.add("Monday"); + days.add("Tuesday"); + days.add("Wednesday"); + days.add("Thursday"); + days.add("Friday"); + days.add("Saturday"); + days.add("Sunday"); + + CircularFifoQueue daysOfWeek = new CircularFifoQueue<>(days); + + Assert.assertEquals(COLLECTION_SIZE, daysOfWeek.maxSize()); + } + + @Test + public void givenAddElements_whenGetElement_correctElement() { + CircularFifoQueue colors = new CircularFifoQueue<>(5); + colors.add("Red"); + colors.add("Blue"); + colors.add("Green"); + colors.offer("White"); + colors.offer("Black"); + + Assert.assertEquals(TEST_COLOR_BY_INDEX, colors.get(1)); + } + + @Test + public void givenAddElements_whenPollElement_correctElement() { + CircularFifoQueue colors = new CircularFifoQueue<>(5); + colors.add("Red"); + colors.add("Blue"); + colors.add("Green"); + colors.offer("White"); + colors.offer("Black"); + + Assert.assertEquals(TEST_COLOR, colors.poll()); + } + + @Test + public void givenAddElements_whenPeekQueue_correctElement() { + CircularFifoQueue colors = new CircularFifoQueue<>(5); + colors.add("Red"); + colors.add("Blue"); + colors.add("Green"); + colors.offer("White"); + colors.offer("Black"); + + Assert.assertEquals(TEST_COLOR, colors.peek()); + } + + @Test + public void givenAddElements_whenElementQueue_correctElement() { + CircularFifoQueue colors = new CircularFifoQueue<>(5); + colors.add("Red"); + colors.add("Blue"); + colors.add("Green"); + colors.offer("White"); + colors.offer("Black"); + + Assert.assertEquals(TEST_COLOR, colors.element()); + } + + @Test + public void givenAddElements_whenRemoveElement_correctElement() { + CircularFifoQueue colors = new CircularFifoQueue<>(5); + colors.add("Red"); + colors.add("Blue"); + colors.add("Green"); + colors.offer("White"); + colors.offer("Black"); + + Assert.assertEquals(TEST_COLOR, colors.remove()); + } + + @Test + public void givenFullQueue_whenClearQueue_getIsEmpty() { + CircularFifoQueue colors = new CircularFifoQueue<>(5); + colors.add("Red"); + colors.add("Blue"); + colors.add("Green"); + colors.offer("White"); + colors.offer("Black"); + + colors.clear(); + + Assert.assertEquals(true, colors.isEmpty()); + } + + @Test + public void givenFullQueue_whenCheckFull_getIsFull() { + CircularFifoQueue colors = new CircularFifoQueue<>(5); + colors.add("Red"); + colors.add("Blue"); + colors.add("Green"); + colors.offer("White"); + colors.offer("Black"); + + Assert.assertEquals(false, colors.isFull()); + } + + @Test + public void givenFullQueue_whenAddMoreElements_getIsAtFullCapacity() { + CircularFifoQueue colors = new CircularFifoQueue<>(5); + colors.add("Red"); + colors.add("Blue"); + colors.add("Green"); + colors.offer("White"); + colors.offer("Black"); + + colors.add("Orange"); + colors.add("Violet"); + colors.add("Pink"); + + Assert.assertEquals(true, colors.isAtFullCapacity()); + } + +}