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/arithmetic/MacUnitTest.lvt

namespace Livt.Math.Tests.Arithmetic

using Livt.Math.Arithmetic

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

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

	/**
	 * Checks that the component starts at zero.
	 */
	@Test
	fn StartsAtZero()
	{
		assert mac.GetResult() == 0
	}

	/**
	 * Checks that Accumulate s products.
	 */
	@Test
	fn AccumulatesProducts()
	{
		mac.Reset()
		mac.Accumulate(3, 4)
		mac.Accumulate(2, 5)

		assert mac.GetResult() == 22
	}

	/**
	 * Checks that the component handles zero input.
	 */
	@Test
	fn HandlesZeroInput()
	{
		mac.Reset()
		mac.Accumulate(0, 255)

		assert mac.GetResult() == 0
	}

	/**
	 * Checks that Reset s accumulator.
	 */
	@Test
	fn ResetsAccumulator()
	{
		mac.Reset()
		mac.Accumulate(3, 4)
		mac.Reset()

		assert mac.GetResult() == 0
	}

	/**
	 * Checks that the component saturates accumulator.
	 */
	@Test
	fn SaturatesAccumulator()
	{
		mac.Reset()
		mac.Accumulate(200, 200)

		assert mac.GetResult() == MacUnit.ACC_MAX
	}

	/**
	 * Checks that the component saturates negative accumulator.
	 */
	@Test
	fn SaturatesNegativeAccumulator()
	{
		mac.Reset()
		mac.Accumulate(-200, 200)

		assert mac.GetResult() == MacUnit.ACC_MIN
	}

	/**
	 * Checks that the component supports dot product pattern.
	 */
	@Test
	fn SupportsDotProductPattern()
	{
		mac.Reset()
		mac.Accumulate(1, 4)
		mac.Accumulate(2, 5)
		mac.Accumulate(3, 6)

		assert mac.GetResult() == 32
	}
}