Stack Test Algorithm

The Stack Test Algorithm is a method used for evaluating the performance of computer systems, particularly in the context of memory and data structure management. This algorithm is primarily used to analyze the behavior of a stack, a fundamental data structure in computer science that follows the Last-In-First-Out (LIFO) principle. In the Stack Test Algorithm, a series of push and pop operations are performed on a stack to measure its efficiency in terms of time and space complexity. The algorithm tests various aspects of stack functionality, including the speed of adding and removing elements, the proper management of memory, and the correct implementation of the LIFO principle. By analyzing the performance of these operations, developers can identify potential bottlenecks and optimize their stack implementations for better overall system performance. The Stack Test Algorithm generally involves two main steps: pushing elements onto the stack and then popping them off. The first step, pushing elements onto the stack, involves adding a predefined number of elements or data items to the stack, typically in a loop. This process helps gauge the time taken to add elements to the stack and the space used by the stack as it grows. The second step, popping elements off the stack, involves removing the elements from the stack, again in a loop, until the stack is empty. This process helps measure the time taken to remove elements from the stack and the efficiency of the stack's memory management when elements are removed. By comparing the time and space complexities for various stack implementations, developers can determine the most efficient stack design for their specific application, ensuring optimal performance and resource usage.
/*
 * 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 StackTest {
    @Test
    fun emptyTest() {
        val stack = Stack<Int>()
        Assert.assertEquals(0, stack.size)
        Assert.assertTrue(stack.isEmpty())
    }

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

    @Test
    fun naiveTest() {
        val stack = Stack<Int>()
        for (i in 0..10) {
            stack.push(i)
        }
        for (i in 10 downTo 0) {
            Assert.assertEquals(i, stack.peek())
            Assert.assertEquals(i, stack.poll())
        }
        Assert.assertEquals(0, stack.size)
    }

    @Test
    fun naiveIteratorTest() {
        val stack = Stack<Int>()
        for (i in 0..10) {
            stack.push(i)
        }

        var k = 10
        for (i in stack) {
            Assert.assertEquals(i, k--)
        }
    }

    @Test
    fun naiveContainsTest() {
        val stack = Stack<Int>()
        for (i in 0..10) {
            stack.push(i)
        }

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

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

LANGUAGE:

DARK MODE: