Difference between revisions of "User:Pedersen/Code Samples/Unit Testing"

From Robowiki
Jump to navigation Jump to search
(migration)
 
m (Using <syntaxhighlight>.)
 
Line 1: Line 1:
 
Following are two examples of unit test scripts.  They are really boring and monotonous to write, but the more exhaustive you are the more likely you are to catch errors with new developments (assuming you run the tests).
 
Following are two examples of unit test scripts.  They are really boring and monotonous to write, but the more exhaustive you are the more likely you are to catch errors with new developments (assuming you run the tests).
  
<pre>
+
<syntaxhighlight>
 
package pedersen.physics;
 
package pedersen.physics;
  
Line 195: Line 195:
  
 
}
 
}
</pre>
+
</syntaxhighlight>
  
<pre>
+
<syntaxhighlight>
 
package pedersen.physics;
 
package pedersen.physics;
  
Line 259: Line 259:
 
 
 
}
 
}
</pre>
+
</syntaxhighlight>

Latest revision as of 09:37, 1 July 2010

Following are two examples of unit test scripts. They are really boring and monotonous to write, but the more exhaustive you are the more likely you are to catch errors with new developments (assuming you run the tests).

package pedersen.physics;

import junit.framework.TestCase;
import pedersen.core.Constraints;

public class StaticHeadingImplTest extends TestCase
{
	
	public void testGetHeading()
	{
		double rawHeading1 = Math.PI * 0.5;
		StaticHeading heading = new StaticHeadingImpl( rawHeading1 );
		assertEquals( "Heading discrepancy.", rawHeading1, heading.getHeading(), Constraints.doubleErrorTolerance );
	}
	
	public void testEqualsAngleWithDoubleForPositiveMatch()
	{
		double rawHeading1 = Math.PI * 0.5;
		double rawHeading2 = Math.PI * 2.5;
		StaticHeading heading = new StaticHeadingImpl( rawHeading1 );
		assertTrue( "Heading discrepancy.", heading.equalsAngle( rawHeading2 ) );
	}
	
	public void testEqualsAngleWithDoubleForNegativeMatch()
	{
		double rawHeading1 = Math.PI * 0.5;
		double rawHeading2 = Math.PI * -1.5;
		StaticHeading heading = new StaticHeadingImpl( rawHeading1 );
		assertTrue( "Heading discrepancy.", heading.equalsAngle( rawHeading2 ) );
	}
	
	public void testEqualsAngleWithDoubleForMismatch()
	{
		double rawHeading1 = Math.PI * 0.5;
		double rawHeading2 = Math.PI * 0.3;
		StaticHeading heading = new StaticHeadingImpl( rawHeading1 );
		assertFalse( "Heading discrepancy.", heading.equalsAngle( rawHeading2 ) );
	}
	
	public void testEqualsAngleWithStaticHeadingForPositiveMatch()
	{
		double rawHeading1 = Math.PI * 0.5;
		double rawHeading2 = Math.PI * 2.5;
		StaticHeading heading1 = new StaticHeadingImpl( rawHeading1 );
		StaticHeading heading2 = new StaticHeadingImpl( rawHeading2 );
		assertTrue( "Heading discrepancy.", heading1.equalsAngle( heading2 ) );
	}
	
	public void testEqualsAngleWithStaticHeadingForNegativeMatch()
	{
		double rawHeading1 = Math.PI * 0.5;
		double rawHeading2 = Math.PI * -1.5;
		StaticHeading heading1 = new StaticHeadingImpl( rawHeading1 );
		StaticHeading heading2 = new StaticHeadingImpl( rawHeading2 );
		assertTrue( "Heading discrepancy.", heading1.equalsAngle( heading2 ) );
	}
	
	public void testEqualsAngleWithStaticHeadingForMismatch()
	{
		double rawHeading1 = Math.PI * 0.5;
		double rawHeading2 = Math.PI * 0.3;
		StaticHeading heading1 = new StaticHeadingImpl( rawHeading1 );
		StaticHeading heading2 = new StaticHeadingImpl( rawHeading2 );
		assertFalse( "Heading discrepancy.", heading1.equalsAngle( heading2 ) );
	}

	public void testGetRelativeAngleWithDoubleForPositiveMatch()
	{
		double rawHeading1 = Math.PI * 0.5;
		double rawHeading2 = Math.PI * 0.9;
		double rawHeading3 = Math.PI * 0.4;
		StaticHeading heading = new StaticHeadingImpl( rawHeading1 );
		double relativeHeading = heading.getRelativeAngle( rawHeading2 );
		assertEquals( "Heading discrepancy.", relativeHeading, rawHeading3, Constraints.doubleErrorTolerance );
	}
	
