Queue Test Algorithm

The Queue Test Algorithm is an efficient method to perform operations on a queue data structure, which is an ordered list of elements in which an element is inserted at one end and removed from the other end. This algorithm allows elements to be processed in a first-in, first-out (FIFO) order, making it highly suitable for various applications such as scheduling, buffering, and managing processes in operating systems. The primary operations in a queue are enqueue (insertion) and dequeue (removal), where enqueue adds an element at the rear end of the queue, and dequeue removes an element from the front end. The Queue Test Algorithm implements these operations using a combination of data structures and pointers, ensuring smooth and efficient functioning. For instance, when enqueue operation is performed, the algorithm checks if there is enough space in the queue to accommodate the new element; if yes, then the element is inserted at the rear end and the rear pointer is updated accordingly. Similarly, during the dequeue operation, the algorithm checks if the queue is not empty; if not, then the front element is removed and the front pointer is updated. Additionally, the algorithm also manages edge cases such as underflow (attempt to dequeue an empty queue) and overflow (attempt to enqueue a full queue) to maintain the integrity of the queue data structure. Overall, the Queue Test Algorithm plays a crucial role in managing and optimizing queue operations in various computing applications.
/*
 * Copyright (c) 2017 Kotlin Algorithm Club
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.algorithmexamples.datastructures

import org.junit.Assert
import org.junit.Test
import java.util.*

class QueueTest {
    @Test
    fun emptyTest() {
        val queue = Queue<Int>()
        Assert.assertEquals(0, queue.size)
        Assert.assertTrue(queue.isEmpty())
    }

    @Test(expected= NoSuchElementException::class)
    fun exceptionTest() {
        val queue = Queue<Int>()
        queue.peek()
    }

    @Test
    fun naiveTest() {
        val queue = Queue<Int>()
        for (i in 0..10) {
            queue.add(i)
        }
        for (i in 0..10) {
            Assert.assertEquals(i, queue.peek())
            Assert.assertEquals(i, queue.poll())
        }
        Assert.assertEquals(0, queue.size)
    }

    @Test
    fun naiveIteratorTest() {
        val queue = Queue<Int>()
        for (i in 0..10) {
            queue.add(i)
        }

        var k = 0
        for (i in queue) {
            Assert.assertEquals(i, k++)
        }
    }

    @Test
    fun naiveContainsTest() {
        val queue = Queue<Int>()
        for (i in 0..10) {
            queue.add(i)
        }

        for (i in 0..10) {
            Assert.assertTrue(queue.contains(i))
        }

        Assert.assertFalse(queue.contains(100))
        Assert.assertFalse(queue.contains(101))
        Assert.assertFalse(queue.contains(103))
    }
}

LANGUAGE:

DARK MODE: