test(US-03): add failing tests for 880 Hz beep generation
This commit is contained in:
parent
a5b3f46eae
commit
5897d2b77b
1 changed files with 50 additions and 0 deletions
50
app/src/test/kotlin/net/jeena/pacer/BeepGeneratorTest.kt
Normal file
50
app/src/test/kotlin/net/jeena/pacer/BeepGeneratorTest.kt
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
package net.jeena.pacer
|
||||||
|
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Assert.assertTrue
|
||||||
|
import org.junit.Test
|
||||||
|
import kotlin.math.abs
|
||||||
|
|
||||||
|
class BeepGeneratorTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `generateBeep returns correct number of samples`() {
|
||||||
|
val durationMs = 50
|
||||||
|
val samples = BeepGenerator.generateBeep(durationMs)
|
||||||
|
val expected = BeepGenerator.SAMPLE_RATE * durationMs / 1000
|
||||||
|
assertEquals(expected, samples.size)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `generateBeep samples are within Short range`() {
|
||||||
|
val samples = BeepGenerator.generateBeep(50)
|
||||||
|
assertTrue(samples.all { it in Short.MIN_VALUE..Short.MAX_VALUE })
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `generateBeep produces non-silent output`() {
|
||||||
|
val samples = BeepGenerator.generateBeep(50)
|
||||||
|
val maxAmplitude = samples.maxOf { abs(it.toInt()) }
|
||||||
|
assertTrue("Expected non-silent beep, max amplitude was $maxAmplitude", maxAmplitude > 1000)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `generateBeep produces approximately 880 Hz frequency`() {
|
||||||
|
val durationMs = 100
|
||||||
|
val samples = BeepGenerator.generateBeep(durationMs)
|
||||||
|
|
||||||
|
// Count zero crossings (positive→negative transitions)
|
||||||
|
var crossings = 0
|
||||||
|
for (i in 1 until samples.size) {
|
||||||
|
if (samples[i - 1] > 0 && samples[i] <= 0) crossings++
|
||||||
|
}
|
||||||
|
|
||||||
|
// At 880 Hz over 100 ms we expect ~88 complete cycles = ~88 crossings
|
||||||
|
val expectedCrossings = BeepGenerator.FREQUENCY * durationMs / 1000
|
||||||
|
val tolerance = expectedCrossings * 0.05 // 5% tolerance
|
||||||
|
assertTrue(
|
||||||
|
"Expected ~$expectedCrossings zero crossings, got $crossings",
|
||||||
|
abs(crossings - expectedCrossings) < tolerance
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue