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

101 lines
3.0 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// PatternTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@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
}
}