summaryrefslogtreecommitdiff
path: root/tests/unit/map65/decode_history/test_map65_decode_history.f90
blob: bd2eb9dcf9a88ce711b918fe11359c0d342343a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
program test_map65_decode_history
  use iso_fortran_env, only: real64
  use npar_ptrs_mod, only: nfft_active, nsmax_active, t_start, manualDecodeFlag, active_input_generation
  use datcom_ptrs_mod, only: ss_old, savg_old
  use decodes_mod, only: nhsym1, nhsym2
  use map65a_mod, only: map65a
  use message_history_mod, only: message_record, recent_messages
  use decode_history_observations
  implicit none
  real, allocatable :: samples(:,:)
  integer :: unit, before_jt65, before_q65
  integer :: messages, rx_log, before_display
  integer :: band_nfa = 0, band_nfb = 0, cursor_khz = 0, fits(3), test_nmode = 11
  integer :: click_bin
  real :: rec_khz
  character(len=128) :: rec
  character(len=*), parameter :: JT65_MSG = 'K1ABC W9XYZ FN42'

  ! Only the center bin has signal power; DSP stand-ins never read samples.
  nfft_active = 1024
  nsmax_active = 1
  allocate(samples(4,nsmax_active), ss_old(4,322,nfft_active), savg_old(4,nfft_active))
  samples = 0.0
  ss_old = 1.0
  savg_old = 1.0
  savg_old(1,nfft_active/2+1) = 10.0
  nhsym1 = 280
  nhsym2 = 302
  manualDecodeFlag = 0
  do unit = 12, 27
    open(unit, status='scratch')
  enddo
  close(23)

  active_input_generation = 1
  call run_pass(nhsym1, 0, 0)
  call require(jt65_attempts == 1 .and. q65_successes == 1, 'early pass decodes both signals')
  call run_pass(nhsym2, 0, 0)
  call require(jt65_attempts == 1 .and. q65_successes == 1, 'final pass retains early successes')

  jt65_attempts = 0
  q65_successes = 0
  call run_pass(nhsym2, 1, 1)
  call require(all(phase_attempts == 1), 'each JT65 trial remains eligible after an earlier success')
  call require(jt65_attempts == 13, 'JT65 runs once per trial, without replaying prior successes')
  call require(q65_successes == 1, 'unchanged Q65 input is decoded once across phase trials')
  call require(summary_count == 1 .and. fitted_phase == 90, 'later better trials determine the fitted phase')

  ! A second calibration must not inherit quality scores from the first.
  phase_has_signal = .false.
  phase_has_signal(6) = .true.
  phase_attempts = 0
  call run_pass(nhsym2, 1, 1)
  call require(all(phase_attempts == 1), 'unsuccessful trials remain eligible in a new calibration')
  call require(summary_count == 2 .and. abs(fitted_phase) == 180, 'missing trials do not retain old quality scores')

  before_jt65 = jt65_attempts
  before_q65 = q65_successes
  active_input_generation = 2
  call run_pass(nhsym1, 0, 0)
  call require(jt65_attempts == before_jt65+1 .and. q65_successes == before_q65+1, &
               'next receive period makes both frequencies eligible')
  active_input_generation = 3
  call run_pass(nhsym1, 0, 0)
  call require(jt65_attempts == before_jt65+2 .and. q65_successes == before_q65+2, &
               'next early pass resets history even without a preceding final pass')
  call run_pass(nhsym2, 0, 0)
  call require(jt65_attempts == before_jt65+2 .and. q65_successes == before_q65+2, &
               'final pass retains successes after a skipped-period boundary')

  ! A double-click decode reaches Messages/Band Map and map65_rx.log
  ! without repeating a decode already recorded by the wideband pass.
  messages = count_history_records(JT65_MSG)
  rx_log = count_records(21, JT65_MSG)
  call require(messages >= 1 .and. rx_log >= 1, 'wideband pass records the JT65 decode')
  before_jt65 = jt65_attempts
  before_display = display_calls
  call click()
  call require(jt65_attempts == before_jt65+1, 'click decodes JT65 at the cursor')
  call require(count_history_records(JT65_MSG) == messages .and. count_records(21, JT65_MSG) == rx_log, &
               'click does not repeat a decode the wideband pass recorded')
  call require(display_calls == before_display+1, 'click refreshes the Messages window')
  active_input_generation = 4
  call click()
  call require(count_history_records(JT65_MSG) == messages+1 .and. count_records(21, JT65_MSG) == rx_log+1, &
               'click records a decode the wideband pass did not')
  call click()
  call require(count_history_records(JT65_MSG) == messages+1 .and. count_records(21, JT65_MSG) == rx_log+1, &
               'second click on the same signal is not recorded again')

  ! A click on a JT65C RRR shorthand: its sync tone in the clicked bin and
  ! the upper tone 30*4 tone spacings (~3 bins) above, noise elsewhere. It
  ! must be found although the click's best JT65 sync bin is elsewhere, be
  ! labeled RRR, be recorded at the clicked kHz (band center (nfa+nfb)/2 =
  ! 130 kHz, cursor 130 kHz: 144.130 MHz), and carry no fit values left in
  ! sig by an earlier decode.
  stub_fit = 7.0
  active_input_generation = 5
  call run_pass(nhsym1, 0, 0)
  stub_fit = 0.0
  click_bin = nfft_active/2 + 1
  ss_old(2,1,:) = -99.0
  ss_old(2,1,click_bin) = 10.0
  ss_old(2,1,click_bin + nint(3*4*10.0*(11025.0/4096.0)/(96000.0/nfft_active))) = 10.0
  stub_short_from_ss = .true.
  test_nmode = 13
  band_nfa = 100
  band_nfb = 160
  cursor_khz = 130
  active_input_generation = 6
  call click()
  call require(last_history_record('RRR', rec), 'shorthand click is found and labeled RRR')
  read(rec(1:8), *) rec_khz
  read(rec(14:22), '(3i3)') fits
  call require(abs(rec_khz - 144.130) < 0.0005, 'shorthand click is recorded at the clicked kHz')
  call require(all(fits == 0), 'shorthand click records no stale fit values')
  print '(a)', 'MAP65 decode history tests passed.'

