Skip to content

Instantly share code, notes, and snippets.

@isidore
Created September 23, 2015 06:54
Show Gist options
  • Save isidore/5d8f9381e986f78834f9 to your computer and use it in GitHub Desktop.
Save isidore/5d8f9381e986f78834f9 to your computer and use it in GitHub Desktop.
package com.spun.triangles.test;
import static org.junit.Assert.assertEquals;
import java.util.Random;
import org.junit.Test;
/**
* Theory based kata
* Level easy: do it
* Level expert: do not use Math or StrictMath
* @author llewellyn
*
*/
public class RightTrianglesTest
{
@Test
public void testProblemSpaceBelow1000()
{
for (int a = 1; a < 1000; a++)
{
for (int b = a; b < 1000; b++)
{
double hypotenuse = RightTriangle.getHypotenuseFor(a, b);
assertHypotenuse(a, b, hypotenuse);
}
}
}
@Test
public void testProblemSpace()
{
Random r = new Random();
for (int i = 1; i < 100000; i++)
{
int a = r.nextInt(Integer.MAX_VALUE);
int b = r.nextInt(Integer.MAX_VALUE);
double hypotenuse = RightTriangle.getHypotenuseFor(a, b);
assertHypotenuse(a, b, hypotenuse);
}
}
private void assertHypotenuse(double a, double b, double hypotenuse)
{
double aa = a * a;
double bb = b * b;
double cc = hypotenuse * hypotenuse;
String message = String.format(
"a^2 + b^2 != c^2\n" + "%s^2 + %s^2 != %s^2\n" + "%s + %s != %s\n" + "%s != %s", a, b, hypotenuse, aa, bb,
cc, (aa + bb), cc);
double delta = hypotenuse * 0.0001;
assertEquals(message, aa + bb, cc, delta);
}
}
class RightTriangle
{
public static double getHypotenuseFor(double a, double b)
{
return 0;
}
}
@JayBazuzi
Copy link

Make line 60 throw a Not Implemented exception, so it's obvious where we should be coding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment