Wideband autonomous SDR analysis engine forked from sdr-visual-suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 line
4.0KB

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