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/sqrt/SqrtNewtonRaphsonTest.lvt

namespace Livt.Math.Tests.Sqrt

using Livt.Math.Sqrt

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

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

	/**
	 * Checks the zero case.
	 */
	@Test
	fn TestZero()
	{
		var result = sqrt.Compute(0)
		assert result == 0
	}

	/**
	 * Checks the one case.
	 */
	@Test
	fn TestOne()
	{
		var result = sqrt.Compute(1)
		assert result == 1
	}

	/**
	 * Checks the non perfect square2 case.
	 */
	@Test
	fn TestNonPerfectSquare2()
	{
		var result = sqrt.Compute(2)
		assert result == 1
	}

	/**
	 * Checks the non perfect square3 case.
	 */
	@Test
	fn TestNonPerfectSquare3()
	{
		var result = sqrt.Compute(3)
		assert result == 1
	}

	/**
	 * Checks the perfect square4 case.
	 */
	@Test
	fn TestPerfectSquare4()
	{
		var result = sqrt.Compute(4)
		assert result == 2
	}

	/**
	 * Checks the perfect square9 case.
	 */
	@Test
	fn TestPerfectSquare9()
	{
		var result = sqrt.Compute(9)
		assert result == 3
	}

	/**
	 * Checks the perfect square100 case.
	 */
	@Test
	fn TestPerfectSquare100()
	{
		var result = sqrt.Compute(100)
		assert result == 10
	}

	/**
	 * Checks the non perfect square99 case.
	 */
	@Test
	fn TestNonPerfectSquare99()
	{
		var result = sqrt.Compute(99)
		assert result == 9
	}

	/**
	 * Checks the perfect square16129 case.
	 */
	@Test
	fn TestPerfectSquare16129()
	{
		var result = sqrt.Compute(16129)
		assert result == 127
	}

	/**
	 * Checks the non perfect square32766 case.
	 */
	@Test
	fn TestNonPerfectSquare32766()
	{
		var result = sqrt.Compute(32766)
		assert result == 181
	}

	/**
	 * Checks the max case.
	 */
	@Test
	fn TestMax()
	{
		var result = sqrt.Compute(SqrtNewtonRaphson.MAX_INPUT)
		assert result == 181
	}

	/**
	 * Checks that Clamp above max.
	 */
	@Test
	fn TestClampAboveMax()
	{
		var result = sqrt.Compute(40000)
		assert result == 181
	}

	/**
	 * Checks that Clamp below zero.
	 */
	@Test
	fn TestClampBelowZero()
	{
		var result = sqrt.Compute(-1)
		assert result == 0
	}
}