1.0.0 release
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
// PatternWeightedRandomTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@Suite("Pattern.weightedRandom")
|
||||
struct PatternWeightedRandomTests {
|
||||
|
||||
@Test func zeroWeightValueNeverPicked() {
|
||||
// Weight 0 on the second value → must never appear.
|
||||
let pat: Pattern<Int> = Pattern.weightedRandom(
|
||||
[1, 2, 3],
|
||||
weights: [1.0, 0.0, 1.0],
|
||||
repeats: 2_000,
|
||||
seed: 42
|
||||
)
|
||||
while let v: Int = pat.next() {
|
||||
#expect(v != 2)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func biasedDistributionFavoursHighWeight() {
|
||||
// 9:1 ratio between two values → first value should appear ~90 % of the time.
|
||||
let n: Int = 5_000
|
||||
let pat: Pattern<String> = Pattern.weightedRandom(
|
||||
["a", "b"],
|
||||
weights: [9.0, 1.0],
|
||||
repeats: n,
|
||||
seed: 99
|
||||
)
|
||||
var aCount: Int = 0
|
||||
while let v: String = pat.next() {
|
||||
if v == "a" { aCount += 1 }
|
||||
}
|
||||
let aFraction: Double = Double(aCount) / Double(n)
|
||||
// 3·SE = 3·√(0.9·0.1/n) ≈ 0.0127 at n=5000. Allow 0.03 for safety.
|
||||
#expect(abs(aFraction - 0.9) < 0.03)
|
||||
}
|
||||
|
||||
@Test func sameSeedProducesIdenticalSequence() {
|
||||
let a: Pattern<Int> = Pattern.weightedRandom(
|
||||
[10, 20, 30],
|
||||
weights: [1.0, 2.0, 3.0],
|
||||
repeats: 50,
|
||||
seed: 77
|
||||
)
|
||||
let b: Pattern<Int> = Pattern.weightedRandom(
|
||||
[10, 20, 30],
|
||||
weights: [1.0, 2.0, 3.0],
|
||||
repeats: 50,
|
||||
seed: 77
|
||||
)
|
||||
for _ in 0..<50 {
|
||||
#expect(a.next() == b.next())
|
||||
}
|
||||
}
|
||||
|
||||
@Test func lengthExhaustsCorrectly() {
|
||||
let pat: Pattern<Int> = Pattern.weightedRandom(
|
||||
[1, 2],
|
||||
weights: [1.0, 1.0],
|
||||
repeats: 7,
|
||||
seed: 1
|
||||
)
|
||||
for _ in 0..<7 { #expect(pat.next() != nil) }
|
||||
#expect(pat.next() == nil)
|
||||
#expect(pat.isExhausted)
|
||||
}
|
||||
|
||||
@Test func resetClearsExhaustion() {
|
||||
let pat: Pattern<Int> = Pattern.weightedRandom(
|
||||
[1, 2, 3],
|
||||
weights: [1.0, 1.0, 1.0],
|
||||
repeats: 3,
|
||||
seed: 13
|
||||
)
|
||||
for _ in 0..<3 { _ = pat.next() }
|
||||
#expect(pat.isExhausted)
|
||||
pat.reset()
|
||||
#expect(!pat.isExhausted)
|
||||
#expect(pat.next() != nil)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user