147 lines
4.7 KiB
Swift
147 lines
4.7 KiB
Swift
// PatternTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||
|
||
@MainActor
|
||
@Suite("Pattern")
|
||
struct PatternTests {
|
||
|
||
// MARK: - Basic cycling
|
||
|
||
@Test func returnsValuesInOrder() {
|
||
let pat: Pattern<Int> = Pattern.sequence([1, 2, 3])
|
||
#expect(pat.next() == 1)
|
||
#expect(pat.next() == 2)
|
||
#expect(pat.next() == 3)
|
||
}
|
||
|
||
@Test func wrapsAroundOnCycle() {
|
||
let pat: Pattern<Int> = Pattern.sequence([10, 20])
|
||
_ = pat.next(); _ = pat.next() // complete cycle 1
|
||
#expect(pat.next() == 10) // cycle 2 starts
|
||
}
|
||
|
||
@Test func infiniteRepeatNeverExhausts() {
|
||
let pat: Pattern<Int> = Pattern.sequence([1, 2], repeats: -1)
|
||
for _ in 0..<100 { _ = pat.next() }
|
||
#expect(!pat.isExhausted)
|
||
#expect(pat.next() != nil)
|
||
}
|
||
|
||
// MARK: - Finite repeats
|
||
|
||
@Test func exhaustsAfterOneRepeat() {
|
||
let pat: Pattern<Int> = Pattern.sequence([1, 2, 3], repeats: 1)
|
||
#expect(pat.next() == 1)
|
||
#expect(pat.next() == 2)
|
||
#expect(pat.next() == 3)
|
||
#expect(pat.isExhausted)
|
||
#expect(pat.next() == nil)
|
||
}
|
||
|
||
@Test func exhaustsAfterTwoRepeats() {
|
||
let pat: Pattern<Int> = Pattern.sequence([5, 6], repeats: 2)
|
||
var count: Int = 0
|
||
while let _ = pat.next() { count += 1 }
|
||
#expect(count == 4) // 2 values × 2 repeats
|
||
#expect(pat.isExhausted)
|
||
}
|
||
|
||
@Test func zeroRepeatsExhaustedImmediately() {
|
||
let pat: Pattern<Int> = Pattern.sequence([1, 2], repeats: 0)
|
||
#expect(pat.isExhausted)
|
||
#expect(pat.next() == nil)
|
||
}
|
||
|
||
// MARK: - reset
|
||
|
||
@Test func resetRestartsFromBeginning() {
|
||
let pat: Pattern<Int> = Pattern.sequence([1, 2], repeats: 1)
|
||
_ = pat.next(); _ = pat.next() // exhaust
|
||
#expect(pat.isExhausted)
|
||
pat.reset()
|
||
#expect(!pat.isExhausted)
|
||
#expect(pat.next() == 1)
|
||
}
|
||
|
||
@Test func resetClearsCycleCount() {
|
||
let pat: Pattern<Int> = Pattern.sequence([1], repeats: 1)
|
||
_ = pat.next() // exhaust after 1 element
|
||
pat.reset()
|
||
#expect(pat.next() == 1)
|
||
#expect(pat.next() == nil) // re-exhausts after one more cycle
|
||
}
|
||
|
||
// MARK: - Edge cases
|
||
|
||
@Test func emptyValuesAlwaysNil() {
|
||
let pat: Pattern<Int> = Pattern<Int>.sequence([])
|
||
#expect(pat.next() == nil)
|
||
#expect(!pat.isExhausted) // empty ≠ exhausted
|
||
}
|
||
|
||
@Test func singleValueCycles() {
|
||
let pat: Pattern<Int> = Pattern.sequence([42], repeats: 3)
|
||
for _ in 0..<3 { #expect(pat.next() == 42) }
|
||
#expect(pat.next() == nil)
|
||
}
|
||
|
||
@Test func worksWithStringValues() {
|
||
let pat: Pattern<String> = Pattern.sequence(["do", "re", "mi"])
|
||
#expect(pat.next() == "do")
|
||
#expect(pat.next() == "re")
|
||
#expect(pat.next() == "mi")
|
||
#expect(pat.next() == "do") // wraps
|
||
}
|
||
|
||
@Test func offsetShiftsOnlyFirstPass() {
|
||
// First pass opens at index 1 and runs short; later cycles run full.
|
||
let pat: Pattern<Int> = Pattern.sequence([1, 2, 3], offset: 1)
|
||
#expect(pat.next() == 2)
|
||
#expect(pat.next() == 3)
|
||
#expect(pat.next() == 1) // full cycle from index 0
|
||
#expect(pat.next() == 2)
|
||
#expect(pat.next() == 3)
|
||
#expect(pat.next() == 1) // and again
|
||
}
|
||
|
||
@Test func offsetResetReturnsToOffsetStart() {
|
||
let pat: Pattern<Int> = Pattern.sequence([1, 2, 3], offset: 2)
|
||
#expect(pat.next() == 3)
|
||
#expect(pat.next() == 1)
|
||
pat.reset()
|
||
#expect(pat.next() == 3) // back to the offset start, not index 0
|
||
}
|
||
|
||
@Test func offsetCountsShortFirstPassAsACycle() {
|
||
// repeats: 2, offset: 1 → short pass [2,3] then one full [1,2,3].
|
||
let pat: Pattern<Int> = Pattern.sequence([1, 2, 3], repeats: 2, offset: 1)
|
||
#expect(pat.next() == 2)
|
||
#expect(pat.next() == 3)
|
||
#expect(pat.next() == 1)
|
||
#expect(pat.next() == 2)
|
||
#expect(pat.next() == 3)
|
||
#expect(pat.next() == nil)
|
||
#expect(pat.isExhausted)
|
||
}
|
||
|
||
@Test func offsetWrapsOutOfRange() {
|
||
// offset 4 on a 3-element list normalises to index 1.
|
||
let pat: Pattern<Int> = Pattern.sequence([1, 2, 3], offset: 4)
|
||
#expect(pat.next() == 2)
|
||
// Negative offsets wrap too: -1 → index 2.
|
||
let neg: Pattern<Int> = Pattern.sequence([1, 2, 3], offset: -1)
|
||
#expect(neg.next() == 3)
|
||
}
|
||
|
||
@Test func offsetOnEmptyValuesIsSafe() {
|
||
let pat: Pattern<Int> = Pattern<Int>.sequence([], offset: 5)
|
||
#expect(pat.next() == nil)
|
||
}
|
||
}
|