Go-based FM stereo transmitter with RDS, Windows-first and cross-platform
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

126 行
2.9KB

  1. package config
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strings"
  6. "testing"
  7. )
  8. func TestDefaultValidate(t *testing.T) {
  9. if err := Default().Validate(); err != nil {
  10. t.Fatalf("default invalid: %v", err)
  11. }
  12. }
  13. func TestLoadAndValidate(t *testing.T) {
  14. dir := t.TempDir()
  15. path := filepath.Join(dir, "config.json")
  16. 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)
  17. cfg, err := Load(path)
  18. if err != nil {
  19. t.Fatalf("load: %v", err)
  20. }
  21. if cfg.Audio.ToneLeftHz != 900 {
  22. t.Fatalf("unexpected left tone: %v", cfg.Audio.ToneLeftHz)
  23. }
  24. }
  25. func TestValidateRejectsBadFrequency(t *testing.T) {
  26. cfg := Default()
  27. cfg.FM.FrequencyMHz = 200
  28. if err := cfg.Validate(); err == nil {
  29. t.Fatal("expected error")
  30. }
  31. }
  32. func TestValidateRejectsBadPreEmphasis(t *testing.T) {
  33. cfg := Default()
  34. cfg.FM.PreEmphasisTauUS = 150
  35. if err := cfg.Validate(); err == nil {
  36. t.Fatal("expected error")
  37. }
  38. }
  39. func TestDefaultPreEmphasis(t *testing.T) {
  40. if Default().FM.PreEmphasisTauUS != 50 {
  41. t.Fatal("expected 50")
  42. }
  43. }
  44. func TestDefaultFMModulation(t *testing.T) {
  45. cfg := Default()
  46. if !cfg.FM.FMModulationEnabled {
  47. t.Fatal("expected true")
  48. }
  49. if cfg.FM.MaxDeviationHz != 75000 {
  50. t.Fatal("expected 75000")
  51. }
  52. }
  53. func TestParsePI(t *testing.T) {
  54. tests := []struct {
  55. in string
  56. want uint16
  57. ok bool
  58. }{
  59. {"1234", 0x1234, true}, {"0xBEEF", 0xBEEF, true}, {"0XCAFE", 0xCAFE, true},
  60. {" 0x2345 ", 0x2345, true}, {"", 0, false}, {"nope", 0, false},
  61. }
  62. for _, tt := range tests {
  63. got, err := ParsePI(tt.in)
  64. if tt.ok && err != nil {
  65. t.Fatalf("ParsePI(%q): %v", tt.in, err)
  66. }
  67. if !tt.ok && err == nil {
  68. t.Fatalf("ParsePI(%q): expected error", tt.in)
  69. }
  70. if tt.ok && got != tt.want {
  71. t.Fatalf("ParsePI(%q): got %x want %x", tt.in, got, tt.want)
  72. }
  73. }
  74. }
  75. func TestValidateRejectsInvalidPI(t *testing.T) {
  76. cfg := Default()
  77. cfg.RDS.PI = "nope"
  78. if err := cfg.Validate(); err == nil {
  79. t.Fatal("expected error")
  80. }
  81. }
  82. func TestValidateRejectsEmptyPI(t *testing.T) {
  83. cfg := Default()
  84. cfg.RDS.PI = ""
  85. if err := cfg.Validate(); err == nil {
  86. t.Fatal("expected error")
  87. }
  88. }
  89. func TestValidateRejectsLongPS(t *testing.T) {
  90. cfg := Default()
  91. cfg.RDS.PS = "TOO_LONG_PS"
  92. if err := cfg.Validate(); err == nil {
  93. t.Fatal("expected error for PS longer than 8 characters")
  94. }
  95. }
  96. func TestValidateRejectsLongRadioText(t *testing.T) {
  97. cfg := Default()
  98. cfg.RDS.RadioText = strings.Repeat("x", 65)
  99. if err := cfg.Validate(); err == nil {
  100. t.Fatal("expected error for RadioText longer than 64 characters")
  101. }
  102. }
  103. func TestEffectiveDeviceRate(t *testing.T) {
  104. cfg := Default()
  105. if cfg.EffectiveDeviceRate() != float64(cfg.FM.CompositeRateHz) {
  106. t.Fatal("expected composite rate")
  107. }
  108. cfg.Backend.DeviceSampleRateHz = 912000
  109. if cfg.EffectiveDeviceRate() != 912000 {
  110. t.Fatal("expected 912000")
  111. }
  112. }