58 lines
1.7 KiB
Swift
58 lines
1.7 KiB
Swift
// DemandShuffleTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@Suite("DemandShuffle")
|
|
struct DemandShuffleTests {
|
|
|
|
@Test func returnsAllValuesExactlyOncePerCycle() {
|
|
let d: DemandShuffle = DemandShuffle(
|
|
values: [1.0, 2.0, 3.0, 4.0, 5.0],
|
|
repeats: 1,
|
|
seed: 42
|
|
)
|
|
var collected: [Double] = []
|
|
while let v: Double = d.nextValue() { collected.append(v) }
|
|
#expect(collected.sorted() == [1.0, 2.0, 3.0, 4.0, 5.0])
|
|
#expect(d.isExhausted)
|
|
}
|
|
|
|
@Test func samePermutationReplaysOnReset() {
|
|
let d: DemandShuffle = DemandShuffle(
|
|
values: [1.0, 2.0, 3.0, 4.0],
|
|
repeats: -1,
|
|
seed: 7
|
|
)
|
|
let first: [Double] = (0..<4).compactMap { _ in d.nextValue() }
|
|
d.reset()
|
|
let second: [Double] = (0..<4).compactMap { _ in d.nextValue() }
|
|
#expect(first == second)
|
|
}
|
|
|
|
@Test func sameSeedProducesSamePermutation() {
|
|
let a: DemandShuffle = DemandShuffle(values: [10.0, 20.0, 30.0, 40.0], repeats: 1, seed: 99)
|
|
let b: DemandShuffle = DemandShuffle(values: [10.0, 20.0, 30.0, 40.0], repeats: 1, seed: 99)
|
|
for _ in 0..<4 {
|
|
#expect(a.nextValue() == b.nextValue())
|
|
}
|
|
}
|
|
|
|
@Test func resetClearsExhaustion() {
|
|
let d: DemandShuffle = DemandShuffle(
|
|
values: [1.0, 2.0, 3.0],
|
|
repeats: 1,
|
|
seed: 5
|
|
)
|
|
while d.nextValue() != nil {}
|
|
#expect(d.isExhausted)
|
|
d.reset()
|
|
#expect(!d.isExhausted)
|
|
#expect(d.nextValue() != nil)
|
|
}
|
|
}
|