Skip to content

Commit 38f4965

Browse files
committed
test bigLFSR
1 parent 643b63a commit 38f4965

File tree

2 files changed

+10
-1
lines changed
  • src
    • commonMain/kotlin/ai/hypergraph/kaliningraph/sampling
    • commonTest/kotlin/ai/hypergraph/kaliningraph/sampling

2 files changed

+10
-1
lines changed

src/commonMain/kotlin/ai/hypergraph/kaliningraph/sampling/Samplers.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ fun makeBigIntFromTaps(taps: List<Int>): BigInteger =
288288
}.reduce { a, c -> a.or(c) }.or(BigInteger.ONE)
289289

290290
fun makeRandBigInt(len: Int): BigInteger =
291-
BigInteger.parseString(Array(len) { if(Random.nextBoolean()) '1' else '0' }.joinToString(""), 2) + 1
291+
BigInteger.parseString(Array(len) { if (it == 0) '1' else if (Random.nextBoolean()) '1' else '0' }.joinToString(""), 2) + 1
292292

293293
class BigLFSR(primitivePoly: BigInteger, val start: BigInteger = BigInteger.ONE) {
294294
private val taps: BigInteger = primitivePoly.shr(1)

src/commonTest/kotlin/ai/hypergraph/kaliningraph/sampling/SamplingTest.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ai.hypergraph.kaliningraph.sampling
33
import ai.hypergraph.kaliningraph.choose
44
import ai.hypergraph.kaliningraph.types.times
55
import kotlin.math.pow
6+
import kotlin.random.Random
67
import kotlin.test.*
78

89
/*
@@ -35,6 +36,14 @@ class SamplingTest {
3536
assertEquals(2.0.pow(i).toInt(), distinct.size + 1)
3637
}
3738

39+
/*
40+
./gradlew jvmTest --tests "ai.hypergraph.kaliningraph.sampling.SamplingTest.testBigLFSR"
41+
*/
42+
@Test
43+
fun testBigLFSR() =
44+
listOf(11, 101, 1_001, 10_001, 100_001).map { it + 2 * it * Random.nextDouble().toInt() }
45+
.forEach { i -> assertEquals(i, bigLFSRSequence(i).toList().distinct().size) }
46+
3847
/*
3948
./gradlew jvmTest --tests "ai.hypergraph.kaliningraph.sampling.SamplingTest.testMDSampler"
4049
*/

0 commit comments

Comments
 (0)