Wideband autonomous SDR analysis engine forked from sdr-visual-suite
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

167 líneas
4.2KB

  1. package runtime
  2. import (
  3. "testing"
  4. "sdr-visual-suite/internal/config"
  5. )
  6. func TestApplyConfigUpdate(t *testing.T) {
  7. cfg := config.Default()
  8. mgr := New(cfg)
  9. center := 7.2e6
  10. sampleRate := 1_024_000
  11. fftSize := 4096
  12. threshold := -35.0
  13. bw := 1536
  14. cfarMode := "OS"
  15. cfarWrap := true
  16. cfarGuard := 2
  17. cfarTrain := 12
  18. cfarRank := 18
  19. cfarScale := 5.5
  20. updated, err := mgr.ApplyConfig(ConfigUpdate{
  21. CenterHz: &center,
  22. SampleRate: &sampleRate,
  23. FFTSize: &fftSize,
  24. TunerBwKHz: &bw,
  25. Detector: &DetectorUpdate{
  26. ThresholdDb: &threshold,
  27. CFARMode: &cfarMode,
  28. CFARWrapAround: &cfarWrap,
  29. CFARGuardCells: &cfarGuard,
  30. CFARTrainCells: &cfarTrain,
  31. CFARRank: &cfarRank,
  32. CFARScaleDb: &cfarScale,
  33. },
  34. })
  35. if err != nil {
  36. t.Fatalf("apply: %v", err)
  37. }
  38. if updated.CenterHz != center {
  39. t.Fatalf("center hz: %v", updated.CenterHz)
  40. }
  41. if updated.SampleRate != sampleRate {
  42. t.Fatalf("sample rate: %v", updated.SampleRate)
  43. }
  44. if updated.FFTSize != fftSize {
  45. t.Fatalf("fft size: %v", updated.FFTSize)
  46. }
  47. if updated.Detector.ThresholdDb != threshold {
  48. t.Fatalf("threshold: %v", updated.Detector.ThresholdDb)
  49. }
  50. if updated.Detector.CFARMode != cfarMode {
  51. t.Fatalf("cfar mode: %v", updated.Detector.CFARMode)
  52. }
  53. if updated.Detector.CFARWrapAround != cfarWrap {
  54. t.Fatalf("cfar wrap: %v", updated.Detector.CFARWrapAround)
  55. }
  56. if updated.Detector.CFARGuardCells != cfarGuard {
  57. t.Fatalf("cfar guard: %v", updated.Detector.CFARGuardCells)
  58. }
  59. if updated.Detector.CFARTrainCells != cfarTrain {
  60. t.Fatalf("cfar train: %v", updated.Detector.CFARTrainCells)
  61. }
  62. if updated.Detector.CFARRank != cfarRank {
  63. t.Fatalf("cfar rank: %v", updated.Detector.CFARRank)
  64. }
  65. if updated.Detector.CFARScaleDb != cfarScale {
  66. t.Fatalf("cfar scale: %v", updated.Detector.CFARScaleDb)
  67. }
  68. if updated.TunerBwKHz != bw {
  69. t.Fatalf("tuner bw: %v", updated.TunerBwKHz)
  70. }
  71. }
  72. func TestApplyConfigRejectsInvalid(t *testing.T) {
  73. cfg := config.Default()
  74. {
  75. mgr := New(cfg)
  76. bad := 0
  77. if _, err := mgr.ApplyConfig(ConfigUpdate{SampleRate: &bad}); err == nil {
  78. t.Fatalf("expected error")
  79. }
  80. snap := mgr.Snapshot()
  81. if snap.SampleRate != cfg.SampleRate {
  82. t.Fatalf("sample rate changed on error")
  83. }
  84. }
  85. {
  86. mgr := New(cfg)
  87. badAlpha := -0.5
  88. if _, err := mgr.ApplyConfig(ConfigUpdate{Detector: &DetectorUpdate{EmaAlpha: &badAlpha}}); err == nil {
  89. t.Fatalf("expected ema_alpha error")
  90. }
  91. if mgr.Snapshot().Detector.EmaAlpha != cfg.Detector.EmaAlpha {
  92. t.Fatalf("ema_alpha changed on error")
  93. }
  94. }
  95. {
  96. mgr := New(cfg)
  97. badAlpha := 1.5
  98. if _, err := mgr.ApplyConfig(ConfigUpdate{Detector: &DetectorUpdate{EmaAlpha: &badAlpha}}); err == nil {
  99. t.Fatalf("expected ema_alpha upper bound error")
  100. }
  101. if mgr.Snapshot().Detector.EmaAlpha != cfg.Detector.EmaAlpha {
  102. t.Fatalf("ema_alpha changed on error")
  103. }
  104. }
  105. {
  106. mgr := New(cfg)
  107. badHyst := -1.0
  108. if _, err := mgr.ApplyConfig(ConfigUpdate{Detector: &DetectorUpdate{HysteresisDb: &badHyst}}); err == nil {
  109. t.Fatalf("expected hysteresis_db error")
  110. }
  111. if mgr.Snapshot().Detector.HysteresisDb != cfg.Detector.HysteresisDb {
  112. t.Fatalf("hysteresis_db changed on error")
  113. }
  114. }
  115. {
  116. mgr := New(cfg)
  117. badStable := 0
  118. if _, err := mgr.ApplyConfig(ConfigUpdate{Detector: &DetectorUpdate{MinStableFrames: &badStable}}); err == nil {
  119. t.Fatalf("expected min_stable_frames error")
  120. }
  121. if mgr.Snapshot().Detector.MinStableFrames != cfg.Detector.MinStableFrames {
  122. t.Fatalf("min_stable_frames changed on error")
  123. }
  124. }
  125. {
  126. mgr := New(cfg)
  127. badGap := -10
  128. if _, err := mgr.ApplyConfig(ConfigUpdate{Detector: &DetectorUpdate{GapToleranceMs: &badGap}}); err == nil {
  129. t.Fatalf("expected gap_tolerance_ms error")
  130. }
  131. if mgr.Snapshot().Detector.GapToleranceMs != cfg.Detector.GapToleranceMs {
  132. t.Fatalf("gap_tolerance_ms changed on error")
  133. }
  134. }
  135. }
  136. func TestApplySettings(t *testing.T) {
  137. cfg := config.Default()
  138. mgr := New(cfg)
  139. agc := true
  140. dc := true
  141. iq := true
  142. updated, err := mgr.ApplySettings(SettingsUpdate{
  143. AGC: &agc,
  144. DCBlock: &dc,
  145. IQBalance: &iq,
  146. })
  147. if err != nil {
  148. t.Fatalf("apply settings: %v", err)
  149. }
  150. if !updated.AGC || !updated.DCBlock || !updated.IQBalance {
  151. t.Fatalf("settings not applied: %+v", updated)
  152. }
  153. }