Livt packages

Livt.Base

Version 1.2.1

Foundational bit, array, string, conversion, formatting, and timing helpers for Livt hardware designs.

Published files (32)

tests/ArrayTest.lvt

namespace Livt.Base.Tests

using Livt.Array

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

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

	// -----------------------------------------------------------------------
	// IsIndexInRange
	// -----------------------------------------------------------------------

	/**
	 * Checks that IsIndexInRange returns true for first index.
	 */
	@Test
	fn IsIndexInRangeReturnsTrueForFirstIndex()
	{
		assert this.arr.IsIndexInRange(0, 16) == true
	}

	/**
	 * Checks that IsIndexInRange returns true for last index.
	 */
	@Test
	fn IsIndexInRangeReturnsTrueForLastIndex()
	{
		assert this.arr.IsIndexInRange(15, 16) == true
	}

	/**
	 * Checks that IsIndexInRange returns true for middle index.
	 */
	@Test
	fn IsIndexInRangeReturnsTrueForMiddleIndex()
	{
		assert this.arr.IsIndexInRange(7, 16) == true
	}

	/**
	 * Checks that IsIndexInRange returns false for negative index.
	 */
	@Test
	fn IsIndexInRangeReturnsFalseForNegativeIndex()
	{
		assert this.arr.IsIndexInRange(-1, 16) == false
	}

	/**
	 * Checks that IsIndexInRange returns false for index at size.
	 */
	@Test
	fn IsIndexInRangeReturnsFalseForIndexAtSize()
	{
		assert this.arr.IsIndexInRange(16, 16) == false
	}

	/**
	 * Checks that IsIndexInRange returns false for index above size.
	 */
	@Test
	fn IsIndexInRangeReturnsFalseForIndexAboveSize()
	{
		assert this.arr.IsIndexInRange(100, 16) == false
	}

	/**
	 * Checks that IsIndexInRange returns false for zero size array.
	 */
	@Test
	fn IsIndexInRangeReturnsFalseForZeroSizeArray()
	{
		assert this.arr.IsIndexInRange(0, 0) == false
	}

	// -----------------------------------------------------------------------
	// Equals — generic (byte[], size)
	// -----------------------------------------------------------------------

	/**
	 * Checks the byte equals returns true for identical16 byte arrays case.
	 */
	@Test
	fn ByteEqualsReturnsTrueForIdentical16ByteArrays()
	{
		var a: byte[16] = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
			0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]
		var b: byte[16] = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
			0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]
		assert this.arr.Equals(a, b, 16) == true
	}

	/**
	 * Checks the byte equals returns true for all zero16 byte arrays case.
	 */
	@Test
	fn ByteEqualsReturnsTrueForAllZero16ByteArrays()
	{
		var a: byte[16] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
		var b: byte[16] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
		assert this.arr.Equals(a, b, 16) == true
	}

	/**
	 * Checks the byte equals returns false when first byte differs16 case.
	 */
	@Test
	fn ByteEqualsReturnsFalseWhenFirstByteDiffers16()
	{
		var a: byte[16] = [0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
		var b: byte[16] = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
		assert this.arr.Equals(a, b, 16) == false
	}

	/**
	 * Checks the byte equals returns false when last byte differs16 case.
	 */
	@Test
	fn ByteEqualsReturnsFalseWhenLastByteDiffers16()
	{
		var a: byte[16] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]
		var b: byte[16] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02]
		assert this.arr.Equals(a, b, 16) == false
	}

	/**
	 * Checks the byte equals returns false when middle byte differs16 case.
	 */
	@Test
	fn ByteEqualsReturnsFalseWhenMiddleByteDiffers16()
	{
		var a: byte[16] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
		var b: byte[16] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
		assert this.arr.Equals(a, b, 16) == false
	}

	/**
	 * Checks the byte equals returns true for identical32 byte arrays case.
	 */
	@Test
	fn ByteEqualsReturnsTrueForIdentical32ByteArrays()
	{
		var a: byte[32] = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
			0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
			0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
			0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20]
		var b: byte[32] = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
			0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
			0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
			0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20]
		assert this.arr.Equals(a, b, 32) == true
	}

	/**
	 * Checks the byte equals returns false when middle byte differs32 case.
	 */
	@Test
	fn ByteEqualsReturnsFalseWhenMiddleByteDiffers32()
	{
		var a: byte[32] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
		var b: byte[32] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
		assert this.arr.Equals(a, b, 32) == false
	}

	/**
	 * Checks the byte equals returns false for64 byte arrays differing at last byte case.
	 */
	@Test
	fn ByteEqualsReturnsFalseFor64ByteArraysDifferingAtLastByte()
	{
		var a: byte[64] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]
		var b: byte[64] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
		assert this.arr.Equals(a, b, 64) == false
	}

	// -----------------------------------------------------------------------
	// IndexOf — generic (byte[], size, value)
	// -----------------------------------------------------------------------

	/**
	 * Checks the byte index of finds value at first position16 case.
	 */
	@Test
	fn ByteIndexOfFindsValueAtFirstPosition16()
	{
		var arr: byte[16] = [0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
		assert this.arr.IndexOf(arr, 16, 0xAA) == 0
	}

	/**
	 * Checks the byte index of finds value at last position16 case.
	 */
	@Test
	fn ByteIndexOfFindsValueAtLastPosition16()
	{
		var arr: byte[16] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBB]
		assert this.arr.IndexOf(arr, 16, 0xBB) == 15
	}

	/**
	 * Checks the byte index of returns minus one when not found16 case.
	 */
	@Test
	fn ByteIndexOfReturnsMinusOneWhenNotFound16()
	{
		var arr: byte[16] = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
			0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]
		assert this.arr.IndexOf(arr, 16, 0xFF) == -1
	}

	/**
	 * Checks the byte index of finds first of duplicates16 case.
	 */
	@Test
	fn ByteIndexOfFindsFirstOfDuplicates16()
	{
		var arr: byte[16] = [0x00, 0x42, 0x00, 0x42, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
		assert this.arr.IndexOf(arr, 16, 0x42) == 1
	}

	/**
	 * Checks the byte index of finds zero byte16 case.
	 */
	@Test
	fn ByteIndexOfFindsZeroByte16()
	{
		var arr: byte[16] = [0x01, 0x02, 0x03, 0x00, 0x05, 0x06, 0x07, 0x08,
			0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]
		assert this.arr.IndexOf(arr, 16, 0x00) == 3
	}

	/**
	 * Checks the byte index of finds value at high position32 case.
	 */
	@Test
	fn ByteIndexOfFindsValueAtHighPosition32()
	{
		var arr: byte[32] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
		assert this.arr.IndexOf(arr, 32, 0xFF) == 20
	}

	/**
	 * Checks the byte index of returns minus one when not found32 case.
	 */
	@Test
	fn ByteIndexOfReturnsMinusOneWhenNotFound32()
	{
		var arr: byte[32] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
		assert this.arr.IndexOf(arr, 32, 0xAA) == -1
	}

	/**
	 * Checks the byte index of finds value at high position64 case.
	 */
	@Test
	fn ByteIndexOfFindsValueAtHighPosition64()
	{
		var arr: byte[64] = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0xCC, 0x00, 0x00, 0x00]
		assert this.arr.IndexOf(arr, 64, 0xCC) == 60
	}

	// -----------------------------------------------------------------------
	// CountOf — generic (byte[], size, value)
	// -----------------------------------------------------------------------

	/**
	 * Checks the byte count of returns zero when value absent16 case.
	 */
	@Test
	fn ByteCountOfReturnsZeroWhenValueAbsent16()
	{
		var arr: byte[16] = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
			0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10]
		assert this.arr.CountOf(arr, 16, 0xFF) == 0
	}

	/**
	 * Checks the byte count of returns correct count16 case.
	 */
	@Test
	fn ByteCountOfReturnsCorrectCount16()
	{
		var arr: byte[16] = [0x00, 0x42, 0x00, 0x42, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42]
		assert this.arr.CountOf(arr, 16, 0x42) == 3
	}

	/**
	 * Checks the byte count of returns full count for all same value16 case.
	 */
	@Test
	fn ByteCountOfReturnsFullCountForAllSameValue16()
	{
		var arr: byte[16] = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
			0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
		assert this.arr.CountOf(arr, 16, 0xFF) == 16
	}

	/**
	 * Checks the byte count of returns correct count32 case.
	 */
	@Test
	fn ByteCountOfReturnsCorrectCount32()
	{
		var arr: byte[32] = [0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]
		assert this.arr.CountOf(arr, 32, 0x01) == 3
	}

	/**
	 * Checks the byte count of returns correct count64 case.
	 */
	@Test
	fn ByteCountOfReturnsCorrectCount64()
	{
		var arr: byte[64] = [0xAB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0xAB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xAB]
		assert this.arr.CountOf(arr, 64, 0xAB) == 3
	}
}