summaryrefslogtreecommitdiff
path: root/tests/unit/jt65
diff options
context:
space:
mode:
authorhibby <d@vehibberd.com>2026-10-06 21:24:40 +0100
committerhibby <d@vehibberd.com>2026-10-06 21:24:40 +0100
commitc25cbdf6fb6d946175f4335fab77bfea601b56db (patch)
tree6f7a94b7b81f999e434f61b73d47e337dfd26bf2 /tests/unit/jt65
parent71f5689252c9f13e70e1c24856904475b2307809 (diff)
parent0685fcada9f95fe90f70b387cd7d757da8209624 (diff)
Merge tag 'debian/3.2.0_rc1+dfsg-2' into debian/trixie-backportsdebian/trixie-backports
tagging wsjtx 3.2.0~rc1+dfsg-2
Diffstat (limited to 'tests/unit/jt65')
-rw-r--r--tests/unit/jt65/CMakeLists.txt33
-rw-r--r--tests/unit/jt65/jt65_test_fixture.f9082
-rw-r--r--tests/unit/jt65/jt65_test_vectors.f9043
-rw-r--r--tests/unit/jt65/test_ccf65_correlator.f90118
-rw-r--r--tests/unit/jt65/test_jt65_decode_pipeline.f90293
-rw-r--r--tests/unit/jt65/test_jt65_source_encoding.f90201
-rw-r--r--tests/unit/jt65/test_jt65_xcor_silence.f9034
-rw-r--r--tests/unit/jt65/test_map65_jt65_candidate_consistency.f9039
-rw-r--r--tests/unit/jt65/test_map65_jt65_handoff.f90150
-rw-r--r--tests/unit/jt65/test_map65_jt65_message_validity.f9077
-rw-r--r--tests/unit/jt65/test_map65_s3avg_contracts.f90137
11 files changed, 1207 insertions, 0 deletions
diff --git a/tests/unit/jt65/CMakeLists.txt b/tests/unit/jt65/CMakeLists.txt
new file mode 100644
index 00000000..e718d200
--- /dev/null
+++ b/tests/unit/jt65/CMakeLists.txt
@@ -0,0 +1,33 @@
+# Compile the shared vectors once so parallel test builds have one producer
+# for the Fortran module interface.
+add_library (jt65_test_vectors STATIC jt65_test_vectors.f90)
+set_target_properties (jt65_test_vectors PROPERTIES
+ Fortran_MODULE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/jt65_test_vectors")
+target_include_directories (jt65_test_vectors PUBLIC
+ "${CMAKE_CURRENT_BINARY_DIR}/jt65_test_vectors")
+
+add_executable (test_jt65_source_encoding
+ test_jt65_source_encoding.f90)
+wsjt_link_common_fortran (test_jt65_source_encoding)
+target_link_libraries (test_jt65_source_encoding PRIVATE jt65_test_vectors)
+
+add_test (NAME test_jt65_source_encoding COMMAND test_jt65_source_encoding)
+
+add_executable (test_jt65_decode_pipeline
+ jt65_test_fixture.f90
+ test_jt65_decode_pipeline.f90)
+set_source_files_properties (test_jt65_decode_pipeline.f90 PROPERTIES
+ COMPILE_FLAGS "-Wno-unused-dummy-argument")
+wsjt_link_common_fortran (test_jt65_decode_pipeline)
+target_link_libraries (test_jt65_decode_pipeline PRIVATE jt65_test_vectors)
+
+add_test (NAME test_jt65_decode_pipeline COMMAND test_jt65_decode_pipeline)
+set_tests_properties (test_jt65_decode_pipeline PROPERTIES
+ COST 18
+ TIMEOUT 180)
+
+add_executable (test_jt65_xcor_silence
+ test_jt65_xcor_silence.f90)
+wsjt_link_common_fortran (test_jt65_xcor_silence)
+
+add_test (NAME test_jt65_xcor_silence COMMAND test_jt65_xcor_silence)
diff --git a/tests/unit/jt65/jt65_test_fixture.f90 b/tests/unit/jt65/jt65_test_fixture.f90
new file mode 100644
index 00000000..a317f706
--- /dev/null
+++ b/tests/unit/jt65/jt65_test_fixture.f90
@@ -0,0 +1,82 @@
+module jt65_test_fixture
+
+ use iso_fortran_env, only: int64, real32, real64
+ implicit none
+
+ integer, parameter :: jt65_sample_rate = 12000
+ integer, parameter :: jt65_sample_count = 60 * jt65_sample_rate
+ integer, parameter :: jt65_symbol_count = 126
+ real(real64), parameter :: jt65_baud = 11025.0_real64 / 4096.0_real64
+ real(real64), parameter :: jt65_pi = 3.1415926535897932384626433832795_real64
+ real(real64), parameter :: jt65_amplitude = 28000.0_real64
+
+contains
+
+ subroutine make_jt65_wave(samples, tones, mode65, f0, amplitude, noise_amplitude, noise_seed)
+ real(real32), intent(out) :: samples(:)
+ integer, intent(in) :: tones(jt65_symbol_count)
+ integer, intent(in) :: mode65
+ real(real64), intent(in) :: f0
+ real(real64), intent(in), optional :: amplitude
+ real(real64), intent(in), optional :: noise_amplitude
+ integer, intent(in), optional :: noise_seed
+ real(real64) :: signal_amplitude
+ real(real64) :: noise_scale, sample_noise, u1, u2
+ real(real64) :: phase, frequency, samples_per_symbol
+ integer(int64) :: noise_state
+ integer :: sample, signal_sample, output_sample, symbol
+
+ signal_amplitude = jt65_amplitude
+ if (present(amplitude)) signal_amplitude = amplitude
+ noise_scale = 0.0_real64
+ if (present(noise_amplitude)) noise_scale = noise_amplitude
+ noise_state = 104729_int64
+ if (present(noise_seed)) noise_state = 104729_int64 + int(noise_seed, int64)
+ if (noise_scale == 0.0_real64) then
+ samples = 0.0_real32
+ else
+ do sample = 1, size(samples)
+ noise_state = modulo(16807_int64 * noise_state, 2147483647_int64)
+ u1 = max(real(noise_state, real64) / 2147483647.0_real64, tiny(1.0_real64))
+ noise_state = modulo(16807_int64 * noise_state, 2147483647_int64)
+ u2 = real(noise_state, real64) / 2147483647.0_real64
+ sample_noise = sqrt(-2.0_real64 * log(u1)) * cos(2.0_real64 * jt65_pi * u2)
+ samples(sample) = real(noise_scale * sample_noise, real32)
+ end do
+ end if
+ phase = 0.0_real64
+ samples_per_symbol = real(jt65_sample_rate, real64) / jt65_baud
+ do signal_sample = 1, size(samples) - jt65_sample_rate
+ symbol = int(floor(real(signal_sample, real64) / samples_per_symbol)) + 1
+ if (symbol > jt65_symbol_count) exit
+ frequency = f0 + real(tones(symbol), real64) * jt65_baud * real(mode65, real64)
+ phase = phase + 2.0_real64 * jt65_pi * frequency / real(jt65_sample_rate, real64)
+ output_sample = jt65_sample_rate + signal_sample
+ samples(output_sample) = samples(output_sample) + real(signal_amplitude * sin(phase), real32)
+ end do
+ end subroutine make_jt65_wave
+
+ subroutine make_map65_wave(dd, tones, mode65, f0)
+ real(real32), intent(out) :: dd(:,:)
+ integer, intent(in) :: tones(jt65_symbol_count)
+ integer, intent(in) :: mode65
+ real(real64), intent(in) :: f0
+ real(real64) :: phase, frequency, samples_per_symbol
+ integer :: signal_sample, output_sample, symbol
+ integer, parameter :: map65_sample_rate = 96000
+
+ dd = 0.0_real32
+ phase = 0.0_real64
+ samples_per_symbol = real(map65_sample_rate, real64) / jt65_baud
+ do signal_sample = 1, size(dd, 2) - map65_sample_rate
+ symbol = int(floor(real(signal_sample, real64) / samples_per_symbol)) + 1
+ if (symbol > jt65_symbol_count) exit
+ frequency = f0 + real(tones(symbol), real64) * jt65_baud * real(mode65, real64)
+ phase = phase + 2.0_real64 * jt65_pi * frequency / real(map65_sample_rate, real64)
+ output_sample = map65_sample_rate + signal_sample
+ dd(1, output_sample) = real(jt65_amplitude * cos(phase), real32)
+ dd(2, output_sample) = real(-jt65_amplitude * sin(phase), real32)
+ end do
+ end subroutine make_map65_wave
+
+end module jt65_test_fixture
diff --git a/tests/unit/jt65/jt65_test_vectors.f90 b/tests/unit/jt65/jt65_test_vectors.f90
new file mode 100644
index 00000000..ab1ac1e8
--- /dev/null
+++ b/tests/unit/jt65/jt65_test_vectors.f90
@@ -0,0 +1,43 @@
+module jt65_test_vectors
+ implicit none
+
+ integer, parameter :: jt65_symbol_count = 126
+
+ integer, parameter :: standard_tones(jt65_symbol_count) = [ &
+ 0, 3, 62, 0, 0, 50, 13, 40, 0, 0, 0, 0, &
+ 0, 0, 23, 0, 52, 0, 10, 39, 13, 0, 15, 0, &
+ 0, 3, 58, 0, 26, 47, 63, 0, 0, 0, 62, 21, &
+ 0, 0, 0, 0, 20, 0, 0, 4, 0, 0, 0, 0, &
+ 21, 24, 60, 0, 0, 56, 0, 46, 0, 37, 0, 0, &
+ 46, 30, 0, 0, 30, 0, 53, 0, 13, 0, 6, 17, &
+ 0, 62, 42, 59, 31, 44, 17, 0, 0, 56, 43, 48, &
+ 8, 42, 49, 40, 0, 0, 59, 0, 15, 3, 0, 37, &
+ 0, 0, 31, 0, 20, 0, 52, 0, 51, 35, 0, 0, &
+ 20, 19, 0, 10, 9, 0, 59, 56, 52, 62, 0, 0, &
+ 0, 0, 0, 0, 0, 0 ]
+
+ integer, parameter :: ooo_tones(jt65_symbol_count) = [ &
+ 3, 0, 0, 62, 50, 0, 0, 0, 13, 40, 23, 52, &
+ 10, 39, 0, 13, 0, 15, 0, 0, 0, 3, 0, 58, &
+ 26, 0, 0, 47, 0, 0, 0, 63, 62, 21, 0, 0, &
+ 20, 4, 21, 24, 0, 60, 56, 0, 46, 37, 46, 30, &
+ 0, 0, 0, 30, 53, 0, 13, 0, 6, 0, 17, 62, &
+ 0, 0, 42, 59, 0, 31, 0, 44, 0, 17, 0, 0, &
+ 56, 0, 0, 0, 0, 0, 0, 43, 48, 0, 0, 0, &
+ 0, 0, 0, 0, 8, 42, 0, 49, 0, 0, 40, 0, &
+ 59, 15, 0, 3, 0, 37, 0, 31, 0, 0, 20, 52, &
+ 0, 0, 51, 0, 0, 35, 0, 0, 0, 0, 20, 19, &
+ 10, 9, 59, 56, 52, 62 ]
+
+contains
+
+ pure function padded_message(text) result(message)
+ character(len=*), intent(in) :: text
+ character(len=22) :: message
+
+ message = ' '
+ message(1:min(len_trim(text), len(message))) = &
+ text(1:min(len_trim(text), len(message)))
+ end function padded_message
+
+end module jt65_test_vectors
diff --git a/tests/unit/jt65/test_ccf65_correlator.f90 b/tests/unit/jt65/test_ccf65_correlator.f90
new file mode 100644
index 00000000..3dd8917c
--- /dev/null
+++ b/tests/unit/jt65/test_ccf65_correlator.f90
@@ -0,0 +1,118 @@
+program test_ccf65_correlator
+
+ use ccf65_legacy_mod, only: ccf65
+ use four2a_legacy_wrap_mod, only: c2r_legacy, r2c_legacy, JT65_NFFT, JT65_NH
+ use, intrinsic :: ieee_arithmetic, only: ieee_is_finite, ieee_quiet_nan, ieee_value
+ implicit none
+
+ integer, parameter :: nhsym = 254
+ real, parameter :: expected_sync = 25.8397
+ real :: ss(4,322)
+ integer :: i
+ real :: sync1, dt, flipk, syncshort, snr2, dt2
+ integer :: ipol1, ipol2
+
+ call test_fft_roundtrip()
+
+ call make_sync_plane(ss, 1.0)
+ call ccf65(ss, nhsym, 1.0e30, sync1, ipol1, 1, dt, flipk, syncshort, snr2, ipol2, dt2)
+ call require(sync1 > 0.0, 'normal JT65 sync is detected')
+ call require(abs(sync1 - expected_sync) < 0.05, 'normal JT65 sync strength')
+ call require(flipk > 0.0, 'normal JT65 polarity')
+ call require(ipol1 == 1, 'normal JT65 polarization')
+ call require(abs(dt + 2.5) < 0.02, 'normal JT65 timing')
+ call require(syncshort < 0.0, 'normal JT65 is not shorthand')
+
+ call make_sync_plane(ss, -1.0)
+ call ccf65(ss, nhsym, 1.0e30, sync1, ipol1, 1, dt, flipk, syncshort, snr2, ipol2, dt2)
+ call require(sync1 > 0.0, 'inverted JT65 sync is detected')
+ call require(abs(sync1 - expected_sync) < 0.05, 'inverted JT65 sync strength')
+ call require(flipk < 0.0, 'inverted JT65 polarity')
+ call require(ipol1 == 1, 'inverted JT65 polarization')
+ call require(abs(dt + 2.5) < 0.02, 'inverted JT65 timing')
+ call require(syncshort < 0.0, 'inverted JT65 is not shorthand')
+
+ ss = 0.0
+ sync1 = -99.0
+ do i = 1, nhsym - 1
+ ss(1,i) = 0.01 * real(mod(i,3) - 1)
+ end do
+ call ccf65(ss, nhsym, 1.0e30, sync1, ipol1, 1, dt, flipk, syncshort, snr2, ipol2, dt2)
+ call require(sync1 < 0.0, 'low-signal input is rejected')
+ call require(syncshort < 0.0, 'low-signal input is not shorthand')
+
+ ss = 0.0
+ call ccf65(ss, nhsym, 1.0e30, sync1, ipol1, 1, dt, flipk, syncshort, snr2, ipol2, dt2)
+ call require(sync1 == -4.0 .and. syncshort == -4.0, 'zero signal has explicit sync defaults')
+ call require(all_finite(sync1, dt, flipk, syncshort, snr2, dt2), 'zero signal outputs are finite')
+
+ call make_sync_plane(ss, 1.0)
+ ss(2:4,:) = ieee_value(0.0, ieee_quiet_nan)
+ call ccf65(ss, nhsym, 1.0e30, sync1, ipol1, 1, dt, flipk, syncshort, snr2, ipol2, dt2)
+ call require(sync1 > 0.0, 'inactive polarization data does not affect JT65 detection')
+ call require(all_finite(sync1, dt, flipk, syncshort, snr2, dt2), &
+ 'inactive polarization data produces finite correlator outputs')
+ print '(a)', 'ccf65 correlator tests passed'
+
+contains
+
+ subroutine require(condition, description)
+ logical, intent(in) :: condition
+ character(len=*), intent(in) :: description
+
+ if (.not. condition) then
+ print '(a)', 'FAIL: '//description
+ error stop 1
+ end if
+ end subroutine require
+
+ subroutine test_fft_roundtrip()
+ real :: input(JT65_NFFT), output(JT65_NFFT)
+ complex :: spectrum(0:JT65_NH)
+ integer :: k
+
+ do k = 1, JT65_NFFT
+ input(k) = sin(0.013*real(k)) + 0.25*cos(0.071*real(k))
+ end do
+
+ call r2c_legacy(input, spectrum)
+ call c2r_legacy(spectrum, output)
+ call require(maxval(abs(output/JT65_NFFT - input)) < 1.0e-5, &
+ 'legacy packed FFT round trip preserves real samples')
+ end subroutine test_fft_roundtrip
+
+ logical function all_finite(v1, v2, v3, v4, v5, v6)
+ real, intent(in) :: v1, v2, v3, v4, v5, v6
+
+ all_finite = ieee_is_finite(v1) .and. ieee_is_finite(v2) .and. &
+ ieee_is_finite(v3) .and. ieee_is_finite(v4) .and. &
+ ieee_is_finite(v5) .and. ieee_is_finite(v6)
+ end function all_finite
+
+ subroutine make_sync_plane(ss_plane, polarity)
+ real, intent(out) :: ss_plane(4,322)
+ real, intent(in) :: polarity
+ integer, parameter :: npr(126) = [ &
+ 1,0,0,1,1,0,0,0,1,1,1,1,1,1,0,1,0,1,0,0, &
+ 0,1,0,1,1,0,0,1,0,0,0,1,1,1,0,0,1,1,1,1, &
+ 0,1,1,0,1,1,1,1,0,0,0,1,1,0,1,0,1,0,1,1, &
+ 0,0,1,1,0,1,0,1,0,1,0,0,1,0,0,0,0,0,0,1, &
+ 1,0,0,0,0,0,0,0,1,1,0,1,0,0,1,0,1,1,0,1, &
+ 0,1,0,1,0,0,1,1,0,0,1,0,0,1,0,0,0,0,1,1, &
+ 1,1,1,1,1,1]
+ integer :: i
+ real :: target_sum
+
+ ! Build a non-negative spectrum whose adjacent-bin sums have one lag only.
+ ss_plane = 0.0
+ ss_plane(1,1) = 2.0
+ do i = 1, nhsym - 1
+ target_sum = 4.0
+ if (mod(i,2) == 1 .and. (i+1)/2 <= size(npr)) then
+ target_sum = target_sum + polarity * real(2*npr((i+1)/2) - 1)
+ endif
+ ss_plane(1,i+1) = target_sum - ss_plane(1,i)
+ end do
+ end subroutine make_sync_plane
+
+end program test_ccf65_correlator
diff --git a/tests/unit/jt65/test_jt65_decode_pipeline.f90 b/tests/unit/jt65/test_jt65_decode_pipeline.f90
new file mode 100644
index 00000000..414b6f80
--- /dev/null
+++ b/tests/unit/jt65/test_jt65_decode_pipeline.f90
@@ -0,0 +1,293 @@
+module jt65_pipeline_callback
+
+ use jt65_decode
+ use jt65_mod, only: s1, s3a
+ use jt65_test_vectors, only: padded_message
+ implicit none
+
+ integer :: callback_count
+ integer :: callback_snr, callback_freq, callback_drift, callback_nflip
+ integer :: callback_ft, callback_qual, callback_nsmo, callback_nsum, callback_minsync
+ integer :: callback_message_count, callback_max_nsum
+ real :: callback_sync, callback_dt, callback_width
+ character(len=22) :: callback_message
+
+contains
+
+ subroutine capture_callback(this, sync, snr, dt, freq, drift, nflip, width, &
+ decoded, ft, qual, nsmo, nsum, minsync)
+ class(jt65_decoder), intent(inout) :: this
+ real, intent(in) :: sync, dt, width
+ integer, intent(in) :: snr, freq, drift, nflip, ft, qual, nsmo, nsum, minsync
+ character(len=22), intent(in) :: decoded
+
+ callback_count = callback_count + 1
+ callback_sync = sync
+ callback_snr = snr
+ callback_dt = dt
+ callback_freq = freq
+ callback_drift = drift
+ callback_nflip = nflip
+ callback_width = width
+ callback_message = decoded
+ callback_ft = ft
+ callback_qual = qual
+ callback_nsmo = nsmo
+ callback_nsum = nsum
+ callback_minsync = minsync
+ callback_max_nsum = max(callback_max_nsum, nsum)
+ if (decoded /= padded_message('')) callback_message_count = callback_message_count + 1
+ end subroutine capture_callback
+
+end module jt65_pipeline_callback
+
+program test_jt65_decode_pipeline
+
+ use iso_fortran_env, only: real32
+ use, intrinsic :: ieee_arithmetic, only: ieee_is_finite
+ use jt65_decode
+ use jt65_pipeline_callback
+ use jt65_test_fixture
+ use jt65_test_vectors
+ implicit none
+
+ type(jt65_decoder) :: decoder
+ real(real32), allocatable :: samples(:)
+ allocate(samples(jt65_sample_count))
+ call test_clean_modes(samples)
+ call test_ooo_signal(samples)
+ call test_silence_and_frequency_rejection(samples)
+ call test_decoder_reuse(samples, decoder)
+ call test_auto_clear_average(samples, decoder)
+ call test_duplicate_average_cursor
+ print '(a)', 'JT65 decoder pipeline tests passed'
+
+contains
+
+ subroutine reset_capture()
+ callback_count = 0
+ callback_snr = 0
+ callback_freq = 0
+ callback_drift = 0
+ callback_nflip = 0
+ callback_ft = 0
+ callback_qual = 0
+ callback_nsmo = 0
+ callback_nsum = 0
+ callback_minsync = 0
+ callback_message_count = 0
+ callback_max_nsum = 0
+ callback_sync = 0.0
+ callback_dt = 0.0
+ callback_width = 0.0
+ callback_message = ' '
+ end subroutine reset_capture
+
+ subroutine require(condition, description)
+ logical, intent(in) :: condition
+ character(len=*), intent(in) :: description
+
+ if (.not. condition) then
+ print '(a)', 'FAIL: '//description
+ error stop 1
+ end if
+ end subroutine require
+
+ subroutine decode_signal(decoder_to_use, waveform, nsubmode, clearave, decode_flags, &
+ decode_nutc, decode_depth, decode_again)
+ type(jt65_decoder), intent(inout) :: decoder_to_use
+ real(real32), intent(in) :: waveform(:)
+ integer, intent(in) :: nsubmode
+ logical, intent(in) :: clearave
+ integer, intent(in), optional :: decode_flags, decode_nutc, decode_depth
+ logical, intent(in), optional :: decode_again
+ logical :: newdat, nagain, nrobust, ljt65apon
+ integer :: npts, nutc, nf1, nf2, nfqso, ntol, minsync, n2pass
+ integer :: ntrials, naggressive, ndepth, nexp_decode, nQSOProgress
+ real :: emedelay
+ character(len=12) :: mycall, hiscall
+ character(len=6) :: hisgrid
+
+ npts = min(size(waveform), 52 * jt65_sample_rate)
+ nutc = 1
+ if (present(decode_nutc)) nutc = decode_nutc
+ nf1 = 1300
+ nf2 = 1700
+ nfqso = 1500
+ ntol = 100
+ minsync = 0
+ n2pass = 1
+ ntrials = 1000
+ naggressive = 5
+ ndepth = 3
+ if (present(decode_depth)) ndepth = decode_depth
+ emedelay = -999.9
+ nexp_decode = 32
+ if (present(decode_flags)) nexp_decode = decode_flags
+ nQSOProgress = 6
+ newdat = .true.
+ nagain = .true.
+ if (present(decode_again)) nagain = decode_again
+ nrobust = .false.
+ ljt65apon = .false.
+ mycall = 'K1ABC '
+ hiscall = 'W9XYZ '
+ hisgrid = 'FN42 '
+
+ call decoder_to_use%decode(capture_callback, waveform, npts, newdat, nutc, nf1, nf2, &
+ nfqso, ntol, nsubmode, minsync, nagain, n2pass, nrobust, ntrials, naggressive, &
+ ndepth, emedelay, clearave, mycall, hiscall, hisgrid, nexp_decode, nQSOProgress, &
+ ljt65apon)
+ end subroutine decode_signal
+
+ subroutine require_decode(expected_message, description)
+ character(len=*), intent(in) :: expected_message, description
+ character(len=22) :: expected
+
+ expected = padded_message(expected_message)
+ call require(callback_count == 1, description//' emits exactly one callback')
+ call require(callback_message == expected, description//' message')
+ call require(abs(real(callback_freq) - 1500.0) <= 4.0, description//' frequency')
+ call require(abs(callback_dt) <= 1.0, description//' timing')
+ call require(callback_sync > 0.0, description//' sync metric')
+ call require(ieee_is_finite(callback_sync), description//' finite sync metric')
+ call require(ieee_is_finite(callback_dt), description//' finite timing')
+ call require(ieee_is_finite(callback_width), description//' finite width metric')
+ end subroutine require_decode
+
+ subroutine test_clean_modes(waveform)
+ real(real32), intent(inout) :: waveform(:)
+ call reset_capture()
+ call make_jt65_wave(waveform, standard_tones, 1, 1500.0_real64)
+ call decode_signal(decoder, waveform, 0, .true.)
+ call require_decode('K1ABC W9XYZ FN42', 'JT65A clean signal')
+
+ call reset_capture()
+ call make_jt65_wave(waveform, standard_tones, 4, 1500.0_real64)
+ call decode_signal(decoder, waveform, 2, .true.)
+ call require_decode('K1ABC W9XYZ FN42', 'JT65C clean signal')
+
+ call reset_capture()
+ call make_jt65_wave(waveform, standard_tones, 2, 1500.0_real64)
+ call decode_signal(decoder, waveform, 1, .true.)
+ call require_decode('K1ABC W9XYZ FN42', 'JT65B clean signal')
+ end subroutine test_clean_modes
+
+ subroutine test_ooo_signal(waveform)
+ real(real32), intent(inout) :: waveform(:)
+ type(jt65_decoder) :: ooo_decoder
+
+ call reset_capture()
+ call make_jt65_wave(waveform, ooo_tones, 1, 1500.0_real64)
+ call decode_signal(ooo_decoder, waveform, 0, .true., 64)
+ call require_decode('K1ABC W9XYZ FN42', 'JT65A VHF OOO signal')
+ call require(callback_nflip < 0, 'JT65A VHF OOO signal polarity')
+
+ call reset_capture()
+ call decode_signal(ooo_decoder, waveform, 0, .true.)
+ call require(callback_count == 0, 'JT65A HF OOO signal is rejected')
+ end subroutine test_ooo_signal
+
+ subroutine test_silence_and_frequency_rejection(waveform)
+ real(real32), intent(inout) :: waveform(:)
+
+ waveform = 0.0_real32
+ call reset_capture()
+ call decode_signal(decoder, waveform, 0, .true.)
+ call require(callback_count == 0, 'silence produces no callback')
+
+ call make_jt65_wave(waveform, standard_tones, 1, 3000.0_real64)
+ call reset_capture()
+ call decode_signal(decoder, waveform, 0, .true.)
+ call require(callback_count == 0, 'out-of-window signal produces no callback')
+ end subroutine test_silence_and_frequency_rejection
+
+ subroutine test_decoder_reuse(waveform, decoder_to_use)
+ real(real32), intent(inout) :: waveform(:)
+ type(jt65_decoder), intent(inout) :: decoder_to_use
+
+ call make_jt65_wave(waveform, standard_tones, 1, 1500.0_real64)
+ call reset_capture()
+ call decode_signal(decoder_to_use, waveform, 0, .true.)
+ call require_decode('K1ABC W9XYZ FN42', 'first reused-decoder signal')
+
+ waveform = 0.0_real32
+ call reset_capture()
+ call decode_signal(decoder_to_use, waveform, 0, .true.)
+ call require(callback_count == 0, 'reused decoder silence produces no callback')
+
+ call make_jt65_wave(waveform, standard_tones, 1, 1500.0_real64)
+ call reset_capture()
+ call decode_signal(decoder_to_use, waveform, 0, .true.)
+ call require_decode('K1ABC W9XYZ FN42', 'second reused-decoder signal')
+ end subroutine test_decoder_reuse
+
+ subroutine test_auto_clear_average(waveform, decoder_to_use)
+ real(real32), intent(inout) :: waveform(:)
+ type(jt65_decoder), intent(inout) :: decoder_to_use
+ integer, parameter :: averaging_depth = 16 + 128
+ real(real64), parameter :: weak_amplitude = 1.1_real64
+ real(real64), parameter :: noise_amplitude = 25.0_real64
+
+ call make_jt65_wave(waveform, standard_tones, 1, 1500.0_real64, weak_amplitude, &
+ noise_amplitude, 1)
+ call reset_capture()
+ call decode_signal(decoder_to_use, waveform, 0, .true., 64, 1, averaging_depth, .false.)
+ call require(callback_message_count == 0, 'weak JT65 signal is saved without decoding')
+
+ call make_jt65_wave(waveform, standard_tones, 1, 1500.0_real64, weak_amplitude, &
+ noise_amplitude, 3)
+ call reset_capture()
+ call decode_signal(decoder_to_use, waveform, 0, .false., 64, 3, averaging_depth, .false.)
+ call require(callback_message_count == 1, 'two weak JT65 signals produce an averaged decode')
+ call require(callback_max_nsum >= 2, 'averaged JT65 decode reports multiple periods')
+
+ call make_jt65_wave(waveform, standard_tones, 1, 1500.0_real64, weak_amplitude, &
+ noise_amplitude, 5)
+ call reset_capture()
+ call decode_signal(decoder_to_use, waveform, 0, .false., 64, 5, averaging_depth, .false.)
+ call require(callback_message_count == 0, &
+ 'auto-clear prevents a previous JT65 average from decoding again')
+ end subroutine test_auto_clear_average
+
+ subroutine test_duplicate_average_cursor
+ integer :: nsave, ndepth, neme, nftt, nsum, ndeepave, nQSOProgress
+ integer :: ntrials, naggressive
+ logical :: clear_avg65, ljt65apon
+ character(len=12) :: mycall, hiscall
+ character(len=6) :: hisgrid
+ character(len=22) :: avemsg, deepave
+ real :: qave
+
+ s1=0.0
+ s3a=0.0
+ nsave=0
+ ndepth=3
+ neme=0
+ nftt=-1
+ nsum=-1
+ ndeepave=0
+ nQSOProgress=0
+ ntrials=1000
+ naggressive=5
+ clear_avg65=.true.
+ ljt65apon=.false.
+ mycall='K1ABC '
+ hiscall='W9XYZ '
+ hisgrid='FN42 '
+
+ call avg65(1,nsave,1.0,0.0,1,1500,1,100,ndepth,.false.,ntrials, &
+ naggressive,clear_avg65,neme,mycall,hiscall,hisgrid,nftt,avemsg, &
+ qave,deepave,nsum,ndeepave,nQSOProgress,ljt65apon)
+ call require(nsave == 1 .and. nsum == 1, &
+ 'first average observation occupies the initial slot')
+
+ nsave=2
+ call avg65(1,nsave,1.0,0.0,1,1500,1,100,ndepth,.false.,ntrials, &
+ naggressive,clear_avg65,neme,mycall,hiscall,hisgrid,nftt,avemsg, &
+ qave,deepave,nsum,ndeepave,nQSOProgress,ljt65apon)
+ call require(nsave == 1 .and. nsum == 1 .and. nftt == 0, &
+ 'duplicate average observation does not advance the ring')
+ end subroutine test_duplicate_average_cursor
+
+end program test_jt65_decode_pipeline
diff --git a/tests/unit/jt65/test_jt65_source_encoding.f90 b/tests/unit/jt65/test_jt65_source_encoding.f90
new file mode 100644
index 00000000..4462cbd9
--- /dev/null
+++ b/tests/unit/jt65/test_jt65_source_encoding.f90
@@ -0,0 +1,201 @@
+program test_jt65_source_encoding
+ use iso_c_binding, only: c_char, c_int
+ use jt65_test_vectors
+ implicit none
+
+ interface
+ subroutine gen65(message, ichk, sent, tones, itype) bind(C, name='gen65')
+ import c_char, c_int
+ character(kind=c_char) :: message(23), sent(23)
+ integer(kind=c_int) :: ichk, tones(126), itype
+ end subroutine gen65
+ end interface
+
+ call test_standard_message()
+ call test_ooo_message()
+ call test_message_forms()
+ call test_shorthand_messages()
+ print '(a)', 'JT65 source encoding tests passed'
+
+contains
+
+ subroutine encode(text, ichk, sent, tones, itype)
+ character(len=*), intent(in) :: text
+ integer, intent(in) :: ichk
+ character(len=1), intent(out) :: sent(23)
+ integer, intent(out) :: tones(jt65_symbol_count), itype
+ character(len=1) :: message(23)
+ integer :: i
+
+ message = char(0)
+ do i = 1, min(len_trim(text), 22)
+ message(i) = text(i:i)
+ end do
+ sent = char(0)
+ tones = -1
+ itype = -1
+ call gen65(message, ichk, sent, tones, itype)
+ end subroutine encode
+
+ subroutine require(condition, description)
+ logical, intent(in) :: condition
+ character(len=*), intent(in) :: description
+
+ if (.not. condition) then
+ print '(a)', 'FAIL: '//description
+ error stop 1
+ end if
+ end subroutine require
+
+ subroutine require_message(sent, expected, description)
+ character(len=1), intent(in) :: sent(23)
+ character(len=22), intent(in) :: expected
+ character(len=*), intent(in) :: description
+ character(len=22) :: actual
+ integer :: i
+
+ actual = ' '
+ do i = 1, 22
+ actual(i:i) = sent(i)
+ end do
+ call require(actual == expected, description)
+ call require(ichar(sent(23)) == 0, description//' has a null terminator')
+ end subroutine require_message
+
+ subroutine require_tone_domain(tones, description)
+ integer, intent(in) :: tones(jt65_symbol_count)
+ character(len=*), intent(in) :: description
+
+ call require(all(tones == 0 .or. (tones >= 2 .and. tones <= 65)), &
+ description//' stays in the JT65 tone domain')
+ end subroutine require_tone_domain
+
+ subroutine test_standard_message()
+ character(len=1) :: sent(23)
+ integer :: tones(jt65_symbol_count), itype
+ character(len=22) :: expected
+
+ call encode('K1ABC W9XYZ FN42', 0, sent, tones, itype)
+ expected = padded_message('K1ABC W9XYZ FN42')
+ call require_message(sent, expected, 'standard message')
+ call require(itype == 1, 'standard message type')
+ call require(all(tones == standard_tones), 'standard message tone vector')
+ call require_tone_domain(tones, 'standard message')
+ call require(count(tones == 0) == 63, 'standard message sync-symbol count')
+ call require(count(tones /= 0) == 63, 'standard message data-symbol count')
+ end subroutine test_standard_message
+
+ subroutine test_ooo_message()
+ character(len=1) :: sent(23), normal_sent(23)
+ integer :: tones(jt65_symbol_count), normal_tones(jt65_symbol_count), itype
+ integer :: normal_data(63), ooo_data(63)
+ character(len=22) :: expected
+ integer :: i, normal_data_index, ooo_data_index
+
+ call encode('K1ABC W9XYZ FN42', 0, normal_sent, normal_tones, itype)
+ call encode('K1ABC W9XYZ FN42 OOO', 0, sent, tones, itype)
+ expected = padded_message('K1ABC W9XYZ FN42 OOO')
+ call require_message(sent, expected, 'OOO message')
+ call require(itype == 1, 'OOO message type')
+ call require(all(tones == ooo_tones), 'OOO message tone vector')
+ call require_tone_domain(tones, 'OOO message')
+ call require(count(tones == 0) == 63, 'OOO sync-symbol count')
+ call require(count(tones /= 0) == 63, 'OOO data-symbol count')
+
+ normal_data_index = 0
+ ooo_data_index = 0
+ do i = 1, jt65_symbol_count
+ if (normal_tones(i) /= 0) then
+ normal_data_index = normal_data_index + 1
+ normal_data(normal_data_index) = normal_tones(i)
+ call require(tones(i) == 0, 'OOO reverses the normal sync polarity')
+ end if
+ if (tones(i) /= 0) then
+ ooo_data_index = ooo_data_index + 1
+ ooo_data(ooo_data_index) = tones(i)
+ call require(normal_tones(i) == 0, 'normal reverses the OOO sync polarity')
+ end if
+ end do
+ call require(normal_data_index == 63, 'normal data count for OOO comparison')
+ call require(ooo_data_index == 63, 'OOO data count for comparison')
+ call require(all(normal_data == ooo_data), 'OOO preserves the data codeword')
+ end subroutine test_ooo_message
+
+ subroutine test_message_forms()
+ character(len=1) :: sent(23)
+ integer :: tones(jt65_symbol_count), itype
+ character(len=22) :: expected
+
+ call encode('1A/KA1ABC WB9XYZ', 0, sent, tones, itype)
+ expected = padded_message('1A/KA1ABC WB9XYZ')
+ call require_message(sent, expected, 'type 1 prefix message')
+ call require(itype == 2, 'type 1 prefix message type')
+ call require_tone_domain(tones, 'type 1 prefix message')
+
+ call encode('KA1ABC WB9XYZ/P', 0, sent, tones, itype)
+ expected = padded_message('KA1ABC WB9XYZ/P')
+ call require_message(sent, expected, 'type 1 suffix message')
+ call require(itype == 3, 'type 1 suffix message type')
+ call require_tone_domain(tones, 'type 1 suffix message')
+
+ call encode('CQ A000/KA1ABC FM07', 0, sent, tones, itype)
+ expected = padded_message('CQ A000/KA1ABC FM07')
+ call require_message(sent, expected, 'type 2 prefix message')
+ call require(itype == 4, 'type 2 prefix message type')
+ call require_tone_domain(tones, 'type 2 prefix message')
+
+ call encode('CQ WB9XYZ/W4 FM07', 0, sent, tones, itype)
+ expected = padded_message('CQ WB9XYZ/W4 FM07')
+ call require_message(sent, expected, 'type 2 suffix message')
+ call require(itype == 5, 'type 2 suffix message type')
+ call require_tone_domain(tones, 'type 2 suffix message')
+
+ call encode('HELLO WORLD', 0, sent, tones, itype)
+ expected = padded_message('HELLO WORLD')
+ call require_message(sent, expected, 'free text message')
+ call require(itype == 6, 'free text message type')
+ call require_tone_domain(tones, 'free text message')
+ end subroutine test_message_forms
+
+ subroutine test_shorthand_messages()
+ character(len=1) :: sent(23)
+ integer :: tones(jt65_symbol_count), itype
+ character(len=22) :: expected
+ integer :: i, group
+
+ call encode('RO', 0, sent, tones, itype)
+ expected = padded_message('RO')
+ call require_message(sent, expected, 'RO shorthand')
+ call require(itype == 7, 'RO shorthand type')
+ do i = 1, jt65_symbol_count
+ group = 1 + (i - 1) / 4
+ call require(tones(i) == merge(0, 20, mod(group, 2) == 1), &
+ 'RO shorthand symbol group')
+ end do
+
+ call encode('RRR', 0, sent, tones, itype)
+ expected = padded_message('RRR')
+ call require_message(sent, expected, 'RRR shorthand')
+ call require(itype == 7, 'RRR shorthand type')
+ call require(all(tones(1:4) == 0), 'RRR shorthand first group')
+ call require(all(tones(5:8) == 30), 'RRR shorthand second group')
+
+ call encode('73', 0, sent, tones, itype)
+ expected = padded_message('73')
+ call require_message(sent, expected, '73 shorthand')
+ call require(itype == 7, '73 shorthand type')
+ call require(all(tones(1:4) == 0), '73 shorthand first group')
+ call require(all(tones(5:8) == 40), '73 shorthand second group')
+
+ call encode(' k1abc w9xyz fn42 ', 0, sent, tones, itype)
+ expected = padded_message('K1ABC W9XYZ FN42')
+ call require_message(sent, expected, 'message normalization')
+ call require(itype == 1, 'normalized message type')
+
+ call encode('K1ABC W9XYZ FN42 OO', 0, sent, tones, itype)
+ expected = padded_message('K1ABC W9XYZ FN42 OOO')
+ call require_message(sent, expected, 'legacy OO suffix')
+ call require(itype == 1, 'legacy OO suffix type')
+ end subroutine test_shorthand_messages
+
+end program test_jt65_source_encoding
diff --git a/tests/unit/jt65/test_jt65_xcor_silence.f90 b/tests/unit/jt65/test_jt65_xcor_silence.f90
new file mode 100644
index 00000000..d43049b2
--- /dev/null
+++ b/tests/unit/jt65/test_jt65_xcor_silence.f90
@@ -0,0 +1,34 @@
+program test_jt65_xcor_silence
+
+ implicit none
+
+ integer, parameter :: lag1 = -32, lag2 = 82, nhmax = 3413, nsmax = 552
+ real :: ss(nsmax,nhmax), ccf(lag1:lag2), ccf0, flip
+ integer :: lagpk
+
+ common /sync/ ss
+
+ call setup65
+ ss = 0.0
+ lagpk = huge(0)
+
+ call xcor(1,nsmax,126,lag1,lag2,ccf,ccf0,lagpk,flip,0.0,0)
+
+ call require(lagpk == lag1, 'silence returns the first valid lag')
+ call require(all(ccf == 0.0), 'silence produces zero correlation')
+ call require(ccf0 == 0.0, 'silence produces a zero peak')
+ call require(flip == 1.0, 'silence keeps normal polarity')
+
+contains
+
+ subroutine require(condition, description)
+ logical, intent(in) :: condition
+ character(len=*), intent(in) :: description
+
+ if (.not. condition) then
+ print '(a)', 'FAIL: '//description
+ error stop 1
+ end if
+ end subroutine require
+
+end program test_jt65_xcor_silence
diff --git a/tests/unit/jt65/test_map65_jt65_candidate_consistency.f90 b/tests/unit/jt65/test_map65_jt65_candidate_consistency.f90
new file mode 100644
index 00000000..433ce775
--- /dev/null
+++ b/tests/unit/jt65/test_map65_jt65_candidate_consistency.f90
@@ -0,0 +1,39 @@
+program test_map65_jt65_candidate_consistency
+ use extract_mod, only: jt65_candidate_is_consistent
+ implicit none
+
+ call require(jt65_candidate_is_consistent(48,79,0.806,5.509), &
+ 'a weak faded decode with high hard distance remains eligible')
+ call require(jt65_candidate_is_consistent(26,28,0.0,24.628), &
+ 'a strong decode with high matched power remains eligible')
+ call require(jt65_candidate_is_consistent(45,81,0.844,4.463), &
+ 'the randomized holdout decode remains eligible')
+
+ call require(.not.jt65_candidate_is_consistent(47,83,0.829,12.700), &
+ 'the reproduced wideband false decode is rejected')
+ call require(.not.jt65_candidate_is_consistent(42,72,0.0,16.740), &
+ 'a high-power distant codeword is rejected')
+ call require(.not.jt65_candidate_is_consistent(44,81,0.844,7.819), &
+ 'an elevated-power high-burden codeword is rejected')
+ call require(.not.jt65_candidate_is_consistent(44,80,0.893,4.772), &
+ 'an ambiguous high-burden codeword is rejected')
+
+ call require(jt65_candidate_is_consistent(41,79,0.0,20.0), &
+ 'matched power alone does not reject a nearby codeword')
+ call require(jt65_candidate_is_consistent(49,80,0.869,7.499), &
+ 'sub-threshold evidence remains eligible')
+
+ print '(a)', 'MAP65 JT65 candidate consistency tests passed.'
+
+contains
+
+ 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 require
+end program test_map65_jt65_candidate_consistency
diff --git a/tests/unit/jt65/test_map65_jt65_handoff.f90 b/tests/unit/jt65/test_map65_jt65_handoff.f90
new file mode 100644
index 00000000..d263bccd
--- /dev/null
+++ b/tests/unit/jt65/test_map65_jt65_handoff.f90
@@ -0,0 +1,150 @@
+program test_map65_jt65_handoff
+
+ use iso_fortran_env, only: real32, real64
+ use, intrinsic :: ieee_arithmetic, only: ieee_get_flag, ieee_invalid, ieee_is_finite, &
+ ieee_quiet_nan, ieee_set_flag, ieee_value
+ use decode1a_mod
+ use filbig_mod
+ use afc65b_mod, only: afc65b
+ use jt65_test_fixture
+ use jt65_test_vectors
+ use npar_ptrs_mod, only: nsmax_active
+ implicit none
+
+ real(real32), allocatable :: dd(:,:)
+ real(real64), parameter :: map65_base_frequency = 118.0_real64 * 11025.0_real64 / 1024.0_real64
+
+ allocate(dd(4, nsmax_active))
+ call test_mono_filter_output(dd)
+ call test_afc_zero_signal()
+ call test_mode(dd, standard_tones, 1, 1, 'JT65A MAP65 handoff')
+ call test_mode(dd, standard_tones, 2, 1, 'JT65B MAP65 handoff')
+ call test_mode(dd, standard_tones, 4, 1, 'JT65C MAP65 handoff')
+ call test_mode(dd, ooo_tones, 1, -1, 'JT65A OOO MAP65 handoff')
+ call test_mode(dd, standard_tones, 2, 1, 'JT65B xpol MAP65 handoff', .true.)
+ print '(a)', 'MAP65 JT65 handoff tests passed'
+
+contains
+
+ subroutine require(condition, description)
+ logical, intent(in) :: condition
+ character(len=*), intent(in) :: description
+
+ if (.not. condition) then
+ print '(a)', 'FAIL: '//description
+ error stop 1
+ end if
+ end subroutine require
+
+ subroutine test_mono_filter_output(waveform)
+ real(real32), intent(inout) :: waveform(:,:)
+ complex(real32), allocatable :: cx(:), cy(:)
+ integer :: newdat, n5
+ logical :: invalid_raised
+
+ allocate(cx(nsmax_active/64), cy(nsmax_active/64))
+ call make_map65_wave(waveform, standard_tones, 2, map65_base_frequency)
+ waveform(3:4,:) = ieee_value(0.0_real32, ieee_quiet_nan)
+ newdat = 1
+ call ieee_set_flag(ieee_invalid, .false.)
+ call filbig(waveform, nsmax_active, map65_base_frequency, newdat, 96000, .false., cx, cy, n5)
+ call ieee_get_flag(ieee_invalid, invalid_raised)
+
+ call require(.not. invalid_raised, 'mono filter avoids invalid inactive-channel arithmetic')
+ call require(all(ieee_is_finite(real(cy(:n5)))) .and. &
+ all(ieee_is_finite(aimag(cy(:n5)))), 'mono filter output is finite')
+ call require(all(abs(cy(:n5)) == 0.0), 'mono filter output is zero')
+ end subroutine test_mono_filter_output
+
+ subroutine test_afc_zero_signal()
+ integer, parameter :: npts = 1024
+ complex :: cx(npts), cy(npts)
+ real :: a(5), ccfbest, dtbest
+ integer :: ipol, nflip, ndphi
+ logical :: invalid_raised
+
+ cx = (0.0, 0.0)
+ cy = (0.0, 0.0)
+ a = 0.0
+ ipol = 1
+ nflip = 1
+ ndphi = 0
+ call ieee_set_flag(ieee_invalid, .false.)
+ call afc65b(cx, cy, npts, 1378.125/4.0, nflip, ipol, .false., ndphi, a, ccfbest, dtbest)
+ call ieee_get_flag(ieee_invalid, invalid_raised)
+
+ call require(.not. invalid_raised, 'flat AFC objective avoids invalid arithmetic')
+ call require(all(ieee_is_finite(a(1:4))) .and. ieee_is_finite(ccfbest) .and. &
+ ieee_is_finite(dtbest), 'flat AFC objective produces finite outputs')
+ call require(all(a(1:3) == 0.0) .and. a(4) == 0.0, &
+ 'flat AFC objective preserves the accepted parameter point')
+ call require(ccfbest == 0.0 .and. dtbest == 0.0 .and. ipol == 1, &
+ 'flat AFC objective has explicit detection defaults')
+ end subroutine test_afc_zero_signal
+
+ subroutine test_mode(waveform, tones, mode65, polarity, description, cross_polarized)
+ real(real32), intent(inout) :: waveform(:,:)
+ integer, intent(in) :: tones(:)
+ integer, intent(in) :: mode65
+ integer, intent(in) :: polarity
+ character(len=*), intent(in) :: description
+ logical, intent(in), optional :: cross_polarized
+ integer :: newdat, nflip, nfsample, neme, ndepth, nqd, ndphi
+ integer :: nutc, nkhz, ndf, ipol, ntol, nkv, nhist, nsum, nsave
+ logical :: invalid_raised, xpol
+ character(len=12) :: mycall, hiscall
+ character(len=6) :: hisgrid
+ character(len=22) :: decoded
+ real :: dphi, sync2, a(5), dt, pol, qual
+
+ call make_map65_wave(waveform, tones, mode65, map65_base_frequency)
+ xpol = .false.
+ if (present(cross_polarized)) xpol = cross_polarized
+ if (xpol) then
+ waveform(3:4,:) = waveform(1:2,:)
+ else
+ waveform(3:4,:) = ieee_value(0.0_real32, ieee_quiet_nan)
+ endif
+ newdat = 1
+ nflip = polarity
+ nfsample = 96000
+ mycall = 'K1ABC '
+ hiscall = 'W9XYZ '
+ hisgrid = 'FN42 '
+ neme = 0
+ ndepth = 3
+ nqd = 1
+ dphi = 0.0
+ ndphi = 0
+ nutc = 1
+ nkhz = 1
+ ndf = 0
+ ipol = 0
+ ntol = 100
+ sync2 = 0.0
+ a = 0.0
+ dt = -1.7
+ pol = 0.0
+ nkv = -1
+ nhist = -1
+ nsum = 0
+ nsave = 0
+ qual = -999.0
+ decoded = ' '
+ call ieee_set_flag(ieee_invalid, .false.)
+ call decode1a(waveform, newdat, map65_base_frequency, nflip, mode65, nfsample, xpol, mycall, hiscall, &
+ hisgrid, neme, ndepth, nqd, dphi, ndphi, nutc, nkhz, ndf, ipol, ntol, sync2, &
+ a, dt, pol, nkv, nhist, nsum, nsave, qual, decoded)
+ call ieee_get_flag(ieee_invalid, invalid_raised)
+
+ call require(decoded == padded_message('K1ABC W9XYZ FN42'), description//' message')
+ call require(.not. invalid_raised, description//' avoids invalid arithmetic')
+ call require(newdat == 0, description//' consumes new data')
+ call require(ieee_is_finite(sync2), description//' finite sync metric')
+ call require(ieee_is_finite(dt), description//' finite timing')
+ call require(ieee_is_finite(pol), description//' finite polarization')
+ call require(ieee_is_finite(qual), description//' finite quality')
+ call require(ieee_is_finite(a(1)) .and. ieee_is_finite(a(2)), description//' finite AFC')
+ end subroutine test_mode
+
+end program test_map65_jt65_handoff
diff --git a/tests/unit/jt65/test_map65_jt65_message_validity.f90 b/tests/unit/jt65/test_map65_jt65_message_validity.f90
new file mode 100644
index 00000000..ec1f3dff
--- /dev/null
+++ b/tests/unit/jt65/test_map65_jt65_message_validity.f90
@@ -0,0 +1,77 @@
+program test_map65_jt65_message_validity
+ use packjt, only: is_valid_jt65_codeword, packmsg, packcall, packgrid
+ implicit none
+
+ integer, parameter :: NBASE = 37*36*10*27*27*27
+ integer :: dat(12), itype, nc1, ng
+ logical :: text
+ character(len=4) :: grid
+ character(len=6) :: call
+ character(len=22) :: message
+
+ message = 'K1ABC W9XYZ FN42'
+ call packmsg(message, dat, itype)
+ call require(is_valid_jt65_codeword(dat), 'a standard structured message is valid')
+
+ message = 'THIS IS FREE TEXT'
+ call packmsg(message, dat, itype)
+ call require(is_valid_jt65_codeword(dat), 'a free-text message is valid')
+
+ call = '498IVA'
+ call packcall(call, nc1, text)
+ grid = 'EE84'
+ call packgrid(grid, ng, text)
+ call pack_fields(nc1, 265779995, ng, dat)
+ call require(.not. is_valid_jt65_codeword(dat), &
+ 'the reproduced false decode has an impossible second callsign')
+
+ call pack_fields(NBASE + 1, NBASE - 1, 0, dat)
+ call require(is_valid_jt65_codeword(dat), 'a reserved first-field message is valid')
+
+ call pack_fields(267796945, NBASE - 1, 0, dat)
+ call require(is_valid_jt65_codeword(dat), 'the final JT65v2 first-field value is valid')
+
+ call pack_fields(NBASE, 0, 0, dat)
+ call require(.not. is_valid_jt65_codeword(dat), &
+ 'the unassigned first-field value is invalid')
+
+ call pack_fields(267796946, 0, 0, dat)
+ call require(.not. is_valid_jt65_codeword(dat), &
+ 'a first field above the JT65v2 range is invalid')
+
+ call pack_fields(0, NBASE, 0, dat)
+ call require(.not. is_valid_jt65_codeword(dat), &
+ 'a non-callsign second field is invalid')
+
+ print '(a)', 'MAP65 JT65 message validity tests passed.'
+
+contains
+
+ subroutine pack_fields(first_call, second_call, packed_grid, symbols)
+ integer, intent(in) :: first_call, second_call, packed_grid
+ integer, intent(out) :: symbols(12)
+
+ symbols(1) = iand(ishft(first_call,-22),63)
+ symbols(2) = iand(ishft(first_call,-16),63)
+ symbols(3) = iand(ishft(first_call,-10),63)
+ symbols(4) = iand(ishft(first_call,-4),63)
+ symbols(5) = 4*iand(first_call,15) + iand(ishft(second_call,-26),3)
+ symbols(6) = iand(ishft(second_call,-20),63)
+ symbols(7) = iand(ishft(second_call,-14),63)
+ symbols(8) = iand(ishft(second_call,-8),63)
+ symbols(9) = iand(ishft(second_call,-2),63)
+ symbols(10) = 16*iand(second_call,3) + iand(ishft(packed_grid,-12),15)
+ symbols(11) = iand(ishft(packed_grid,-6),63)
+ symbols(12) = iand(packed_grid,63)
+ end subroutine pack_fields
+
+ 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 require
+end program test_map65_jt65_message_validity
diff --git a/tests/unit/jt65/test_map65_s3avg_contracts.f90 b/tests/unit/jt65/test_map65_s3avg_contracts.f90
new file mode 100644
index 00000000..9a8b1bce
--- /dev/null
+++ b/tests/unit/jt65/test_map65_s3avg_contracts.f90
@@ -0,0 +1,137 @@
+program test_map65_s3avg_contracts
+ use encode65_mod, only: encode65
+ use s3avg_mod, only: s3avg
+ implicit none
+
+ call test_group_isolation_and_tolerance
+ call test_timing_and_duplicates
+ print '(a)', 'MAP65 s3avg contract tests passed.'
+
+contains
+
+ subroutine test_group_isolation_and_tolerance
+ real :: s3(64,63)
+ character(len=22) :: target, decoded
+ integer :: code(63), nsave, nsum, nkv
+
+ target='K1ABC W9XYZ FN42'
+ call make_spectrum(target,code,s3)
+ nsave=0
+
+ call next_slot(nsave)
+ nsum=-77
+ call s3avg(nsave,1,1000,1000,0.0,1,1,100,s3,nsum,nkv,decoded)
+ call require(nsum == 1 .and. nkv == 0 .and. len_trim(decoded) == 0, &
+ 'first frequency observation is isolated')
+
+ call next_slot(nsave)
+ call s3avg(nsave,1,1002,1000,0.0,2,1,100,s3,nsum,nkv,decoded)
+ call require(nsum == 2 .and. nkv == 2 .and. decoded(1:len_trim(target)) == target, &
+ 'same-parity frequency observations decode as a pair')
+
+ call next_slot(nsave)
+ call s3avg(nsave,1,1003,3000,0.0,1,1,100,s3,nsum,nkv,decoded)
+ call require(nsum == 1 .and. nkv == 0 .and. len_trim(decoded) == 0, &
+ 'frequency-separated observations do not join the group')
+
+ call next_slot(nsave)
+ call s3avg(nsave,1,1101,5000,0.0,1,1,100,s3,nsum,nkv,decoded)
+ call require(nsum == 1 .and. nkv == 0, 'dynamic tolerance starts a new group')
+
+ call next_slot(nsave)
+ call s3avg(nsave,1,1103,5050,0.0,1,1,50,s3,nsum,nkv,decoded)
+ call require(nsum == 2 .and. nkv == 2 .and. decoded(1:len_trim(target)) == target, &
+ 'dynamic tolerance includes its inclusive boundary')
+
+ call next_slot(nsave)
+ call s3avg(nsave,1,1105,5000,0.0,1,1,10,s3,nsum,nkv,decoded)
+ call require(nsum == 2 .and. nkv == 2 .and. decoded(1:len_trim(target)) == target, &
+ 'dynamic tolerance excludes the saved 5050-Hz frame')
+
+ call next_slot(nsave)
+ call s3avg(nsave,1,1107,5011,0.0,1,1,10,s3,nsum,nkv,decoded)
+ call require(nsum == 1 .and. nkv == 0 .and. len_trim(decoded) == 0, &
+ 'dynamic tolerance rejects frequencies outside its boundary')
+ end subroutine test_group_isolation_and_tolerance
+
+ subroutine test_timing_and_duplicates
+ real :: s3(64,63)
+ character(len=22) :: target, decoded
+ integer :: code(63), nsave, nsum, nkv, duplicate_slot
+
+ target='K1ABC W9XYZ FN42'
+ call make_spectrum(target,code,s3)
+ nsave=7
+
+ call next_slot(nsave)
+ call s3avg(nsave,1,1200,7000,0.00,1,1,100,s3,nsum,nkv,decoded)
+ call require(nsum == 1 .and. nkv == 0, 'timing fixture starts alone')
+
+ call next_slot(nsave)
+ call s3avg(nsave,1,1202,7000,0.19,1,1,100,s3,nsum,nkv,decoded)
+ call require(nsum == 2 .and. nkv == 2 .and. decoded(1:len_trim(target)) == target, &
+ 'nearby timing observations join the group')
+
+ call next_slot(nsave)
+ call s3avg(nsave,1,1204,7000,0.50,1,1,100,s3,nsum,nkv,decoded)
+ call require(nsum == 1 .and. nkv == 0 .and. len_trim(decoded) == 0, &
+ 'distant timing observations remain isolated')
+
+ call next_slot(nsave)
+ call s3avg(nsave,1,1300,9000,0.00,1,1,100,s3,nsum,nkv,decoded)
+ call require(nsum == 1 .and. nkv == 0, 'duplicate fixture starts alone')
+ duplicate_slot=nsave
+
+ call next_slot(nsave)
+ call s3avg(nsave,1,1300,9000,0.01,1,1,100,s3,nsum,nkv,decoded)
+ call require(nsum == 1 .and. nkv == 0 .and. nsave == duplicate_slot, &
+ 'same-UTC duplicate is suppressed and ring position rolls back')
+
+ call next_slot(nsave)
+ call s3avg(nsave,1,1302,9000,0.02,1,1,100,s3,nsum,nkv,decoded)
+ call require(nsum == 2 .and. nkv == 2 .and. decoded(1:len_trim(target)) == target, &
+ 'next same-parity observation forms exactly a pair after duplicate')
+
+ call next_slot(nsave)
+ call s3avg(nsave,1,1400,11000,0.00,1,1,100,s3,nsum,nkv,decoded)
+ call require(nsum == 1 .and. nkv == 0, 'sync-polarity fixture starts alone')
+
+ call next_slot(nsave)
+ call s3avg(nsave,1,1402,11000,0.00,1,-1,100,s3,nsum,nkv,decoded)
+ call require(nsum == 1 .and. nkv == 0 .and. len_trim(decoded) == 0, &
+ 'opposite sync polarities do not join the group')
+
+ call next_slot(nsave)
+ call s3avg(nsave,1,1404,11000,0.00,1,-1,100,s3,nsum,nkv,decoded)
+ call require(nsum == 2 .and. nkv == 2 .and. decoded(1:len_trim(target)) == target, &
+ 'matching negative sync polarities decode as a pair')
+ end subroutine test_timing_and_duplicates
+
+ subroutine make_spectrum(message,code,s3)
+ character(len=*), intent(in) :: message
+ integer, intent(out) :: code(63)
+ real, intent(out) :: s3(64,63)
+ integer :: j
+
+ call encode65(message,code)
+ s3=1.0
+ do j=1,63
+ s3(code(j)+1,j)=100.0
+ enddo
+ end subroutine make_spectrum
+
+ subroutine next_slot(nsave)
+ integer, intent(inout) :: nsave
+ nsave=mod(nsave,64)+1
+ end subroutine next_slot
+
+ 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 require
+
+end program test_map65_s3avg_contracts