TDD is a waste of time… until you use it on a project that matters to you

Image for post
Image for post
Photo by Aziz Acharki on Unsplash
    // STUB TO FAIL THE FIRST TEST
public static int nthPrime(int index) {
return -1024;
}
    @Test
public void testNthPrimeFirstPrimeIs2() {
int expected = 2;
int actual = nthPrime(0);
assertEquals(expected, actual);
}
    public static int nthPrime(int index) {
return 2;
}
    @Test
public void testNthPrimeSecondPrimeIs3() {
int expected = 3;
int actual = nthPrime(1);
assertEquals(expected, actual);
}
    public static int nthPrime(int index) {
return index + 2;
}
    @Test(timeout = 1000)
public void testNthPrimeLargest32BitPrime() {
int actual = nthPrime(105097564);
assertEquals(Integer.MAX_VALUE, actual);
}

@Test(expected = ArithmeticException.class)
public void testNthPrimeBeyondRange() {
int index = 105097565;
int actual = nthPrime(index);
System.out.println(index + "th prime is said to be "
+ actual);
}
    @Test(expected = ArithmeticException.class)
public void testNthPrimeBeyondRange() {
int index = 105097565
+ (int) Math.floor(Math.random() * 300000000);
System.out.println("Prime at " + index + "?");
int actual = EratosthenesSieve.nthPrime(index);
System.out.println(index + "th prime is said to be "
+ actual);
}
    @Test
public void testNthPrimeCluster() {
int actual;
for (int i = 1129; i < PRIME_PI_10000; i++) {
actual = nthPrime(i);
assertEquals(PRIMES_TO_10000.get(i).intValue(), actual);
}
}
    public static int nthPrime(int index) {
List<Integer> primes = new ArrayList<>();
primes.add(2);
int currIndex = 0;
int currTestIndex, currDiv;
int currNum = 1;
int currLen = 1;
boolean divNotFoundYet;
while (currIndex < index) {
divNotFoundYet = true;
currNum += 2;
currTestIndex = 0;
while (divNotFoundYet && currTestIndex < currLen) {
currDiv = primes.get(currTestIndex);
divNotFoundYet = (currNum % currDiv != 0);
currTestIndex++;
}
if (divNotFoundYet) {
primes.add(currNum);
currLen++;
currIndex++;
}
}
return currNum;
}
    @Test
public void testNthPrimeZeroethPrimeIs1() {
int expected = 1;
int actual = nthPrime(0);
assertEquals(expected, actual);
}
    @Test
public void testNthPrimeFirstPrimeIs2() {
int expected = 2;
int actual = nthPrime(1);
assertEquals(expected, actual);
}

@Test
public void testNthPrimeSecondPrimeIs3() {
int expected = 3;
int actual = nthPrime(2);
assertEquals(expected, actual);
}

@Test(timeout = 1000)
public void testNthPrimeLargest32BitPrime() {
int actual = nthPrime(105097565);
assertEquals(Integer.MAX_VALUE, actual);
}

@Test(expected = ArithmeticException.class)
public void testNthPrimeBeyondRange() {
int index = 105097566
+ (int) Math.floor(Math.random() * 300000000);
System.out.println("Prime at " + index + "?");
int actual = nthPrime(index);
System.out.println(index + "th prime is said to be "
+ actual);
}

@Test
public void testNthPrimeCluster() {
int actual;
for (int i = 1129; i < PRIME_PI_10000; i++) {
actual = nthPrime(i);
assertEquals(PRIMES_TO_10000.get(i - 1).intValue(),
actual);
}
}

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