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

58 lines
1.5 KiB
Swift

// PatternLimitTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@Suite("Pattern.limit")
struct PatternLimitTests {
@Test func emitsAtMostCountValues() {
let pat: Pattern<Int> = Pattern.limit(
Pattern.sequence([1, 2, 3, 4, 5], repeats: -1),
count: 3
)
#expect(pat.next() == 1)
#expect(pat.next() == 2)
#expect(pat.next() == 3)
#expect(pat.next() == nil)
#expect(pat.isExhausted)
}
@Test func countZeroEmitsNothing() {
let pat: Pattern<Int> = Pattern.limit(
Pattern.sequence([1, 2, 3], repeats: -1),
count: 0
)
#expect(pat.next() == nil)
#expect(pat.isExhausted)
}
@Test func upstreamExhaustionLatchesEarly() {
// Source emits only 2 values; limit asks for 5.
let pat: Pattern<Int> = Pattern.limit(
Pattern.sequence([10, 20], repeats: 1),
count: 5
)
#expect(pat.next() == 10)
#expect(pat.next() == 20)
#expect(pat.next() == nil)
#expect(pat.isExhausted)
}
@Test func resetRestartsCount() {
let pat: Pattern<Int> = Pattern.limit(
Pattern.sequence([1, 2, 3], repeats: -1),
count: 2
)
_ = pat.next(); _ = pat.next()
#expect(pat.isExhausted)
pat.reset()
#expect(!pat.isExhausted)
#expect(pat.next() == 1)
}
}