import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; import java.util.Random; public class SolutionTest { private static final Random RANDOM = new Random(); @Test public void twoShouldBeAPrime() { testPrime(2, true); } @Test public void fourShouldNotBeAPrime() { testPrime(4, false); } @Test public void anyNumberGreaterThanFiveAndEndingWithFiveShouldBeAPrime(){ testPrime(15, false); } @Test public void aSquareShouldNotAPrime() { for (int i = 0; i < 10000; ++i) { int randomNumber = RANDOM.nextInt(Short.MAX_VALUE) + 2; int numberToCheck = randomNumber * randomNumber; testPrime(numberToCheck, false); } } @Test public void aProductOfTwoIntegersShouldNotBeAPrime() { for (int i = 0; i < 100000; ++i) { int a = RANDOM.nextInt(Short.MAX_VALUE) + 2; int b = RANDOM.nextInt(Short.MAX_VALUE) + 2; int numberToCheck = a * b; testPrime(numberToCheck, false); } } private static void testPrime(int numberToCheck, boolean expected) { boolean actual = Primes.isAPrime(numberToCheck); assertEquals(Integer.toString(numberToCheck), expected, actual); } }
- import org.junit.Test;
- import static org.junit.Assert.assertEquals;
- import org.junit.runners.JUnit4;
- import java.util.Random;
- public class SolutionTest {
- private static final Random RANDOM = new Random();
- @Test
- public void twoShouldBeAPrime() {
- testPrime(2, true);
- }
- @Test
- public void fourShouldNotBeAPrime() {
- testPrime(4, false);
- }
- @Test
- public void anyNumberGreaterThanFiveAndEndingWithFiveShouldBeAPrime(){
- testPrime(15, false);
- }
- @Test
- public void aSquareShouldNotAPrime() {
- for (int i = 0; i < 10000; ++i) {
- int randomNumber = RANDOM.nextInt(Short.MAX_VALUE) + 2;
- int numberToCheck = randomNumber * randomNumber;
- testPrime(numberToCheck, false);
- }
- }
- @Test
- public void aProductOfTwoIntegersShouldNotBeAPrime() {
- for (int i = 0; i < 100000; ++i) {
- int a = RANDOM.nextInt(Short.MAX_VALUE) + 2;
- int b = RANDOM.nextInt(Short.MAX_VALUE) + 2;
- int numberToCheck = a * b;
- testPrime(numberToCheck, false);
- }
- }
- private static void testPrime(int numberToCheck, boolean expected) {
- boolean actual = Primes.isAPrime(numberToCheck);
- assertEquals(Integer.toString(numberToCheck), expected, actual);
- }
- }