package dsp

import (
	"math"
	"testing"
)

func TestFMModulatorOutputMagnitude(t *testing.T) {
	mod := NewFMModulator(228000)
	// IQ output should always have magnitude ~1
	for n := 0; n < 1000; n++ {
		composite := math.Sin(2 * math.Pi * 1000 * float64(n) / 228000)
		i, q := mod.Modulate(composite)
		mag := math.Sqrt(i*i + q*q)
		if math.Abs(mag-1.0) > 1e-9 {
			t.Fatalf("sample %d: magnitude=%.9f, expected 1.0", n, mag)
		}
	}
}

func TestFMModulatorZeroInput(t *testing.T) {
	mod := NewFMModulator(228000)
	// Zero input = no deviation = phase stays at 0
	for n := 0; n < 100; n++ {
		i, q := mod.Modulate(0)
		if math.Abs(i-1.0) > 1e-9 || math.Abs(q) > 1e-9 {
			t.Fatalf("sample %d: expected (1,0) got (%.6f,%.6f)", n, i, q)
		}
	}
}

func TestFMModulatorReset(t *testing.T) {
	mod := NewFMModulator(228000)
	mod.Modulate(0.5)
	mod.Modulate(0.5)
	mod.Reset()
	i, q := mod.Modulate(0)
	if math.Abs(i-1.0) > 1e-9 || math.Abs(q) > 1e-9 {
		t.Fatalf("after reset: expected (1,0) got (%.6f,%.6f)", i, q)
	}
}
