view CustomMath/IBBMathTests.cs @ 49:99e4c1949c92 default tip

* Update to v2.6 of NUnit and new syntax/API changes
author IBBoard <dev@ibboard.co.uk>
date Sun, 28 Apr 2013 19:32:14 +0100
parents 37642712f632
children
line wrap: on
line source

//  This file (IBBMathTest.cs) is a part of the IBBoard.Tests project and is copyright 2009 IBBoard
// 
//  The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
// 

using System;
using NUnit.Framework;

namespace IBBoard.CustomMath
{
	[TestFixture()]	
	public class IBBMathTest
	{
		[Test()]
		public void TestRoundHalfPerformsBankersRoundingBelowLowMidpoint()
		{
			Assert.AreEqual(3, IBBMath.RoundToHalf(3.2));
		}
		
		[Test()]
		public void TestRoundHalfPerformsBankersRoundingJustBelowLowMidpoint()
		{
			Assert.AreEqual(3, IBBMath.RoundToHalf(3.24));
		}
		
		[Test()]
		public void TestRoundHalfPerformsBankersRoundingOnLowMidpoint()
		{
			Assert.AreEqual(3, IBBMath.RoundToHalf(3.25));
		}

		[Test()]
		public void TestRoundHalfPerformsBankersRoundingJustAboveLowMidpoint()
		{
			Assert.AreEqual(3.5, IBBMath.RoundToHalf(3.26));
		}
		
		[Test()]
		public void TestRoundHalfPerformsBankersRoundingAboveLowMidpoint()
		{
			Assert.AreEqual(3.5, IBBMath.RoundToHalf(3.3));
		}
		
		[Test()]
		public void TestRoundHalfPerformsBankersRoundingBelowHighMidpoint()
		{
			Assert.AreEqual(3.5, IBBMath.RoundToHalf(3.7));
		}
		
		[Test()]
		public void TestRoundHalfPerformsBankersRoundingJustBelowHighMidpoint()
		{
			Assert.AreEqual(3.5, IBBMath.RoundToHalf(3.74));
		}
		
		[Test()]
		public void TestRoundHalfPerformsBankersRoundingOnHighMidpoint()
		{
			Assert.AreEqual(4, IBBMath.RoundToHalf(3.75));
		}
		
		[Test()]
		public void TestRoundHalfPerformsBankersRoundingJustAboveHighMidpoint()
		{
			Assert.AreEqual(4, IBBMath.RoundToHalf(3.76));
		}
		
		[Test()]
		public void TestRoundHalfPerformsBankersRoundingAboveHighMidpoint()
		{
			Assert.AreEqual(4, IBBMath.RoundToHalf(3.8));
		}
		
		[Test()]
		public void TestRoundHalfDoesntAlterHalf()
		{
			Assert.AreEqual(3.5, IBBMath.RoundToHalf(3.5));
		}
		
		[Test()]
		public void TestRoundHalfDoesntRoundEvenNumber()
		{
			Assert.AreEqual(4, IBBMath.RoundToHalf(4));
		}
		
		[Test()]
		public void TestRoundHalfDoesntRoundOddNumber()
		{
			Assert.AreEqual(3, IBBMath.RoundToHalf(3));
		}
		
		[Test()]
		public void TestRoundHalfRoundsUpBelowLowMidpoint()
		{
			Assert.AreEqual(3.5, IBBMath.CeilToHalf(3.2));
		}
		
		[Test()]
		public void TestRoundHalfRoundsUpJustBelowLowMidpoint()
		{
			Assert.AreEqual(3.5, IBBMath.CeilToHalf(3.24));
		}
		
		[Test()]
		public void TestRoundHalfRoundsUpOnLowMidpoint()
		{
			Assert.AreEqual(3.5, IBBMath.CeilToHalf(3.25));
		}

		[Test()]
		public void TestRoundHalfRoundsUpJustAboveLowMidpoint()
		{
			Assert.AreEqual(3.5, IBBMath.CeilToHalf(3.26));
		}
		
		[Test()]
		public void TestRoundHalfRoundsUpAboveLowMidpoint()
		{
			Assert.AreEqual(3.5, IBBMath.CeilToHalf(3.3));
		}
		
		[Test()]
		public void TestRoundHalfRoundsUpBelowHighMidpoint()
		{
			Assert.AreEqual(4, IBBMath.CeilToHalf(3.7));
		}
		
		[Test()]
		public void TestRoundHalfRoundsUpJustBelowHighMidpoint()
		{
			Assert.AreEqual(4, IBBMath.CeilToHalf(3.74));
		}
		
		[Test()]
		public void TestRoundHalfRoundsUpOnHighMidpoint()
		{
			Assert.AreEqual(4, IBBMath.CeilToHalf(3.75));
		}
		
		[Test()]
		public void TestRoundHalfRoundsUpJustAboveHighMidpoint()
		{
			Assert.AreEqual(4, IBBMath.CeilToHalf(3.76));
		}
		
		[Test()]
		public void TestRoundHalfRoundsUpAboveHighMidpoint()
		{
			Assert.AreEqual(4, IBBMath.CeilToHalf(3.8));
		}
		
