Livt packages

Livt.Collections

Version 1.4.2

Fixed-capacity FIFOs, queues, stacks, circular buffers, lists, and bit sets for Livt hardware.

Published files (62)

tests/stack/Stack32Test.lvt

namespace Livt.Collections.Tests.Stack

using Livt.Collections.Stack

/**
 * Exercises Stack32 with a shared fixture instance. Each scenario establishes the inputs
 * and state it needs.
 */
@Test
component Stack32Test
{
	// Shared fixture instances, signals, and observation state.
	stack: Stack32

	/**
	 * Creates the shared fixture instances (Stack32). Tests initialize the state required by
	 * their scenario.
	 */
	new()
	{
		this.stack = new Stack32()
	}

	/**
	 * Checks the lifo order and peek case.
	 */
	@Test
	fn TestLifoOrderAndPeek()
	{
		this.stack.Clear()
		this.stack.Push(0b0000_0001)
		this.stack.Push(0b0000_0010)
		this.stack.Push(0b0000_0011)
		assert this.stack.Peek() == 0b0000_0011
		assert this.stack.GetSize() == 3
		assert this.stack.Pop() == 0b0000_0011
		assert this.stack.Pop() == 0b0000_0010
		assert this.stack.Pop() == 0b0000_0001
		assert this.stack.IsEmpty() == true
	}

	/**
	 * Checks the full stack rejects additional element case.
	 */
	@Test
	fn TestFullStackRejectsAdditionalElement()
	{
		this.stack.Clear()

		for (var i = 0; i < 32; i++) {
			this.stack.Push(0b0000_0001)
		}

		this.stack.Push(0b0000_0010)
		assert this.stack.IsFull() == true
		assert this.stack.GetSize() == 32

		for (var i = 0; i < 32; i++) {
			assert this.stack.Pop() == 0b0000_0001
		}
	}

	/**
	 * Checks that Clear allows reuse.
	 */
	@Test
	fn TestClearAllowsReuse()
	{
		this.stack.Clear()
		this.stack.Push(0b0000_0001)
		this.stack.Clear()
		this.stack.Push(0b0000_0010)
		assert this.stack.GetSize() == 1
		assert this.stack.Pop() == 0b0000_0010
	}
}