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

147 lines
5.2 KiB
Swift

// EuclideanRhythmTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@Suite("EuclideanRhythm")
struct EuclideanRhythmTests {
// MARK: - Pulse count
@Test("returns correct pulse count")
func pulseCount() {
let pat: Pattern<Bool> = euclideanRhythm(pulses: 3, steps: 8)
var hits: Int = 0
for _ in 0..<8 { if pat.next() == true { hits += 1 } }
#expect(hits == 3)
}
@Test("zero pulses all false")
func zeroPulses() {
let pat: Pattern<Bool> = euclideanRhythm(pulses: 0, steps: 4)
for _ in 0..<4 { #expect(pat.next() == false) }
}
@Test("full pulses all true")
func fullPulses() {
let pat: Pattern<Bool> = euclideanRhythm(pulses: 4, steps: 4)
for _ in 0..<4 { #expect(pat.next() == true) }
}
// MARK: - Classic patterns
@Test("E(3,8) Cuban tresillo")
func tresillo() {
// E(3,8) = [1,0,0,1,0,0,1,0]
let expected: [Bool] = [true,false,false,true,false,false,true,false]
let pat: Pattern<Bool> = euclideanRhythm(pulses: 3, steps: 8)
let actual: [Bool] = (0..<8).compactMap { _ in pat.next() }
#expect(actual == expected)
}
@Test("E(2,3) standard two-against-three")
func twoThree() {
// E(2,3) = [1,0,1]
let expected: [Bool] = [true, false, true]
let pat: Pattern<Bool> = euclideanRhythm(pulses: 2, steps: 3)
let actual: [Bool] = (0..<3).compactMap { _ in pat.next() }
#expect(actual == expected)
}
@Test("E(5,8) base output")
func fiveEight() {
// Bresenham distribution of 5 hits in 8 steps
let expected: [Bool] = [true,false,true,false,true,true,false,true]
let pat: Pattern<Bool> = euclideanRhythm(pulses: 5, steps: 8)
let actual: [Bool] = (0..<8).compactMap { _ in pat.next() }
#expect(actual == expected)
}
@Test("E(5,8) rotation:2 yields Cinquillo")
func fiveEightCinquillo() {
// Cinquillo [1,0,1,1,0,1,1,0] is E(5,8) shifted by 2
let expected: [Bool] = [true,false,true,true,false,true,true,false]
let pat: Pattern<Bool> = euclideanRhythm(pulses: 5, steps: 8, rotation: 2)
let actual: [Bool] = (0..<8).compactMap { _ in pat.next() }
#expect(actual == expected)
}
@Test("E(4,4) all hits")
func fourFour() {
let pat: Pattern<Bool> = euclideanRhythm(pulses: 4, steps: 4)
let actual: [Bool] = (0..<4).compactMap { _ in pat.next() }
#expect(actual == [true, true, true, true])
}
// MARK: - Rotation
@Test("rotation shifts pattern cyclically")
func rotation() {
let base: Pattern<Bool> = euclideanRhythm(pulses: 3, steps: 8, rotation: 0)
let rotated: Pattern<Bool> = euclideanRhythm(pulses: 3, steps: 8, rotation: 1)
let baseSeq: [Bool] = (0..<8).compactMap { _ in base.next() }
let rotSeq: [Bool] = (0..<8).compactMap { _ in rotated.next() }
// rotated[i] == base[(i+1) % 8]
let expected: [Bool] = (0..<8).map { i in baseSeq[(i + 1) % 8] }
#expect(rotSeq == expected)
}
@Test("negative rotation wraps correctly")
func negativeRotation() {
let forward: Pattern<Bool> = euclideanRhythm(pulses: 3, steps: 8, rotation: 7)
let backward: Pattern<Bool> = euclideanRhythm(pulses: 3, steps: 8, rotation: -1)
let fSeq: [Bool] = (0..<8).compactMap { _ in forward.next() }
let bSeq: [Bool] = (0..<8).compactMap { _ in backward.next() }
#expect(fSeq == bSeq)
}
@Test("rotation by steps is identity")
func rotationByStepsIdentity() {
let base: Pattern<Bool> = euclideanRhythm(pulses: 5, steps: 12, rotation: 0)
let shifted: Pattern<Bool> = euclideanRhythm(pulses: 5, steps: 12, rotation: 12)
let bSeq: [Bool] = (0..<12).compactMap { _ in base.next() }
let sSeq: [Bool] = (0..<12).compactMap { _ in shifted.next() }
#expect(bSeq == sSeq)
}
// MARK: - Pattern properties
@Test("pattern cycles infinitely by default")
func infiniteCycling() {
let pat: Pattern<Bool> = euclideanRhythm(pulses: 3, steps: 8)
for _ in 0..<100 { let v: Bool? = pat.next(); #expect(v != nil) }
#expect(!pat.isExhausted)
}
@Test("edge case steps=1 pulses=1")
func oneStepOnePulse() {
let pat: Pattern<Bool> = euclideanRhythm(pulses: 1, steps: 1)
#expect(pat.next() == true)
}
@Test("edge case steps=1 pulses=0")
func oneStepZeroPulse() {
let pat: Pattern<Bool> = euclideanRhythm(pulses: 0, steps: 1)
#expect(pat.next() == false)
}
@Test("pulses clamped above steps")
func pulsesClampedAboveSteps() {
let pat: Pattern<Bool> = euclideanRhythm(pulses: 10, steps: 4)
let seq: [Bool] = (0..<4).compactMap { _ in pat.next() }
let hits: Int = seq.filter { $0 }.count
#expect(hits == 4)
}
@Test("negative pulses treated as zero")
func negativePulsesTreatedAsZero() {
let pat: Pattern<Bool> = euclideanRhythm(pulses: -2, steps: 4)
let seq: [Bool] = (0..<4).compactMap { _ in pat.next() }
#expect(seq == [false, false, false, false])
}
}