|
| 1 | +/* |
| 2 | + * java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project. |
| 3 | + * Copyright (C) 2019-2024 Tilman Neumann - [email protected] |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License |
| 6 | + * as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 9 | + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 10 | + * |
| 11 | + * You should have received a copy of the GNU General Public License along with this program; |
| 12 | + * if not, see <http://www.gnu.org/licenses/>. |
| 13 | + */ |
| 14 | +package de.tilman_neumann.jml.primes.exact; |
| 15 | + |
| 16 | +import org.junit.BeforeClass; |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | + |
| 21 | +import de.tilman_neumann.jml.primes.bounds.NthPrimeUpperBounds; |
| 22 | +import de.tilman_neumann.util.ConfigUtil; |
| 23 | + |
| 24 | +/** |
| 25 | + * Test performance and correctness of results of prime sieves. |
| 26 | + * @author Tilman Neumann |
| 27 | + */ |
| 28 | +public class SieveTest { |
| 29 | + private static final int NCOUNT = 10000000; // 100m is feasible, but array-storing algorithms will fail soon above that |
| 30 | + |
| 31 | + private static long nthPrimeUpperBound; |
| 32 | + private static int correctCount; |
| 33 | + private static int[] correctResult; |
| 34 | + |
| 35 | + @BeforeClass |
| 36 | + public static void setup() { |
| 37 | + ConfigUtil.initProject(); |
| 38 | + // get correct data |
| 39 | + CollectingCallback correctCallback = new CollectingCallback(NCOUNT); |
| 40 | + SimpleSieve correctSieve = new SimpleSieve(correctCallback); |
| 41 | + nthPrimeUpperBound = NthPrimeUpperBounds.combinedUpperBound(NCOUNT); |
| 42 | + correctSieve.sieve(nthPrimeUpperBound); |
| 43 | + correctCount = correctCallback.count; |
| 44 | + correctResult = correctCallback.array; |
| 45 | + assertEquals(NCOUNT, correctCount); |
| 46 | + assertEquals(2, correctResult[0]); |
| 47 | + assertEquals(3, correctResult[1]); |
| 48 | + assertEquals(5, correctResult[2]); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testSegmentedSieve() { |
| 53 | + CollectingCallback segmentedCallback = new CollectingCallback(NCOUNT); |
| 54 | + SegmentedSieve segmentedSieve = new SegmentedSieve(segmentedCallback); |
| 55 | + segmentedSieve.sieve(nthPrimeUpperBound); |
| 56 | + int[] segmentedResult = segmentedCallback.array; |
| 57 | + assertEquals(NCOUNT, segmentedCallback.count); |
| 58 | + for (int i=0; i<NCOUNT; i++) { |
| 59 | + assertEquals(correctResult[i], segmentedResult[i]); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testAutoExpandingPrimesArray() { |
| 65 | + AutoExpandingPrimesArray primesArray = AutoExpandingPrimesArray.get().ensurePrimeCount(NCOUNT); |
| 66 | + for (int i=0; i<NCOUNT; i++) { |
| 67 | + assertEquals(correctResult[i], primesArray.getPrime(i)); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments