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
|
! Phase 9 parse-unit regression driver. Phase 9 unpacks two bit-packed legacy ints
! into 9 wire keys; parse_control_frame folds them BACK into the derived members
! cfg%ndepth_packed / cfg%nexp_decode_packed (the post-pass pack_phase9_), so this
! driver is the authoritative guard for the PACKING (each key -> its bit), the
! contest_type -> ncontest table (incl. the SpecOp 5/8/9 -> 1 collapse), the two
! atomic-decline errors (legacy depth co-present with any unpacked ndepth key; a
! depth_level vs q65_maxiters_level inconsistency), the bit-width MASKING of
! depth_level/q65_maxiters_level, and the noise_blanker -3 offset + [0,255] clamp.
!
! WHY a Fortran unit driver (not only fixtures through jt9): only depth_level is
! decode-observable on FT8/JT9 (Gate 24); the other 8 keys are inert (Q65/JT65/
! FST4/JT4 or contest-branch only), and the bit-PACKING of even
! depth_level is invisible to a decode-count gate (ndepth=3 and ndepth=3|16 both
! decode 21 on FT8). This driver + run-apply-routing-unit.sh (the packed-int APPLY
! routing) + no-regression are the coverage for the 8 inert keys, exactly like the
! Phase 4/6/7 inert fields. Compiled + run by run-phase9-parse-unit.sh.
program p9_parse_test
use streaming_control, only: parse_control_frame, configure_fields, &
control_type_error, CTRL_CONFIGURE
implicit none
integer :: nfail
nfail = 0
call frame_A()
call frame_B()
call frame_C()
call frame_D()
call frame_E()
call frame_F()
call frame_G()
call frame_H()
call frame_I()
call frame_J()
call frame_K()
call frame_L()
call frame_M()
write(*,'(a)') '------------------------------------------------------------'
if (nfail .eq. 0) then
write(*,'(a)') 'ALL PHASE 9 PARSE-UNIT CHECKS PASSED'
else
write(*,'(a,i0,a)') 'PHASE 9 PARSE-UNIT: ', nfail, ' CHECK(S) FAILED'
call exit(1)
end if
contains
subroutine ok(label, cond)
character(len=*), intent(in) :: label
logical, intent(in) :: cond
if (cond) then
write(*,'(a,a)') ' PASS ', label
else
write(*,'(a,a)') ' FAIL ', label
nfail = nfail + 1
end if
end subroutine ok
! Frame A: ndepth bit-pack. depth_level=2 (bits0-2), use_averaging (0x10),
! deep_ap_search (0x20), q65_auto_clear_average (0x80) -> ndepth = 2|16|32|128 =
! 178. Asserts each bit lands (a dropped/mis-shifted bit changes the total).
subroutine frame_A()
type(configure_fields) :: cfg
type(control_type_error) :: terr
integer :: action
character(len=*), parameter :: b = &
'{"t":"configure","depth_level":2,"use_averaging":true,' // &
'"deep_ap_search":true,"q65_auto_clear_average":true}'
write(*,'(a)') 'Frame A: ndepth bit-pack (2 | 0x10 | 0x20 | 0x80 = 178)'
call parse_control_frame(b, action, cfg, terr)
call ok('action == CTRL_CONFIGURE', action .eq. CTRL_CONFIGURE)
call ok('terr not present', .not. terr%present)
call ok('ndepth_packed set', cfg%ndepth_packed_set)
call ok('ndepth_packed == 178', cfg%ndepth_packed .eq. 178)
! Individual bits, so a single dropped key surfaces:
call ok('bit 0-2 (depth_level=2)', iand(cfg%ndepth_packed, 7) .eq. 2)
call ok('bit 4 (use_averaging)', iand(cfg%ndepth_packed, 16) .eq. 16)
call ok('bit 5 (deep_ap_search)', iand(cfg%ndepth_packed, 32) .eq. 32)
call ok('bit 7 (q65_auto_clear_avg)', iand(cfg%ndepth_packed, 128).eq. 128)
call ok('bit 3 unused (clear)', iand(cfg%ndepth_packed, 8) .eq. 0)
call ok('bit 6 unused (clear)', iand(cfg%ndepth_packed, 64) .eq. 0)
call ok('nexp_decode NOT packed (no nexp key)', .not. cfg%nexp_decode_packed_set)
end subroutine frame_A
! Frame B: contest_type -> ncontest table (incl. SpecOp/collapse).
! Each string parsed in its own frame. The 5/8/9 -> 1 collapse is the key risk.
subroutine frame_B()
write(*,'(a)') 'Frame B: contest_type -> ncontest table (incl. SpecOp 5/8/9 collapse)'
call con_ok('none -> 0', 'none', 0)
call con_ok('na_vhf -> 1', 'na_vhf', 1)
call con_ok('eu_vhf -> 2', 'eu_vhf', 2)
call con_ok('field_day -> 3', 'field_day', 3)
call con_ok('rtty -> 4', 'rtty', 4)
call con_ok('ww_digi -> 1 (SpecOp 5 collapsed)', 'ww_digi', 1)
call con_ok('fox -> 6', 'fox', 6)
call con_ok('hound -> 7', 'hound', 7)
call con_ok('arrl_digi -> 1 (SpecOp 8 collapsed)', 'arrl_digi', 1)
call con_ok('q65_pileup -> 1 (SpecOp 9 collapsed)', 'q65_pileup', 1)
end subroutine frame_B
! Helper: parse {"t":"configure","contest_type":"<s>"} and assert the collapsed
! ncontest lands in nexp_decode_packed bits 0-2 (and contest_type member).
subroutine con_ok(label, s, want)
character(len=*), intent(in) :: label, s
integer, intent(in) :: want
type(configure_fields) :: cfg
type(control_type_error) :: terr
integer :: action
character(len=80) :: b
b = '{"t":"configure","contest_type":"' // trim(s) // '"}'
call parse_control_frame(trim(b), action, cfg, terr)
call ok(label, (.not. terr%present) .and. cfg%contest_type_set .and. &
cfg%contest_type .eq. want .and. cfg%nexp_decode_packed_set .and. &
iand(cfg%nexp_decode_packed, 7) .eq. want)
end subroutine con_ok
! Frame C: nexp_decode full pack. contest_type=fox (6) | single_decode (0x20) |
! vhf_features (0x40) | noise_blanker_level=2 ((2+3)<<8 = 1280) -> 6+32+64+1280 =
! 1382. Asserts each sub-field lands in its slot.
subroutine frame_C()
type(configure_fields) :: cfg
type(control_type_error) :: terr
integer :: action
character(len=*), parameter :: b = &
'{"t":"configure","contest_type":"fox","single_decode":true,' // &
'"vhf_features":true,"noise_blanker_level":2}'
write(*,'(a)') 'Frame C: nexp_decode full pack (6 | 0x20 | 0x40 | (2+3)<<8 = 1382)'
call parse_control_frame(b, action, cfg, terr)
call ok('terr not present', .not. terr%present)
call ok('nexp_decode_packed == 1382', cfg%nexp_decode_packed .eq. 1382)
call ok('bits 0-2 (ncontest fox=6)', iand(cfg%nexp_decode_packed, 7) .eq. 6)
call ok('bit 5 (single_decode)', iand(cfg%nexp_decode_packed, 32) .eq. 32)
call ok('bit 6 (vhf_features)', iand(cfg%nexp_decode_packed, 64) .eq. 64)
call ok('upper byte (nb+3 = 5)', cfg%nexp_decode_packed / 256 .eq. 5)
call ok('ndepth NOT packed (no ndepth key)', .not. cfg%ndepth_packed_set)
end subroutine frame_C
! Frame D: depth_level vs q65_maxiters_level consistency (bits 0-1
! overlap). (a) consistent pair (depth_level=6 low2=2, q65_maxiters=2) -> OK,
! ndepth bits0-2 = 6. (b) inconsistent pair -> configure_type_error.
subroutine frame_D()
type(configure_fields) :: cfg
type(control_type_error) :: terr
integer :: action
character(len=*), parameter :: ba = &
'{"t":"configure","depth_level":6,"q65_maxiters_level":2}'
character(len=*), parameter :: bb = &
'{"t":"configure","depth_level":6,"q65_maxiters_level":1}'
write(*,'(a)') 'Frame D: depth_level/q65_maxiters_level consistency (bits 0-1 overlap)'
call parse_control_frame(ba, action, cfg, terr)
call ok('(a) consistent (6 low2 == 2): no error', .not. terr%present)
call ok('(a) ndepth_packed == 6', cfg%ndepth_packed .eq. 6)
call parse_control_frame(bb, action, cfg, terr)
call ok('(b) inconsistent (6 low2=2 != 1): terr present', terr%present)
call ok('(b) key == q65_maxiters_level', trim(terr%key) .eq. 'q65_maxiters_level')
call ok('(b) expected == match depth_lvl', trim(terr%expected) .eq. 'match depth_lvl')
call ok('(b) got == conflict', trim(terr%got) .eq. 'conflict')
call ok('(b) ndepth NOT packed (declined)', .not. cfg%ndepth_packed_set)
end subroutine frame_D
! Frame E: legacy depth co-present with an unpacked ndepth key -> conflict
! configure_type_error (the two encodings are mutually exclusive).
subroutine frame_E()
type(configure_fields) :: cfg
type(control_type_error) :: terr
integer :: action
character(len=*), parameter :: b = &
'{"t":"configure","depth":3,"depth_level":1}'
write(*,'(a)') 'Frame E: depth + depth_level conflict -> type error'
call parse_control_frame(b, action, cfg, terr)
call ok('terr present', terr%present)
call ok('key == ndepth', trim(terr%key) .eq. 'ndepth')
call ok('expected == depth|unpacked', trim(terr%expected) .eq. 'depth|unpacked')
call ok('got == conflict', trim(terr%got) .eq. 'conflict')
call ok('ndepth NOT packed (declined)', .not. cfg%ndepth_packed_set)
end subroutine frame_E
! Frame F: bit-width MASKING. An out-of-range depth_level=99 (0x63) must mask to
! its 3 bits (99 & 7 = 3) so it cannot corrupt sibling bits (4/5/7). Likewise a
! lone q65_maxiters_level=99 masks to 2 bits (99 & 3 = 3). No crash.
subroutine frame_F()
type(configure_fields) :: cfg
type(control_type_error) :: terr
integer :: action
character(len=*), parameter :: ba = '{"t":"configure","depth_level":99}'
character(len=*), parameter :: bb = '{"t":"configure","q65_maxiters_level":99}'
write(*,'(a)') 'Frame F: out-of-range depth_level/q65_maxiters masked to bit width'
call parse_control_frame(ba, action, cfg, terr)
call ok('(a) depth_level=99 -> ndepth = 3 (99 & 7)', cfg%ndepth_packed .eq. 3)
call ok('(a) no sibling bits set', iand(cfg%ndepth_packed, 240) .eq. 0)
call ok('(a) no type error', .not. terr%present)
call parse_control_frame(bb, action, cfg, terr)
call ok('(b) q65_maxiters=99 -> ndepth = 3 (99 & 3)', cfg%ndepth_packed .eq. 3)
end subroutine frame_F
! Frame G: q65_maxiters_level alone (no depth_level) -> derive depth_level's
! bits 0-1 from it (receiver-side derivation). q65_maxiters=3 -> ndepth=3.
subroutine frame_G()
type(configure_fields) :: cfg
type(control_type_error) :: terr
integer :: action
character(len=*), parameter :: b = &
'{"t":"configure","q65_maxiters_level":3,"use_averaging":true}'
write(*,'(a)') 'Frame G: q65_maxiters_level alone -> ndepth bits 0-1 derived'
call parse_control_frame(b, action, cfg, terr)
call ok('terr not present', .not. terr%present)
call ok('ndepth_packed == 3|16 = 19', cfg%ndepth_packed .eq. 19)
call ok('bits 0-1 = q65_maxiters 3', iand(cfg%ndepth_packed, 3) .eq. 3)
end subroutine frame_G
! Frame H: noise_blanker_level -3 offset + [0,255] clamp (upper byte).
! nb=0 -> (0+3)<<8 = 768; nb=-3 -> 0; nb=300 -> clamp 255 -> 255<<8 = 65280.
! Each still sets nexp_decode_packed_set (the key was present).
subroutine frame_H()
write(*,'(a)') 'Frame H: noise_blanker_level offset + clamp'
call nb_ok('nb=0 -> upper byte 3 (768)', 0, 768)
call nb_ok('nb=2 -> upper byte 5 (1280)', 2, 1280)
call nb_ok('nb=-3 -> upper byte 0 (clamp)', -3, 0)
call nb_ok('nb=300 -> upper byte 255 (clamp)', 300, 65280)
end subroutine frame_H
subroutine nb_ok(label, nb, want)
character(len=*), intent(in) :: label
integer, intent(in) :: nb, want
type(configure_fields) :: cfg
type(control_type_error) :: terr
integer :: action
character(len=16) :: ns
character(len=64) :: b
write(ns, '(i0)') nb
b = '{"t":"configure","noise_blanker_level":' // trim(ns) // '}'
call parse_control_frame(trim(b), action, cfg, terr)
call ok(label, (.not. terr%present) .and. cfg%nexp_decode_packed_set .and. &
cfg%nexp_decode_packed .eq. want)
end subroutine nb_ok
! Frame I: wrong-TYPE detection. depth_level as a string (int expected) and
! contest_type as a number (string expected) -> check_type_ rejects.
subroutine frame_I()
type(configure_fields) :: cfg
type(control_type_error) :: terr
integer :: action
character(len=*), parameter :: b1 = '{"t":"configure","depth_level":"deep"}'
character(len=*), parameter :: b2 = '{"t":"configure","contest_type":5}'
character(len=*), parameter :: b3 = '{"t":"configure","use_averaging":7}'
write(*,'(a)') 'Frame I: wrong-type Phase 9 keys -> type error'
call parse_control_frame(b1, action, cfg, terr)
call ok('depth_level string: terr present', terr%present)
call ok('depth_level string: key', trim(terr%key) .eq. 'depth_level')
call ok('depth_level string: expected', trim(terr%expected) .eq. 'int')
call ok('depth_level string: got', trim(terr%got) .eq. 'string')
call ok('depth_level string: ndepth NOT packed', .not. cfg%ndepth_packed_set)
call parse_control_frame(b2, action, cfg, terr)
call ok('contest_type number: terr present', terr%present)
call ok('contest_type number: key', trim(terr%key) .eq. 'contest_type')
call ok('contest_type number: expected', trim(terr%expected) .eq. 'string')
call parse_control_frame(b3, action, cfg, terr)
call ok('use_averaging number: terr present', terr%present)
call ok('use_averaging number: expected bool', trim(terr%expected) .eq. 'bool')
end subroutine frame_I
! Frame J: unknown contest_type string -> value-domain SILENT SKIP (left unset,
! NO type error — mirrors the unknown-mode-name skip). nexp not packed.
subroutine frame_J()
type(configure_fields) :: cfg
type(control_type_error) :: terr
integer :: action
character(len=*), parameter :: b = &
'{"t":"configure","contest_type":"sweepstakes","mode":"FT8"}'
write(*,'(a)') 'Frame J: unknown contest_type -> silent skip (no false decline)'
call parse_control_frame(b, action, cfg, terr)
call ok('terr not present', .not. terr%present)
call ok('contest_type stays UNSET', .not. cfg%contest_type_set)
call ok('nexp NOT packed', .not. cfg%nexp_decode_packed_set)
call ok('mode still parsed (FT8)', cfg%mode_set .and. cfg%mode .eq. 8)
end subroutine frame_J
! Frame K: value-vs-key aliasing. The mycall VALUE is the literal
! token "depth_level" (11 chars, fits mycall len=12) with no real depth_level
! key. The anchored locator must NOT mistake the value for the key.
subroutine frame_K()
type(configure_fields) :: cfg
type(control_type_error) :: terr
integer :: action
character(len=*), parameter :: b = '{"t":"configure","mycall":"depth_level"}'
write(*,'(a)') 'Frame K: mycall value == "depth_level" token (aliasing guard)'
call parse_control_frame(b, action, cfg, terr)
call ok('terr not present (no false decline)', .not. terr%present)
call ok('mycall == depth_level', cfg%mycall_set .and. trim(cfg%mycall) .eq. 'depth_level')
call ok('depth_level stays UNSET', .not. cfg%depth_level_set)
call ok('ndepth NOT packed', .not. cfg%ndepth_packed_set)
end subroutine frame_K
! Frame L: Phase 9 keys absent -> all unset, no error, no packing (keys optional).
subroutine frame_L()
type(configure_fields) :: cfg
type(control_type_error) :: terr
integer :: action
character(len=*), parameter :: b = '{"t":"configure","mode":"FT8","depth":3}'
write(*,'(a)') 'Frame L: Phase 9 keys absent -> unset (legacy depth alone OK)'
call parse_control_frame(b, action, cfg, terr)
call ok('terr not present', .not. terr%present)
call ok('depth (legacy) still set', cfg%depth_set .and. cfg%depth .eq. 3)
call ok('depth_level unset', .not. cfg%depth_level_set)
call ok('contest_type unset', .not. cfg%contest_type_set)
call ok('ndepth NOT packed', .not. cfg%ndepth_packed_set)
call ok('nexp_decode NOT packed', .not. cfg%nexp_decode_packed_set)
end subroutine frame_L
! Frame M: per-bool SINGLE-BIT isolation (closes the swap-blindness where
! two bool keys that only ever appear together-all-true can be
! swapped invisibly: Frame A sets all 3 ndepth bools true, Frame C both nexp
! bools true, so a deep_ap_search<->q65_auto_clear_average (0x20<->0x80) or a
! single_decode<->vhf_features (0x20<->0x40) swap leaves the total unchanged).
! Each bool set ALONE asserts EXACTLY its bit, so every bool->bit assignment is
! now independently load-bearing. (use_averaging is also disambiguated by Frame G,
! but assert it here too for symmetry.)
subroutine frame_M()
write(*,'(a)') 'Frame M: per-bool single-bit isolation (swap-blindness guard)'
call bit_ok_ndepth('use_averaging alone -> ndepth 0x10', 'use_averaging', 16)
call bit_ok_ndepth('deep_ap_search alone -> ndepth 0x20', 'deep_ap_search', 32)
call bit_ok_ndepth('q65_auto_clear_average alone -> ndepth 0x80', 'q65_auto_clear_average', 128)
call bit_ok_nexp('single_decode alone -> nexp 0x20', 'single_decode', 32)
call bit_ok_nexp('vhf_features alone -> nexp 0x40', 'vhf_features', 64)
end subroutine frame_M
! Helper: parse {"t":"configure","<key>":true} and assert ndepth_packed == want
! EXACTLY (so the bit isolated is the only one set; a mis-shift fails the equality).
subroutine bit_ok_ndepth(label, key, want)
character(len=*), intent(in) :: label, key
integer, intent(in) :: want
type(configure_fields) :: cfg
type(control_type_error) :: terr
integer :: action
character(len=80) :: b
b = '{"t":"configure","' // trim(key) // '":true}'
call parse_control_frame(trim(b), action, cfg, terr)
call ok(label, (.not. terr%present) .and. cfg%ndepth_packed_set .and. &
cfg%ndepth_packed .eq. want)
end subroutine bit_ok_ndepth
subroutine bit_ok_nexp(label, key, want)
character(len=*), intent(in) :: label, key
integer, intent(in) :: want
type(configure_fields) :: cfg
type(control_type_error) :: terr
integer :: action
character(len=80) :: b
b = '{"t":"configure","' // trim(key) // '":true}'
call parse_control_frame(trim(b), action, cfg, terr)
call ok(label, (.not. terr%present) .and. cfg%nexp_decode_packed_set .and. &
cfg%nexp_decode_packed .eq. want)
end subroutine bit_ok_nexp
end program p9_parse_test
|