85 lines
2.8 KiB
Swift
85 lines
2.8 KiB
Swift
// PatternInterleaveTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@Suite("Pattern.interleave")
|
||
struct PatternInterleaveTests {
|
||
|
||
@Test func threeCyclesOfThreeListsMatchesSCPlace() {
|
||
// SC: Place([[1,2,3], [4,5,6], [7,8,9]], 3) → 1,4,7, 2,5,8, 3,6,9
|
||
let pat: Pattern<Int> = Pattern.interleave([
|
||
[1, 2, 3],
|
||
[4, 5, 6],
|
||
[7, 8, 9]
|
||
], repeats: 3)
|
||
var collected: [Int] = []
|
||
while let v: Int = pat.next() { collected.append(v) }
|
||
#expect(collected == [1, 4, 7, 2, 5, 8, 3, 6, 9])
|
||
#expect(pat.isExhausted)
|
||
}
|
||
|
||
@Test func repeatsCountsOuterCycles() {
|
||
// repeats: 1 → one outer cycle → lists.count values total.
|
||
let pat: Pattern<Int> = Pattern.interleave([
|
||
[1, 2, 3],
|
||
[4, 5, 6],
|
||
[7, 8, 9]
|
||
], repeats: 1)
|
||
var collected: [Int] = []
|
||
while let v: Int = pat.next() { collected.append(v) }
|
||
#expect(collected == [1, 4, 7])
|
||
}
|
||
|
||
@Test func raggedSubListsWrapModuloOwnLength() {
|
||
// [[1,2], [4,5,6]], repeats: 4 →
|
||
// j=0: lists[0].wrapAt(0)=1, lists[1].wrapAt(0)=4
|
||
// j=1: lists[0].wrapAt(1)=2, lists[1].wrapAt(1)=5
|
||
// j=2: lists[0].wrapAt(2)=1 (2%2=0), lists[1].wrapAt(2)=6
|
||
// j=3: lists[0].wrapAt(3)=2 (3%2=1), lists[1].wrapAt(3)=4 (3%3=0)
|
||
let pat: Pattern<Int> = Pattern.interleave([
|
||
[1, 2],
|
||
[4, 5, 6]
|
||
], repeats: 4)
|
||
var collected: [Int] = []
|
||
while let v: Int = pat.next() { collected.append(v) }
|
||
#expect(collected == [1, 4, 2, 5, 1, 6, 2, 4])
|
||
}
|
||
|
||
@Test func emptySubListsAreSkipped() {
|
||
let pat: Pattern<Int> = Pattern.interleave([
|
||
[1, 2],
|
||
[],
|
||
[10, 20]
|
||
], repeats: 2)
|
||
var collected: [Int] = []
|
||
while let v: Int = pat.next() { collected.append(v) }
|
||
// Effective lists: [[1,2], [10,20]]. 2 outer cycles × 2 lists = 4 emits.
|
||
#expect(collected == [1, 10, 2, 20])
|
||
}
|
||
|
||
@Test func emptyOuterListExhaustsImmediately() {
|
||
let pat: Pattern<Int> = Pattern.interleave([], repeats: -1)
|
||
#expect(pat.next() == nil)
|
||
#expect(pat.isExhausted)
|
||
}
|
||
|
||
@Test func resetRewindsBothCounters() {
|
||
let pat: Pattern<Int> = Pattern.interleave([[1, 2], [3, 4]], repeats: 2)
|
||
_ = pat.next(); _ = pat.next()
|
||
pat.reset()
|
||
#expect(pat.next() == 1)
|
||
#expect(pat.next() == 3)
|
||
}
|
||
|
||
@Test func infiniteRepeatsNeverExhausts() {
|
||
let pat: Pattern<Int> = Pattern.interleave([[1], [2], [3]], repeats: -1)
|
||
for _ in 0..<1_000 { _ = pat.next() }
|
||
#expect(!pat.isExhausted)
|
||
}
|
||
}
|