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

68 lines
2.1 KiB
Swift

// SPSCQueueTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
@Suite("SPSCQueue")
struct SPSCQueueTests {
@Test func emptyQueueReturnsNil() {
let q: SPSCQueue<Int> = SPSCQueue<Int>(capacity: 4)
#expect(q.dequeue() == nil)
}
@Test func singleEnqueueDequeue() {
let q: SPSCQueue<Int> = SPSCQueue<Int>(capacity: 4)
let enqueued: Bool = q.enqueue(42)
#expect(enqueued == true)
#expect(q.dequeue() == 42)
#expect(q.dequeue() == nil)
}
@Test func fifoOrdering() {
let q: SPSCQueue<Int> = SPSCQueue<Int>(capacity: 8)
for i: Int in 0..<5 {
_ = q.enqueue(i)
}
for i: Int in 0..<5 {
#expect(q.dequeue() == i)
}
#expect(q.dequeue() == nil)
}
@Test func fullQueueReturnsFalse() {
let q: SPSCQueue<Int> = SPSCQueue<Int>(capacity: 4)
#expect(q.enqueue(0) == true)
#expect(q.enqueue(1) == true)
#expect(q.enqueue(2) == true)
#expect(q.enqueue(3) == true)
#expect(q.enqueue(4) == false)
}
@Test func capacityMustBeExactPowerOfTwo() {
// Exact power-of-two capacities are accepted; the queue holds exactly that many slots.
// Non-power-of-two values trap (precondition) — tested manually, not in-process.
let q4: SPSCQueue<Int> = SPSCQueue<Int>(capacity: 4)
let q128: SPSCQueue<Int> = SPSCQueue<Int>(capacity: 128)
#expect(q4.capacity == 4)
#expect(q128.capacity == 128)
}
@Test func fillDrainRefill() {
let q: SPSCQueue<Int> = SPSCQueue<Int>(capacity: 4)
// Fill
for i: Int in 0..<4 { _ = q.enqueue(i) }
// Drain
for i: Int in 0..<4 { #expect(q.dequeue() == i) }
#expect(q.dequeue() == nil)
// Refill with different values
for i: Int in 10..<14 { _ = q.enqueue(i) }
for i: Int in 10..<14 { #expect(q.dequeue() == i) }
#expect(q.dequeue() == nil)
}
}