summaryrefslogtreecommitdiff
path: root/tests/unit/jtty/test_jtty_tbcc_list_decoder.f90
blob: 545e02214c25895a8e98282db485891bd6bbfe80 (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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
program test_jtty_tbcc_list_decoder
  use, intrinsic :: iso_fortran_env, only: int32, int64, real32, real64
  use jtty_tbcc_code_profiles, only: jtty_tbcc_code_profile, &
       JTTY_TBCC_PROFILE_1167_1545_80F
  use tbcc, only: tbcc_encode, encode_crc12
  use jtty_tbcc_list_decoder
  implicit none

  call expect_optimized_reference_parity()
  call expect_noiseless_tailbiting_decode()
  call expect_reserved_bit_pruning()
  call expect_candidate_contract()
  call expect_workspace_isolation()
  call expect_deterministic_hypothesis_budget()
  call expect_score_symmetry()
  call expect_coherent_normalization()

  print *, 'test_jtty_tbcc_list_decoder: all checks passed'

contains

  subroutine expect_optimized_reference_parity()
    integer(int32), parameter :: coherent_lengths(3) = [1_int32, 2_int32, 4_int32]
    integer(int32), parameter :: survivor_widths(2) = [1_int32, 4_int32]
    type(jtty_tbcc_decoder_plan) :: plan
    type(jtty_tbcc_decoder_workspace) :: workspace
    complex(real32) :: correlations(0:3, JTTY_TBCC_INFORMATION_BITS)
    integer(int32) :: coherent_index, width_index, symbol, tone, wraps

    call make_pattern_correlations(correlations)

    do wraps = 1, 2
      do coherent_index = 1, size(coherent_lengths)
        do width_index = 1, size(survivor_widths)
          call jtty_tbcc_init_decoder_plan(plan, JTTY_TBCC_PROFILE_1167_1545_80F, &
               coherent_lengths(coherent_index), survivor_widths(width_index), wraps, &
               JTTY_TBCC_MAX_HYPOTHESES)
          call jtty_tbcc_init_decoder_workspace(workspace, plan)
          call expect_decode_parity(JTTY_TBCC_PROFILE_1167_1545_80F, plan, workspace, correlations, &
               coherent_lengths(coherent_index), survivor_widths(width_index), wraps, .false.)
          call expect_decode_parity(JTTY_TBCC_PROFILE_1167_1545_80F, plan, workspace, correlations, &
               coherent_lengths(coherent_index), survivor_widths(width_index), wraps, .true.)
        end do
      end do
    end do

    ! Equal branch energies exercise deterministic ties and wrap deduplication.
    correlations = cmplx(0.0_real32, 0.0_real32, real32)
    do width_index = 1, 4
      call jtty_tbcc_init_decoder_plan(plan, JTTY_TBCC_PROFILE_1167_1545_80F, 4_int32, &
           width_index, 2_int32, JTTY_TBCC_MAX_HYPOTHESES)
      call jtty_tbcc_init_decoder_workspace(workspace, plan)
      call expect_decode_parity(JTTY_TBCC_PROFILE_1167_1545_80F, plan, workspace, correlations, &
           4_int32, width_index, 2_int32, .false.)
    end do

    ! A common large branch collapses distinct accumulated metrics into ties.
    do symbol = 1, JTTY_TBCC_INFORMATION_BITS
      do tone = 0, 3
        correlations(tone, symbol) = scale(1.0_real32, -18)*cmplx( &
             real(modulo(17*tone + 11*symbol, 29) - 14, real32)/8.0_real32, &
             real(modulo(7*tone + 19*symbol, 31) - 15, real32)/9.0_real32, real32)
      end do
    end do
    correlations(:, 13) = cmplx(scale(1.0_real32, 20), 0.0_real32, real32)
    do coherent_index = 1, size(coherent_lengths)
      call jtty_tbcc_init_decoder_plan(plan, JTTY_TBCC_PROFILE_1167_1545_80F, &
           coherent_lengths(coherent_index), 4_int32, 2_int32, JTTY_TBCC_MAX_HYPOTHESES)
      call jtty_tbcc_init_decoder_workspace(workspace, plan)
      call expect_decode_parity(JTTY_TBCC_PROFILE_1167_1545_80F, plan, workspace, correlations, &
           coherent_lengths(coherent_index), 4_int32, 2_int32, .false.)
    end do
  end subroutine expect_optimized_reference_parity

  subroutine expect_decode_parity(code, plan, workspace, correlations, &
       coherent_length, survivor_width, wraps, prune)
    type(jtty_tbcc_code_profile), intent(in) :: code
    type(jtty_tbcc_decoder_plan), intent(in) :: plan
    type(jtty_tbcc_decoder_workspace), intent(inout) :: workspace
    complex(real32), intent(in) :: correlations(0:3, JTTY_TBCC_INFORMATION_BITS)
    integer(int32), intent(in) :: coherent_length, survivor_width, wraps
    logical, intent(in) :: prune
    integer(int32) :: reference_bits(JTTY_TBCC_INFORMATION_BITS, JTTY_TBCC_MAX_HYPOTHESES)
    integer(int32) :: optimized_bits(JTTY_TBCC_INFORMATION_BITS, JTTY_TBCC_MAX_HYPOTHESES)
    integer(int32) :: reference_states(JTTY_TBCC_MAX_HYPOTHESES)
    integer(int32) :: optimized_states(JTTY_TBCC_MAX_HYPOTHESES)
    integer(int32) :: reference_count, optimized_count, reference_pool, optimized_pool
    integer(int64) :: reference_ids(JTTY_TBCC_MAX_HYPOTHESES)
    integer(int64) :: optimized_ids(JTTY_TBCC_MAX_HYPOTHESES)
    real(real64) :: reference_clean(JTTY_TBCC_MAX_HYPOTHESES)
    real(real64) :: optimized_clean(JTTY_TBCC_MAX_HYPOTHESES)
    real(real64) :: reference_wava(JTTY_TBCC_MAX_HYPOTHESES)
    real(real64) :: optimized_wava(JTTY_TBCC_MAX_HYPOTHESES)
    logical :: reference_crc(JTTY_TBCC_MAX_HYPOTHESES)
    logical :: optimized_crc(JTTY_TBCC_MAX_HYPOTHESES)
    integer(int32) :: tracked_bits(JTTY_TBCC_INFORMATION_BITS) = 0_int32
    integer(int32) :: reference_pool_rank, optimized_pool_rank, reference_h_rank, optimized_h_rank
    logical :: reference_present, optimized_present

    call jtty_tbcc_list_wava_reference(correlations, code, coherent_length, &
         survivor_width, wraps, JTTY_TBCC_MAX_HYPOTHESES, reference_bits, &
         reference_ids, reference_clean, reference_wava, reference_states, &
         reference_crc, reference_count, reference_pool, prune_reserved_zero=prune, &
         reference_bits=tracked_bits, reference_present=reference_present, &
         reference_pool_rank=reference_pool_rank, reference_h_rank=reference_h_rank)
    call jtty_tbcc_list_wava_optimized(plan, workspace, correlations, optimized_bits, &
         optimized_ids, optimized_clean, optimized_wava, optimized_states, &
         optimized_crc, optimized_count, optimized_pool, prune_reserved_zero=prune, &
         reference_bits=tracked_bits, reference_present=optimized_present, &
         reference_pool_rank=optimized_pool_rank, reference_h_rank=optimized_h_rank)

    if (reference_count /= optimized_count .or. reference_pool /= optimized_pool .or. &
         any(reference_bits /= optimized_bits) .or. any(reference_ids /= optimized_ids) .or. &
         .not.real_arrays_agree(reference_clean, optimized_clean) .or. &
         .not.real_arrays_agree(reference_wava, optimized_wava) .or. &
         any(reference_states /= optimized_states) .or. &
         any(reference_crc .neqv. optimized_crc) .or. &
         (reference_present .neqv. optimized_present) .or. &
         reference_pool_rank /= optimized_pool_rank .or. reference_h_rank /= optimized_h_rank) &
         error stop 'TBCC list decoder optimized/reference mismatch'
  end subroutine expect_decode_parity

  subroutine expect_noiseless_tailbiting_decode()
    type(jtty_tbcc_code_profile) :: code
    complex(real32) :: correlations(0:3, JTTY_TBCC_INFORMATION_BITS)
    integer(int32) :: information_bits(JTTY_TBCC_INFORMATION_BITS)
    integer(int32) :: tones(JTTY_TBCC_INFORMATION_BITS)
    integer(int32) :: candidates(JTTY_TBCC_INFORMATION_BITS, JTTY_TBCC_MAX_HYPOTHESES)
    integer(int32) :: states(JTTY_TBCC_MAX_HYPOTHESES), candidate_count, pool_count
    integer(int64) :: identities(JTTY_TBCC_MAX_HYPOTHESES)
    real(real64) :: clean_metrics(JTTY_TBCC_MAX_HYPOTHESES)
    real(real64) :: path_metrics(JTTY_TBCC_MAX_HYPOTHESES), rescored_metric
    logical :: crc_valid(JTTY_TBCC_MAX_HYPOTHESES), closed

    code = JTTY_TBCC_PROFILE_1167_1545_80F
    call make_codeword(.false., information_bits, tones)
    call make_noiseless_correlations(tones, correlations)
    call jtty_tbcc_list_wava(correlations, code, 4_int32, 4_int32, 2_int32, &
         JTTY_TBCC_MAX_HYPOTHESES, candidates, identities, clean_metrics, path_metrics, &
         states, crc_valid, candidate_count, pool_count, prune_reserved_zero=.true.)

    if (candidate_count < 1_int32 .or. pool_count < candidate_count) &
         error stop 'TBCC list decoder returned no noiseless candidate'
    if (any(candidates(:, 1) /= information_bits) .or. .not.crc_valid(1)) &
         error stop 'TBCC list decoder noiseless candidate mismatch'
    if (states(1) /= jtty_tbcc_tailbiting_state(code, information_bits)) &
         error stop 'TBCC list decoder tail-biting state mismatch'
    call jtty_tbcc_score_candidate(code, correlations, 4_int32, candidates(:, 1), &
         rescored_metric, closed)
    if (.not.closed .or. rescored_metric /= clean_metrics(1) .or. &
         abs(clean_metrics(1) - 46.0_real64) > 1.0e-12_real64) &
         error stop 'TBCC list decoder final L2 block or clean rescore mismatch'
  end subroutine expect_noiseless_tailbiting_decode

  subroutine expect_reserved_bit_pruning()
    type(jtty_tbcc_code_profile) :: code
    complex(real32) :: correlations(0:3, JTTY_TBCC_INFORMATION_BITS)
    integer(int32) :: information_bits(JTTY_TBCC_INFORMATION_BITS)
    integer(int32) :: tones(JTTY_TBCC_INFORMATION_BITS)
    integer(int32) :: candidates(JTTY_TBCC_INFORMATION_BITS, JTTY_TBCC_MAX_HYPOTHESES)
    integer(int32) :: states(JTTY_TBCC_MAX_HYPOTHESES), candidate_count, pool_count
    integer(int64) :: identities(JTTY_TBCC_MAX_HYPOTHESES), forbidden_identity
    real(real64) :: clean_metrics(JTTY_TBCC_MAX_HYPOTHESES)
    real(real64) :: path_metrics(JTTY_TBCC_MAX_HYPOTHESES)
    logical :: crc_valid(JTTY_TBCC_MAX_HYPOTHESES)

    code = JTTY_TBCC_PROFILE_1167_1545_80F
    call make_codeword(.true., information_bits, tones)
    call make_noiseless_correlations(tones, correlations)
    forbidden_identity = jtty_tbcc_candidate_identity(information_bits)
    call jtty_tbcc_list_wava(correlations, code, 4_int32, 4_int32, 2_int32, &
         JTTY_TBCC_MAX_HYPOTHESES, candidates, identities, clean_metrics, path_metrics, &
         states, crc_valid, candidate_count, pool_count, prune_reserved_zero=.false.)
    if (candidate_count < 1_int32 .or. identities(1) /= forbidden_identity) &
         error stop 'TBCC list decoder unpruned control mismatch'

    call jtty_tbcc_list_wava(correlations, code, 4_int32, 4_int32, 2_int32, &
         JTTY_TBCC_MAX_HYPOTHESES, candidates, identities, clean_metrics, path_metrics, &
         states, crc_valid, candidate_count, pool_count, prune_reserved_zero=.true.)
    if (candidate_count > 0_int32) then
      if (any(candidates(JTTY_TBCC_RESERVED_BIT, 1:candidate_count) /= 0_int32)) &
           error stop 'TBCC list decoder reserved-bit pruning failed'
      if (any(identities(1:candidate_count) == forbidden_identity)) &
           error stop 'TBCC list decoder retained reserved-invalid identity'
    end if
  end subroutine expect_reserved_bit_pruning

  subroutine expect_candidate_contract()
    complex(real32) :: correlations(0:3, JTTY_TBCC_INFORMATION_BITS)
    integer(int32) :: bits(JTTY_TBCC_INFORMATION_BITS, JTTY_TBCC_MAX_HYPOTHESES)
    integer(int32) :: states(JTTY_TBCC_MAX_HYPOTHESES), count, pool, rank, earlier
    integer(int32) :: encoded(2, JTTY_TBCC_INFORMATION_BITS)
    integer(int64) :: ids(JTTY_TBCC_MAX_HYPOTHESES)
    real(real64) :: clean(JTTY_TBCC_MAX_HYPOTHESES), paths(JTTY_TBCC_MAX_HYPOTHESES), rescored
    logical :: crc(JTTY_TBCC_MAX_HYPOTHESES), closed, expected_crc

    call make_pattern_correlations(correlations)
    call jtty_tbcc_list_wava(correlations, JTTY_TBCC_PROFILE_1167_1545_80F, &
         2_int32, 4_int32, 2_int32, JTTY_TBCC_MAX_HYPOTHESES, &
         bits, ids, clean, paths, states, crc, count, pool, prune_reserved_zero=.true.)
    if (count /= JTTY_TBCC_MAX_HYPOTHESES .or. pool < count) &
         error stop 'TBCC candidate contract fixture did not export four candidates'

    do rank = 1, count
      if (any(bits(:, rank) < 0_int32) .or. any(bits(:, rank) > 1_int32)) &
           error stop 'TBCC candidate contains a nonbinary bit'
      if (bits(JTTY_TBCC_RESERVED_BIT, rank) /= 0_int32) &
           error stop 'TBCC candidate retained a reserved bit'
      if (ids(rank) /= jtty_tbcc_candidate_identity(bits(:, rank))) &
           error stop 'TBCC candidate identity disagrees with its bits'
      if (states(rank) /= jtty_tbcc_tailbiting_state(JTTY_TBCC_PROFILE_1167_1545_80F, bits(:, rank))) &
           error stop 'TBCC candidate start state disagrees with its bits'
      call encode_crc12(bits(1:34, rank), encoded, JTTY_TBCC_PROFILE_1167_1545_80F)
      expected_crc = all(encoded(1, :) == bits(:, rank))
      if (crc(rank) .neqv. expected_crc) error stop 'TBCC candidate CRC status disagrees with encoding'
      call jtty_tbcc_score_candidate(JTTY_TBCC_PROFILE_1167_1545_80F, correlations, &
           2_int32, bits(:, rank), rescored, closed)
      if (.not.closed .or. .not.real_arrays_agree([rescored], [clean(rank)])) &
           error stop 'TBCC candidate clean score or closure disagrees with rescore'
      do earlier = 1, rank - 1
        if (ids(rank) == ids(earlier)) error stop 'TBCC candidate identity is duplicated'
      end do
      if (rank == 1) cycle
      if (clean(rank) > clean(rank - 1)) error stop 'TBCC candidate clean scores are not descending'
      if (clean(rank) == clean(rank - 1) .and. ids(rank) < ids(rank - 1)) &
           error stop 'TBCC tied candidate identities are not ascending'
    end do
  end subroutine expect_candidate_contract

  subroutine expect_workspace_isolation()
    type(jtty_tbcc_decoder_plan) :: plan
    type(jtty_tbcc_decoder_workspace) :: workspace
    complex(real32) :: correlations(0:3, JTTY_TBCC_INFORMATION_BITS)
    integer(int32) :: bits_a(JTTY_TBCC_INFORMATION_BITS, JTTY_TBCC_MAX_HYPOTHESES)
    integer(int32) :: bits_b(JTTY_TBCC_INFORMATION_BITS, JTTY_TBCC_MAX_HYPOTHESES)
    integer(int32) :: states_a(JTTY_TBCC_MAX_HYPOTHESES), states_b(JTTY_TBCC_MAX_HYPOTHESES)
    integer(int32) :: count_a, count_b, pool_a, pool_b
    integer(int64) :: ids_a(JTTY_TBCC_MAX_HYPOTHESES), ids_b(JTTY_TBCC_MAX_HYPOTHESES)
    real(real64) :: clean_a(JTTY_TBCC_MAX_HYPOTHESES), clean_b(JTTY_TBCC_MAX_HYPOTHESES)
    real(real64) :: paths_a(JTTY_TBCC_MAX_HYPOTHESES), paths_b(JTTY_TBCC_MAX_HYPOTHESES)
    logical :: crc_a(JTTY_TBCC_MAX_HYPOTHESES), crc_b(JTTY_TBCC_MAX_HYPOTHESES)

    call jtty_tbcc_init_decoder_plan(plan, JTTY_TBCC_PROFILE_1167_1545_80F, &
         2_int32, 4_int32, 2_int32, JTTY_TBCC_MAX_HYPOTHESES)
    call jtty_tbcc_init_decoder_workspace(workspace, plan)
    call make_pattern_correlations(correlations)
    call jtty_tbcc_list_wava_optimized(plan, workspace, correlations, &
         bits_a, ids_a, clean_a, paths_a, states_a, crc_a, count_a, pool_a, prune_reserved_zero=.true.)
    correlations = cmplx(0.0_real32, 0.0_real32, real32)
    call jtty_tbcc_list_wava_optimized(plan, workspace, correlations, &
         bits_b, ids_b, clean_b, paths_b, states_b, crc_b, count_b, pool_b, prune_reserved_zero=.false.)
    call make_pattern_correlations(correlations)
    call jtty_tbcc_list_wava_optimized(plan, workspace, correlations, &
         bits_b, ids_b, clean_b, paths_b, states_b, crc_b, count_b, pool_b, prune_reserved_zero=.true.)
    if (count_a /= count_b .or. pool_a /= pool_b .or. any(bits_a /= bits_b) .or. &
         any(ids_a /= ids_b) .or. any(states_a /= states_b) .or. &
         .not.real_arrays_agree(clean_a, clean_b) .or. &
         .not.real_arrays_agree(paths_a, paths_b) .or. any(crc_a .neqv. crc_b)) &
         error stop 'TBCC workspace retained state across A-B-A decodes'
  end subroutine expect_workspace_isolation

  subroutine expect_deterministic_hypothesis_budget()
    type(jtty_tbcc_code_profile) :: code
    complex(real32) :: correlations(0:3, JTTY_TBCC_INFORMATION_BITS)
    integer(int32) :: bits_a(JTTY_TBCC_INFORMATION_BITS, JTTY_TBCC_MAX_HYPOTHESES)
    integer(int32) :: bits_b(JTTY_TBCC_INFORMATION_BITS, JTTY_TBCC_MAX_HYPOTHESES)
    integer(int32) :: states_a(JTTY_TBCC_MAX_HYPOTHESES)
    integer(int32) :: states_b(JTTY_TBCC_MAX_HYPOTHESES)
    integer(int32) :: count_a, count_b, pool_a, pool_b, rank
    integer(int64) :: ids_a(JTTY_TBCC_MAX_HYPOTHESES), ids_b(JTTY_TBCC_MAX_HYPOTHESES)
    real(real64) :: clean_a(JTTY_TBCC_MAX_HYPOTHESES), clean_b(JTTY_TBCC_MAX_HYPOTHESES)
    real(real64) :: path_a(JTTY_TBCC_MAX_HYPOTHESES), path_b(JTTY_TBCC_MAX_HYPOTHESES)
    logical :: crc_a(JTTY_TBCC_MAX_HYPOTHESES), crc_b(JTTY_TBCC_MAX_HYPOTHESES)
    integer(int32) :: one_bit(JTTY_TBCC_INFORMATION_BITS, 1), one_state(1)
    integer(int32) :: one_count, one_pool
    integer(int64) :: one_id(1)
    real(real64) :: one_clean(1), one_path(1)
    logical :: one_crc(1)

    code = JTTY_TBCC_PROFILE_1167_1545_80F
    correlations = cmplx(0.0_real32, 0.0_real32, real32)
    call jtty_tbcc_list_wava(correlations, code, 4_int32, 4_int32, 2_int32, &
         JTTY_TBCC_MAX_HYPOTHESES, bits_a, ids_a, clean_a, path_a, states_a, &
         crc_a, count_a, pool_a, prune_reserved_zero=.true.)
    call jtty_tbcc_list_wava(correlations, code, 4_int32, 4_int32, 2_int32, &
         JTTY_TBCC_MAX_HYPOTHESES, bits_b, ids_b, clean_b, path_b, states_b, &
         crc_b, count_b, pool_b, prune_reserved_zero=.true.)

    if (count_a /= JTTY_TBCC_MAX_HYPOTHESES .or. count_b /= count_a .or. &
         pool_a < count_a .or. pool_b /= pool_a) &
         error stop 'TBCC list decoder did not enforce the H4 budget'
    if (any(bits_a /= bits_b) .or. any(ids_a /= ids_b) .or. &
         any(states_a /= states_b) .or. .not.real_arrays_agree(clean_a, clean_b) .or. &
         .not.real_arrays_agree(path_a, path_b) .or. any(crc_a .neqv. crc_b)) &
         error stop 'TBCC list decoder tie order is not deterministic'
    do rank = 2, count_a
      if (ids_a(rank) <= ids_a(rank - 1)) &
           error stop 'TBCC list decoder did not deduplicate tied identities'
    end do

    call jtty_tbcc_list_wava(correlations, code, 4_int32, 4_int32, 2_int32, &
         1_int32, one_bit, one_id, one_clean, one_path, one_state, one_crc, &
         one_count, one_pool, prune_reserved_zero=.true.)
    if (one_count /= 1_int32 .or. one_id(1) /= ids_a(1) .or. one_pool /= pool_a) &
         error stop 'TBCC list decoder did not enforce the H1 budget'

    ! The nonzero pool must exceed H4 so this checks the admission boundary.
    call make_pattern_correlations(correlations)
    call jtty_tbcc_list_wava(correlations, code, 2_int32, 4_int32, 2_int32, &
         JTTY_TBCC_MAX_HYPOTHESES, bits_a, ids_a, clean_a, path_a, states_a, &
         crc_a, count_a, pool_a, prune_reserved_zero=.true.)
    call jtty_tbcc_list_wava(correlations, code, 2_int32, 4_int32, 2_int32, &
         1_int32, one_bit, one_id, one_clean, one_path, one_state, one_crc, &
         one_count, one_pool, prune_reserved_zero=.true.)
    if (count_a /= JTTY_TBCC_MAX_HYPOTHESES .or. pool_a <= count_a .or. &
         one_count /= 1_int32 .or. one_pool /= pool_a) &
         error stop 'TBCC nonzero H1/H4 fixture did not cross the hypothesis boundary'
    if (any(one_bit(:, 1) /= bits_a(:, 1)) .or. one_id(1) /= ids_a(1) .or. &
         one_state(1) /= states_a(1) .or. one_crc(1) .neqv. crc_a(1) .or. &
         .not.real_arrays_agree(one_clean, clean_a(1:1)) .or. &
         .not.real_arrays_agree(one_path, path_a(1:1))) &
         error stop 'TBCC nonzero H1 differs from the first H4 candidate'
  end subroutine expect_deterministic_hypothesis_budget

  subroutine expect_score_symmetry()
    complex(real32) :: correlations(0:3, JTTY_TBCC_INFORMATION_BITS)
    integer(int32) :: bits_a(JTTY_TBCC_INFORMATION_BITS, JTTY_TBCC_MAX_HYPOTHESES)
    integer(int32) :: bits_b(JTTY_TBCC_INFORMATION_BITS, JTTY_TBCC_MAX_HYPOTHESES)
    integer(int32) :: states_a(JTTY_TBCC_MAX_HYPOTHESES), states_b(JTTY_TBCC_MAX_HYPOTHESES)
    integer(int32) :: count_a, count_b, pool_a, pool_b
    integer(int64) :: ids_a(JTTY_TBCC_MAX_HYPOTHESES), ids_b(JTTY_TBCC_MAX_HYPOTHESES)
    real(real64) :: clean_a(JTTY_TBCC_MAX_HYPOTHESES), clean_b(JTTY_TBCC_MAX_HYPOTHESES)
    real(real64) :: paths_a(JTTY_TBCC_MAX_HYPOTHESES), paths_b(JTTY_TBCC_MAX_HYPOTHESES)
    logical :: crc_a(JTTY_TBCC_MAX_HYPOTHESES), crc_b(JTTY_TBCC_MAX_HYPOTHESES)

    call make_pattern_correlations(correlations)
    call jtty_tbcc_list_wava(correlations, JTTY_TBCC_PROFILE_1167_1545_80F, &
         2_int32, 4_int32, 2_int32, JTTY_TBCC_MAX_HYPOTHESES, &
         bits_a, ids_a, clean_a, paths_a, states_a, crc_a, count_a, pool_a, prune_reserved_zero=.true.)
    if (count_a /= JTTY_TBCC_MAX_HYPOTHESES) error stop 'TBCC symmetry fixture lacks four candidates'
    if (any(clean_a(1:count_a-1) - clean_a(2:count_a) <= 1.0e-6_real64)) &
         error stop 'TBCC symmetry fixture has near-tied clean scores'
    correlations = -correlations
    call jtty_tbcc_list_wava(correlations, JTTY_TBCC_PROFILE_1167_1545_80F, &
         2_int32, 4_int32, 2_int32, JTTY_TBCC_MAX_HYPOTHESES, &
         bits_b, ids_b, clean_b, paths_b, states_b, crc_b, count_b, pool_b, prune_reserved_zero=.true.)
    if (count_b /= count_a .or. pool_b /= pool_a .or. any(bits_b /= bits_a) .or. &
         any(ids_b /= ids_a) .or. any(states_b /= states_a) .or. any(crc_b .neqv. crc_a) .or. &
         .not.real_arrays_agree(clean_b, clean_a)) &
         error stop 'TBCC candidate order or score changed under negation'
    correlations = 2.0_real32*correlations
    call jtty_tbcc_list_wava(correlations, JTTY_TBCC_PROFILE_1167_1545_80F, &
         2_int32, 4_int32, 2_int32, JTTY_TBCC_MAX_HYPOTHESES, &
         bits_b, ids_b, clean_b, paths_b, states_b, crc_b, count_b, pool_b, prune_reserved_zero=.true.)
    if (count_b /= count_a .or. pool_b /= pool_a .or. any(bits_b /= bits_a) .or. &
         any(ids_b /= ids_a) .or. any(states_b /= states_a) .or. any(crc_b .neqv. crc_a) .or. &
         .not.real_arrays_agree(clean_b, 4.0_real64*clean_a)) &
         error stop 'TBCC candidate order or score changed under amplitude doubling'
  end subroutine expect_score_symmetry

  subroutine expect_coherent_normalization()
    complex(real32) :: correlations(0:3, JTTY_TBCC_INFORMATION_BITS)
    integer(int32) :: tones4(4), tones2(2)
    real(real64) :: metric

    correlations = cmplx(0.0_real32, 0.0_real32, real32)
    tones4 = [0_int32, 1_int32, 2_int32, 3_int32]
    correlations(0, 3) = cmplx(1.0_real32, 0.0_real32, real32)
    correlations(1, 4) = cmplx(2.0_real32, 0.0_real32, real32)
    correlations(2, 5) = cmplx(3.0_real32, 0.0_real32, real32)
    correlations(3, 6) = cmplx(4.0_real32, 0.0_real32, real32)
    metric = jtty_tbcc_coherent_metric(correlations, 3_int32, tones4)
    if (metric /= 25.0_real64) error stop 'TBCC L4 normalization mismatch'

    tones2 = [1_int32, 3_int32]
    correlations(1, 45) = cmplx(2.0_real32, 0.0_real32, real32)
    correlations(3, 46) = cmplx(4.0_real32, 0.0_real32, real32)
    metric = jtty_tbcc_coherent_metric(correlations, 45_int32, tones2)
    if (metric /= 18.0_real64) error stop 'TBCC final L2 normalization mismatch'
  end subroutine expect_coherent_normalization

  subroutine make_codeword(set_reserved_bit, information_bits, tones)
    logical, intent(in) :: set_reserved_bit
    integer(int32), intent(out) :: information_bits(JTTY_TBCC_INFORMATION_BITS)
    integer(int32), intent(out) :: tones(JTTY_TBCC_INFORMATION_BITS)
    integer(int32) :: payload(34), encoded(2, JTTY_TBCC_INFORMATION_BITS), bit_index

    do bit_index = 1, size(payload)
      payload(bit_index) = modulo(5*bit_index + bit_index/3, 2)
    end do
    payload(JTTY_TBCC_RESERVED_BIT) = merge(1_int32, 0_int32, set_reserved_bit)
    call encode_crc12(payload, encoded, JTTY_TBCC_PROFILE_1167_1545_80F)
    call tbcc_encode(payload, tones, JTTY_TBCC_PROFILE_1167_1545_80F)
    information_bits = encoded(1, :)
  end subroutine make_codeword

  subroutine make_noiseless_correlations(tones, correlations)
    integer(int32), intent(in) :: tones(JTTY_TBCC_INFORMATION_BITS)
    complex(real32), intent(out) :: correlations(0:3, JTTY_TBCC_INFORMATION_BITS)
    integer(int32) :: symbol

    correlations = cmplx(0.0_real32, 0.0_real32, real32)
    do symbol = 1, JTTY_TBCC_INFORMATION_BITS
      correlations(tones(symbol), symbol) = cmplx(1.0_real32, 0.0_real32, real32)
    end do
  end subroutine make_noiseless_correlations

  subroutine make_pattern_correlations(correlations)
    complex(real32), intent(out) :: correlations(0:3, JTTY_TBCC_INFORMATION_BITS)
    integer(int32) :: symbol, tone

    do symbol = 1, JTTY_TBCC_INFORMATION_BITS
      do tone = 0, 3
        correlations(tone, symbol) = cmplx( &
             real(modulo(17*tone + 11*symbol, 29) - 14, real32)/8.0_real32, &
             real(modulo(7*tone + 19*symbol, 31) - 15, real32)/9.0_real32, real32)
      end do
    end do
  end subroutine make_pattern_correlations

  pure logical function real_arrays_agree(left, right) result(agree)
    real(real64), intent(in) :: left(:), right(:)
    integer(int32) :: element

    agree = size(left) == size(right)
    if (.not.agree) return
    do element = 1, size(left)
      if (left(element) == right(element)) cycle
      if (abs(left(element)-right(element)) <= 64.0_real64*epsilon(0.0_real64)* &
           max(1.0_real64, abs(left(element)), abs(right(element)))) cycle
      agree = .false.
      return
    end do
  end function real_arrays_agree

end program test_jtty_tbcc_list_decoder