|
| 1 | +package com.thealgorithms.others; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import java.math.BigInteger; |
| 5 | +import static org.junit.jupiter.api.Assertions.*; |
| 6 | + |
| 7 | +public class ShorAlgorithmTest { |
| 8 | + |
| 9 | + @Test |
| 10 | + public void testFactorizationOfEvenNumber() { |
| 11 | + ShorAlgorithm shor = new ShorAlgorithm(); |
| 12 | + BigInteger number = new BigInteger("15"); |
| 13 | + BigInteger[] factors = shor.shorAlgorithm(number); |
| 14 | + |
| 15 | + assertNotNull(factors, "Factors should not be null for composite numbers."); |
| 16 | + assertEquals(2, factors.length, "There should be two factors."); |
| 17 | + |
| 18 | + BigInteger p = factors[0]; |
| 19 | + BigInteger q = factors[1]; |
| 20 | + |
| 21 | + assertEquals(number, p.multiply(q), "Factors should multiply to the original number."); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void testFactorizationOfPrimeNumber() { |
| 26 | + ShorAlgorithm shor = new ShorAlgorithm(); |
| 27 | + BigInteger number = new BigInteger("13"); |
| 28 | + BigInteger[] factors = shor.shorAlgorithm(number); |
| 29 | + |
| 30 | + assertNull(factors, "Factors should be null for prime numbers."); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testFactorizationOfEvenCompositeNumber() { |
| 35 | + ShorAlgorithm shor = new ShorAlgorithm(); |
| 36 | + BigInteger number = new BigInteger("20"); |
| 37 | + BigInteger[] factors = shor.shorAlgorithm(number); |
| 38 | + |
| 39 | + assertNotNull(factors, "Factors should not be null for composite numbers."); |
| 40 | + assertEquals(2, factors.length, "There should be two factors."); |
| 41 | + |
| 42 | + BigInteger p = factors[0]; |
| 43 | + BigInteger q = factors[1]; |
| 44 | + |
| 45 | + assertEquals(number, p.multiply(q), "Factors should multiply to the original number."); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testLargeCompositeNumber() { |
| 50 | + ShorAlgorithm shor = new ShorAlgorithm(); |
| 51 | + BigInteger number = new BigInteger("21"); |
| 52 | + BigInteger[] factors = shor.shorAlgorithm(number); |
| 53 | + |
| 54 | + assertNotNull(factors, "Factors should not be null for composite numbers."); |
| 55 | + assertEquals(2, factors.length, "There should be two factors."); |
| 56 | + |
| 57 | + BigInteger p = factors[0]; |
| 58 | + BigInteger q = factors[1]; |
| 59 | + |
| 60 | + assertEquals(number, p.multiply(q), "Factors should multiply to the original number."); |
| 61 | + } |
| 62 | +} |
| 63 | + |
0 commit comments