Files
Lyrebird/Tests/LyrebirdTests/PatternShuffleTests.swift
T
2026-06-01 18:27:40 -07:00

60 lines
2.1 KiB
Swift

// PatternShuffleTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@Suite("Pattern.shuffle")
struct PatternShuffleTests {
@Test func returnsAllValuesExactlyOncePerCycle() {
let pat: Pattern<Int> = Pattern.shuffle([1, 2, 3, 4, 5], repeats: 1, seed: 42)
var collected: [Int] = []
while let v: Int = pat.next() { collected.append(v) }
#expect(collected.sorted() == [1, 2, 3, 4, 5])
#expect(collected.count == 5)
#expect(pat.isExhausted)
}
@Test func samePermutationReplaysOnReset() {
let pat: Pattern<Int> = Pattern.shuffle([1, 2, 3, 4], repeats: -1, seed: 7)
let firstPass: [Int] = (0..<4).compactMap { _ in pat.next() }
pat.reset()
let secondPass: [Int] = (0..<4).compactMap { _ in pat.next() }
#expect(firstPass == secondPass)
}
@Test func sameSeedProducesSamePermutation() {
let a: Pattern<Int> = Pattern.shuffle([10, 20, 30, 40, 50], repeats: 1, seed: 99)
let b: Pattern<Int> = Pattern.shuffle([10, 20, 30, 40, 50], repeats: 1, seed: 99)
for _ in 0..<5 {
#expect(a.next() == b.next())
}
}
@Test func multipleRepeatsCycleSamePermutation() {
let pat: Pattern<Int> = Pattern.shuffle([1, 2, 3], repeats: 3, seed: 1)
let first3: [Int] = (0..<3).compactMap { _ in pat.next() }
let next3: [Int] = (0..<3).compactMap { _ in pat.next() }
let last3: [Int] = (0..<3).compactMap { _ in pat.next() }
#expect(first3 == next3)
#expect(next3 == last3)
#expect(pat.next() == nil)
#expect(pat.isExhausted)
}
@Test func emptyValuesYieldsNil() {
let pat: Pattern<Int> = Pattern.shuffle([], repeats: -1, seed: 1)
#expect(pat.next() == nil)
}
@Test func singleValueAlwaysReturnsThatValue() {
let pat: Pattern<Int> = Pattern.shuffle([42], repeats: 5, seed: 1)
for _ in 0..<5 { #expect(pat.next() == 42) }
#expect(pat.next() == nil)
}
}