An important aspect of test-driven development: What’s my motivation?

Image for post
Image for post
Photo by Marc Kleen on Unsplash
    public static int gcd(int a, int b) {
return -1;
}
    @Test
public void testGCDOnSameNumber() {
int expected = 10;
int actual = gcd(expected, expected);
assertEquals(expected, actual);
}
    public static int gcd(int a, int b) {
return a;
}
    @Test
public void testGCDOnSameNegativeNumber() {
int expected = 10;
int actual = gcd(-expected, -expected);
assertEquals(expected, actual);
}
    public static int gcd(int a, int b) {
return Math.abs(a);
}
    @Test
public void testGCD10501125() {
int expected = 75;
int actual = gcd(1050, 1125);
assertEquals(expected, actual);
}
    public static int gcd(int a, int b) {
if (a == b) {
return Math.abs(a);
} else {
return Math.abs(a - b);
}
}
    @Test
public void testGCDOnSortOfRandomNumbers() {
int psRanNum = (int) Math.floor(Math.random() * 200 + 1);
psRanNum = 6 * psRanNum + 1; // Ensure it's odd and not
// divisible by 3
int currPowRan = psRanNum;
int threshold = Integer.MAX_VALUE / 16;
int expResult, result, holderA, holderB;
int curr3Pow = 3;
while ((currPowRan > 0) && (currPowRan < threshold)) {
expResult = 1;
result = gcd(currPowRan, currPowRan + 1);
assertEquals(expResult, result);
expResult = psRanNum;
result = gcd(currPowRan, psRanNum);
assertEquals(expResult, result);
expResult *= 2;
holderA = 2 * currPowRan;
holderB = 2 * curr3Pow * psRanNum;
result = gcd(holderA, holderB);
assertEquals(expResult, result);
currPowRan *= psRanNum;
curr3Pow *= 3;
}
}
scala> (1 to 1050).filter(1050 % _ == 0)
res0: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 5, 6, 7, 10, 14, 15, 21, 25, 30, 35, 42, 50, 70, 75, 105, 150, 175, 210, 350, 525, 1050)
scala> (1 to 1125).filter(1125 % _ == 0)
res1: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 3, 5, 9, 15, 25, 45, 75, 125, 225, 375, 1125)
scala> res0.intersect(res1)
res2: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 3, 5, 15, 25, 75)
  def gcd(a: Int, b: Int, eucFn: Int => Int = Math.abs _): Int = {
// Euclidean algorithm goes here
currB // Or currA? Don't need "return" either way
}
    @Test(expected = ArithmeticException.class)
public void testGCDIntMinVal() {
gcd(Integer.MIN_VALUE, Integer.MIN_VALUE);
}

is a composer and photographer from Detroit, Michigan. He has been working on a Java program to display certain mathematical diagrams.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store