Skip to content

Commit

Permalink
unit test for PollardRhoBrent
Browse files Browse the repository at this point in the history
  • Loading branch information
TilmanNeumann committed Dec 29, 2024
1 parent e287a8d commit 55eb8a8
Showing 1 changed file with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.
* Copyright (C) 2018-2024 Tilman Neumann - tilman.neumann@web.de
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program;
* if not, see <http://www.gnu.org/licenses/>.
*/
package de.tilman_neumann.jml.factor.pollardRho;

import java.math.BigInteger;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.BeforeClass;
import org.junit.Test;

import de.tilman_neumann.jml.factor.base.FactorArguments;
import de.tilman_neumann.jml.factor.base.FactorResult;
import de.tilman_neumann.util.ConfigUtil;
import de.tilman_neumann.util.SortedMultiset;
import de.tilman_neumann.util.SortedMultiset_BottomUp;

import static org.junit.Assert.assertEquals;

public class PollardRhoBrentTest {
private static final Logger LOG = LogManager.getLogger(PollardRhoBrentTest.class);

private static PollardRhoBrent pollardRho;

@BeforeClass
public static void setup() {
ConfigUtil.initProject();
pollardRho = new PollardRhoBrent();
}

@Test
public void testSomeInputs() {
assertFactorizationSuccess("5679148659138759837165981543", "3^3 * 466932157 * 450469808245315337");
assertFactorizationSuccess("18446744073709551617", "274177 * 67280421310721"); // F6
assertFactorizationSuccess("8225267468394993133669189614204532935183709603155231863020477010700542265332938919716662623",
"1234567891 * 1234567907 * 1234567913 * 1234567927 * 1234567949 * 1234567967 * 1234567981 * 1234568021 * 1234568029 * 1234568047");
}

private void assertFactorizationSuccess(String oddNStr, String expectedPrimeFactorizationStr) {
long t0, t1;
t0 = System.currentTimeMillis();
BigInteger N = new BigInteger(oddNStr);
SortedMultiset<BigInteger> factorResult = pollardRho.factor(N);
assertEquals(expectedPrimeFactorizationStr, factorResult.toString("*", "^"));
t1 = System.currentTimeMillis();
LOG.info("Factoring " + oddNStr + " took " + (t1-t0) + "ms");
}
}

0 comments on commit 55eb8a8

Please sign in to comment.