		[Test()]
		public void TestRoundHalfUpDoesntAlterHalf()
		{
			Assert.AreEqual(3.5, IBBMath.CeilToHalf(3.5));
		}
		
		[Test()]
		public void TestRoundHalfUpDoesntRoundEvenNumber()
		{
			Assert.AreEqual(4, IBBMath.CeilToHalf(4));
		}
		
		[Test()]
		public void TestRoundHalfUpDoesntRoundOddNumber()
		{
			Assert.AreEqual(3, IBBMath.CeilToHalf(3));
		}
		
		[Test()]
		public void TestRoundHalfRoundsDownBelowLowMidpoint()
		{
			Assert.AreEqual(3, IBBMath.FloorToHalf(3.2));
		}
		
		[Test()]
		public void TestRoundHalfRoundsDownJustBelowLowMidpoint()
		{
			Assert.AreEqual(3, IBBMath.FloorToHalf(3.24));
		}
		
		[Test()]
		public void TestRoundHalfRoundsDownOnLowMidpoint()
		{
			Assert.AreEqual(3, IBBMath.FloorToHalf(3.25));
		}

		[Test()]
		public void TestRoundHalfRoundsDownJustAboveLowMidpoint()
		{
			Assert.AreEqual(3, IBBMath.FloorToHalf(3.26));
		}
		
		[Test()]
		public void TestRoundHalfRoundsDownAboveLowMidpoint()
		{
			Assert.AreEqual(3, IBBMath.FloorToHalf(3.3));
		}
		
		[Test()]
		public void TestRoundHalfRoundsDownBelowHighMidpoint()
		{
			Assert.AreEqual(3.5, IBBMath.FloorToHalf(3.7));
		}
		
		[Test()]
		public void TestRoundHalfRoundsDownJustBelowHighMidpoint()
		{
			Assert.AreEqual(3.5, IBBMath.FloorToHalf(3.74));
		}
		
		[Test()]
		public void TestRoundHalfRoundsDownOnHighMidpoint()
		{
			Assert.AreEqual(3.5, IBBMath.FloorToHalf(3.75));
		}
		
		[Test()]
		public void TestRoundHalfRoundsDownJustAboveHighMidpoint()
		{
			Assert.AreEqual(3.5, IBBMath.FloorToHalf(3.76));
		}
		
		[Test()]
		public void TestRoundHalfRoundsDownAboveHighMidpoint()
		{
			Assert.AreEqual(3.5, IBBMath.FloorToHalf(3.8));
		}
		
		[Test()]
		public void TestRoundHalfDownDoesntAlterHalf()
		{
			Assert.AreEqual(3.5, IBBMath.FloorToHalf(3.5));
		}
		
		[Test()]
		public void TestRoundHalfDownDoesntRoundEvenNumber()
		{
			Assert.AreEqual(4, IBBMath.FloorToHalf(4));
		}
		
		[Test()]
		public void TestRoundHalfDownDoesntRoundOddNumber()
		{
			Assert.AreEqual(3, IBBMath.FloorToHalf(3));
		}

		[Test()]
		public void TestPercentageCalculations()
		{
			Assert.AreEqual(50, IBBMath.Percentage(1,2));
			Assert.AreEqual(50, IBBMath.Percentage(2,4));
			Assert.AreEqual(25, IBBMath.Percentage(1,4));
			Assert.AreEqual(10, IBBMath.Percentage(1,10));
			Assert.AreEqual(1, IBBMath.Percentage(1,100));
			Assert.AreEqual(0.1, IBBMath.Percentage(1,1000));
			Assert.AreEqual(200, IBBMath.Percentage(2,1));
			Assert.AreEqual(-50, IBBMath.Percentage(-1,2));
			Assert.AreEqual(-50, IBBMath.Percentage(-2,4));
			Assert.AreEqual(-50, IBBMath.Percentage(1,-2));
			Assert.AreEqual(-50, IBBMath.Percentage(2,-4));
		}

		[Test()]
		public void TestPercentageCalculationsForZero()
		{
			Assert.AreEqual(0, IBBMath.Percentage(0,2));
			Assert.AreEqual(0, IBBMath.Percentage(0,4));
			Assert.AreEqual(0, IBBMath.Percentage(0,10));
			Assert.AreEqual(0, IBBMath.Percentage(0,-2));
			Assert.AreEqual(0, IBBMath.Percentage(0,-4));
			Assert.AreEqual(0, IBBMath.Percentage(0,-10));
		}

		[Test()]
		public void TestPercentageCalculationsWithZeroAsDivisor()
		{
			Assert.AreEqual(double.PositiveInfinity, IBBMath.Percentage(1,0));
			Assert.AreEqual(double.PositiveInfinity, IBBMath.Percentage(2,0));
			Assert.AreEqual(double.NegativeInfinity, IBBMath.Percentage(-1,0));
			Assert.AreEqual(double.NegativeInfinity, IBBMath.Percentage(-2,0));
		}

		[Test()]
		public void TestPercentageCalculationsWithZeroAsNumberAndDivisor()
		{
			Assert.AreEqual(double.NaN, IBBMath.Percentage(0,0));
		}
	}
}