Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

71 rinda
2.4KB

  1. package config
  2. import (
  3. "os"
  4. "path/filepath"
  5. "testing"
  6. )
  7. func TestDefaultValidate(t *testing.T) {
  8. if err := Default().Validate(); err != nil { t.Fatalf("default invalid: %v", err) }
  9. }
  10. func TestLoadAndValidate(t *testing.T) {
  11. dir := t.TempDir()
  12. path := filepath.Join(dir, "config.json")
  13. os.WriteFile(path, []byte(`{"audio":{"toneLeftHz":900,"toneRightHz":1700,"toneAmplitude":0.3},"fm":{"frequencyMHz":99.9},"backend":{"kind":"file","outputPath":"out.f32"},"control":{"listenAddress":"127.0.0.1:8088"}}`), 0o644)
  14. cfg, err := Load(path)
  15. if err != nil { t.Fatalf("load: %v", err) }
  16. if cfg.Audio.ToneLeftHz != 900 { t.Fatalf("unexpected left tone: %v", cfg.Audio.ToneLeftHz) }
  17. }
  18. func TestValidateRejectsBadFrequency(t *testing.T) {
  19. cfg := Default(); cfg.FM.FrequencyMHz = 200
  20. if err := cfg.Validate(); err == nil { t.Fatal("expected error") }
  21. }
  22. func TestValidateRejectsBadPreEmphasis(t *testing.T) {
  23. cfg := Default(); cfg.FM.PreEmphasisTauUS = 150
  24. if err := cfg.Validate(); err == nil { t.Fatal("expected error") }
  25. }
  26. func TestDefaultPreEmphasis(t *testing.T) {
  27. if Default().FM.PreEmphasisTauUS != 50 { t.Fatal("expected 50") }
  28. }
  29. func TestDefaultFMModulation(t *testing.T) {
  30. cfg := Default()
  31. if !cfg.FM.FMModulationEnabled { t.Fatal("expected true") }
  32. if cfg.FM.MaxDeviationHz != 75000 { t.Fatal("expected 75000") }
  33. }
  34. func TestParsePI(t *testing.T) {
  35. tests := []struct{ in string; want uint16; ok bool }{
  36. {"1234", 0x1234, true}, {"0xBEEF", 0xBEEF, true}, {"0XCAFE", 0xCAFE, true},
  37. {" 0x2345 ", 0x2345, true}, {"", 0, false}, {"nope", 0, false},
  38. }
  39. for _, tt := range tests {
  40. got, err := ParsePI(tt.in)
  41. if tt.ok && err != nil { t.Fatalf("ParsePI(%q): %v", tt.in, err) }
  42. if !tt.ok && err == nil { t.Fatalf("ParsePI(%q): expected error", tt.in) }
  43. if tt.ok && got != tt.want { t.Fatalf("ParsePI(%q): got %x want %x", tt.in, got, tt.want) }
  44. }
  45. }
  46. func TestValidateRejectsInvalidPI(t *testing.T) {
  47. cfg := Default(); cfg.RDS.PI = "nope"
  48. if err := cfg.Validate(); err == nil { t.Fatal("expected error") }
  49. }
  50. func TestValidateRejectsEmptyPI(t *testing.T) {
  51. cfg := Default(); cfg.RDS.PI = ""
  52. if err := cfg.Validate(); err == nil { t.Fatal("expected error") }
  53. }
  54. func TestEffectiveDeviceRate(t *testing.T) {
  55. cfg := Default()
  56. if cfg.EffectiveDeviceRate() != float64(cfg.FM.CompositeRateHz) { t.Fatal("expected composite rate") }
  57. cfg.Backend.DeviceSampleRateHz = 912000
  58. if cfg.EffectiveDeviceRate() != 912000 { t.Fatal("expected 912000") }
  59. }