|
| 1 | +package com.thealgorithms.maths; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.params.ParameterizedTest; |
| 10 | +import org.junit.jupiter.params.provider.MethodSource; |
| 11 | + |
| 12 | +/** |
| 13 | + * Unit tests for the {@link SolovayStrassenPrimalityTest} class. |
| 14 | + * This class tests the functionality of the Solovay-Strassen primality test implementation. |
| 15 | + */ |
| 16 | +class SolovayStrassenPrimalityTestTest { |
| 17 | + |
| 18 | + private static final int RANDOM_SEED = 123; // Seed for reproducibility |
| 19 | + private SolovayStrassenPrimalityTest testInstance; |
| 20 | + |
| 21 | + /** |
| 22 | + * Sets up a new instance of {@link SolovayStrassenPrimalityTest} |
| 23 | + * before each test case, using a fixed random seed for consistency. |
| 24 | + */ |
| 25 | + @BeforeEach |
| 26 | + void setUp() { |
| 27 | + testInstance = SolovayStrassenPrimalityTest.getSolovayStrassenPrimalityTest(RANDOM_SEED); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Provides test cases for prime numbers with various values of n and k (iterations). |
| 32 | + * |
| 33 | + * @return an array of objects containing pairs of n and k values |
| 34 | + */ |
| 35 | + static Object[][] primeNumbers() { |
| 36 | + return new Object[][] {{2, 1}, {3, 1}, {5, 5}, {7, 10}, {11, 20}, {13, 10}, {17, 5}, {19, 1}}; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Tests known prime numbers with various values of n and k (iterations). |
| 41 | + * |
| 42 | + * @param n the number to be tested for primality |
| 43 | + * @param k the number of iterations to use in the primality test |
| 44 | + */ |
| 45 | + @ParameterizedTest |
| 46 | + @MethodSource("primeNumbers") |
| 47 | + void testPrimeNumbersWithDifferentNAndK(int n, int k) { |
| 48 | + assertTrue(testInstance.solovayStrassen(n, k), n + " should be prime"); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Provides test cases for composite numbers with various values of n and k (iterations). |
| 53 | + * |
| 54 | + * @return an array of objects containing pairs of n and k values |
| 55 | + */ |
| 56 | + static Object[][] compositeNumbers() { |
| 57 | + return new Object[][] {{4, 1}, {6, 5}, {8, 10}, {9, 20}, {10, 1}, {12, 5}, {15, 10}}; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Tests known composite numbers with various values of n and k (iterations). |
| 62 | + * |
| 63 | + * @param n the number to be tested for primality |
| 64 | + * @param k the number of iterations to use in the primality test |
| 65 | + */ |
| 66 | + @ParameterizedTest |
| 67 | + @MethodSource("compositeNumbers") |
| 68 | + void testCompositeNumbersWithDifferentNAndK(int n, int k) { |
| 69 | + assertFalse(testInstance.solovayStrassen(n, k), n + " should be composite"); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Tests edge cases for the primality test. |
| 74 | + * This includes negative numbers and small integers (0 and 1). |
| 75 | + */ |
| 76 | + @Test |
| 77 | + void testEdgeCases() { |
| 78 | + assertFalse(testInstance.solovayStrassen(-1, 10), "-1 should not be prime"); |
| 79 | + assertFalse(testInstance.solovayStrassen(0, 10), "0 should not be prime"); |
| 80 | + assertFalse(testInstance.solovayStrassen(1, 10), "1 should not be prime"); |
| 81 | + |
| 82 | + // Test small primes and composites |
| 83 | + assertTrue(testInstance.solovayStrassen(2, 1), "2 is a prime number (single iteration)"); |
| 84 | + assertFalse(testInstance.solovayStrassen(9, 1), "9 is a composite number (single iteration)"); |
| 85 | + |
| 86 | + // Test larger primes and composites |
| 87 | + long largePrime = 104729; // Known large prime number |
| 88 | + long largeComposite = 104730; // Composite number (even) |
| 89 | + |
| 90 | + assertTrue(testInstance.solovayStrassen(largePrime, 20), "104729 is a prime number"); |
| 91 | + assertFalse(testInstance.solovayStrassen(largeComposite, 20), "104730 is a composite number"); |
| 92 | + |
| 93 | + // Test very large numbers (may take longer) |
| 94 | + long veryLargePrime = 512927357; // Known very large prime number |
| 95 | + long veryLargeComposite = 512927358; // Composite number (even) |
| 96 | + |
| 97 | + assertTrue(testInstance.solovayStrassen(veryLargePrime, 20), Long.MAX_VALUE - 1 + " is likely a prime number."); |
| 98 | + |
| 99 | + assertFalse(testInstance.solovayStrassen(veryLargeComposite, 20), Long.MAX_VALUE + " is a composite number."); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Tests the Jacobi symbol calculation directly for known values. |
| 104 | + * This verifies that the Jacobi symbol method behaves as expected. |
| 105 | + */ |
| 106 | + @Test |
| 107 | + void testJacobiSymbolCalculation() { |
| 108 | + // Jacobi symbol (a/n) where n is odd and positive |
| 109 | + int jacobi1 = testInstance.calculateJacobi(6, 11); // Should return -1 |
| 110 | + int jacobi2 = testInstance.calculateJacobi(5, 11); // Should return +1 |
| 111 | + |
| 112 | + assertEquals(-1, jacobi1); |
| 113 | + assertEquals(+1, jacobi2); |
| 114 | + |
| 115 | + // Edge case: Jacobi symbol with even n or non-positive n |
| 116 | + int jacobi4 = testInstance.calculateJacobi(5, -11); // Should return 0 (invalid) |
| 117 | + int jacobi5 = testInstance.calculateJacobi(5, 0); // Should return 0 (invalid) |
| 118 | + |
| 119 | + assertEquals(0, jacobi4); |
| 120 | + assertEquals(0, jacobi5); |
| 121 | + } |
| 122 | +} |
0 commit comments