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/circular/CircularBufferTest.lvt

namespace Livt.Collections.Tests.Circular

using Livt.Collections.Circular

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

	// Shared fixture instances, signals, and observation state.
	buffer16: CircularBuffer16

	// Shared fixture instances, signals, and observation state.
	buffer32: CircularBuffer32

	// Shared fixture instances, signals, and observation state.
	buffer64: CircularBuffer64

	/**
	 * Creates the shared fixture instances (CircularBuffer8, CircularBuffer16,
	 * CircularBuffer32, CircularBuffer64). Tests initialize the state required by their
	 * scenario.
	 */
	new()
	{
		this.buffer8 = new CircularBuffer8()
		this.buffer16 = new CircularBuffer16()
		this.buffer32 = new CircularBuffer32()
		this.buffer64 = new CircularBuffer64()
	}

	/**
	 * Checks the buffer8 overwrites oldest case.
	 */
	@Test
	fn TestBuffer8OverwritesOldest()
	{
		this.buffer8.Clear()
		this.buffer8.Write(0b0000_0001)

		for (var i = 1; i < 8; i++) {
			this.buffer8.Write(0b0000_0010)
		}

		this.buffer8.Write(0b0000_0011)
		assert this.buffer8.IsFull() == true
		assert this.buffer8.GetSize() == 8
		assert this.buffer8.Read() == 0b0000_0010
	}

	/**
	 * Checks the buffer16 capacity case.
	 */
	@Test
	fn TestBuffer16Capacity()
	{
		this.buffer16.Clear()

		for (var i = 0; i < 16; i++) {
			this.buffer16.Write(0b0000_0001)
		}

		this.buffer16.Write(0b0000_0010)
		assert this.buffer16.IsFull() == true
		assert this.buffer16.GetSize() == 16
		assert this.buffer16.Read() == 0b0000_0001
	}

	/**
	 * Checks the buffer32 wrap around case.
	 */
	@Test
	fn TestBuffer32WrapAround()
	{
		this.buffer32.Clear()

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

		for (var i = 0; i < 16; i++) {
			this.buffer32.Read()
		}

		for (var i = 0; i < 16; i++) {
			this.buffer32.Write(0b0000_0010)
		}

		for (var i = 0; i < 16; i++) {
			assert this.buffer32.Read() == 0b0000_0001
		}

		for (var i = 0; i < 16; i++) {
			assert this.buffer32.Read() == 0b0000_0010
		}

		assert this.buffer32.IsEmpty() == true
	}

	/**
	 * Checks the buffer64 fifo and overwrite case.
	 */
	@Test
	fn TestBuffer64FifoAndOverwrite()
	{
		this.buffer64.Clear()
		this.buffer64.Write(0b0000_0001)

		for (var i = 1; i < 64; i++) {
			this.buffer64.Write(0b0000_0010)
		}

		this.buffer64.Write(0b0000_0011)
		assert this.buffer64.GetSize() == 64

		for (var i = 0; i < 63; i++) {
			assert this.buffer64.Read() == 0b0000_0010
		}

		assert this.buffer64.Read() == 0b0000_0011
	}

	/**
	 * Checks the empty read peek and clear case.
	 */
	@Test
	fn TestEmptyReadPeekAndClear()
	{
		this.buffer8.Clear()
		assert this.buffer8.Read() == 0b0000_0000
		assert this.buffer8.Peek() == 0b0000_0000
		this.buffer8.Write(0b0000_0011)
		assert this.buffer8.Peek() == 0b0000_0011
		assert this.buffer8.GetSize() == 1
		this.buffer8.Clear()
		assert this.buffer8.IsEmpty() == true
	}
}