contains
  integer function count_history_records(text)
    character(len=*), intent(in) :: text
    type(message_record), allocatable :: records(:)
    integer :: i

    call recent_messages(huge(0), records)
    count_history_records = 0
    do i = 1, size(records)
      if (index(records(i)%line, text) > 0) count_history_records = count_history_records + 1
    enddo
  end function

  logical function last_history_record(text, found)
    character(len=*), intent(in) :: text
    character(len=*), intent(out) :: found
    type(message_record), allocatable :: records(:)
    integer :: i

    call recent_messages(huge(0), records)
    last_history_record = .false.
    found = ' '
    do i = 1, size(records)
      if (index(records(i)%line, text) == 0) cycle
      found = records(i)%line
      last_history_record = .true.
    enddo
  end function

  subroutine run_pass(half_symbols, again, find_phase)
    integer, intent(in) :: half_symbols, again, find_phase
    integer :: newdat, utc, done, ndphi, mcall3b, nsum, nsave
    newdat = 1
    utc = 1200 + int(active_input_generation)
    done = 0
    ndphi = find_phase
    mcall3b = 0
    nsum = 0
    nsave = 0
    call system_clock(t_start)
    call map65a(samples,newdat,utc,144.0_real64,100,0,band_nfa,band_nfb,0,cursor_khz, &
                again,done,0,ndphi,0, &
                -1270,20,mcall3b,nsum,nsave,0,'K1ABC       ','FN42  ',0,3,0, &
                'W9XYZ       ','EN50  ',half_symbols,96000,0,1,test_nmode,0)
  end subroutine

  subroutine click()
    ! MainWindow::freezeDecode() sets nagain=1 and the manual decode flag.
    manualDecodeFlag = 1
    call run_pass(nhsym2, 1, 0)
  end subroutine

  integer function count_records(u, text)
    integer, intent(in) :: u
    character(len=*), intent(in) :: text
    character(len=128) :: rec
    integer :: ios
    count_records = 0
    rewind(u)
    do
      read(u, '(a)', iostat=ios) rec
      if (ios /= 0) exit
      if (index(rec, text) > 0) count_records = count_records + 1
    enddo
    backspace(u)
  end function

  subroutine require(condition, description)
    logical, intent(in) :: condition
    character(len=*), intent(in) :: description
    if (.not. condition) then
      print '(a)', 'FAIL: '//description
      error stop 1
    endif
  end subroutine
end program