View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.numbers.primes;
18  
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.junit.jupiter.api.Assertions;
25  import org.junit.jupiter.api.Test;
26  
27  class SmallPrimesTest {
28  
29      // Primes larger than the small PRIMES array in SmallPrimes
30      private static final int[] LARGE_PRIME = {3673, 3677};
31  
32      @Test
33      void smallTrialDivision_smallComposite() {
34          final List<Integer> factors = new ArrayList<>();
35          final int result = SmallPrimes.smallTrialDivision(3 * 7 * 23, factors);
36          Assertions.assertEquals(1, result);
37          Assertions.assertEquals(Arrays.asList(3, 7, 23), factors);
38      }
39  
40      @Test
41      void smallTrialDivision_repeatedFactors() {
42          final List<Integer> factors = new ArrayList<>();
43          final int result = SmallPrimes.smallTrialDivision(2 * 2 * 3 * 3 * 3, factors);
44          Assertions.assertEquals(1, result);
45          Assertions.assertEquals(Arrays.asList(2, 2, 3, 3, 3), factors);
46      }
47  
48      @Test
49      void smallTrialDivision_oneFactor() {
50          final List<Integer> factors = new ArrayList<>();
51          final int result = SmallPrimes.smallTrialDivision(59, factors);
52          Assertions.assertEquals(1, result);
53          Assertions.assertEquals(Collections.singletonList(59), factors);
54      }
55  
56      @Test
57      void smallTrialDivision_BoundaryPrimes() {
58          final List<Integer> factors = new ArrayList<>();
59          final int penultimatePrime = SmallPrimes.PRIMES[SmallPrimes.PRIMES.length - 2];
60          final int result = SmallPrimes.smallTrialDivision(penultimatePrime * SmallPrimes.PRIMES_LAST, factors);
61          Assertions.assertEquals(1, result);
62          Assertions.assertEquals(Arrays.asList(penultimatePrime, SmallPrimes.PRIMES_LAST), factors);
63      }
64  
65      @Test
66      void smallTrialDivision_largeComposite() {
67          final List<Integer> factors = new ArrayList<>();
68          final int result = SmallPrimes.smallTrialDivision(2 * 5 * LARGE_PRIME[0], factors);
69          Assertions.assertEquals(LARGE_PRIME[0], result);
70          Assertions.assertEquals(Arrays.asList(2, 5), factors);
71      }
72  
73      @Test
74      void smallTrialDivision_noSmallPrimeFactors() {
75          final List<Integer> factors = new ArrayList<>();
76          final int result = SmallPrimes.smallTrialDivision(LARGE_PRIME[0] * LARGE_PRIME[1], factors);
77          Assertions.assertEquals(LARGE_PRIME[0] * LARGE_PRIME[1], result);
78          Assertions.assertEquals(Collections.<Integer>emptyList(), factors);
79      }
80  
81      @Test
82      void boundedTrialDivision_twoDifferentFactors() {
83          final List<Integer> factors = new ArrayList<>();
84          SmallPrimes.boundedTrialDivision(LARGE_PRIME[0] * LARGE_PRIME[1], Integer.MAX_VALUE, factors);
85          Assertions.assertEquals(Arrays.asList(LARGE_PRIME[0], LARGE_PRIME[1]), factors);
86      }
87  
88      @Test
89      void boundedTrialDivision_square() {
90          final List<Integer> factors = new ArrayList<>();
91          SmallPrimes.boundedTrialDivision(LARGE_PRIME[0] * LARGE_PRIME[0], Integer.MAX_VALUE, factors);
92          Assertions.assertEquals(Arrays.asList(LARGE_PRIME[0], LARGE_PRIME[0]), factors);
93      }
94  
95      @Test
96      void trialDivision_smallComposite() {
97          final List<Integer> factors = SmallPrimes.trialDivision(5 * 11 * 29 * 103);
98          Assertions.assertEquals(Arrays.asList(5, 11, 29, 103), factors);
99      }
100 
101     @Test
102     void trialDivision_repeatedFactors() {
103         final List<Integer> factors = SmallPrimes.trialDivision(2 * 2 * 2 * 2 * 5 * 5);
104         Assertions.assertEquals(Arrays.asList(2, 2, 2, 2, 5, 5), factors);
105     }
106 
107     @Test
108     void trialDivision_oneSmallFactor() {
109         final List<Integer> factors = SmallPrimes.trialDivision(101);
110         Assertions.assertEquals(Collections.singletonList(101), factors);
111     }
112 
113     @Test
114     void trialDivision_largeComposite() {
115         final List<Integer> factors = SmallPrimes.trialDivision(2 * 3 * LARGE_PRIME[0]);
116         Assertions.assertEquals(Arrays.asList(2, 3, LARGE_PRIME[0]), factors);
117     }
118 
119     @Test
120     void trialDivision_veryLargeComposite() {
121         final List<Integer> factors = SmallPrimes.trialDivision(2 * LARGE_PRIME[0] * LARGE_PRIME[1]);
122         Assertions.assertEquals(Arrays.asList(2, LARGE_PRIME[0], LARGE_PRIME[1]), factors);
123     }
124 
125     @Test
126     void millerRabinPrimeTest_primes() {
127         for (final int n : PrimesTest.PRIMES) {
128             if (n % 2 == 1) {
129                 Assertions.assertTrue(SmallPrimes.millerRabinPrimeTest(n));
130             }
131         }
132     }
133 
134     @Test
135     void millerRabinPrimeTest_composites() {
136         for (final int n : PrimesTest.NOT_PRIMES) {
137             if (n % 2 == 1) {
138                 Assertions.assertFalse(SmallPrimes.millerRabinPrimeTest(n));
139             }
140         }
141     }
142 }