We want to test whether our boolean
isPrime(final long candidate)
method classifies
prime numbers as such and whether this message is able to
tell non-primes as well. We achieve this by defining a
boolean array having indexes ranging from 0 to 100
(inclusive). We then:
This array then allows us to test our method for
correctness for values up to 100:
public class TestPrimes {
@Test
public void primeTest() {
final int[] primeNumbers = { 2, 3, 5, 7, 11, 13, 17, 19, 23,
31, 37, 41, 43, 47, 53, 59, 29,
61, 67, 71, 73, 79, 83, 89, 97};
final boolean[] isPrime = new boolean[101]; //Testing 2, 3, 4, .., 99, 100
for (int i = 2; i <= 100; i++) {
isPrime[i] = false;
}
for (final int prime: primeNumbers) {
isPrime[prime] = true;
}
for (int i = 2; i <= 100; i++) {
assertEquals("Index=" + i , isPrime[i], PrimeNumbers.isPrime(i));
}
}
Executing this test yields an error at index 49. This
is due to the chosen limit i * i <
candidate
in:
public static boolean isPrime(final long candidate) {
for (long i = 2; i * i < candidate; i++) {
...
This is wrong: Having candidate
== 49
the last value of i to be considered will be 6.
So the required test 49 % 7
will never be executed thus erroneously returning true. We
have to modify the loop's limit slightly by using i * i <=
candidate
instead:
public static boolean isPrime(final long candidate) {
for (long i = 2; i * i <= candidate; i++) {
...
This way 49 % 7
will be
evaluated to zero thus returning false
and thereby categorizing 49 as
a non-prime number.