Programming Resources
For Fun and Learning
Charles Cusack
Computer Science
Hope College
main

Python
C++

JAVA


PHP
SQL
Alice

RoundIt


RoundIt.java
TestRounding.java

TestRounding.java

package stuffFor255;

import static org.junit.Assert.*;

import org.junit.BeforeClass;
import org.junit.Test;

public class TestRounding {
	// -/-
	@Test
	public void testnZero() {
		testRunner(0,0, -133);
		testRunner(0,0, -7);
		testRunner(0,0, -4);
		testRunner(0,0, -32);
		testRunner(0,0, -1);
		testRunner(0,0, 1);
		testRunner(0,0, 4);
		testRunner(0,0, 35);
	}

	@Test
	public void testPP() {
		// +/+
		testRunner(10,41, 4);
		testRunner(2,7, 4);
		testRunner(3,5, 2);
		testRunner(6,12, 2);
		testRunner(1,6, 7);
		testRunner(4,25, 7);
		testRunner(3,105, 35);
		testRunner(1,4, 7);
		testRunner(5,41, 8);
		testRunner(6,41, 7);
		testRunner(4,41, 10);
		testRunner(5,41, 9);
		testRunner(3,41, 12);
		testRunner(3,41, 14);
		testRunner(1,4, 7);
		testRunner(1,5, 7);
		testRunner(1,6, 7);
		testRunner(1,7, 7);
		testRunner(1,8, 7);
		testRunner(1,9, 7);
		testRunner(1,10, 7);
		testRunner(2,11, 7);
	}
	@Test
	public void testPP_RoundToZero() {
		// +/+
		testRunner(0,1, 7);
		testRunner(0,2, 7);
		testRunner(0,3, 7);
	}

	// -/+
	@Test
	public void testMP() {
		testRunner(-10,-41, 4);
		testRunner(-2,-7, 4);
		testRunner(-3,-5, 2);
		testRunner(-6,-12, 2);
		testRunner(-1,-6, 7);
		testRunner(-4,-25, 7);
		testRunner(-1,-4, 7);
		testRunner(-1,-5, 7);
		testRunner(-1,-6, 7);
		testRunner(-1,-7, 7);
		testRunner(-1,-8, 7);
		testRunner(-1,-9, 7);
		testRunner(-1,-10, 7);
		testRunner(-2,-11, 7);
	}
	
	// -/+
	@Test
	public void testMP_RoundToZero() {
		testRunner(0,-1, 7);
		testRunner(0,-2, 7);
		testRunner(0,-3, 7);
	}

	// +/-
	@Test
	public void testPM() {
		testRunner(-10,41, -4);
		testRunner(-2,7, -4);
		testRunner(-3,5, -2);
		testRunner(-6,12, -2);
		testRunner(-1,6, -7);
		testRunner(-4,25, -7);
		testRunner(-1,4, -7);
		testRunner(-1,5, -7);
		testRunner(-1,6, -7);
		testRunner(-1,7, -7);
		testRunner(-1,8, -7);
		testRunner(-1,9, -7);
		testRunner(-1,10, -7);
		testRunner(-2,11, -7);
	}
	
	// +/-
	@Test
	public void testPM_RoundToZero() {
		testRunner(0,1, -7);
		testRunner(0,2, -7);
		testRunner(0,3, -7);
	}

	// -/-
	@Test
	public void testMM() {
		testRunner(10,-41, -4);
		testRunner(2,-7, -4);
		testRunner(3,-5, -2);
		testRunner(6,-12, -2);
		testRunner(1,-6, -7);
		testRunner(4,-25, -7);
		testRunner(1,-4, -7);
		testRunner(1,-5, -7);
		testRunner(1,-6, -7);
		testRunner(1,-7, -7);
		testRunner(1,-8, -7);
		testRunner(1,-9, -7);
		testRunner(1,-10, -7);
		testRunner(2,-11, -7);
	}
	// -/-
	@Test
	public void testMM_RountToZero() {
		testRunner(0,-1, -7);
		testRunner(0,-2, -7);
		testRunner(0,-3, -7);
	}
	
	// Big numbers
	@Test
	public void testBig() {
		testRunner(0,3, 700056);
		testRunner(0,73543, 8675309);
		
		testRunner(0,-3, 700056);
		testRunner(0,-73543, 8675309);
		
		testRunner(0,3, -700056);
		testRunner(0,73543, -8675309);

		testRunner(0,-3, -700056);
		testRunner(0,-73543, -8675309);
	}
	@Test
	public void testBigPP_RoundToZero() {
		testRunner(0,3, 700056);
		testRunner(0,73543, 8675309);
	}
	@Test
	public void testBigPM_RoundToZero() {		
		testRunner(0,3, -700056);
		testRunner(0,73543, -8675309);
	}	
	@Test
	public void testBigMP_RoundToZero() {
		testRunner(0,-3, 700056);
		testRunner(0,-73543, 8675309);
	}	
	@Test
	public void testBigMM_RoundToZero() {
		testRunner(0,-3, -700056);
		testRunner(0,-73543, -8675309);
	}

	// REALLY big numbers
	@Test
	public void testReallyBigPP() {		
		testRunner(2,972512543,486256271);
		testRunner(6,972512543,176820462); // should round up
		testRunner(5,972512543,176820463); // should round down
	}
	@Test
	public void testReallyBigPM() {
		testRunner(-2,972512543,-486256271);
		testRunner(-6,972512543,-176820462); // should round up
		testRunner(-5,972512543,-176820463); // should round down
	}
	@Test
	public void testReallyBigMP() {
		testRunner(-2,-972512543,486256271);
		testRunner(-6,-972512543,176820462); // should round up
		testRunner(-5,-972512543,176820463); // should round down
	}
	@Test
	public void testReallyBigMM() {
		testRunner(2,-972512543,-486256271);
		testRunner(6,-972512543,-176820462); // should round up
		testRunner(5,-972512543,-176820463); // should round down
	}
		
	@Test
	public void testReallyBigPP_RoundToZero() {
		testRunner(0,2, 1872512543);
	}
	@Test
	public void testReallyBigPM_RoundToZero() {
		testRunner(0,2, -1872512543);
	}
	@Test
	public void testReallyBigMP_RoundToZero() {
		testRunner(0,-2, 1872512543);
	}
	@Test
	public void testReallyBigMM_RoundToZero() {
		testRunner(0,-2, -1872512543);
	}
	
	public void testRunner(int expected, int n, int m) {
		RoundIt re = new RoundIt();
		int result = re.roundDivision(n,m);
		String message="Expected "+expected+" for "+n+"/"+m+" but got "+result+".";
		assertEquals(message,expected,result);
	}

}