60 lines
2.0 KiB
Swift
60 lines
2.0 KiB
Swift
// PatternRoundRobinTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@Suite("Pattern.roundRobin")
|
|
struct PatternRoundRobinTests {
|
|
|
|
@Test func interleavesElementByElement() {
|
|
let pat: Pattern<Int> = Pattern.roundRobin([
|
|
Pattern.sequence([1, 2, 3], repeats: 1),
|
|
Pattern.sequence([10, 20, 30], repeats: 1),
|
|
Pattern.sequence([100, 200, 300], repeats: 1)
|
|
])
|
|
var collected: [Int] = []
|
|
while let v: Int = pat.next() { collected.append(v) }
|
|
#expect(collected == [1, 10, 100, 2, 20, 200, 3, 30, 300])
|
|
#expect(pat.isExhausted)
|
|
}
|
|
|
|
@Test func raggedPatternsDropOutIndividually() {
|
|
let pat: Pattern<Int> = Pattern.roundRobin([
|
|
Pattern.sequence([1, 2], repeats: 1), // 2 values
|
|
Pattern.sequence([10, 20, 30], repeats: 1), // 3 values
|
|
Pattern.sequence([100], repeats: 1) // 1 value
|
|
])
|
|
var collected: [Int] = []
|
|
while let v: Int = pat.next() { collected.append(v) }
|
|
// Round 1: 1, 10, 100 — third drops out next round
|
|
// Round 2: 2, 20, (skip 3rd)
|
|
// Round 3: (skip 1st), 30, (skip 3rd)
|
|
#expect(collected == [1, 10, 100, 2, 20, 30])
|
|
#expect(pat.isExhausted)
|
|
}
|
|
|
|
@Test func emptyListExhaustsImmediately() {
|
|
let pat: Pattern<Int> = Pattern.roundRobin([])
|
|
#expect(pat.next() == nil)
|
|
#expect(pat.isExhausted)
|
|
}
|
|
|
|
@Test func resetRestartsAll() {
|
|
let pat: Pattern<Int> = Pattern.roundRobin([
|
|
Pattern.sequence([1, 2], repeats: 1),
|
|
Pattern.sequence([10, 20], repeats: 1)
|
|
])
|
|
var first: [Int] = []
|
|
while let v: Int = pat.next() { first.append(v) }
|
|
#expect(first == [1, 10, 2, 20])
|
|
pat.reset()
|
|
var second: [Int] = []
|
|
while let v: Int = pat.next() { second.append(v) }
|
|
#expect(second == first)
|
|
}
|
|
}
|