prep for 1.2.0 release updates
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
// DemandWeightedRandomTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
@MainActor
|
||||
@Suite("DemandWeightedRandom")
|
||||
struct DemandWeightedRandomTests {
|
||||
|
||||
@Test func zeroWeightValueNeverPicked() {
|
||||
let d: DemandWeightedRandom = DemandWeightedRandom(
|
||||
values: [1.0, 2.0, 3.0],
|
||||
weights: [1.0, 0.0, 1.0],
|
||||
repeats: 2_000,
|
||||
seed: 42
|
||||
)
|
||||
while let v: Double = d.nextValue() {
|
||||
#expect(v != 2.0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test func biasedDistributionFavoursHighWeight() {
|
||||
// 9:1 ratio → ~90 % of picks favour the first value.
|
||||
let n: Int = 5_000
|
||||
let d: DemandWeightedRandom = DemandWeightedRandom(
|
||||
values: [100.0, 200.0],
|
||||
weights: [9.0, 1.0],
|
||||
repeats: n,
|
||||
seed: 99
|
||||
)
|
||||
var lowCount: Int = 0
|
||||
while let v: Double = d.nextValue() {
|
||||
if v == 100.0 { lowCount += 1 }
|
||||
}
|
||||
let fraction: Double = Double(lowCount) / Double(n)
|
||||
#expect(abs(fraction - 0.9) < 0.03)
|
||||
}
|
||||
|
||||
@Test func sameSeedProducesIdenticalSequence() {
|
||||
let a: DemandWeightedRandom = DemandWeightedRandom(
|
||||
values: [10.0, 20.0, 30.0],
|
||||
weights: [1.0, 2.0, 3.0],
|
||||
repeats: 50,
|
||||
seed: 77
|
||||
)
|
||||
let b: DemandWeightedRandom = DemandWeightedRandom(
|
||||
values: [10.0, 20.0, 30.0],
|
||||
weights: [1.0, 2.0, 3.0],
|
||||
repeats: 50,
|
||||
seed: 77
|
||||
)
|
||||
for _ in 0..<50 {
|
||||
#expect(a.nextValue() == b.nextValue())
|
||||
}
|
||||
}
|
||||
|
||||
@Test func resetClearsExhaustion() {
|
||||
let d: DemandWeightedRandom = DemandWeightedRandom(
|
||||
values: [1.0, 2.0],
|
||||
weights: [1.0, 1.0],
|
||||
repeats: 3,
|
||||
seed: 13
|
||||
)
|
||||
for _ in 0..<3 { _ = d.nextValue() }
|
||||
#expect(d.isExhausted)
|
||||
d.reset()
|
||||
#expect(!d.isExhausted)
|
||||
#expect(d.nextValue() != nil)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user