64 lines
2.1 KiB
Swift
64 lines
2.1 KiB
Swift
// PatternNonRepeatingRandomTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
@Suite("Pattern.nonRepeatingRandom")
|
|
struct PatternNonRepeatingRandomTests {
|
|
|
|
@Test func neverRepeatsSameIndexConsecutively() {
|
|
// Use unique values so index-equality and value-equality coincide.
|
|
let pat: Pattern<Int> = Pattern.nonRepeatingRandom(
|
|
[1, 2, 3, 4, 5],
|
|
repeats: 1_000,
|
|
seed: 42
|
|
)
|
|
var prev: Int? = nil
|
|
while let v: Int = pat.next() {
|
|
if let p: Int = prev {
|
|
#expect(v != p, "Consecutive repeat: \(p) → \(v)")
|
|
}
|
|
prev = v
|
|
}
|
|
}
|
|
|
|
@Test func singleValueAlwaysReturnsThatValue() {
|
|
let pat: Pattern<Int> = Pattern.nonRepeatingRandom([7], repeats: 5, seed: 1)
|
|
for _ in 0..<5 { #expect(pat.next() == 7) }
|
|
#expect(pat.next() == nil)
|
|
}
|
|
|
|
@Test func lengthExhaustsCorrectly() {
|
|
let pat: Pattern<Int> = Pattern.nonRepeatingRandom([1, 2, 3], repeats: 6, seed: 1)
|
|
for _ in 0..<6 { #expect(pat.next() != nil) }
|
|
#expect(pat.next() == nil)
|
|
#expect(pat.isExhausted)
|
|
}
|
|
|
|
@Test func sameSeedProducesIdenticalSequence() {
|
|
let a: Pattern<Int> = Pattern.nonRepeatingRandom([10, 20, 30, 40], repeats: 50, seed: 99)
|
|
let b: Pattern<Int> = Pattern.nonRepeatingRandom([10, 20, 30, 40], repeats: 50, seed: 99)
|
|
for _ in 0..<50 {
|
|
#expect(a.next() == b.next())
|
|
}
|
|
}
|
|
|
|
@Test func resetClearsExhaustionAndLastPick() {
|
|
let pat: Pattern<Int> = Pattern.nonRepeatingRandom([1, 2, 3], repeats: 3, seed: 1)
|
|
for _ in 0..<3 { _ = pat.next() }
|
|
#expect(pat.isExhausted)
|
|
pat.reset()
|
|
#expect(!pat.isExhausted)
|
|
#expect(pat.next() != nil)
|
|
}
|
|
|
|
@Test func emptyValuesYieldsNil() {
|
|
let pat: Pattern<Int> = Pattern.nonRepeatingRandom([], repeats: -1, seed: 1)
|
|
#expect(pat.next() == nil)
|
|
}
|
|
}
|