|
| 1 | +/* |
| 2 | + * 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. |
| 3 | + * Copyright (C) 2018-2025 Tilman Neumann - tilman.neumann@web.de |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License |
| 6 | + * as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 9 | + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 10 | + * |
| 11 | + * You should have received a copy of the GNU General Public License along with this program; |
| 12 | + * if not, see <http://www.gnu.org/licenses/>. |
| 13 | + */ |
| 14 | +package de.tilman_neumann.jml.random; |
| 15 | + |
| 16 | +import static org.junit.Assert.assertEquals; |
| 17 | +import static org.junit.Assert.assertTrue; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | + |
| 21 | +import org.apache.logging.log4j.LogManager; |
| 22 | +import org.apache.logging.log4j.Logger; |
| 23 | +import org.junit.BeforeClass; |
| 24 | +import org.junit.Test; |
| 25 | + |
| 26 | +import de.tilman_neumann.util.ConfigUtil; |
| 27 | + |
| 28 | +public final class SplitMix64Test { |
| 29 | + private static final Logger LOG = LogManager.getLogger(SplitMix64Test.class); |
| 30 | + |
| 31 | + private static final int NCOUNT = 1000000; |
| 32 | + |
| 33 | + private static final SplitMix64 rng = new SplitMix64(); |
| 34 | + |
| 35 | + @BeforeClass |
| 36 | + public static void setup() { |
| 37 | + ConfigUtil.initProject(); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testRosettaExamples() { |
| 42 | + SplitMix64 random = new SplitMix64(1234567); |
| 43 | + assertEquals(6457827717110365317L, random.nextLong()); |
| 44 | + assertEquals(3203168211198807973L, random.nextLong()); |
| 45 | + assertEquals(-8629252141511181193L, random.nextLong()); // 9817491932198370423 - 2^64 |
| 46 | + assertEquals(4593380528125082431L, random.nextLong()); |
| 47 | + assertEquals(-2037821214251327795L, random.nextLong()); // 16408922859458223821 - 2^64 |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testNextLongNoArgs() { |
| 52 | + long[] firstElements = new long[100]; |
| 53 | + long min = Long.MAX_VALUE, max = Long.MIN_VALUE; |
| 54 | + boolean generatesEven = false, generatesOdd = false; |
| 55 | + for (int i=0; i<NCOUNT; i++) { |
| 56 | + long n = rng.nextLong(); |
| 57 | + if (i<100) firstElements[i] = n; |
| 58 | + if (n<min) min = n; |
| 59 | + if (n>max) max = n; |
| 60 | + if ((n & 1) == 1) generatesOdd = true; else generatesEven = true; |
| 61 | + } |
| 62 | + LOG.debug("First 100 elements: " + Arrays.toString(firstElements)); |
| 63 | + LOG.debug(NCOUNT + " numbers from " + rng.getClass().getSimpleName() + ".nextLong() gave min = " + min + ", max = " + max); |
| 64 | + assertTrue(min < 0); |
| 65 | + assertTrue(generatesEven); |
| 66 | + assertTrue(generatesOdd); |
| 67 | + } |
| 68 | +} |
0 commit comments