From 78c4241c617269189ba777bea7f647ee44679498 Mon Sep 17 00:00:00 2001 From: ksetoue Date: Tue, 30 Nov 2021 02:22:56 -0300 Subject: [PATCH 1/3] :sparkles: add: queue implementation using arrays --- .../kotlin/datastructures/queues/Queue.kt | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/main/kotlin/datastructures/queues/Queue.kt diff --git a/src/main/kotlin/datastructures/queues/Queue.kt b/src/main/kotlin/datastructures/queues/Queue.kt new file mode 100644 index 0000000..2a3ca32 --- /dev/null +++ b/src/main/kotlin/datastructures/queues/Queue.kt @@ -0,0 +1,74 @@ +package datastructures + +const val DEFAULT_CAPACITY = 10 + +/** + * This is an implementation of a Queue + * that uses an array as primary structure + * + * A queue data structure functions the same as a real world queue. The elements + * that are added first are the first to be removed. New elements are added to + * the back/rear of the queue. + */ +class Queue (size: Int?) { + + private val maxCapacity = size ?: DEFAULT_CAPACITY + + private var itemsCount = 0 + + private val queue = IntArray(maxCapacity) + + private var last = -1 + + private var first = 0 + + /** + * @param value is an integer that will be inserted into the queue + * @returns false if the queue is full and true if the item was inserted + */ + fun enqueue(value: Int): Boolean { + if (isFull()) return false + + itemsCount++ + + last = (last + 1) % maxCapacity + queue[last] = value + + return true + } + + /** + * @returns the first item inserted into the queue, and null if it's empty + */ + fun dequeue(): Int? { + if (isEmpty()) { + return null + } + + val itemToReturn = queue[first] + first = (first + 1) % maxCapacity + + itemsCount-- + + return itemToReturn + } + + /** + * @returns the first item inserted into the queue as string + */ + fun peekFirst(): String = queue[first].toString() + + /** + * @returns the last item inserted into the queue as string + */ + fun peekLast(): String = queue[last].toString() + + /** + * @returns the amount of itens inside the queue + */ + fun getSize(): Int = itemsCount + + private fun isFull(): Boolean = itemsCount == maxCapacity + + private fun isEmpty(): Boolean = itemsCount == 0 +} \ No newline at end of file From b0ee9a82f480235ba4f186eea42a006f576cba7b Mon Sep 17 00:00:00 2001 From: ksetoue Date: Tue, 30 Nov 2021 02:23:28 -0300 Subject: [PATCH 2/3] :white_check_mark: add unit test for queue --- .../kotlin/datastructures/queues/QueueTest.kt | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/test/kotlin/datastructures/queues/QueueTest.kt diff --git a/src/test/kotlin/datastructures/queues/QueueTest.kt b/src/test/kotlin/datastructures/queues/QueueTest.kt new file mode 100644 index 0000000..0b728c4 --- /dev/null +++ b/src/test/kotlin/datastructures/queues/QueueTest.kt @@ -0,0 +1,59 @@ +package datastructures.queues +import org.assertj.core.api.Assertions.assertThat +import datastructures.Queue + +import org.junit.Test + +internal class QueueTest { + + @Test + fun `#enqueue must return true queue is not full`() { + val valueToInsert = 1 + + val newQueue = Queue(10) + val returnedValue = newQueue.enqueue(valueToInsert) + + assertThat(returnedValue).isTrue() + } + + @Test + fun `#enqueue must return false queue is full`() { + val valueToInsert = 20 + val queueSize = 4 + val newQueue = Queue(queueSize) + + for(i in 0 until queueSize + 1) { + newQueue.enqueue(i) + } + + val returnedValue = newQueue.enqueue(valueToInsert) + + assertThat(returnedValue).isFalse() + } + + @Test + fun `#dequeue must return null when queue is empty`() { + val queueSize = 2 + val newQueue = Queue(queueSize) + + val returnedValue = newQueue.dequeue() + + assertThat(returnedValue).isNull() + } + + @Test + fun `#dequeue must return a value when queue is not empty`() { + val valueToInsert = 20 + val queueSize = 4 + val newQueue = Queue(queueSize) + + for(i in 0 until queueSize + 1) { + newQueue.enqueue(i) + } + + val returnedValue = newQueue.dequeue() + + assertThat(returnedValue).isNotNull() + assertThat(returnedValue).isEqualTo(0) + } +} \ No newline at end of file From e98fe3c93e90d00d93bf561a07b31ddfed8ef7e7 Mon Sep 17 00:00:00 2001 From: ksetoue Date: Tue, 30 Nov 2021 02:24:27 -0300 Subject: [PATCH 3/3] :heavy_plus_sign: add junit package --- build.gradle | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build.gradle b/build.gradle index a301a3b..e543d5c 100644 --- a/build.gradle +++ b/build.gradle @@ -14,6 +14,8 @@ repositories { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" + implementation 'org.assertj:assertj-core:3.21.0' + testCompile group: 'junit', name: 'junit', version: '4.12' }