Wideband autonomous SDR analysis engine forked from sdr-visual-suite
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

170 строки
4.3KB

  1. package runtime
  2. import (
  3. "testing"
  4. "sdr-wideband-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.Surveillance.AnalysisFFTSize != fftSize {
  48. t.Fatalf("analysis fft size: %v", updated.Surveillance.AnalysisFFTSize)
  49. }
  50. if updated.Detector.ThresholdDb != threshold {
  51. t.Fatalf("threshold: %v", updated.Detector.ThresholdDb)
  52. }
  53. if updated.Detector.CFARMode != cfarMode {
  54. t.Fatalf("cfar mode: %v", updated.Detector.CFARMode)
  55. }
  56. if updated.Detector.CFARWrapAround != cfarWrap {
  57. t.Fatalf("cfar wrap: %v", updated.Detector.CFARWrapAround)
  58. }
  59. if updated.Detector.CFARGuardCells != cfarGuard {
  60. t.Fatalf("cfar guard: %v", updated.Detector.CFARGuardCells)
  61. }
  62. if updated.Detector.CFARTrainCells != cfarTrain {
  63. t.Fatalf("cfar train: %v", updated.Detector.CFARTrainCells)
  64. }
  65. if updated.Detector.CFARRank != cfarRank {
  66. t.Fatalf("cfar rank: %v", updated.Detector.CFARRank)
  67. }
  68. if updated.Detector.CFARScaleDb != cfarScale {
  69. t.Fatalf("cfar scale: %v", updated.Detector.CFARScaleDb)
  70. }
  71. if updated.TunerBwKHz != bw {
  72. t.Fatalf("tuner bw: %v", updated.TunerBwKHz)
  73. }
  74. }
  75. func TestApplyConfigRejectsInvalid(t *testing.T) {
  76. cfg := config.Default()
  77. {
  78. mgr := New(cfg)
  79. bad := 0
  80. if _, err := mgr.ApplyConfig(ConfigUpdate{SampleRate: &bad}); err == nil {
  81. t.Fatalf("expected error")
  82. }
  83. snap := mgr.Snapshot()
  84. if snap.SampleRate != cfg.SampleRate {
  85. t.Fatalf("sample rate changed on error")
  86. }
  87. }
  88. {
  89. mgr := New(cfg)
  90. badAlpha := -0.5
  91. if _, err := mgr.ApplyConfig(ConfigUpdate{Detector: &DetectorUpdate{EmaAlpha: &badAlpha}}); err == nil {
  92. t.Fatalf("expected ema_alpha error")
  93. }
  94. if mgr.Snapshot().Detector.EmaAlpha != cfg.Detector.EmaAlpha {
  95. t.Fatalf("ema_alpha changed on error")
  96. }
  97. }
  98. {
  99. mgr := New(cfg)
  100. badAlpha := 1.5
  101. if _, err := mgr.ApplyConfig(ConfigUpdate{Detector: &DetectorUpdate{EmaAlpha: &badAlpha}}); err == nil {
  102. t.Fatalf("expected ema_alpha upper bound error")
  103. }
  104. if mgr.Snapshot().Detector.EmaAlpha != cfg.Detector.EmaAlpha {
  105. t.Fatalf("ema_alpha changed on error")
  106. }
  107. }
  108. {
  109. mgr := New(cfg)
  110. badHyst := -1.0
  111. if _, err := mgr.ApplyConfig(ConfigUpdate{Detector: &DetectorUpdate{HysteresisDb: &badHyst}}); err == nil {
  112. t.Fatalf("expected hysteresis_db error")
  113. }
  114. if mgr.Snapshot().Detector.HysteresisDb != cfg.Detector.HysteresisDb {
  115. t.Fatalf("hysteresis_db changed on error")
  116. }
  117. }
  118. {
  119. mgr := New(cfg)
  120. badStable := 0
  121. if _, err := mgr.ApplyConfig(ConfigUpdate{Detector: &DetectorUpdate{MinStableFrames: &badStable}}); err == nil {
  122. t.Fatalf("expected min_stable_frames error")
  123. }
  124. if mgr.Snapshot().Detector.MinStableFrames != cfg.Detector.MinStableFrames {
  125. t.Fatalf("min_stable_frames changed on error")
  126. }
  127. }
  128. {
  129. mgr := New(cfg)
  130. badGap := -10
  131. if _, err := mgr.ApplyConfig(ConfigUpdate{Detector: &DetectorUpdate{GapToleranceMs: &badGap}}); err == nil {
  132. t.Fatalf("expected gap_tolerance_ms error")
  133. }
  134. if mgr.Snapshot().Detector.GapToleranceMs != cfg.Detector.GapToleranceMs {
  135. t.Fatalf("gap_tolerance_ms changed on error")
  136. }
  137. }
  138. }
  139. func TestApplySettings(t *testing.T) {
  140. cfg := config.Default()
  141. mgr := New(cfg)
  142. agc := true
  143. dc := true
  144. iq := true
  145. updated, err := mgr.ApplySettings(SettingsUpdate{
  146. AGC: &agc,
  147. DCBlock: &dc,
  148. IQBalance: &iq,
  149. })
  150. if err != nil {
  151. t.Fatalf("apply settings: %v", err)
  152. }
  153. if !updated.AGC || !updated.DCBlock || !updated.IQBalance {
  154. t.Fatalf("settings not applied: %+v", updated)
  155. }
  156. }