Livt packages

Livt.Math

Version 1.7.2

Reusable integer and fixed-point arithmetic, complex numbers, trigonometry, and statistics for FPGA applications written in Livt.

Published files (90)

tests/fixedpoint/UQ8Test.lvt

namespace Livt.Math.Tests.FixedPoint

using Livt.Math.FixedPoint

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

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

	/**
	 * Checks that Clamp s boundaries.
	 */
	@Test
	fn ClampsBoundaries()
	{
		uq8.Clamp(300)
		assert uq8.GetResult() == 255
		uq8.Clamp(-1)
		assert uq8.GetResult() == 0
		uq8.Clamp(42)
		assert uq8.GetResult() == 42
	}

	/**
	 * Checks that Add s and subtracts saturating.
	 */
	@Test
	fn AddsAndSubtractsSaturating()
	{
		uq8.AddSaturating(250, 10)
		assert uq8.GetResult() == 255
		uq8.SubSaturating(4, 10)
		assert uq8.GetResult() == 0
		uq8.AddSaturating(20, 7)
		assert uq8.GetResult() == 27
	}

	/**
	 * Checks that Mul tiplies with rescale.
	 */
	@Test
	fn MultipliesWithRescale()
	{
		uq8.Mul(128, 128)
		assert uq8.GetResult() == 64
		uq8.Mul(255, 255)
		assert uq8.GetResult() == 254
	}

	/**
	 * Checks the direct context free helpers case.
	 */
	@Test
	fn DirectContextFreeHelpers()
	{
		var high: int = UQ8.ClampValue(300)
		var low: int = UQ8.ClampValue(-1)
		var added: int = UQ8.AddSaturatingValue(250, 10)
		var subtracted: int = UQ8.SubSaturatingValue(4, 10)
		var multiplied: int = UQ8.MulValue(128, 128)

		assert high == 255
		assert low == 0
		assert added == 255
		assert subtracted == 0
		assert multiplied == 64
	}
}