|
| 1 | +package com.thealgorithms.maths; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | +import static org.mockito.ArgumentMatchers.any; |
| 7 | + |
| 8 | +import java.math.BigInteger; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.mockito.MockedStatic; |
| 11 | +import org.mockito.Mockito; |
| 12 | + |
| 13 | +class EulerPseudoprimeTest { |
| 14 | + |
| 15 | + @Test |
| 16 | + void testPrimeNumbers() { |
| 17 | + assertTrue(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(7), 5)); |
| 18 | + assertTrue(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(13), 5)); |
| 19 | + assertTrue(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(101), 5)); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + void testCompositeNumbers() { |
| 24 | + assertFalse(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(9), 5)); |
| 25 | + assertFalse(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(21), 5)); |
| 26 | + assertFalse(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(221), 5)); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + void testEvenNumbers() { |
| 31 | + assertFalse(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(4), 5)); |
| 32 | + assertFalse(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(100), 5)); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + void testEdgeCases() { |
| 37 | + assertFalse(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(0), 5)); |
| 38 | + assertFalse(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(1), 5)); |
| 39 | + assertTrue(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(2), 5)); |
| 40 | + assertTrue(EulerPseudoprime.isProbablePrime(BigInteger.valueOf(3), 5)); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testIsProbablePrimeWhenJacobiSymbolIsZero() { |
| 45 | + try (MockedStatic<EulerPseudoprime> mockedPrimality = Mockito.mockStatic(EulerPseudoprime.class, Mockito.CALLS_REAL_METHODS)) { |
| 46 | + |
| 47 | + // Mock jacobiSymbol to return 0 to test the branch |
| 48 | + mockedPrimality.when(() -> EulerPseudoprime.jacobiSymbol(any(BigInteger.class), any(BigInteger.class))).thenReturn(0); |
| 49 | + |
| 50 | + boolean result = EulerPseudoprime.isProbablePrime(BigInteger.valueOf(15), 1); |
| 51 | + |
| 52 | + assertFalse(result); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void testJacobiSymbolThrowsForEvenOrNonPositiveN() throws Exception { |
| 58 | + var method = EulerPseudoprime.class.getDeclaredMethod("jacobiSymbol", BigInteger.class, BigInteger.class); |
| 59 | + |
| 60 | + // Helper lambda to unwrap InvocationTargetException |
| 61 | + Runnable invokeJacobi = () -> { |
| 62 | + try { |
| 63 | + method.invoke(null, BigInteger.valueOf(2), BigInteger.valueOf(8)); |
| 64 | + } catch (Exception e) { |
| 65 | + // unwrap |
| 66 | + Throwable cause = e.getCause(); |
| 67 | + if (cause instanceof IllegalArgumentException) { |
| 68 | + throw (IllegalArgumentException) cause; |
| 69 | + } else { |
| 70 | + throw new RuntimeException(e); |
| 71 | + } |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + // Now check that it actually throws |
| 76 | + assertThrows(IllegalArgumentException.class, invokeJacobi::run); |
| 77 | + |
| 78 | + // Another case: non-positive n |
| 79 | + Runnable invokeJacobi2 = () -> { |
| 80 | + try { |
| 81 | + method.invoke(null, BigInteger.valueOf(5), BigInteger.valueOf(-3)); |
| 82 | + } catch (Exception e) { |
| 83 | + Throwable cause = e.getCause(); |
| 84 | + if (cause instanceof IllegalArgumentException) { |
| 85 | + throw (IllegalArgumentException) cause; |
| 86 | + } else { |
| 87 | + throw new RuntimeException(e); |
| 88 | + } |
| 89 | + } |
| 90 | + }; |
| 91 | + assertThrows(IllegalArgumentException.class, invokeJacobi2::run); |
| 92 | + } |
| 93 | +} |
0 commit comments