	public void testGetRelativeAngleWithDoubleForNegativeMatch()
	{
		double rawHeading1 = Math.PI * 0.5;
		double rawHeading2 = Math.PI * 0.1;
		double rawHeading3 = Math.PI * -0.4;
		StaticHeading heading = new StaticHeadingImpl( rawHeading1 );
		double relativeHeading = heading.getRelativeAngle( rawHeading2 );
		assertEquals( "Heading discrepancy.", relativeHeading, rawHeading3, Constraints.doubleErrorTolerance );
	}
	
	public void testGetRelativeAngleWithDoubleForMismatch()
	{
		double rawHeading1 = Math.PI * 0.5;
		double rawHeading2 = Math.PI * 0.3;
		double rawHeading3 = Math.PI * 0.4;
		StaticHeading heading = new StaticHeadingImpl( rawHeading1 );
		double relativeHeading = heading.getRelativeAngle( rawHeading2 );
		assertFalse( "Heading discrepancy.", Constraints.areEqual( relativeHeading, rawHeading3 ) );
	}
	
	public void testGetRelativeAngleWithStaticHeadingForPositiveMatch()
	{
		double rawHeading1 = Math.PI * 0.5;
		double rawHeading2 = Math.PI * 0.9;
		double rawHeading3 = Math.PI * 0.4;
		StaticHeading heading1 = new StaticHeadingImpl( rawHeading1 );
		StaticHeading heading2 = new StaticHeadingImpl( rawHeading2 );
		double relativeHeading = heading1.getRelativeAngle( heading2 );
		assertEquals( "Heading discrepancy.", relativeHeading, rawHeading3, Constraints.doubleErrorTolerance );
	}
	
	public void testGetRelativeAngleWithStaticHeadingForNegativeMatch()
	{
		double rawHeading1 = Math.PI * 0.5;
		double rawHeading2 = Math.PI * 0.1;
		double rawHeading3 = Math.PI * -0.4;
		StaticHeading heading1 = new StaticHeadingImpl( rawHeading1 );
		StaticHeading heading2 = new StaticHeadingImpl( rawHeading2 );
		double relativeHeading = heading1.getRelativeAngle( heading2 );
		assertEquals( "Heading discrepancy.", relativeHeading, rawHeading3, Constraints.doubleErrorTolerance );
	}
	
	public void testGetRelativeAngleWithStaticHeadingForMismatch()
	{
		double rawHeading1 = Math.PI * 0.5;
		double rawHeading2 = Math.PI * 0.3;
		double rawHeading3 = Math.PI * 0.4;
		StaticHeading heading1 = new StaticHeadingImpl( rawHeading1 );
		StaticHeading heading2 = new StaticHeadingImpl( rawHeading2 );
		double relativeHeading = heading1.getRelativeAngle( heading2 );
		assertFalse( "Heading discrepancy.", Constraints.areEqual( relativeHeading, rawHeading3 ) );
	}

	public void testGetCompoundAngleWithDoubleForPositiveMatch()
	{
		double rawHeading1 = Math.PI * 0.5;
		double rawHeading2 = Math.PI * 0.9;
		double rawHeading3 = Math.PI * 1.4;
		StaticHeading heading = new StaticHeadingImpl( rawHeading1 );
		double compoundHeading = heading.getCompoundAngle( rawHeading2 );
		assertEquals( "Heading discrepancy.", compoundHeading, rawHeading3, Constraints.doubleErrorTolerance );
	}
	
	public void testGetCompoundAngleWithDoubleForNegativeMatch()
	{
		double rawHeading1 = Math.PI * 0.5;
		double rawHeading2 = Math.PI * -0.9;
		double rawHeading3 = Math.PI * 1.6;
		StaticHeading heading = new StaticHeadingImpl( rawHeading1 );
		double compoundHeading = heading.getCompoundAngle( rawHeading2 );
		assertEquals( "Heading discrepancy.", compoundHeading, rawHeading3, Constraints.doubleErrorTolerance );
	}
	
	public void testGetCompoundAngleWithDoubleForMismatch()
	{
		double rawHeading1 = Math.PI * 0.5;
		double rawHeading2 = Math.PI * 0.3;
		double rawHeading3 = Math.PI * 0.4;
		StaticHeading heading = new StaticHeadingImpl( rawHeading1 );
		double compoundHeading = heading.getCompoundAngle( rawHeading2 );
		assertFalse( "Heading discrepancy.", Constraints.areEqual( compoundHeading, rawHeading3 ) );
	}

	public void testGetCompoundAngleWithStaticHeadingForPositiveMatch()
	{
		double rawHeading1 = Math.PI * 0.5;
		double rawHeading2 = Math.PI * 0.9;
		double rawHeading3 = Math.PI * 1.4;
		StaticHeading heading1 = new StaticHeadingImpl( rawHeading1 );
		StaticHeading heading2 = new StaticHeadingImpl( rawHeading2 );
		double compoundHeading = heading1.getCompoundAngle( heading2 );
		assertEquals( "Heading discrepancy.", compoundHeading, rawHeading3, Constraints.doubleErrorTolerance );
	}
	
