package main import ( "math" "testing" "time" ) // Test decodeMuLaw basic functionality func TestDecodeMuLaw(t *testing.T) { data := []byte{0x00, 0xFF, 0x7F, 0x80} pcm, err := decodeMuLaw(data) if err != nil { t.Fatalf("decodeMuLaw returned error: %v", err) } if len(pcm) != len(data) { t.Errorf("Expected %d samples, got %d", len(data), len(pcm)) } } // Test Goertzel power calculation func TestGoertzelPower(t *testing.T) { fs := 8000.0 N := 100 freq := 1000.0 g := newGoertzel(freq, fs, N) signal := make([]float64, N) for i := range signal { signal[i] = math.Sin(2 * math.Pi * freq * float64(i) / fs) } power := g.Power(signal) if power <= 0 { t.Errorf("Expected positive power, got %f", power) } } // Test windowHann modifies slice func TestWindowHann(t *testing.T) { x := []float64{1, 1, 1, 1} windowHann(x) for i, v := range x { if v == 1 { t.Errorf("windowHann did not modify value at index %d", i) } } } // Test rmsPCM returns correct RMS func TestRmsPCM(t *testing.T) { buf := []int16{1000, -1000, 1000, -1000} rms := rmsPCM(buf) if rms < 900 || rms > 1100 { t.Errorf("Unexpected RMS value: %f", rms) } } // Test twoToneDetector reset func TestTwoToneDetectorReset(t *testing.T) { d := newTwoToneDetector(8000, 100, 50, 0.65, 300, 1000, 3000, 5000) d.inA = true d.aFreq = 1000 d.reset() if d.inA || d.aFreq != 0 { t.Errorf("reset did not clear state") } } // Test min function func TestMin(t *testing.T) { if min(1, 2) != 1 { t.Errorf("min(1,2) should be 1") } if min(2, 1) != 1 { t.Errorf("min(2,1) should be 1") } if min(2, 2) != 2 { t.Errorf("min(2,2) should be 2") } } // Test saveToWAV (does not check file output, just runs function) func TestSaveToWAV(t *testing.T) { data := make([]int16, 8000) for i := range data { data[i] = int16(i % 32768) } saveToWAV(data, 8000) // No assertion, just ensure no panic } // Test stepWindow returns expected values for silence func TestStepWindowSilence(t *testing.T) { d := newTwoToneDetector(8000, 100, 50, 0.65, 300, 1000, 3000, 5000) buf := make([]int16, 100) t0 := time.Now() event, aFreq, aDur, bFreq, bDur := d.stepWindow(buf, t0) if event != "" || aFreq != 0 || aDur != 0 || bFreq != 0 || bDur != 0 { t.Errorf("stepWindow should return zero values for silence") } }