From 38f49653b5657babf0118f0dd1581b25124e0a81 Mon Sep 17 00:00:00 2001 From: breandan Date: Mon, 16 Dec 2024 20:48:25 -0500 Subject: [PATCH] test bigLFSR --- .../ai/hypergraph/kaliningraph/sampling/Samplers.kt | 2 +- .../ai/hypergraph/kaliningraph/sampling/SamplingTest.kt | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/commonMain/kotlin/ai/hypergraph/kaliningraph/sampling/Samplers.kt b/src/commonMain/kotlin/ai/hypergraph/kaliningraph/sampling/Samplers.kt index 0701df21..338e523f 100644 --- a/src/commonMain/kotlin/ai/hypergraph/kaliningraph/sampling/Samplers.kt +++ b/src/commonMain/kotlin/ai/hypergraph/kaliningraph/sampling/Samplers.kt @@ -288,7 +288,7 @@ fun makeBigIntFromTaps(taps: List): BigInteger = }.reduce { a, c -> a.or(c) }.or(BigInteger.ONE) fun makeRandBigInt(len: Int): BigInteger = - BigInteger.parseString(Array(len) { if(Random.nextBoolean()) '1' else '0' }.joinToString(""), 2) + 1 + BigInteger.parseString(Array(len) { if (it == 0) '1' else if (Random.nextBoolean()) '1' else '0' }.joinToString(""), 2) + 1 class BigLFSR(primitivePoly: BigInteger, val start: BigInteger = BigInteger.ONE) { private val taps: BigInteger = primitivePoly.shr(1) diff --git a/src/commonTest/kotlin/ai/hypergraph/kaliningraph/sampling/SamplingTest.kt b/src/commonTest/kotlin/ai/hypergraph/kaliningraph/sampling/SamplingTest.kt index 18669027..f17285b4 100644 --- a/src/commonTest/kotlin/ai/hypergraph/kaliningraph/sampling/SamplingTest.kt +++ b/src/commonTest/kotlin/ai/hypergraph/kaliningraph/sampling/SamplingTest.kt @@ -3,6 +3,7 @@ package ai.hypergraph.kaliningraph.sampling import ai.hypergraph.kaliningraph.choose import ai.hypergraph.kaliningraph.types.times import kotlin.math.pow +import kotlin.random.Random import kotlin.test.* /* @@ -35,6 +36,14 @@ class SamplingTest { assertEquals(2.0.pow(i).toInt(), distinct.size + 1) } +/* +./gradlew jvmTest --tests "ai.hypergraph.kaliningraph.sampling.SamplingTest.testBigLFSR" +*/ + @Test + fun testBigLFSR() = + listOf(11, 101, 1_001, 10_001, 100_001).map { it + 2 * it * Random.nextDouble().toInt() } + .forEach { i -> assertEquals(i, bigLFSRSequence(i).toList().distinct().size) } + /* ./gradlew jvmTest --tests "ai.hypergraph.kaliningraph.sampling.SamplingTest.testMDSampler" */