	public void testGetCompoundAngleWithStaticHeadingForNegativeMatch()
	{
		double rawHeading1 = Math.PI * 0.5;
		double rawHeading2 = Math.PI * -0.9;
		double rawHeading3 = Math.PI * 1.6;
		StaticHeading heading1 = new StaticHeadingImpl( rawHeading1 );
		StaticHeading heading2 = new StaticHeadingImpl( rawHeading2 );
		double compoundHeading = heading1.getCompoundAngle( heading2 );
		assertEquals( "Heading discrepancy.", compoundHeading, rawHeading3, Constraints.doubleErrorTolerance );
	}
	
	public void testGetCompoundAngleWithStaticHeadingForMismatch()
	{
		double rawHeading1 = Math.PI * 0.5;
		double rawHeading2 = Math.PI * 0.3;
		double rawHeading3 = Math.PI * 0.4;
		StaticHeading heading1 = new StaticHeadingImpl( rawHeading1 );
		StaticHeading heading2 = new StaticHeadingImpl( rawHeading2 );
		double compoundHeading = heading1.getCompoundAngle( heading2 );
		assertFalse( "Heading discrepancy.", Constraints.areEqual( compoundHeading, rawHeading3 ) );
	}

}
package pedersen.physics;

import junit.framework.TestCase;
import pedersen.core.Constraints;
import pedersen.core.Conversions;
import pedersen.core.Snapshot;
import pedersen.core.SnapshotImpl;

public class ProjectionTest extends TestCase
{

	public void testConstructorWithSnapshot()
	{
		double x = 5.0, y = -2.0;
		double heading = Math.PI * 0.25;
		double velocity = -5.5;
		Snapshot snapshot = new SnapshotImpl( 0, 0, x, y, heading, velocity, 0.0 );
		Projection projection = new Projection( snapshot );
		assertEquals( "Projection discrepancy.", x, projection.getX(), Constraints.doubleErrorTolerance );
		assertEquals( "Projection discrepancy.", y, projection.getY(), Constraints.doubleErrorTolerance );
		assertEquals( "Projection discrepancy.", heading, projection.getHeading(), Constraints.doubleErrorTolerance );
		assertEquals( "Projection discrepancy.", velocity, projection.getVelocity(), Constraints.doubleErrorTolerance );
	}
	
	public void testConstructorWithStaticPositionDoubleDouble()
	{
		double x = 5.0, y = -2.0;
		double heading = Math.PI * 0.25;
		double velocity = -5.5;
		StaticPosition staticPosition = new StaticPositionImpl( x, y );
		Projection projection = new Projection( staticPosition, heading, velocity );
		assertEquals( "Projection discrepancy.", x, projection.getX(), Constraints.doubleErrorTolerance );
		assertEquals( "Projection discrepancy.", y, projection.getY(), Constraints.doubleErrorTolerance );
		assertEquals( "Projection discrepancy.", heading, projection.getHeading(), Constraints.doubleErrorTolerance );
		assertEquals( "Projection discrepancy.", velocity, projection.getVelocity(), Constraints.doubleErrorTolerance );
	}

	public void testProject()
	{
		double x1 = 5.0, y1 = -2.0;
		double heading1 = Math.PI * 45.0 / 180.0; // 45 degrees
		double velocity1 = -5.5;
		double targetHeading = Math.PI * -0.25; // -45 degrees / 315 degrees
		double maxTurnRate = Conversions.getAbsMaxTurnRateFromVelocity( velocity1 );
		double targetVelocity = -8.0;
		double heading2 = heading1 - maxTurnRate;
		double velocity2 = -6.5;
		double x2 = x1 + Math.sin( heading2 ) * velocity2;
		double y2 = y1 + Math.cos( heading2 ) * velocity2;
		StaticPosition staticPosition = new StaticPositionImpl( x1, y1 );
		Projection projection = new Projection( staticPosition, heading1, velocity1 );
		projection.setAbsoluteTargetHeading( targetHeading );
		projection.setAbsoluteTargetVelocity( targetVelocity );
		projection.project();
		assertEquals( "Projection discrepancy.", x2, projection.getX(), Constraints.doubleErrorTolerance );
		assertEquals( "Projection discrepancy.", y2, projection.getY(), Constraints.doubleErrorTolerance );
		assertEquals( "Projection discrepancy.", heading2, projection.getHeading(), Constraints.doubleErrorTolerance );
		assertEquals( "Projection discrepancy.", velocity2, projection.getVelocity(), Constraints.doubleErrorTolerance );
	}
	
}