Added some tests and fixed a bug
This commit is contained in:
@@ -15,7 +15,7 @@ import java.util.TimerTask;
|
||||
*/
|
||||
public abstract class MainProcess {
|
||||
|
||||
private final static String VERSION = "v1.2";
|
||||
private final static String VERSION = "v1.3";
|
||||
|
||||
private final static SimpleDateFormat DATE_TO_NUM = new SimpleDateFormat("yyyyMMddHHmm");
|
||||
|
||||
|
||||
@@ -86,8 +86,10 @@ public abstract class PrimeCalculator {
|
||||
long end = start + BLOCK_MAX;
|
||||
long endSqrt = (long) Math.sqrt(end) + 1;
|
||||
|
||||
System.out.println(number);
|
||||
|
||||
byte block = 0;
|
||||
int p = 3;
|
||||
int p = 2;
|
||||
while (p < endSqrt) {
|
||||
p = nextPrime(p);
|
||||
|
||||
@@ -102,6 +104,9 @@ public abstract class PrimeCalculator {
|
||||
}
|
||||
byte mask = (byte) (1 << ((number >> 1) & 7));
|
||||
|
||||
System.out.println(byteToBinary(block));
|
||||
System.out.println(byteToBinary(mask));
|
||||
|
||||
return !((block & mask) != 0);
|
||||
}
|
||||
|
||||
@@ -125,5 +130,9 @@ public abstract class PrimeCalculator {
|
||||
return !getBit(number);
|
||||
return isPrimeByBlock(number);
|
||||
}
|
||||
|
||||
public static String byteToBinary(byte b) {
|
||||
return String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package fr.klemek.primedate;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class PrimeCalculatorTests {
|
||||
|
||||
public void testNumber(boolean expected, long number) throws Exception {
|
||||
if(expected)
|
||||
assertTrue(number+" should be prime", PrimeCalculator.isPrime(number));
|
||||
else
|
||||
assertFalse(number+" should not be prime", PrimeCalculator.isPrime(number));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsPrimeSmall() throws Exception {
|
||||
testNumber(true, 1);
|
||||
testNumber(true, 2);
|
||||
testNumber(true, 3);
|
||||
testNumber(false, 4);
|
||||
testNumber(true, 5);
|
||||
testNumber(false, 6);
|
||||
testNumber(true, 7);
|
||||
testNumber(false, 9);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsPrimeNormal() throws Exception {
|
||||
testNumber(true, 8011);
|
||||
testNumber(true, 8941);
|
||||
testNumber(false, 8943);
|
||||
testNumber(true, 9283);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsPrimeBig() throws Exception {
|
||||
testNumber(true, 201802181381L);
|
||||
testNumber(false, 201802181383L);
|
||||
testNumber(false, 201802181307L);
|
||||
testNumber(false, 201802181409L);
|
||||
testNumber(true, 201802181411L);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user