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/ConvertTest.lvt

namespace Livt.Base.Tests

using Livt.Convert

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

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

	// -----------------------------------------------------------------------
	// ToUInt8Clamped
	// -----------------------------------------------------------------------

	/**
	 * Checks that ToUInt8Clamped returns zero for minimum.
	 */
	@Test
	fn ToUInt8ClampedReturnsZeroForMinimum()
	{
		assert this.conv.ToUInt8Clamped(0) == 0
	}

	/**
	 * Checks that ToUInt8Clamped returns255 for maximum.
	 */
	@Test
	fn ToUInt8ClampedReturns255ForMaximum()
	{
		assert this.conv.ToUInt8Clamped(255) == 255
	}

	/**
	 * Checks that ToUInt8Clamped clamps negative to zero.
	 */
	@Test
	fn ToUInt8ClampedClampsNegativeToZero()
	{
		assert this.conv.ToUInt8Clamped(-1) == 0
		assert this.conv.ToUInt8Clamped(-100) == 0
	}

	/**
	 * Checks that ToUInt8Clamped clamps overflow to255.
	 */
	@Test
	fn ToUInt8ClampedClampsOverflowTo255()
	{
		assert this.conv.ToUInt8Clamped(256) == 255
		assert this.conv.ToUInt8Clamped(1000) == 255
	}

	/**
	 * Checks that ToUInt8Clamped passes through valid value.
	 */
	@Test
	fn ToUInt8ClampedPassesThroughValidValue()
	{
		assert this.conv.ToUInt8Clamped(128) == 128
		assert this.conv.ToUInt8Clamped(100) == 100
	}

	// -----------------------------------------------------------------------
	// ToUInt8Wrapping
	// -----------------------------------------------------------------------

	/**
	 * Checks that ToUInt8Wrapping passes through in range.
	 */
	@Test
	fn ToUInt8WrappingPassesThroughInRange()
	{
		assert this.conv.ToUInt8Wrapping(0) == 0
		assert this.conv.ToUInt8Wrapping(255) == 255
	}

	/**
	 * Checks that ToUInt8Wrapping wraps above255.
	 */
	@Test
	fn ToUInt8WrappingWrapsAbove255()
	{
		assert this.conv.ToUInt8Wrapping(256) == 0
		assert this.conv.ToUInt8Wrapping(257) == 1
		assert this.conv.ToUInt8Wrapping(511) == 255
	}

	/**
	 * Checks that ToUInt8Wrapping wraps negative values.
	 */
	@Test
	fn ToUInt8WrappingWrapsNegativeValues()
	{
		assert this.conv.ToUInt8Wrapping(-1) == 255
		assert this.conv.ToUInt8Wrapping(-256) == 0
	}

	// -----------------------------------------------------------------------
	// ToUInt8Truncated
	// -----------------------------------------------------------------------

	/**
	 * Checks that ToUInt8Truncated keeps low8 bits.
	 */
	@Test
	fn ToUInt8TruncatedKeepsLow8Bits()
	{
		assert this.conv.ToUInt8Truncated(0) == 0
		assert this.conv.ToUInt8Truncated(255) == 255
		assert this.conv.ToUInt8Truncated(256) == 0
		assert this.conv.ToUInt8Truncated(257) == 1
	}

	// -----------------------------------------------------------------------
	// ToInt8Clamped
	// -----------------------------------------------------------------------

	/**
	 * Checks that ToInt8Clamped returns minus one hundred twenty eight for minimum.
	 */
	@Test
	fn ToInt8ClampedReturnsMinusOneHundredTwentyEightForMinimum()
	{
		assert this.conv.ToInt8Clamped(-128) == -128
	}

	/**
	 * Checks that ToInt8Clamped returns one twenty seven for maximum.
	 */
	@Test
	fn ToInt8ClampedReturnsOneTwentySevenForMaximum()
	{
		assert this.conv.ToInt8Clamped(127) == 127
	}

	/**
	 * Checks that ToInt8Clamped clamps underflow to minus twenty eight.
	 */
	@Test
	fn ToInt8ClampedClampsUnderflowToMinusTwentyEight()
	{
		assert this.conv.ToInt8Clamped(-129) == -128
		assert this.conv.ToInt8Clamped(-1000) == -128
	}

	/**
	 * Checks that ToInt8Clamped clamps overflow to one twenty seven.
	 */
	@Test
	fn ToInt8ClampedClampsOverflowToOneTwentySeven()
	{
		assert this.conv.ToInt8Clamped(128) == 127
		assert this.conv.ToInt8Clamped(1000) == 127
	}

	/**
	 * Checks that ToInt8Clamped passes through valid values.
	 */
	@Test
	fn ToInt8ClampedPassesThroughValidValues()
	{
		assert this.conv.ToInt8Clamped(0) == 0
		assert this.conv.ToInt8Clamped(-1) == -1
		assert this.conv.ToInt8Clamped(50) == 50
		assert this.conv.ToInt8Clamped(-50) == -50
	}

	// -----------------------------------------------------------------------
	// ToInt8Wrapping
	// -----------------------------------------------------------------------

	/**
	 * Checks that ToInt8Wrapping passes through in range.
	 */
	@Test
	fn ToInt8WrappingPassesThroughInRange()
	{
		assert this.conv.ToInt8Wrapping(0) == 0
		assert this.conv.ToInt8Wrapping(-128) == -128
		assert this.conv.ToInt8Wrapping(127) == 127
	}

	/**
	 * Checks that ToInt8Wrapping wraps positive overflow.
	 */
	@Test
	fn ToInt8WrappingWrapsPositiveOverflow()
	{
		assert this.conv.ToInt8Wrapping(128) == -128
		assert this.conv.ToInt8Wrapping(255) == -1
		assert this.conv.ToInt8Wrapping(256) == 0
	}

	/**
	 * Checks that ToInt8Wrapping wraps negative underflow.
	 */
	@Test
	fn ToInt8WrappingWrapsNegativeUnderflow()
	{
		assert this.conv.ToInt8Wrapping(-129) == 127
	}

	// -----------------------------------------------------------------------
	// ToInt8Truncated
	// -----------------------------------------------------------------------

	/**
	 * Checks that ToInt8Truncated passes through in range.
	 */
	@Test
	fn ToInt8TruncatedPassesThroughInRange()
	{
		assert this.conv.ToInt8Truncated(0) == 0
		assert this.conv.ToInt8Truncated(127) == 127
		assert this.conv.ToInt8Truncated(-128) == -128
	}

	/**
	 * Checks that ToInt8Truncated wraps out of range.
	 */
	@Test
	fn ToInt8TruncatedWrapsOutOfRange()
	{
		assert this.conv.ToInt8Truncated(128) == -128
		assert this.conv.ToInt8Truncated(255) == -1
	}

	// -----------------------------------------------------------------------
	// ToUInt16Clamped
	// -----------------------------------------------------------------------

	/**
	 * Checks that ToUInt16Clamped passes through valid value.
	 */
	@Test
	fn ToUInt16ClampedPassesThroughValidValue()
	{
		assert this.conv.ToUInt16Clamped(0) == 0
		assert this.conv.ToUInt16Clamped(65535) == 65535
		assert this.conv.ToUInt16Clamped(1000) == 1000
	}

	/**
	 * Checks that ToUInt16Clamped clamps negative to zero.
	 */
	@Test
	fn ToUInt16ClampedClampsNegativeToZero()
	{
		assert this.conv.ToUInt16Clamped(-1) == 0
	}

	/**
	 * Checks that ToUInt16Clamped clamps overflow to65535.
	 */
	@Test
	fn ToUInt16ClampedClampsOverflowTo65535()
	{
		assert this.conv.ToUInt16Clamped(65536) == 65535
	}

	// -----------------------------------------------------------------------
	// ToUInt16Wrapping
	// -----------------------------------------------------------------------

	/**
	 * Checks that ToUInt16Wrapping wraps above65535.
	 */
	@Test
	fn ToUInt16WrappingWrapsAbove65535()
	{
		assert this.conv.ToUInt16Wrapping(65536) == 0
		assert this.conv.ToUInt16Wrapping(65537) == 1
	}

	/**
	 * Checks that ToUInt16Wrapping wraps negative values.
	 */
	@Test
	fn ToUInt16WrappingWrapsNegativeValues()
	{
		assert this.conv.ToUInt16Wrapping(-1) == 65535
	}

	// -----------------------------------------------------------------------
	// ToInt16Clamped
	// -----------------------------------------------------------------------

	/**
	 * Checks that ToInt16Clamped passes through valid values.
	 */
	@Test
	fn ToInt16ClampedPassesThroughValidValues()
	{
		assert this.conv.ToInt16Clamped(0) == 0
		assert this.conv.ToInt16Clamped(-32768) == -32768
		assert this.conv.ToInt16Clamped(32767) == 32767
	}

	/**
	 * Checks that ToInt16Clamped clamps underflow.
	 */
	@Test
	fn ToInt16ClampedClampsUnderflow()
	{
		assert this.conv.ToInt16Clamped(-32769) == -32768
	}

	/**
	 * Checks that ToInt16Clamped clamps overflow.
	 */
	@Test
	fn ToInt16ClampedClampsOverflow()
	{
		assert this.conv.ToInt16Clamped(32768) == 32767
	}

	// -----------------------------------------------------------------------
	// ToInt16Wrapping
	// -----------------------------------------------------------------------

	/**
	 * Checks that ToInt16Wrapping passes through in range.
	 */
	@Test
	fn ToInt16WrappingPassesThroughInRange()
	{
		assert this.conv.ToInt16Wrapping(0) == 0
		assert this.conv.ToInt16Wrapping(-32768) == -32768
		assert this.conv.ToInt16Wrapping(32767) == 32767
	}

	/**
	 * Checks that ToInt16Wrapping wraps positive overflow.
	 */
	@Test
	fn ToInt16WrappingWrapsPositiveOverflow()
	{
		assert this.conv.ToInt16Wrapping(32768) == -32768
		assert this.conv.ToInt16Wrapping(65535) == -1
	}

	/**
	 * Checks that ToInt16Wrapping wraps negative underflow.
	 */
	@Test
	fn ToInt16WrappingWrapsNegativeUnderflow()
	{
		assert this.conv.ToInt16Wrapping(-32769) == 32767
	}

	// -----------------------------------------------------------------------
	// ToByteClamped, ToByteWrapping, ToByteTruncated
	// -----------------------------------------------------------------------

	/**
	 * Checks that ToByteClamped clamps to byte range.
	 */
	@Test
	fn ToByteClampedClampsToByteRange()
	{
		assert this.conv.ToByteClamped(0) == 0
		assert this.conv.ToByteClamped(255) == 255
		assert this.conv.ToByteClamped(-1) == 0
		assert this.conv.ToByteClamped(256) == 255
	}

	/**
	 * Checks that ToByteWrapping wraps to byte range.
	 */
	@Test
	fn ToByteWrappingWrapsToByteRange()
	{
		assert this.conv.ToByteWrapping(0) == 0
		assert this.conv.ToByteWrapping(255) == 255
		assert this.conv.ToByteWrapping(256) == 0
		assert this.conv.ToByteWrapping(-1) == 255
	}

	/**
	 * Checks that ToByteTruncated keeps low8 bits.
	 */
	@Test
	fn ToByteTruncatedKeepsLow8Bits()
	{
		assert this.conv.ToByteTruncated(0) == 0
		assert this.conv.ToByteTruncated(255) == 255
		assert this.conv.ToByteTruncated(256) == 0
		assert this.conv.ToByteTruncated(257) == 1
	}
}