summaryrefslogtreecommitdiff
path: root/tests/unit/jtty/test_jtty_structured_decode.f90
blob: 56f9256a96556cea11c25a6a36981e4e9c4bac0c (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
program test_jtty_structured_decode

  use iso_c_binding, only: c_int,c_char,c_null_char,c_sizeof
  use iso_fortran_env, only: int16,int32,real32
  use jtty_fec, only: is13,PAYLOAD_BITS,TOTAL_K,tbcc_encode
  use jtty_tbcc_code_profiles
  use jtty_tbcc_decoder, only: jtty_tbcc_decode
  use jtty_mdec, only: npending,pending_updates,nactive,discard_pending_updates,display_message_text
  use jtty_mod, only: jtty_source_atom,jtty_source_atom_c,jtty_call_atom, &
       jtty_exch_num_atom,unpack_jtty,MAX_FRAMES,JTTY_CALL_CALL,JTTY_ROLE_FULL, &
       JTTY_NUM_SERIAL,JTTY_ATOM_CALL,JTTY_ATOM_EXCH_NUM,JTTY_ATOM_EXCH_LOC, &
       JTTY_ATOM_EXCH_PAIR, &
       JTTY_ATOM_CONTROL,JTTY_ATOM_GRID4,JTTY_LOC_STATE_PROVINCE, &
       JTTY_PAIR_CLASS_SECTION,JTTY_CONTROL_QSL_TU,JTTY_ROLE_FIELD_ONLY, &
       JTTY_ENCODE_OK,JTTY_ENCODE_INVALID_DESCRIPTOR, &
       JTTY_ENCODE_UNKNOWN_SECTION
  implicit none

  integer, parameter :: nsps=384
  integer, parameter :: frame_symbols=size(is13)+TOTAL_K
  integer :: failures

  interface
     function jtty_cpp_n1mm_smoke(tones,nsym) result(status) bind(C)
       import :: c_int
       integer(c_int), intent(out) :: tones(*),nsym
       integer(c_int) :: status
     end function jtty_cpp_n1mm_smoke
     function jtty_cpp_cq_smoke(tones,nsym,text) result(status) bind(C)
       import :: c_int,c_char
       integer(c_int), intent(out) :: tones(*),nsym
       character(kind=c_char), intent(out) :: text(*)
       integer(c_int) :: status
     end function jtty_cpp_cq_smoke
     function jtty_cpp_rtty_smoke(tones,nsym) result(status) bind(C)
       import :: c_int
       integer(c_int), intent(out) :: tones(*),nsym
       integer(c_int) :: status
     end function jtty_cpp_rtty_smoke
     function jtty_cpp_rtty_line_refused() result(refused) bind(C)
       import :: c_int
       integer(c_int) :: refused
     end function jtty_cpp_rtty_line_refused
     function jtty_cpp_rtty_line_tones(tones,nsym) result(planned) bind(C)
       import :: c_int
       integer(c_int), intent(out) :: tones(*),nsym
       integer(c_int) :: planned
     end function jtty_cpp_rtty_line_tones
     subroutine genjtty_atoms(atoms,natoms,itone,nsym)
       use jtty_mod, only: jtty_source_atom
       type(jtty_source_atom), intent(in) :: atoms(:)
       integer, intent(in) :: natoms
       integer, intent(out) :: itone(*)
       integer, intent(out) :: nsym
     end subroutine genjtty_atoms
     subroutine genjtty_atoms_c(atoms,natoms,itone,nsym,status) bind(C,name='genjtty_atoms_c')
       import :: c_int,jtty_source_atom_c
       type(jtty_source_atom_c), intent(in) :: atoms(*)
       integer(c_int), value, intent(in) :: natoms
       integer(c_int), intent(out) :: itone(*)
       integer(c_int), intent(out) :: nsym
       integer(c_int), intent(out) :: status
     end subroutine genjtty_atoms_c
  end interface

  failures=0
  call reject_reserved_struct_family(failures)
  call decode_native_call_and_serial(failures)
  call decode_c_adapter_atoms(failures)
  call reject_invalid_c_descriptors(failures)
  call reject_invalid_c_batch(failures)
  call decode_cpp_compiled_n1mm(failures)
  call decode_compact_text(failures)
  call decode_profile_text(failures)
  call expect(jtty_cpp_rtty_line_refused().eq.1, &
       'an RTTY line whose serial does not fit is refused, not split',failures)
  call decode_accepted_rtty_line(failures)

  if(failures.ne.0) then
     write(*,'(a,i0)') 'test_jtty_structured_decode: failures=',failures
     stop 1
  endif
  write(*,'(a)') 'test_jtty_structured_decode: all checks passed'

contains

  subroutine decode_profile_text(count)
    integer, intent(inout) :: count
    integer(c_int) :: tones(MAX_FRAMES*frame_symbols),nsymbols,status

    status=jtty_cpp_rtty_smoke(tones,nsymbols)
    call expect(status.eq.JTTY_ENCODE_OK .and. nsymbols.eq.frame_symbols, &
         'C++ RTTY profile text matches native F8 while unknown text uses two frames',count)
    if(status.ne.JTTY_ENCODE_OK .or. nsymbols.ne.frame_symbols) return
    call expect_noiseless_message(tones,int(nsymbols),'599 123','RTTY serial text',count)
  end subroutine decode_profile_text

  subroutine decode_compact_text(count)
    integer, intent(inout) :: count
    integer(c_int) :: native_tones(MAX_FRAMES*frame_symbols),native_nsymbols,status
    character(kind=c_char) :: c_text(80)
    character(len=80) :: text
    integer :: tones(MAX_FRAMES*frame_symbols),nsymbols,i

    status=jtty_cpp_cq_smoke(native_tones,native_nsymbols,c_text)
    call expect(status.eq.JTTY_ENCODE_OK .and. native_nsymbols.eq.frame_symbols, &
         'native F1 generates one frame',count)
    if(status.ne.JTTY_ENCODE_OK .or. native_nsymbols.ne.frame_symbols) return
    do i=1,len(text)
       text(i:i)=c_text(i)
    enddo
    call genjtty(text,tones,nsymbols)
    call expect(nsymbols.eq.native_nsymbols,'GUI CQ text generates one frame',count)
    if(nsymbols.ne.native_nsymbols) return
    call expect(all(tones(1:nsymbols).eq.native_tones(1:nsymbols)), &
         'GUI CQ text and native F1 generate identical channel symbols',count)
    call expect_noiseless_message(native_tones,int(native_nsymbols),'CQ K1ABC CQ','compact CQ',count)

    text='TEST WB9XYZ 599 123 QSL TU'
    call genjtty(text,tones,nsymbols)
    call expect(nsymbols.eq.4*frame_symbols,'mixed text generates four frames',count)
    if(nsymbols.ne.4*frame_symbols) return
    call decode_waveform(tones,nsymbols,10.0)
    call expect(npending.eq.1,'mixed text produces one update on the transmitted channel',count)
    if(npending.eq.1) then
       call expect(trim(normalized(pending_updates(1)%decoded)).eq.trim(text) .and. &
            pending_updates(1)%complete .and. nactive.eq.0, &
            'mixed TEXT5/call/exchange/control preserves spacing and EOM',count)
    endif
  end subroutine decode_compact_text

  subroutine decode_cpp_compiled_n1mm(count)
    integer, intent(inout) :: count
    integer(c_int) :: tones(MAX_FRAMES*frame_symbols),nsymbols,status

    status=jtty_cpp_n1mm_smoke(tones,nsymbols)
    call expect(status.eq.JTTY_ENCODE_OK .and. nsymbols.eq.2*frame_symbols, &
         'C++ compiled descriptors cross the native encoder ABI',count)
    if(status.ne.JTTY_ENCODE_OK .or. nsymbols.ne.2*frame_symbols) return
    call decode_waveform(tones,int(nsymbols))
    call expect(npending.eq.1,'C++ compiled N1MM message produces one update',count)
    if(npending.ne.1) return
    call expect(trim(normalized(pending_updates(1)%decoded)).eq.'W9XYZ 32D EMA' .and. &
         pending_updates(1)%complete .and. nactive.eq.0, &
         'C++ call/exchange descriptors preserve canonical text and EOM',count)
  end subroutine decode_cpp_compiled_n1mm

  subroutine decode_native_call_and_serial(count)
    integer, intent(inout) :: count
    type(jtty_source_atom) :: atoms(2)
    integer :: tones(MAX_FRAMES*frame_symbols),nsymbols

    atoms(1)=jtty_call_atom(JTTY_CALL_CALL,'WB9XYZ')
    atoms(2)=jtty_exch_num_atom(JTTY_ROLE_FULL,JTTY_NUM_SERIAL,1234)
    call genjtty_atoms(atoms,size(atoms),tones,nsymbols)

    call expect(nsymbols.eq.size(atoms)*frame_symbols, &
         'native call plus serial generates two frames',count)
    if(nsymbols.le.0) return

    call expect_noiseless_message(tones,nsymbols,'WB9XYZ 599 1234','native call plus serial',count)
  end subroutine decode_native_call_and_serial

  subroutine decode_c_adapter_atoms(count)
    integer, intent(inout) :: count
    type(jtty_source_atom_c) :: atoms(7)
    integer(c_int) :: tones(MAX_FRAMES*frame_symbols),nsymbols,status

    call expect(c_sizeof(atoms(1)).eq.20,'C atom descriptor remains 20 bytes',count)
    call initialize_c_atom(atoms(1),JTTY_ATOM_CALL,JTTY_CALL_CALL,0,0,'K1ABC')
    call initialize_c_atom(atoms(2),JTTY_ATOM_EXCH_NUM,JTTY_NUM_SERIAL, &
         JTTY_ROLE_FULL,12,'')
    call initialize_c_atom(atoms(3),JTTY_ATOM_EXCH_LOC,JTTY_LOC_STATE_PROVINCE, &
         JTTY_ROLE_FULL,0,'CA')
    call initialize_c_atom(atoms(4),JTTY_ATOM_EXCH_PAIR,JTTY_PAIR_CLASS_SECTION, &
         3,1,'EMA')
    call initialize_c_atom(atoms(5),JTTY_ATOM_EXCH_PAIR,JTTY_PAIR_CLASS_SECTION,0,10,'CT')
    atoms(5)%text(3)=' '
    call initialize_c_atom(atoms(6),JTTY_ATOM_GRID4,0,JTTY_ROLE_FIELD_ONLY,0,'FN42')
    call initialize_c_atom(atoms(7),JTTY_ATOM_CONTROL,JTTY_CONTROL_QSL_TU,0,0,'')
    call genjtty_atoms_c(atoms,size(atoms),tones,nsymbols,status)
    call expect(nsymbols.eq.size(atoms)*frame_symbols, &
         'C adapter generates serial/state/FD/grid/control frames',count)
    call expect(status.eq.JTTY_ENCODE_OK,'C adapter reports successful encoding',count)
    if(nsymbols.le.0) return

    call expect_noiseless_message(tones,int(nsymbols), &
         'K1ABC 599 012 599 CA 1D EMA 10A CT FN42 QSL TU','C adapter atoms',count)
  end subroutine decode_c_adapter_atoms

  subroutine reject_invalid_c_descriptors(count)
    integer, intent(inout) :: count
    type(jtty_source_atom_c) :: atom(1)
    character(len=4), parameter :: invalid_sections(*)=['ZZZ ','EMAX','CT X']
    integer :: i
    integer(c_int) :: tones(frame_symbols),nsymbols,status

    call initialize_c_atom(atom(1),JTTY_ATOM_EXCH_LOC,JTTY_LOC_STATE_PROVINCE,1,0,'CA')
    atom(1)%reserved=1
    call genjtty_atoms_c(atom,1,tones,nsymbols,status)
    call expect(nsymbols.eq.0,'C adapter rejects nonzero reserved byte',count)
    call expect(status.eq.JTTY_ENCODE_INVALID_DESCRIPTOR, &
         'C adapter identifies invalid descriptor',count)

    call initialize_c_atom(atom(1),JTTY_ATOM_CALL,JTTY_CALL_CALL,0,0,'K1ABC')
    atom(1)%text='A'
    call genjtty_atoms_c(atom,1,tones,nsymbols,status)
    call expect(nsymbols.eq.0,'C adapter rejects unterminated text',count)

    do i=1,size(invalid_sections)
       call initialize_c_atom(atom(1),JTTY_ATOM_EXCH_PAIR,JTTY_PAIR_CLASS_SECTION,0,1,invalid_sections(i))
       call genjtty_atoms_c(atom,1,tones,nsymbols,status)
       call expect(nsymbols.eq.0,'C adapter rejects unknown Field Day section '//trim(invalid_sections(i)),count)
       call expect(status.eq.JTTY_ENCODE_UNKNOWN_SECTION, &
            'C adapter identifies unknown Field Day section '//trim(invalid_sections(i)),count)
    enddo

    call initialize_c_atom(atom(1),JTTY_ATOM_EXCH_PAIR,0,0,1,'EMA')
    call genjtty_atoms_c(atom,1,tones,nsymbols,status)
    call expect(nsymbols.eq.0,'C adapter rejects non-CLASS_SECTION pair schema',count)

    call initialize_c_atom(atom(1),JTTY_ATOM_GRID4,0,0,0,'SA00')
    call genjtty_atoms_c(atom,1,tones,nsymbols,status)
    call expect(nsymbols.eq.0,'C adapter rejects invalid GRID4',count)

    call initialize_c_atom(atom(1),JTTY_ATOM_CONTROL,18,0,0,'')
    call genjtty_atoms_c(atom,1,tones,nsymbols,status)
    call expect(nsymbols.eq.0,'C adapter rejects unassigned control phrase',count)
  end subroutine reject_invalid_c_descriptors

  subroutine reject_invalid_c_batch(count)
    integer, intent(inout) :: count
    type(jtty_source_atom_c) :: atoms(3)
    integer(c_int) :: tones(3*frame_symbols),nsymbols,status

    call initialize_c_atom(atoms(1),JTTY_ATOM_CALL,JTTY_CALL_CALL,0,0,'K1ABC')
    call initialize_c_atom(atoms(2),JTTY_ATOM_EXCH_PAIR,JTTY_PAIR_CLASS_SECTION,0,1,'EMAX')
    call initialize_c_atom(atoms(3),JTTY_ATOM_CONTROL,JTTY_CONTROL_QSL_TU,0,0,'')
    call genjtty_atoms_c(atoms,size(atoms),tones,nsymbols,status)
    call expect(nsymbols.eq.0 .and. status.eq.JTTY_ENCODE_UNKNOWN_SECTION, &
         'invalid section rejects the whole descriptor batch',count)

    call initialize_c_atom(atoms(2),JTTY_ATOM_EXCH_NUM,JTTY_NUM_SERIAL,JTTY_ROLE_FULL,131072,'')
    call genjtty_atoms_c(atoms,size(atoms),tones,nsymbols,status)
    call expect(nsymbols.eq.0 .and. status.eq.JTTY_ENCODE_INVALID_DESCRIPTOR, &
         'invalid source atom rejects the whole descriptor batch',count)

    call initialize_c_atom(atoms(2),JTTY_ATOM_EXCH_PAIR,JTTY_PAIR_CLASS_SECTION,0,1,'EMA')
    call genjtty_atoms_c(atoms,size(atoms),tones,nsymbols,status)
    call expect(nsymbols.eq.size(atoms)*frame_symbols .and. status.eq.JTTY_ENCODE_OK, &
         'valid descriptor batch encodes after a rejected batch',count)
  end subroutine reject_invalid_c_batch

  subroutine initialize_c_atom(atom,kind,subtype,role,value,text)
    type(jtty_source_atom_c), intent(out) :: atom
    integer, intent(in) :: kind,subtype,role,value
    character(len=*), intent(in) :: text
    integer :: i,n

    atom%kind=kind; atom%subtype=subtype; atom%role=role; atom%reserved=0
    atom%value=value; atom%text=c_null_char
    n=min(8,len_trim(text))
    do i=1,n
       atom%text(i)=text(i:i)
    enddo
  end subroutine initialize_c_atom

  subroutine reject_reserved_struct_family(count)
    integer, intent(inout) :: count
    character(len=34) :: frame,frames(MAX_FRAMES)
    character(len=80) :: decoded
    integer(int32) :: payload(PAYLOAD_BITS),encoded(TOTAL_K)
    integer :: tones(frame_symbols)
    logical :: source_valid,is_last_frame

    ! With an otherwise-zero STRUCT30 body, n32=22 selects reserved family 101.
    write(frame(1:32),'(b32.32)') 22_int32
    frame(33:34)='01'
    frames=''
    frames(1)=frame
    call unpack_jtty(frames,1,decoded,is_last_frame=is_last_frame, &
         source_valid=source_valid)
    call expect(.not.source_valid, &
         'reserved STRUCT30 family is source-invalid',count)
    call expect(.not.is_last_frame, &
         'source-invalid frame cannot report EOM',count)

    read(frame,'(34i1)') payload
    call tbcc_encode(payload,encoded,JTTY_TBCC_PROFILE_1167_1545_80F)
    tones(1:size(is13))=is13
    tones(size(is13)+1:frame_symbols)=encoded
    call decode_waveform(tones,frame_symbols)
    call expect(npending.eq.0 .and. nactive.eq.0, &
         'CRC/FEC-valid source-invalid frame creates no message or update',count)
  end subroutine reject_reserved_struct_family

  subroutine expect_noiseless_message(tones,nsymbols,expected,description,count)
    integer, intent(in) :: tones(:),nsymbols
    character(len=*), intent(in) :: expected,description
    integer, intent(inout) :: count
    complex(real32) :: correlations(0:3,TOTAL_K),halves(0:3,TOTAL_K)
    integer(int32) :: payload(PAYLOAD_BITS)
    character(len=34) :: frames(MAX_FRAMES)
    character(len=80) :: decoded
    integer :: iframe,nframes,offset,symbol
    logical :: success,source_valid,is_last_frame

    nframes=nsymbols/frame_symbols
    call expect(nframes.ge.1 .and. nframes.le.MAX_FRAMES .and. &
         nsymbols.eq.nframes*frame_symbols,description//': complete channel frames',count)
    if(nframes.lt.1 .or. nframes.gt.MAX_FRAMES .or. nsymbols.ne.nframes*frame_symbols) return
    frames=''
    halves=cmplx(0.0_real32,0.0_real32,real32)
    do iframe=1,nframes
       offset=(iframe-1)*frame_symbols
       call expect(all(tones(offset+1:offset+size(is13)).eq.is13),description//': sync symbols',count)
       offset=offset+size(is13)
       call expect(all(tones(offset+1:offset+TOTAL_K).ge.0) .and. &
            all(tones(offset+1:offset+TOTAL_K).le.3),description//': payload tones',count)
       if(any(tones(offset+1:offset+TOTAL_K).lt.0) .or. any(tones(offset+1:offset+TOTAL_K).gt.3)) return
       correlations=cmplx(0.0_real32,0.0_real32,real32)
       do symbol=1,TOTAL_K
          correlations(tones(offset+symbol),symbol)=cmplx(100.0_real32,0.0_real32,real32)
       enddo
       call jtty_tbcc_decode(correlations,halves,payload,success)
       call expect(success,description//': TBCC payload recovery',count)
       if(.not.success) return
       write(frames(iframe),'(34i1)') payload
       call unpack_jtty(frames,iframe,decoded,is_last_frame=is_last_frame,source_valid=source_valid)
       call expect(source_valid,description//': source-valid prefix',count)
       call expect(is_last_frame.eqv.(iframe.eq.nframes),description//': final-frame marker',count)
    enddo
    call expect(trim(normalized(decoded)).eq.expected,description//': canonical text and ordering',count)
  end subroutine expect_noiseless_message

  subroutine decode_waveform(tones,nsymbols,channel_width)
    integer, intent(in) :: tones(:),nsymbols
    real, intent(in), optional :: channel_width
    integer :: nsamples,total_samples,nfa,nfb
    real :: tolerance
    integer(int16), allocatable :: pcm(:)
    real, allocatable :: wave(:)
    complex, allocatable :: complex_wave(:)

    nsamples=nsymbols*nsps
    total_samples=(nsymbols+frame_symbols)*nsps
    allocate(wave(nsamples),complex_wave(nsamples),pcm(total_samples))
    call gen_jttywave(tones,nsymbols,nsps,2.0,12000.0,1500.0, &
         complex_wave,wave,0,nsamples)
    pcm=0_int16
    pcm(1:nsamples)=int(nint(30000.0*wave),int16)

    nfa=200; nfb=2800; tolerance=50.0
    if(present(channel_width)) then
       tolerance=channel_width
       nfa=ceiling(1500.0-tolerance)
       nfb=floor(1500.0+tolerance)
    endif
    call discard_pending_updates()
    call rjtty_sub(pcm,1,nsps,nfa,nfb,1500.0,tolerance)
    call rjtty_sub(pcm,total_samples,nsps,nfa,nfb,1500.0,tolerance)

    deallocate(pcm,wave,complex_wave)
  end subroutine decode_waveform

  subroutine decode_accepted_rtty_line(count)
    integer, intent(inout) :: count
    integer(c_int) :: tones(MAX_FRAMES*frame_symbols),nsymbols
    character(len=80) :: expected

    expected=repeat('A',72)//' 599 005'
    call expect(jtty_cpp_rtty_line_tones(tones,nsymbols).eq.1, &
         'the longest RTTY line that fits is one segment',count)
    if(nsymbols.le.0) return
    call decode_waveform(tones,int(nsymbols))
    call expect(npending.ge.1,'the accepted RTTY line produces updates',count)
    if(npending.lt.1) return
    call expect(trim(normalized(pending_updates(npending)%decoded)).eq.trim(expected) .and. &
         pending_updates(npending)%complete .and. nactive.eq.0, &
         'the accepted RTTY line arrives whole, serial included',count)
  end subroutine decode_accepted_rtty_line

  function normalized(value) result(result_value)
    character(len=*), intent(in) :: value
    character(len=80) :: result_value
    result_value=display_message_text(value)
  end function normalized

  subroutine expect(condition,description,count)
    logical, intent(in) :: condition
    character(len=*), intent(in) :: description
    integer, intent(inout) :: count

    if(.not.condition) then
       count=count+1
       write(*,'(a)') 'FAIL: '//description
    endif
  end subroutine expect

end program test_jtty_structured_decode