summaryrefslogtreecommitdiff
path: root/tests/unit/map65
diff options
context:
space:
mode:
authorDave Hibberd <hibby@debian.org>2026-10-20 22:35:06 +0100
committerDave Hibberd <hibby@debian.org>2026-10-20 22:35:06 +0100
commit82ebb1ed1af30cee4dfbb1c628431d32885a00e0 (patch)
tree1a1f782d3e576b758be0bc61171574a7cd8f2376 /tests/unit/map65
parent061658210acb6425517733ea012dda803a7f72a1 (diff)
parent61d684922b0f1c8455cc60eb3d123df058c9e1f5 (diff)
Merge tag 'debian/3.3.0_beta2+dfsg-1' into hibbian/trixie-backports
tagging wsjtx 3.3.0~beta2+dfsg-1
Diffstat (limited to 'tests/unit/map65')
-rw-r--r--tests/unit/map65/CMakeLists.txt11
-rw-r--r--tests/unit/map65/decode_history/decode_history_stubs.f9016
-rw-r--r--tests/unit/map65/decode_history/test_map65_decode_history.f90108
-rw-r--r--tests/unit/map65/messages_retention/CMakeLists.txt17
-rw-r--r--tests/unit/map65/messages_retention/messages_retention_stubs.f9035
-rw-r--r--tests/unit/map65/messages_retention/test_map65_messages_retention.f90189
-rw-r--r--tests/unit/map65/test_map65_deep_search_cache.f905
-rw-r--r--tests/unit/map65/test_map65_filbig_boundary.f9064
-rw-r--r--tests/unit/map65/test_map65_pass_snapshots.f90100
-rw-r--r--tests/unit/map65/test_map65_ptt_unix.c489
-rw-r--r--tests/unit/map65/test_map65_ptt_windows.c323
-rw-r--r--tests/unit/map65/test_map65_q65_dphi.f9017
-rw-r--r--tests/unit/map65/test_map65_q65_handoff.f90117
-rw-r--r--tests/unit/map65/test_map65_symspec_contract.f90118
-rw-r--r--tests/unit/map65/windows_fake/windows.h29
15 files changed, 1630 insertions, 8 deletions
diff --git a/tests/unit/map65/CMakeLists.txt b/tests/unit/map65/CMakeLists.txt
index 88064d64..d02e4604 100644
--- a/tests/unit/map65/CMakeLists.txt
+++ b/tests/unit/map65/CMakeLists.txt
@@ -20,3 +20,14 @@ add_executable (test_map65_runtime_paths
target_include_directories (test_map65_runtime_paths PRIVATE ${CMAKE_SOURCE_DIR}/map65)
target_link_libraries (test_map65_runtime_paths Qt5::Test)
add_test (NAME test_map65_runtime_paths COMMAND test_map65_runtime_paths)
+
+if (UNIX)
+ add_executable (test_map65_ptt_unix test_map65_ptt_unix.c)
+ set_target_properties (test_map65_ptt_unix PROPERTIES AUTOMOC OFF)
+ add_test (NAME test_map65_ptt_unix COMMAND test_map65_ptt_unix)
+endif ()
+
+add_executable (test_map65_ptt_windows test_map65_ptt_windows.c)
+target_include_directories (test_map65_ptt_windows PRIVATE windows_fake)
+set_target_properties (test_map65_ptt_windows PROPERTIES AUTOMOC OFF)
+add_test (NAME test_map65_ptt_windows COMMAND test_map65_ptt_windows)
diff --git a/tests/unit/map65/decode_history/decode_history_stubs.f90 b/tests/unit/map65/decode_history/decode_history_stubs.f90
index 9582e19a..89dd9bac 100644
--- a/tests/unit/map65/decode_history/decode_history_stubs.f90
+++ b/tests/unit/map65/decode_history/decode_history_stubs.f90
@@ -5,6 +5,12 @@ module decode_history_observations
integer :: jt65_attempts = 0, q65_successes = 0
integer :: phase_attempts(0:12) = 0
integer :: summary_count = 0, fitted_phase = -999
+ integer :: display_calls = 0
+ ! decode1a's fit values (a), and whether ccf65 reports each bin's
+ ! shorthand sync from ss(2,1) of that bin instead of -99, so a test can
+ ! place a shorthand's two tones in chosen bins.
+ real :: stub_fit = 0.0
+ logical :: stub_short_from_ss = .false.
logical :: phase_has_signal(0:12) = .true.
end module
@@ -47,6 +53,7 @@ contains
end module
module ccf65_legacy_mod
+ use decode_history_observations
implicit none
contains
subroutine ccf65(ss_plane, nhsym, ssmax, sync1, ipol1, jpz, dt1, flipk, &
@@ -59,6 +66,7 @@ contains
dt1 = 0.0
flipk = 1.0
syncshort = -99.0
+ if (stub_short_from_ss) syncshort = ss_plane(2,1)
snr2 = 0.0
dt2 = 0.0
ipol1 = 1
@@ -96,7 +104,7 @@ contains
endif
decoded = 'K1ABC W9XYZ FN42'
sync2 = 100.0
- a = 0.0
+ a = stub_fit
dt = 0.0
pol = 0.0
qual = 5.0 + 4.0*sin(dphi)
@@ -139,11 +147,13 @@ contains
end module
module display_mod
+ use decode_history_observations
implicit none
contains
- subroutine display(nkeep, ftol)
- integer, intent(in) :: nkeep
+ subroutine display(nkeep, ftol, nutc)
+ integer, intent(in) :: nkeep, nutc
real, intent(in) :: ftol
+ display_calls = display_calls + 1
end subroutine
end module
diff --git a/tests/unit/map65/decode_history/test_map65_decode_history.f90 b/tests/unit/map65/decode_history/test_map65_decode_history.f90
index eb5ce18c..bd2eb9dc 100644
--- a/tests/unit/map65/decode_history/test_map65_decode_history.f90
+++ b/tests/unit/map65/decode_history/test_map65_decode_history.f90
@@ -4,10 +4,17 @@ program test_map65_decode_history
use datcom_ptrs_mod, only: ss_old, savg_old
use decodes_mod, only: nhsym1, nhsym2
use map65a_mod, only: map65a
+ use message_history_mod, only: message_record, recent_messages
use decode_history_observations
implicit none
real, allocatable :: samples(:,:)
integer :: unit, before_jt65, before_q65
+ integer :: messages, rx_log, before_display
+ integer :: band_nfa = 0, band_nfb = 0, cursor_khz = 0, fits(3), test_nmode = 11
+ integer :: click_bin
+ real :: rec_khz
+ character(len=128) :: rec
+ character(len=*), parameter :: JT65_MSG = 'K1ABC W9XYZ FN42'
! Only the center bin has signal power; DSP stand-ins never read samples.
nfft_active = 1024
@@ -60,9 +67,84 @@ program test_map65_decode_history
call run_pass(nhsym2, 0, 0)
call require(jt65_attempts == before_jt65+2 .and. q65_successes == before_q65+2, &
'final pass retains successes after a skipped-period boundary')
+
+ ! A double-click decode reaches Messages/Band Map and map65_rx.log
+ ! without repeating a decode already recorded by the wideband pass.
+ messages = count_history_records(JT65_MSG)
+ rx_log = count_records(21, JT65_MSG)
+ call require(messages >= 1 .and. rx_log >= 1, 'wideband pass records the JT65 decode')
+ before_jt65 = jt65_attempts
+ before_display = display_calls
+ call click()
+ call require(jt65_attempts == before_jt65+1, 'click decodes JT65 at the cursor')
+ call require(count_history_records(JT65_MSG) == messages .and. count_records(21, JT65_MSG) == rx_log, &
+ 'click does not repeat a decode the wideband pass recorded')
+ call require(display_calls == before_display+1, 'click refreshes the Messages window')
+ active_input_generation = 4
+ call click()
+ call require(count_history_records(JT65_MSG) == messages+1 .and. count_records(21, JT65_MSG) == rx_log+1, &
+ 'click records a decode the wideband pass did not')
+ call click()
+ call require(count_history_records(JT65_MSG) == messages+1 .and. count_records(21, JT65_MSG) == rx_log+1, &
+ 'second click on the same signal is not recorded again')
+
+ ! A click on a JT65C RRR shorthand: its sync tone in the clicked bin and
+ ! the upper tone 30*4 tone spacings (~3 bins) above, noise elsewhere. It
+ ! must be found although the click's best JT65 sync bin is elsewhere, be
+ ! labeled RRR, be recorded at the clicked kHz (band center (nfa+nfb)/2 =
+ ! 130 kHz, cursor 130 kHz: 144.130 MHz), and carry no fit values left in
+ ! sig by an earlier decode.
+ stub_fit = 7.0
+ active_input_generation = 5
+ call run_pass(nhsym1, 0, 0)
+ stub_fit = 0.0
+ click_bin = nfft_active/2 + 1
+ ss_old(2,1,:) = -99.0
+ ss_old(2,1,click_bin) = 10.0
+ ss_old(2,1,click_bin + nint(3*4*10.0*(11025.0/4096.0)/(96000.0/nfft_active))) = 10.0
+ stub_short_from_ss = .true.
+ test_nmode = 13
+ band_nfa = 100
+ band_nfb = 160
+ cursor_khz = 130
+ active_input_generation = 6
+ call click()
+ call require(last_history_record('RRR', rec), 'shorthand click is found and labeled RRR')
+ read(rec(1:8), *) rec_khz
+ read(rec(14:22), '(3i3)') fits
+ call require(abs(rec_khz - 144.130) < 0.0005, 'shorthand click is recorded at the clicked kHz')
+ call require(all(fits == 0), 'shorthand click records no stale fit values')
print '(a)', 'MAP65 decode history tests passed.'
contains
+ integer function count_history_records(text)
+ character(len=*), intent(in) :: text
+ type(message_record), allocatable :: records(:)
+ integer :: i
+
+ call recent_messages(huge(0), records)
+ count_history_records = 0
+ do i = 1, size(records)
+ if (index(records(i)%line, text) > 0) count_history_records = count_history_records + 1
+ enddo
+ end function
+
+ logical function last_history_record(text, found)
+ character(len=*), intent(in) :: text
+ character(len=*), intent(out) :: found
+ type(message_record), allocatable :: records(:)
+ integer :: i
+
+ call recent_messages(huge(0), records)
+ last_history_record = .false.
+ found = ' '
+ do i = 1, size(records)
+ if (index(records(i)%line, text) == 0) cycle
+ found = records(i)%line
+ last_history_record = .true.
+ enddo
+ end function
+
subroutine run_pass(half_symbols, again, find_phase)
integer, intent(in) :: half_symbols, again, find_phase
integer :: newdat, utc, done, ndphi, mcall3b, nsum, nsave
@@ -74,11 +156,33 @@ contains
nsum = 0
nsave = 0
call system_clock(t_start)
- call map65a(samples,newdat,utc,144.0_real64,100,0,0,0,0,0,again,done,0,ndphi,0, &
+ call map65a(samples,newdat,utc,144.0_real64,100,0,band_nfa,band_nfb,0,cursor_khz, &
+ again,done,0,ndphi,0, &
-1270,20,mcall3b,nsum,nsave,0,'K1ABC ','FN42 ',0,3,0, &
- 'W9XYZ ','EN50 ',half_symbols,96000,0,1,11,0)
+ 'W9XYZ ','EN50 ',half_symbols,96000,0,1,test_nmode,0)
end subroutine
+ subroutine click()
+ ! MainWindow::freezeDecode() sets nagain=1 and the manual decode flag.
+ manualDecodeFlag = 1
+ call run_pass(nhsym2, 1, 0)
+ end subroutine
+
+ integer function count_records(u, text)
+ integer, intent(in) :: u
+ character(len=*), intent(in) :: text
+ character(len=128) :: rec
+ integer :: ios
+ count_records = 0
+ rewind(u)
+ do
+ read(u, '(a)', iostat=ios) rec
+ if (ios /= 0) exit
+ if (index(rec, text) > 0) count_records = count_records + 1
+ enddo
+ backspace(u)
+ end function
+
subroutine require(condition, description)
logical, intent(in) :: condition
character(len=*), intent(in) :: description
diff --git a/tests/unit/map65/messages_retention/CMakeLists.txt b/tests/unit/map65/messages_retention/CMakeLists.txt
new file mode 100644
index 00000000..407ae630
--- /dev/null
+++ b/tests/unit/map65/messages_retention/CMakeLists.txt
@@ -0,0 +1,17 @@
+# Run the production display() against a stand-in stdout channel that
+# records the "@" (Messages window) lines instead of sending them to the GUI.
+# Keep replacement module files out of the production module directory.
+add_executable (test_map65_messages_retention
+ messages_retention_stubs.f90
+ test_map65_messages_retention.f90
+ ${CMAKE_SOURCE_DIR}/map65/libm65/message_history_mod.f90
+ ${CMAKE_SOURCE_DIR}/map65/libm65/display.f90)
+target_include_directories (test_map65_messages_retention BEFORE PRIVATE
+ ${CMAKE_CURRENT_BINARY_DIR}
+ ${PROJECT_BINARY_DIR}/map65/libm65)
+target_link_libraries (test_map65_messages_retention PRIVATE m65impl)
+target_compile_options (test_map65_messages_retention PRIVATE
+ $<$<COMPILE_LANG_AND_ID:Fortran,GNU>:-fcheck=bounds>)
+add_test (NAME test_map65_messages_retention COMMAND test_map65_messages_retention)
+set_tests_properties (test_map65_messages_retention PROPERTIES
+ WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} TIMEOUT 60)
diff --git a/tests/unit/map65/messages_retention/messages_retention_stubs.f90 b/tests/unit/map65/messages_retention/messages_retention_stubs.f90
new file mode 100644
index 00000000..a92d952e
--- /dev/null
+++ b/tests/unit/map65/messages_retention/messages_retention_stubs.f90
@@ -0,0 +1,35 @@
+! Stand-in for the decoder's stdout channel: summarizes the "@" (Messages)
+! and "&" (Band Map) lines one display() call sends to the GUI.
+module stdout_channel_mod
+ implicit none
+ integer :: shown = 0, shown_at_utc = 0, want_utc = -1, oldest_utc = 9999
+ integer :: bandmap_entries = 0, display_begins = 0
+ integer :: first_shown_utc = -1, second_shown_utc = -1
+ character(len=3) :: first_bandmap_frequency = ' '
+ ! Mode character (# JT65, : Q65) of each of the first 8 Band Map entries.
+ character(len=8) :: bandmap_modes = ' '
+ character(len=4) :: first_bandmap_utc = ' '
+ character(len=12) :: first_bandmap_call = ' '
+contains
+ subroutine write_stdout(line)
+ character(len=*), intent(in) :: line
+ integer :: utc
+ if (index(line, '<Map65DisplayBegin>') == 1) display_begins = display_begins + 1
+ if (line(1:1) == '&' .and. len_trim(line) > 2) then
+ bandmap_entries = bandmap_entries + 1
+ if (bandmap_entries == 1) first_bandmap_frequency = line(2:4)
+ ! "&" + kHz(3) + ndf(5) + " " + callsign(12) + age(2) + mode(1) + UTC(4)
+ if (bandmap_entries == 1 .and. len(line) >= 22) first_bandmap_call = line(11:22)
+ if (bandmap_entries <= 8 .and. len(line) >= 25) &
+ bandmap_modes(bandmap_entries:bandmap_entries) = line(25:25)
+ if (bandmap_entries == 1 .and. len(line) >= 29) first_bandmap_utc = line(26:29)
+ endif
+ if (line(1:1) /= '@') return
+ shown = shown + 1
+ read(line(20:23), '(i4)') utc ! hhmm column of the "@" line
+ if (shown == 1) first_shown_utc = utc
+ if (shown == 2) second_shown_utc = utc
+ if (utc == want_utc) shown_at_utc = shown_at_utc + 1
+ oldest_utc = min(oldest_utc, utc)
+ end subroutine
+end module
diff --git a/tests/unit/map65/messages_retention/test_map65_messages_retention.f90 b/tests/unit/map65/messages_retention/test_map65_messages_retention.f90
new file mode 100644
index 00000000..b5b26547
--- /dev/null
+++ b/tests/unit/map65/messages_retention/test_map65_messages_retention.f90
@@ -0,0 +1,189 @@
+program test_map65_messages_retention
+ use iso_fortran_env, only: int64, real64
+ use stdout_channel_mod
+ use display_mod, only: display
+ use message_history_mod, only: message_record, append_message, clear_messages, recent_messages, &
+ message_expired
+ implicit none
+ integer :: missed, retained, begins_before
+
+ ! A light band keeps exactly Timeout minutes of history.
+ call run_band(40, 3, 60, missed, retained)
+ call require(missed == 0, 'light band: every minute''s decodes are shown')
+ call require(oldest_utc == 1219, '40-minute timeout keeps 40 minutes (1219-1259)')
+ call run_band(10, 3, 60, missed, retained)
+ call require(oldest_utc == 1249, '10-minute timeout keeps 10 minutes (1249-1259)')
+
+ ! History retains the timeout interval while display shows the newest rows.
+ call run_band(40, 25, 120, missed, retained)
+ call require(missed == 0, 'busy band: newest decodes shown every minute')
+ call require(retained == 1025, 'busy band: all 40 minutes remain in memory')
+ call run_band(20, 18, 120, missed, retained)
+ call require(missed == 0, '20-minute timeout, 18 decodes/min: newest shown every minute')
+ call test_bandmap_capacity()
+ call test_midnight_expiry()
+ call test_bandmap_newest_across_groups()
+ call test_expiry_edge()
+ call test_bandmap_modes()
+ call test_bandmap_compound_call()
+ call clear_messages()
+ shown = 0
+ begins_before = display_begins
+ call display(10, 0.010, 1200)
+ call require(shown == 0, 'empty history emits no Messages rows')
+ call require(display_begins == begins_before + 1, 'empty history emits a snapshot boundary')
+ print '(a)', 'MAP65 Messages retention tests passed.'
+
+contains
+ subroutine run_band(nkeep, per_min, minutes, missed, retained)
+ integer, intent(in) :: nkeep, per_min, minutes
+ integer, intent(out) :: missed, retained
+ integer :: k, minute, nutc
+ character(len=22) :: msg
+ character(len=83) :: rec
+ real(real64) :: f0
+ type(message_record), allocatable :: records(:)
+ call clear_messages()
+ missed = 0
+ do minute = 0, minutes - 1
+ nutc = 100*(12 + minute/60) + mod(minute, 60)
+ do k = 1, per_min
+ f0 = 144.0_real64 + 0.001_real64*(100 + 3*k)
+ write(msg, '(a,i3.3,a)') 'CQ K', k, 'X FN42'
+ write(rec, 1014) f0, 0, 0, 0, 0, 0.0, 0, 5, -20, nutc, msg, '#', ' ', '#B'
+ call append_message(rec, nutc, f0, 0)
+ enddo
+ call display(nkeep, 0.010, nutc)
+ shown = 0; shown_at_utc = 0; want_utc = nutc; oldest_utc = 9999
+ call display(nkeep, 0.010, nutc)
+ if (shown_at_utc /= per_min) missed = missed + 1
+ enddo
+ call recent_messages(2000, records)
+ retained = size(records)
+1014 format(f8.3, i5, 3i3, f5.1, i4, i3, i4, i5.4, 4x, a22, 7x, 2a1, 2x, a2)
+ end subroutine
+
+ ! Records exactly Timeout minutes old by UTC are kept whatever mode stored
+ ! them. A Q65 record is stored during the pass, up to about 20 s before a
+ ! JT65 record stored at its end, so it is older by the wall clock.
+ subroutine test_expiry_edge()
+ call require(.not. message_expired(10, 600_int64, 10), 'edge: record stored at the end of the pass is kept')
+ call require(.not. message_expired(10, 620_int64, 10), 'edge: record stored earlier in the pass is kept')
+ call require(message_expired(11, 660_int64, 10), 'edge: record older than the Timeout by UTC expires')
+ call require(message_expired(10, 86400_int64, 10), 'edge: same UTC minute a day later expires')
+ call require(.not. message_expired(10, -1_int64, 10), 'edge: unknown storage time falls back to UTC age')
+ end subroutine
+
+ subroutine test_midnight_expiry()
+ character(len=83) :: rec
+ character(len=22) :: msg
+ type(message_record), allocatable :: records(:)
+
+ call clear_messages()
+ msg = 'CQ K001X FN42'
+ write(rec, 1014) 144.100_real64, 0, 0, 0, 0, 0.0, 0, 5, -20, 2358, msg, '#', ' ', '#B'
+ call append_message(rec, 2358, 144.100_real64, 0)
+ write(rec, 1014) 144.101_real64, 0, 0, 0, 0, 0.0, 0, 5, -20, 1, msg, '#', ' ', '#B'
+ call append_message(rec, 1, 144.101_real64, 0)
+ shown = 0; bandmap_entries = 0; first_bandmap_frequency = ' '
+ call display(10, 0.010, 2)
+ call recent_messages(10, records)
+ call require(size(records) == 2, 'midnight: recent records remain')
+ call require(first_shown_utc == 2358 .and. second_shown_utc == 1, &
+ 'midnight: Messages preserve chronological order')
+ call require(bandmap_entries == 1 .and. first_bandmap_frequency == '101', &
+ 'midnight: Band Map uses the newest frequency for a call')
+ call display(10, 0.010, 10)
+ call recent_messages(10, records)
+ call require(size(records) == 1 .and. records(1)%utc == 1, 'midnight: old record expires')
+1014 format(f8.3, i5, 3i3, f5.1, i4, i3, i4, i5.4, 4x, a22, 7x, 2a1, 2x, a2)
+ end subroutine
+
+ subroutine test_bandmap_newest_across_groups()
+ character(len=22) :: msg
+ character(len=83) :: rec
+
+ call clear_messages()
+ msg = 'CQ K001X FN42'
+ write(rec, 1014) 144.200_real64, 0, 0, 0, 0, 0.0, 0, 5, -20, 1200, msg, '#', ' ', '#B'
+ call append_message(rec, 1200, 144.200_real64, 0)
+ write(rec, 1014) 144.100_real64, 0, 0, 0, 0, 0.0, 0, 5, -20, 1201, msg, '#', ' ', '#B'
+ call append_message(rec, 1201, 144.100_real64, 0)
+ bandmap_entries = 0; first_bandmap_frequency = ' '; first_bandmap_utc = ' '
+ call display(10, 0.010, 1201)
+ call require(bandmap_entries == 1 .and. first_bandmap_frequency == '100', &
+ 'Band Map uses the newest decode across frequency groups')
+ ! The waterfall label ages from this UTC, so it must be the newest decode's.
+ call require(first_bandmap_utc == '1201', 'Band Map entry carries the newest decode''s UTC')
+1014 format(f8.3, i5, 3i3, f5.1, i4, i3, i4, i5.4, 4x, a22, 7x, 2a1, 2x, a2)
+ end subroutine
+
+ ! Band Map entries carry the decode's mode, for the waterfall label color.
+ subroutine test_bandmap_modes()
+ character(len=22) :: msg
+ character(len=83) :: rec
+
+ call clear_messages()
+ msg = 'CQ K001X FN42'
+ write(rec, 1014) 144.100_real64, 0, 0, 0, 0, 0.0, 0, 5, -20, 1200, msg, '#', ' ', '#B'
+ call append_message(rec, 1200, 144.100_real64, 0)
+ msg = 'CQ K002X FN42'
+ write(rec, 1014) 144.200_real64, 0, 0, 0, 0, 0.0, 0, 5, -20, 1200, msg, ':', ' ', ':A'
+ call append_message(rec, 1200, 144.200_real64, 0)
+ bandmap_entries = 0; bandmap_modes = ' '
+ call display(10, 0.010, 1200)
+ call require(bandmap_entries == 2 .and. bandmap_modes(1:2) == '#:', &
+ 'Band Map entries carry each decode''s mode')
+1014 format(f8.3, i5, 3i3, f5.1, i4, i3, i4, i5.4, 4x, a22, 7x, 2a1, 2x, a2)
+ end subroutine
+
+ ! A compound call (prefix/call) reaches the Band Map whole, with the mode
+ ! and UTC after it, for the Band Map window and the waterfall label.
+ subroutine test_bandmap_compound_call()
+ character(len=22) :: msg
+ character(len=83) :: rec
+
+ call clear_messages()
+ msg = 'CQ ER/EA8DBM'
+ write(rec, 1014) 144.124_real64, -500, 0, 0, 0, 0.0, 0, 5, -20, 1200, msg, ':', ' ', ':A'
+ call append_message(rec, 1200, 144.124_real64, -500)
+ bandmap_entries = 0; bandmap_modes = ' '; first_bandmap_call = ' '; first_bandmap_utc = ' '
+ call display(10, 0.010, 1200)
+ call require(bandmap_entries == 1 .and. first_bandmap_call == 'ER/EA8DBM', &
+ 'compound call reaches the Band Map whole')
+ call require(bandmap_modes(1:1) == ':' .and. first_bandmap_utc == '1200', &
+ 'compound call entry keeps its mode and UTC')
+1014 format(f8.3, i5, 3i3, f5.1, i4, i3, i4, i5.4, 4x, a22, 7x, 2a1, 2x, a2)
+ end subroutine
+
+ subroutine test_bandmap_capacity()
+ integer :: k
+ character(len=22) :: msg
+ character(len=83) :: rec
+ real(real64) :: f0
+
+ call clear_messages()
+ do k = 1, 600
+ f0 = 144.0_real64 + 0.001_real64*(100 + k)
+ write(msg, '(a,i3.3,a)') 'CQ K', k, 'X FN42'
+ write(rec, 1014) f0, 0, 0, 0, 0, 0.0, 0, 5, -20, 1200, msg, '#', ' ', '#B'
+ call append_message(rec, 1200, f0, 0)
+ enddo
+ shown = 0; shown_at_utc = 0; want_utc = 1200; bandmap_entries = 0
+ first_bandmap_frequency = ' '
+ call display(40, 0.010, 1200)
+ call require(shown_at_utc == 600, '600 busy-band Messages rows are shown')
+ call require(bandmap_entries == 600, 'Band Map includes every visible call')
+ call require(first_bandmap_frequency == '101', 'Band Map remains frequency ordered')
+1014 format(f8.3, i5, 3i3, f5.1, i4, i3, i4, i5.4, 4x, a22, 7x, 2a1, 2x, a2)
+ end subroutine
+
+ subroutine require(condition, description)
+ logical, intent(in) :: condition
+ character(len=*), intent(in) :: description
+ if (.not. condition) then
+ print '(a)', 'FAIL: '//description
+ error stop 1
+ endif
+ end subroutine
+end program
diff --git a/tests/unit/map65/test_map65_deep_search_cache.f90 b/tests/unit/map65/test_map65_deep_search_cache.f90
index 6479bcad..a30b1165 100644
--- a/tests/unit/map65/test_map65_deep_search_cache.f90
+++ b/tests/unit/map65/test_map65_deep_search_cache.f90
@@ -130,8 +130,9 @@ contains
integer :: cached_count
cached_count = ntot
- cached_messages = testmsg(1:ntot)
- cached_symbols = ncode(:, 1:ntot)
+ allocate(cached_messages(cached_count), cached_symbols(size(ncode, 1), cached_count))
+ cached_messages(:) = testmsg(1:cached_count)
+ cached_symbols(:, :) = ncode(:, 1:cached_count)
call build_fresh(local_call, dx_call, dx_grid, eme_only)
call require(ntot == cached_count, description//': cached and fresh candidate counts match')
call require(all(testmsg(1:ntot) == cached_messages), description//': cached and fresh messages match')
diff --git a/tests/unit/map65/test_map65_filbig_boundary.f90 b/tests/unit/map65/test_map65_filbig_boundary.f90
new file mode 100644
index 00000000..80931ac9
--- /dev/null
+++ b/tests/unit/map65/test_map65_filbig_boundary.f90
@@ -0,0 +1,64 @@
+program test_map65_filbig_boundary
+ use iso_fortran_env, only: real32, real64
+ use, intrinsic :: ieee_arithmetic, only: ieee_is_finite, ieee_quiet_nan, ieee_value
+ use filbig_mod, only: filbig, MAXFFT1, MAXFFT2
+ use cacb_mod, only: init_cacb, ca, cb
+ use decode1a_mod, only: jt65c_center_offset
+ use npar_ptrs_mod, only: set_runtime_params
+ implicit none
+
+ real(real32) :: dd(4,1)
+ complex :: cx(MAXFFT2), cy(MAXFFT2)
+ real(real64) :: f0
+ integer :: rate, nfft, nfft_big, newdat, n4
+ integer, parameter :: nfft_short = 77175
+ character(len=16) :: argument
+
+ call get_command_argument(1, argument)
+ read(argument, *) rate
+ nfft = 32768 * (rate / 96000)
+ nfft_big = 56 * rate
+ call set_runtime_params(rate, nfft, nfft_big)
+ call init_cacb(MAXFFT1)
+
+ ! Poison unused storage so reads beyond the active FFT cannot pass unnoticed.
+ ca = cmplx(ieee_value(0.0_real32, ieee_quiet_nan), 0.0_real32)
+ ca(:nfft_big) = cmplx(1.0_real32, -0.25_real32)
+ cb = ca
+ dd = 0.0_real32
+ newdat = 0
+
+ f0 = real(nfft - 52, real64) * real(rate, real64) / real(nfft, real64)
+ call check_extraction(f0)
+ call check_extraction(f0 + jt65c_center_offset)
+ call check_extraction(0.0_real64)
+
+ call filbig(dd, 1, real(rate, real64) + 1378.125_real64, newdat, rate, .true., cx, cy, n4)
+ call require(all(abs(cx(:nfft_short)) == 0.0), 'out-of-band X extraction is zero')
+ call require(all(abs(cy(:nfft_short)) == 0.0), 'out-of-band Y extraction is zero')
+ print '(a)', 'MAP65 filter boundary tests passed'
+
+contains
+
+ subroutine check_extraction(center)
+ real(real64), intent(in) :: center
+
+ call filbig(dd, 1, center, newdat, rate, .true., cx, cy, n4)
+ call require(all(ieee_is_finite(real(cx(:nfft_short)))) .and. &
+ all(ieee_is_finite(aimag(cx(:nfft_short)))), 'X extraction is finite')
+ call require(all(ieee_is_finite(real(cy(:nfft_short)))) .and. &
+ all(ieee_is_finite(aimag(cy(:nfft_short)))), 'Y extraction is finite')
+ call require(any(abs(cx(:nfft_short)) > 0.0), 'available in-band samples are retained')
+ call require(all(cx(:nfft_short) == cy(:nfft_short)), 'polarizations use the same boundary handling')
+ end subroutine check_extraction
+
+ 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_filbig_boundary
diff --git a/tests/unit/map65/test_map65_pass_snapshots.f90 b/tests/unit/map65/test_map65_pass_snapshots.f90
new file mode 100644
index 00000000..cfbd12b7
--- /dev/null
+++ b/tests/unit/map65/test_map65_pass_snapshots.f90
@@ -0,0 +1,100 @@
+program test_map65_pass_snapshots
+ ! Each automatic decode pass must decode data frozen at the moment it was
+ ! triggered. Simulates the new minute resetting the live buffers while the
+ ! 302-symbol pass is still queued behind the running 280-symbol pass.
+ use iso_c_binding, only: c_float
+ use datcom_ptrs_mod, only: dd, ss, savg, snap, snapshot_buffers
+ use npar_ptrs_mod, only: snap_latest_idx, set_nhsym, set_nagain, set_ndiskdat, &
+ publish_decode_request, claim_decode_request, active_request_expired, &
+ active_snap_idx, snapshot_release, advance_live_input_generation
+ implicit none
+
+ integer, parameter :: nsmax = 4000, ncol = 12, nfft = 16
+ integer(8) :: rid
+
+ allocate(dd(4,nsmax), ss(4,ncol,nfft), savg(4,nfft))
+ allocate(snap(1)%dd(4,nsmax), snap(1)%ss(4,ncol,nfft), snap(1)%savg(4,nfft))
+ allocate(snap(2)%dd(4,nsmax), snap(2)%ss(4,ncol,nfft), snap(2)%savg(4,nfft))
+ snap(1)%dd = -1.0; snap(1)%ss = -1.0; snap(1)%savg = -1.0
+ snap(2)%dd = -1.0; snap(2)%ss = -1.0; snap(2)%savg = -1.0
+
+ ! Early pass (nhsym 280 stand-in): live buffers hold cycle A up to sample 3000
+ dd = 0.0; dd(:,1:3000) = 1.0
+ ss = 0.0; ss(:,1:10,:) = 1.0
+ savg = 10.0
+ call snapshot_buffers(1, 3000, 10)
+ call require(snap_latest_idx == 1, 'latest snapshot is the early one')
+
+ ! Later: live buffers keep filling, then the new minute resets them
+ dd(:,3001:3800) = 2.0
+ ss(:,11:12,:) = 2.0
+ savg = 20.0
+ call snapshot_buffers(2, 3800, 12)
+ call require(snap_latest_idx == 2, 'latest snapshot is the final one')
+
+ dd = 7.0; ss = 7.0; savg = 0.0 !Next cycle overwrites / resets live
+
+ call require(all(snap(1)%dd(:,1:3000) == 1.0), 'early dd frozen')
+ call require(all(snap(1)%dd(:,3513:) == 0.0), 'early dd past lookahead is zero')
+ call require(all(snap(1)%ss(:,1:10,:) == 1.0), 'early ss frozen')
+ call require(all(snap(1)%ss(:,11:,:) == 0.0), 'early ss past nhsym is zero')
+ call require(all(snap(1)%savg == 10.0), 'early savg frozen')
+
+ call require(all(snap(2)%dd(:,1:3000) == 1.0), 'final dd has early part')
+ call require(all(snap(2)%dd(:,3001:3800) == 2.0), 'final dd has later part')
+ call require(all(snap(2)%ss(:,11:12,:) == 2.0), 'final ss frozen')
+ call require(all(snap(2)%savg == 20.0), 'final savg frozen')
+
+ ! ---- request expiry follows the snapshot, not the minute boundary ----
+ call set_nagain(0); call set_ndiskdat(0)
+
+ ! 1. A final-pass request claimed after the minute rolled over still runs
+ ! (its snapshot is intact), and holds its slot while running.
+ dd = 3.0; ss = 3.0; savg = 3.0
+ call set_nhsym(302)
+ call snapshot_buffers(2, 3800, 12)
+ rid = publish_decode_request()
+ call advance_live_input_generation() !new minute begins
+ call require(claim_decode_request(), 'request claimed')
+ call require(.not. active_request_expired, 'late request with intact snapshot runs')
+ call require(active_snap_idx == 2, 'late request reads its own slot')
+
+ ! 2. While that pass is running, the next cycle's fill must not overwrite
+ ! the slot; the request published for it is then dropped, not mis-decoded.
+ dd = 4.0
+ call snapshot_buffers(2, 3800, 12)
+ call require(all(snap(2)%dd(:,1:3800) == 3.0), 'busy slot not overwritten')
+ rid = publish_decode_request()
+ call snapshot_release()
+ call require(claim_decode_request(), 'second request claimed')
+ call require(active_request_expired, 'request whose fill was skipped is dropped')
+
+ ! 3. A queued request whose slot is refilled before it is claimed is dropped.
+ dd = 5.0
+ call snapshot_buffers(2, 3800, 12)
+ rid = publish_decode_request()
+ dd = 6.0
+ call snapshot_buffers(2, 3800, 12)
+ call require(claim_decode_request(), 'third request claimed')
+ call require(active_request_expired, 'overwritten snapshot request is dropped')
+
+ ! 4. More than one generation old (monitoring stopped etc.) is dropped.
+ call snapshot_buffers(2, 3800, 12)
+ rid = publish_decode_request()
+ call advance_live_input_generation()
+ call advance_live_input_generation()
+ call require(claim_decode_request(), 'fourth request claimed')
+ call require(active_request_expired, 'request two generations old is dropped')
+ print *, 'test_map65_pass_snapshots: PASS'
+
+contains
+
+ subroutine require(ok, what)
+ logical, intent(in) :: ok
+ character(*), intent(in) :: what
+ if (.not. ok) then
+ print *, 'FAIL: ', what
+ error stop 1
+ end if
+ end subroutine require
+end program test_map65_pass_snapshots
diff --git a/tests/unit/map65/test_map65_ptt_unix.c b/tests/unit/map65/test_map65_ptt_unix.c
new file mode 100644
index 00000000..65af255d
--- /dev/null
+++ b/tests/unit/map65/test_map65_ptt_unix.c
@@ -0,0 +1,489 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/param.h>
+#include <errno.h>
+#include <string.h>
+#include <stdarg.h>
+#include <poll.h>
+
+static int serial_open(const char *, int, ...);
+static int serial_ioctl(int, unsigned long, ...);
+static int serial_close(int);
+static int serial_poll(struct pollfd *, nfds_t, int);
+
+#define open serial_open
+#define ioctl serial_ioctl
+#define close serial_close
+#define poll serial_poll
+#ifndef PTT_UNIX_SOURCE
+#define PTT_UNIX_SOURCE "../../../map65/libm65/ptt_unix.c"
+#endif
+#include PTT_UNIX_SOURCE
+#undef open
+#undef ioctl
+#undef close
+#undef poll
+
+enum operation { OPEN_PORT, GET_LINES, SET_LINES, CLOSE_PORT };
+struct event {
+ enum operation operation;
+ int descriptor;
+ int lines;
+ char path[256];
+};
+
+static struct event events[128];
+static int event_count;
+static int lines[64];
+static int opened[64];
+static int next_descriptor = 10;
+static int fail_open;
+static unsigned long fail_ioctl_request;
+static int fail_ioctl_countdown;
+static int failed_descriptor;
+static short poll_events;
+static int fail_poll;
+static int mock_failed;
+static int iptt;
+static const int ptt_lines = TIOCM_RTS | TIOCM_DTR;
+
+#define CHECK(condition) do { \
+ if (!(condition)) { \
+ fprintf(stderr, "%s:%d: %s\n", __func__, __LINE__, #condition); \
+ return 0; \
+ } \
+} while (0)
+
+static struct event *record(enum operation operation, int descriptor)
+{
+ struct event *event;
+ if (event_count == (int)(sizeof(events) / sizeof(events[0]))) {
+ fprintf(stderr, "Serial event buffer exhausted\n");
+ exit(2);
+ }
+ event = &events[event_count++];
+ memset(event, 0, sizeof(*event));
+ event->operation = operation;
+ event->descriptor = descriptor;
+ return event;
+}
+
+static int serial_open(const char *path, int flags, ...)
+{
+ int descriptor = next_descriptor++;
+ struct event *event = record(OPEN_PORT, descriptor);
+ snprintf(event->path, sizeof(event->path), "%s", path);
+ if (!(flags & O_NONBLOCK) || !(flags & O_NOCTTY)
+ || (flags & O_ACCMODE) != O_RDWR
+ || descriptor >= (int)(sizeof(opened) / sizeof(opened[0]))) {
+ mock_failed = 1;
+ return -1;
+ }
+ if (fail_open) {
+ errno = EACCES;
+ return -1;
+ }
+ opened[descriptor] = 1;
+ lines[descriptor] = TIOCM_CTS | TIOCM_DTR | TIOCM_RTS;
+ return descriptor;
+}
+
+static int serial_ioctl(int descriptor, unsigned long request, ...)
+{
+ va_list arguments;
+ int *value;
+ struct event *event = record(request == TIOCMGET ? GET_LINES : SET_LINES,
+ descriptor);
+ va_start(arguments, request);
+ value = va_arg(arguments, int *);
+ va_end(arguments);
+ if (descriptor < 0 || descriptor >= 64 || !opened[descriptor]
+ || (request != TIOCMGET && request != TIOCMSET)) {
+ mock_failed = 1;
+ return -1;
+ }
+ event->lines = request == TIOCMSET ? *value : lines[descriptor];
+ if (descriptor == failed_descriptor
+ || (request == fail_ioctl_request && --fail_ioctl_countdown == 0)) {
+ errno = EIO;
+ return -1;
+ }
+ if (request == TIOCMGET)
+ *value = lines[descriptor];
+ else
+ lines[descriptor] = *value;
+ return 0;
+}
+
+static int serial_close(int descriptor)
+{
+ record(CLOSE_PORT, descriptor);
+ if (descriptor < 0 || descriptor >= 64 || !opened[descriptor]) {
+ mock_failed = 1;
+ return -1;
+ }
+ opened[descriptor] = 0;
+ return 0;
+}
+
+static int serial_poll(struct pollfd *ports, nfds_t count, int timeout)
+{
+ if (count != 1 || timeout != 0 || ports[0].events != 0
+ || ports[0].fd < 0 || ports[0].fd >= 64 || !opened[ports[0].fd]) {
+ mock_failed = 1;
+ return -1;
+ }
+ if (fail_poll) {
+ errno = EINTR;
+ return -1;
+ }
+ ports[0].revents = poll_events;
+ return poll_events ? 1 : 0;
+}
+
+static int command(int on)
+{
+ int unused = 0;
+ return ptt_(&unused, &on, &iptt);
+}
+
+static void fail_next_ioctl(unsigned long request, int occurrence)
+{
+ fail_ioctl_request = request;
+ fail_ioctl_countdown = occurrence;
+}
+
+static void reset(void)
+{
+ fail_open = 0;
+ fail_ioctl_request = 0;
+ failed_descriptor = -1;
+ poll_events = 0;
+ fail_poll = 0;
+ ptt_close();
+ ptt_set_override(NULL);
+ event_count = 0;
+ next_descriptor = 10;
+ mock_failed = 0;
+ iptt = 0;
+ memset(opened, 0, sizeof(opened));
+ memset(lines, 0, sizeof(lines));
+}
+
+static int count(enum operation operation)
+{
+ int result = 0;
+ int i;
+ for (i = 0; i < event_count; ++i)
+ result += events[i].operation == operation;
+ return result;
+}
+
+static int first(enum operation operation)
+{
+ int i;
+ for (i = 0; i < event_count; ++i)
+ if (events[i].operation == operation)
+ return i;
+ return -1;
+}
+
+static int disabled_and_unopened_off(void)
+{
+ CHECK(command(1) == 0 && iptt == 1);
+ CHECK(command(0) == 0 && iptt == 0);
+ CHECK(event_count == 0);
+ ptt_set_override("/dev/serial-A");
+ CHECK(command(0) == 0 && iptt == 0);
+ CHECK(event_count == 0);
+ return 1;
+}
+
+static int reuse_and_preserve_lines(void)
+{
+ ptt_set_override("/dev/serial-A");
+ CHECK(command(1) == 0 && iptt == 1);
+ CHECK(events[0].operation == OPEN_PORT);
+ CHECK(events[1].operation == GET_LINES);
+ CHECK(events[2].operation == SET_LINES && events[2].lines == TIOCM_CTS);
+ CHECK(lines[10] == (TIOCM_CTS | ptt_lines));
+ CHECK(command(0) == 0 && iptt == 0);
+ CHECK(lines[10] == TIOCM_CTS);
+ ptt_set_override("/dev/serial-A");
+ CHECK(command(1) == 0 && iptt == 1);
+ CHECK(count(OPEN_PORT) == 1 && count(CLOSE_PORT) == 0);
+ return 1;
+}
+
+static int idle_switch(void)
+{
+ ptt_set_override("/dev/serial-A");
+ CHECK(command(1) == 0);
+ CHECK(command(0) == 0);
+ event_count = 0;
+ ptt_set_override("/dev/serial-B");
+ CHECK(command(1) == 0 && iptt == 1);
+ CHECK(count(CLOSE_PORT) == 1 && count(OPEN_PORT) == 1);
+ CHECK(first(CLOSE_PORT) < first(OPEN_PORT));
+ CHECK(strcmp(events[first(OPEN_PORT)].path, "/dev/serial-B") == 0);
+ CHECK(!opened[10] && opened[11]);
+ CHECK(lines[10] == TIOCM_CTS && lines[11] == (TIOCM_CTS | ptt_lines));
+ return 1;
+}
+
+static int active_switch(void)
+{
+ ptt_set_override("/dev/serial-A");
+ CHECK(command(1) == 0);
+ event_count = 0;
+ ptt_set_override("/dev/serial-B");
+ CHECK(count(OPEN_PORT) == 0 && count(CLOSE_PORT) == 0);
+ CHECK(command(1) == 0 && iptt == 1);
+ CHECK(count(OPEN_PORT) == 0 && count(CLOSE_PORT) == 0);
+ CHECK(command(0) == 0 && iptt == 0);
+ CHECK(count(OPEN_PORT) == 0 && count(CLOSE_PORT) == 1);
+ CHECK(lines[10] == TIOCM_CTS);
+ CHECK(first(SET_LINES) < first(CLOSE_PORT));
+ CHECK(command(1) == 0);
+ CHECK(strcmp(events[first(OPEN_PORT)].path, "/dev/serial-B") == 0);
+ return 1;
+}
+
+static int active_disable(void)
+{
+ ptt_set_override("/dev/serial-A");
+ CHECK(command(1) == 0);
+ event_count = 0;
+ ptt_set_override(NULL);
+ CHECK(count(CLOSE_PORT) == 0);
+ CHECK(command(0) == 0 && iptt == 0);
+ CHECK(lines[10] == TIOCM_CTS && !opened[10]);
+ CHECK(count(CLOSE_PORT) == 1 && count(OPEN_PORT) == 0);
+ event_count = 0;
+ CHECK(command(1) == 0 && iptt == 1);
+ CHECK(command(0) == 0 && iptt == 0);
+ CHECK(event_count == 0);
+ return 1;
+}
+
+static int repeated_switches(void)
+{
+ ptt_set_override("/dev/serial-A");
+ CHECK(command(1) == 0);
+ ptt_set_override("/dev/serial-B");
+ ptt_set_override("/dev/serial-A");
+ CHECK(command(0) == 0);
+ CHECK(count(OPEN_PORT) == 1 && count(CLOSE_PORT) == 0);
+ CHECK(command(1) == 0);
+ ptt_set_override("/dev/serial-B");
+ ptt_set_override(NULL);
+ ptt_set_override("/dev/serial-C");
+ CHECK(command(0) == 0);
+ CHECK(count(OPEN_PORT) == 1 && count(CLOSE_PORT) == 1);
+ event_count = 0;
+ CHECK(command(1) == 0);
+ CHECK(strcmp(events[first(OPEN_PORT)].path, "/dev/serial-C") == 0);
+ return 1;
+}
+
+static int open_failure(void)
+{
+ ptt_set_override("/dev/serial-A");
+ fail_open = 1;
+ CHECK(command(1) != 0 && iptt == 0);
+ CHECK(count(GET_LINES) == 0 && count(SET_LINES) == 0);
+ fail_open = 0;
+ CHECK(command(1) == 0 && iptt == 1);
+ return 1;
+}
+
+static int initialization_failure(unsigned long request)
+{
+ int i;
+ ptt_set_override("/dev/serial-A");
+ fail_next_ioctl(request, 1);
+ CHECK(command(1) != 0 && iptt == 0);
+ for (i = 0; i < event_count; ++i)
+ CHECK(events[i].operation != SET_LINES || !(events[i].lines & ptt_lines));
+ event_count = 0;
+ CHECK(command(1) != 0 && iptt == 0);
+ CHECK(count(OPEN_PORT) == 0 && count(SET_LINES) == 0);
+ CHECK(command(0) == 0 && iptt == 0);
+ CHECK(command(1) == 0 && iptt == 1);
+ return 1;
+}
+
+static int initial_get_failure(void) { return initialization_failure(TIOCMGET); }
+static int initial_set_failure(void) { return initialization_failure(TIOCMSET); }
+
+static int keying_failure(unsigned long request)
+{
+ ptt_set_override("/dev/serial-A");
+ fail_next_ioctl(request, 2);
+ CHECK(command(1) != 0 && iptt == 0);
+ event_count = 0;
+ ptt_set_override("/dev/serial-B");
+ CHECK(command(1) != 0 && iptt == 0);
+ CHECK(count(OPEN_PORT) == 0 && count(CLOSE_PORT) == 0);
+ CHECK(command(0) == 0 && iptt == 0);
+ CHECK(!opened[10]);
+ CHECK(command(1) == 0 && iptt == 1);
+ CHECK(strcmp(events[first(OPEN_PORT)].path, "/dev/serial-B") == 0);
+ return 1;
+}
+
+static int keying_get_failure(void) { return keying_failure(TIOCMGET); }
+static int keying_set_failure(void) { return keying_failure(TIOCMSET); }
+
+static int release_failure(unsigned long request, const char *next_path)
+{
+ ptt_set_override("/dev/serial-A");
+ CHECK(command(1) == 0 && iptt == 1);
+ event_count = 0;
+ ptt_set_override(next_path);
+ fail_next_ioctl(request, 1);
+ CHECK(command(0) != 0 && iptt == 1);
+ CHECK(opened[10] && count(OPEN_PORT) == 0 && count(CLOSE_PORT) == 0);
+ CHECK(command(1) != 0 && iptt == 1);
+ CHECK(count(OPEN_PORT) == 0 && count(CLOSE_PORT) == 0);
+ CHECK(command(0) == 0 && iptt == 0);
+ CHECK(lines[10] == TIOCM_CTS && !opened[10]);
+ CHECK(count(CLOSE_PORT) == 1);
+ CHECK(command(1) == 0 && iptt == 1);
+ CHECK(count(OPEN_PORT) == (next_path ? 1 : 0));
+ return 1;
+}
+
+static int release_get_failure(void) { return release_failure(TIOCMGET, "/dev/serial-B"); }
+static int release_set_failure(void) { return release_failure(TIOCMSET, "/dev/serial-B"); }
+static int disable_release_failure(void) { return release_failure(TIOCMSET, NULL); }
+
+static int disconnected_port(int during_tx, const char *next_path)
+{
+ ptt_set_override("/dev/serial-A");
+ CHECK(command(1) == PTT_OK);
+ if (!during_tx)
+ CHECK(command(0) == PTT_OK);
+ failed_descriptor = 10;
+ poll_events = POLLHUP | POLLERR;
+ event_count = 0;
+ CHECK(command(during_tx ? 0 : 1) == PTT_DEVICE_LOST && iptt == 0);
+ CHECK(!opened[10] && count(CLOSE_PORT) == 1);
+ CHECK(count(OPEN_PORT) == 0);
+ ptt_set_override(next_path);
+ poll_events = 0;
+ CHECK(command(1) == PTT_OK && iptt == 1);
+ CHECK(count(OPEN_PORT) == (next_path ? 1 : 0));
+ if (next_path)
+ CHECK(strcmp(events[first(OPEN_PORT)].path, next_path) == 0);
+ CHECK(command(0) == PTT_OK && iptt == 0);
+ return 1;
+}
+
+static int disconnect_idle(void) { return disconnected_port(0, "/dev/serial-B"); }
+static int disconnect_active(void) { return disconnected_port(1, "/dev/serial-B"); }
+static int disconnect_same_path(void) { return disconnected_port(1, "/dev/serial-A"); }
+static int disconnect_disable(void) { return disconnected_port(1, NULL); }
+
+static int persistent_control_failure(void)
+{
+ int i;
+ ptt_set_override("/dev/serial-A");
+ CHECK(command(1) == PTT_OK);
+ ptt_set_override("/dev/serial-B");
+ failed_descriptor = 10;
+ event_count = 0;
+ for (i = 0; i < 3; ++i) {
+ poll_events = i == 1 ? POLLERR : 0;
+ fail_poll = i == 2;
+ CHECK(command(0) == PTT_ERROR && iptt == 1);
+ CHECK(command(1) == PTT_ERROR && iptt == 1);
+ CHECK(opened[10] && count(CLOSE_PORT) == 0 && count(OPEN_PORT) == 0);
+ }
+ fail_poll = 0;
+ poll_events = POLLHUP;
+ CHECK(command(0) == PTT_DEVICE_LOST && iptt == 0);
+ CHECK(!opened[10] && count(CLOSE_PORT) == 1);
+ poll_events = 0;
+ CHECK(command(1) == PTT_OK && iptt == 1);
+ CHECK(strcmp(events[first(OPEN_PORT)].path, "/dev/serial-B") == 0);
+ return 1;
+}
+
+static int disconnect_initialization(void)
+{
+ ptt_set_override("/dev/serial-A");
+ failed_descriptor = 10;
+ poll_events = POLLHUP;
+ CHECK(command(1) == PTT_DEVICE_LOST && iptt == 0);
+ CHECK(!opened[10] && count(CLOSE_PORT) == 1);
+ CHECK(count(SET_LINES) == 0);
+ poll_events = 0;
+ CHECK(command(1) == PTT_OK && iptt == 1);
+ return 1;
+}
+
+static int shutdown_cleanup(void)
+{
+ ptt_set_override("/dev/serial-A");
+ CHECK(command(1) == 0);
+ event_count = 0;
+ ptt_close();
+ CHECK(lines[10] == TIOCM_CTS && !opened[10]);
+ CHECK(count(SET_LINES) == 1 && count(CLOSE_PORT) == 1);
+ CHECK(first(SET_LINES) < first(CLOSE_PORT));
+ event_count = 0;
+ ptt_close();
+ CHECK(event_count == 0);
+ CHECK(command(1) == 0);
+ fail_next_ioctl(TIOCMSET, 1);
+ event_count = 0;
+ ptt_close();
+ CHECK(count(SET_LINES) == 1 && count(CLOSE_PORT) == 1 && !opened[11]);
+ return 1;
+}
+
+int main(void)
+{
+ const struct {
+ const char *name;
+ int (*run)(void);
+ } tests[] = {
+ {"disabled and unopened off", disabled_and_unopened_off},
+ {"reuse and preserve lines", reuse_and_preserve_lines},
+ {"idle switch", idle_switch},
+ {"active switch", active_switch},
+ {"active disable", active_disable},
+ {"repeated switches", repeated_switches},
+ {"open failure", open_failure},
+ {"initial get failure", initial_get_failure},
+ {"initial set failure", initial_set_failure},
+ {"keying get failure", keying_get_failure},
+ {"keying set failure", keying_set_failure},
+ {"release get failure", release_get_failure},
+ {"release set failure", release_set_failure},
+ {"disable release failure", disable_release_failure},
+ {"disconnect while idle", disconnect_idle},
+ {"disconnect while active", disconnect_active},
+ {"disconnect and reuse path", disconnect_same_path},
+ {"disconnect and disable", disconnect_disable},
+ {"persistent control failure", persistent_control_failure},
+ {"disconnect during initialization", disconnect_initialization},
+ {"shutdown cleanup", shutdown_cleanup}
+ };
+ int failed = 0;
+ size_t i;
+ for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) {
+ int passed;
+ reset();
+ passed = tests[i].run() && !mock_failed;
+ printf("%s: %s\n", passed ? "PASS" : "FAIL", tests[i].name);
+ failed += !passed;
+ }
+ reset();
+ return failed ? 1 : 0;
+}
diff --git a/tests/unit/map65/test_map65_ptt_windows.c b/tests/unit/map65/test_map65_ptt_windows.c
new file mode 100644
index 00000000..23113c6c
--- /dev/null
+++ b/tests/unit/map65/test_map65_ptt_windows.c
@@ -0,0 +1,323 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <windows.h>
+
+#ifndef PTT_WINDOWS_SOURCE
+#define PTT_WINDOWS_SOURCE "../../../map65/libm65/ptt.c"
+#endif
+/* Redirect ptt.c's call (and our mock below) so our definition doesn't collide with libkernel32's real GetLastError at link time. */
+#define GetLastError GetLastErr
+DWORD GetLastErr(void);
+#include PTT_WINDOWS_SOURCE
+
+enum { OPEN_PORT = 100, CLOSE_PORT, RTS = 1, DTR = 2 };
+struct event {
+ DWORD operation;
+ int handle;
+ char path[64];
+};
+
+static struct event events[128];
+static int event_count;
+static int opened[64];
+static int lines[64];
+static int next_handle = 10;
+static int fail_open;
+static int fail_close;
+static int failures[10];
+static DWORD errors[10];
+static DWORD last_error;
+static int mock_failed;
+static int iptt;
+static int port;
+
+#define CHECK(condition) do { \
+ if (!(condition)) { \
+ fprintf(stderr, "%s:%d: %s\n", __func__, __LINE__, #condition); \
+ return 0; \
+ } \
+} while (0)
+
+static struct event *record(DWORD operation, int handle)
+{
+ struct event *event;
+ if (event_count == (int)(sizeof(events) / sizeof(events[0])))
+ exit(2);
+ event = &events[event_count++];
+ memset(event, 0, sizeof(*event));
+ event->operation = operation;
+ event->handle = handle;
+ return event;
+}
+
+static int valid_handle(HANDLE handle)
+{
+ intptr_t value = (intptr_t)handle;
+ if (value < 0 || value >= 64 || !opened[value]) {
+ mock_failed = 1;
+ return 0;
+ }
+ return 1;
+}
+
+HANDLE CreateFileA(const char *path, DWORD access, DWORD sharing, void *security,
+ DWORD creation, DWORD attributes, HANDLE template_file)
+{
+ int handle = next_handle++;
+ struct event *event = record(OPEN_PORT, handle);
+ snprintf(event->path, sizeof(event->path), "%s", path);
+ if (access != GENERIC_WRITE || sharing || security || creation != OPEN_EXISTING
+ || attributes != FILE_ATTRIBUTE_NORMAL || template_file || handle >= 64)
+ mock_failed = 1;
+ if (mock_failed || fail_open) {
+ last_error = ERROR_ACCESS_DENIED;
+ return INVALID_HANDLE_VALUE;
+ }
+ opened[handle] = 1;
+ return (HANDLE)(intptr_t)handle;
+}
+
+BOOL EscapeCommFunction(HANDLE handle, DWORD operation)
+{
+ int value = (int)(intptr_t)handle;
+ record(operation, value);
+ if (!valid_handle(handle))
+ return 0;
+ if (operation != SETRTS && operation != SETDTR && operation != CLRRTS
+ && operation != CLRDTR && operation != CLRBREAK) {
+ mock_failed = 1;
+ return 0;
+ }
+ if (failures[operation]) {
+ if (failures[operation] > 0)
+ --failures[operation];
+ last_error = errors[operation];
+ return 0;
+ }
+ last_error = 0;
+ if (operation == SETRTS) lines[value] |= RTS;
+ if (operation == SETDTR) lines[value] |= DTR;
+ if (operation == CLRRTS) lines[value] &= ~RTS;
+ if (operation == CLRDTR) lines[value] &= ~DTR;
+ return 1;
+}
+
+BOOL CloseHandle(HANDLE handle)
+{
+ record(CLOSE_PORT, (int)(intptr_t)handle);
+ if (!valid_handle(handle))
+ return 0;
+ if (fail_close) {
+ last_error = ERROR_GEN_FAILURE;
+ return 0;
+ }
+ opened[(intptr_t)handle] = 0;
+ return 1;
+}
+
+DWORD GetLastError(void) { return last_error; }
+
+static int command(int on) { return ptt_(&port, &on, &iptt); }
+
+static int count(DWORD operation)
+{
+ int total = 0;
+ int i;
+ for (i = 0; i < event_count; ++i)
+ total += events[i].operation == operation;
+ return total;
+}
+
+static void fail(DWORD operation, DWORD error, int attempts)
+{
+ failures[operation] = attempts;
+ errors[operation] = error;
+}
+
+static void reset(void)
+{
+ memset(failures, 0, sizeof(failures));
+ fail_open = fail_close = 0;
+ ptt_close();
+ memset(opened, 0, sizeof(opened));
+ memset(lines, 0, sizeof(lines));
+ event_count = mock_failed = iptt = 0;
+ next_handle = 10;
+ port = 3;
+}
+
+static int disabled_and_unopened_release(void)
+{
+ CHECK(command(0) == PTT_OK && !iptt);
+ port = 0;
+ CHECK(command(1) == PTT_OK && iptt);
+ CHECK(command(0) == PTT_OK && !iptt);
+ ptt_close();
+ CHECK(event_count == 0);
+ return 1;
+}
+
+static int normal_cycle_and_switch(void)
+{
+ CHECK(command(1) == PTT_OK && iptt);
+ CHECK(strcmp(events[0].path, "COM3") == 0);
+ CHECK(lines[10] == (RTS | DTR));
+ event_count = 0;
+ port = 12;
+ CHECK(command(1) == PTT_OK && iptt);
+ CHECK(!event_count);
+ CHECK(command(0) == PTT_OK && !iptt);
+ CHECK(!lines[10] && !opened[10]);
+ CHECK(events[0].operation == CLRRTS && events[1].operation == CLRDTR);
+ CHECK(events[2].operation == CLRBREAK && events[3].operation == CLOSE_PORT);
+ CHECK(command(1) == PTT_OK && iptt);
+ CHECK(strcmp(events[4].path, "\\\\.\\COM12") == 0);
+ port = 0;
+ CHECK(command(0) == PTT_OK && !iptt && !opened[11] && !lines[11]);
+ event_count = 0;
+ CHECK(command(1) == PTT_OK && iptt);
+ CHECK(command(0) == PTT_OK && !iptt && !event_count);
+ return 1;
+}
+
+static int open_failure(void)
+{
+ fail_open = 1;
+ CHECK(command(1) == PTT_ERROR && !iptt);
+ CHECK(event_count == 1);
+ CHECK(command(0) == PTT_OK && !iptt && event_count == 1);
+ fail_open = 0;
+ CHECK(command(1) == PTT_OK && iptt);
+ return 1;
+}
+
+static int key_failure(DWORD operation)
+{
+ fail(operation, ERROR_GEN_FAILURE, 1);
+ CHECK(command(1) == PTT_ERROR && !iptt);
+ CHECK(opened[10] && count(CLOSE_PORT) == 0);
+ CHECK(lines[10] == (operation == SETDTR ? RTS : 0));
+ if (operation == SETRTS) CHECK(count(SETDTR) == 0);
+ event_count = 0;
+ port = 12;
+ CHECK(command(1) == PTT_ERROR && !iptt && !event_count);
+ CHECK(command(0) == PTT_OK && !iptt && !opened[10] && !lines[10]);
+ CHECK(command(1) == PTT_OK && iptt);
+ CHECK(strcmp(events[4].path, "\\\\.\\COM12") == 0);
+ return 1;
+}
+
+static int rts_key_failure(void) { return key_failure(SETRTS); }
+static int dtr_key_failure(void) { return key_failure(SETDTR); }
+
+static int release_failure(DWORD operation, int attempts)
+{
+ CHECK(command(1) == PTT_OK && iptt);
+ event_count = 0;
+ port = 0;
+ fail(operation, ERROR_GEN_FAILURE, attempts);
+ CHECK(command(0) == PTT_ERROR && iptt && opened[10]);
+ CHECK(count(CLRRTS) == 1 && count(CLRDTR) == 1 && count(CLRBREAK) == 1);
+ CHECK(count(CLOSE_PORT) == 0);
+ CHECK(command(1) == PTT_ERROR && iptt);
+ CHECK(count(SETRTS) == 0 && count(SETDTR) == 0 && count(OPEN_PORT) == 0);
+ if (attempts < 0) {
+ CHECK(command(0) == PTT_ERROR && iptt && opened[10]);
+ CHECK(count(CLOSE_PORT) == 0);
+ failures[operation] = 0;
+ }
+ CHECK(command(0) == PTT_OK && !iptt && !opened[10] && !lines[10]);
+ CHECK(count(CLOSE_PORT) == 1);
+ event_count = 0;
+ CHECK(command(1) == PTT_OK && iptt && !event_count);
+ return 1;
+}
+
+static int transient_rts_failure(void) { return release_failure(CLRRTS, 1); }
+static int persistent_dtr_failure(void) { return release_failure(CLRDTR, -1); }
+static int unsupported_break(void)
+{
+ CHECK(command(1) == PTT_OK && iptt);
+ event_count = 0;
+ fail(CLRBREAK, ERROR_GEN_FAILURE, -1);
+ CHECK(command(0) == PTT_OK && !iptt && !opened[10] && !lines[10]);
+ CHECK(count(CLRRTS) == 1 && count(CLRDTR) == 1 && count(CLRBREAK) == 1);
+ CHECK(count(CLOSE_PORT) == 1);
+ CHECK(command(1) == PTT_OK && iptt && opened[11]);
+ return 1;
+}
+
+static int device_loss(DWORD operation, DWORD error)
+{
+ if (operation == CLRRTS) CHECK(command(1) == PTT_OK && iptt);
+ fail(operation, error, -1);
+ CHECK(command(operation == SETDTR) == PTT_DEVICE_LOST && !iptt);
+ CHECK(!opened[10] && count(CLOSE_PORT) == 1);
+ event_count = 0;
+ CHECK(command(0) == PTT_OK && !iptt && !event_count);
+ failures[operation] = 0;
+ CHECK(command(1) == PTT_OK && iptt && opened[11]);
+ return 1;
+}
+
+static int lost_during_key(void) { return device_loss(SETDTR, ERROR_NO_SUCH_DEVICE); }
+static int lost_during_release(void) { return device_loss(CLRRTS, ERROR_DEVICE_NOT_CONNECTED); }
+
+static int close_failure(void)
+{
+ CHECK(command(1) == PTT_OK);
+ fail_close = 1;
+ CHECK(command(0) == PTT_ERROR && !iptt && opened[10] && !lines[10]);
+ event_count = 0;
+ CHECK(command(1) == PTT_ERROR && !event_count);
+ fail_close = 0;
+ CHECK(command(0) == PTT_OK && !opened[10]);
+ return 1;
+}
+
+static int shutdown_cleanup(void)
+{
+ CHECK(command(1) == PTT_OK);
+ event_count = 0;
+ fail(CLRRTS, ERROR_GEN_FAILURE, -1);
+ ptt_close();
+ CHECK(count(CLRRTS) == 1 && count(CLRDTR) == 1 && count(CLRBREAK) == 1);
+ CHECK(count(CLOSE_PORT) == 1 && !opened[10]);
+ event_count = 0;
+ ptt_close();
+ CHECK(!event_count);
+ return 1;
+}
+
+int main(void)
+{
+ const struct {
+ const char *name;
+ int (*run)(void);
+ } tests[] = {
+ {"disabled and unopened release", disabled_and_unopened_release},
+ {"normal cycle and switch", normal_cycle_and_switch},
+ {"open failure", open_failure},
+ {"RTS key failure", rts_key_failure},
+ {"DTR key failure", dtr_key_failure},
+ {"transient RTS release failure", transient_rts_failure},
+ {"persistent DTR release failure", persistent_dtr_failure},
+ {"unsupported break", unsupported_break},
+ {"lost during key", lost_during_key},
+ {"lost during release", lost_during_release},
+ {"close failure", close_failure},
+ {"shutdown cleanup", shutdown_cleanup}
+ };
+ int failed = 0;
+ size_t i;
+ for (i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i) {
+ int passed;
+ reset();
+ passed = tests[i].run() && !mock_failed;
+ printf("%s: %s\n", passed ? "PASS" : "FAIL", tests[i].name);
+ failed += !passed;
+ }
+ reset();
+ return failed ? 1 : 0;
+}
diff --git a/tests/unit/map65/test_map65_q65_dphi.f90 b/tests/unit/map65/test_map65_q65_dphi.f90
index a9c0527d..3008a787 100644
--- a/tests/unit/map65/test_map65_q65_dphi.f90
+++ b/tests/unit/map65/test_map65_q65_dphi.f90
@@ -22,6 +22,8 @@ program test_map65_q65_dphi
integer, allocatable :: seed(:)
type(candidate) :: candidates(MAX_CANDIDATES)
character(len=24) :: sent_message
+ character(len=12) :: hiscall
+ character(len=6) :: hisgrid
integer(int8) :: lstrong(0:1023)
integer :: i, k, nwave, seed_size, ncand, selected, newdat, n4, idec
integer :: nb, nbslider, ihsym, nzap, nkh
@@ -70,6 +72,8 @@ program test_map65_q65_dphi
idphi = 180
fcenter = 144.125_real64
nkhz_center = 125
+ hiscall = ' '
+ hisgrid = ' '
nb = 0
nbslider = 40
@@ -103,12 +107,23 @@ program test_map65_q65_dphi
sample_rate, .true., c4a, c4b, n4)
call system_clock(t_start)
call q65b(1, 0, 0, fcenter, 0, sample_rate, nkhz_center, 0, 100, .true., idphi, &
- 'N0CALL ', 'FN31 ', ' ', ' ', 2, &
+ 'N0CALL ', 'FN31 ', hiscall, hisgrid, 2, &
real(candidates(selected)%f, real64), 1.270, newdat, 0, 0, 0, idec)
call require(.not. logical(abort_decode), 'decode within the time budget')
call require(nsnr0 > -99 .and. trim(msg0) == trim(message), &
'recover the Q65 message despite opposite-phase receiver channels')
+
+ manualDecodeFlag = 1
+ hiscall = ' '
+ hisgrid = ' '
+ call system_clock(t_start)
+ call q65b(1, 0, 0, fcenter, 0, sample_rate, nkhz_center, 0, 100, .true., idphi, &
+ 'N0CALL ', 'FN31 ', hiscall, hisgrid, 2, &
+ real(candidates(selected)%f, real64) + 0.040_real64, 1.270, 0, 1, 0, 0, idec)
+ call require(nsnr0 > -99 .and. trim(msg0) == trim(message), &
+ 'manual center recovers Q65 when f0 is displaced from the clicked frequency')
+ call require(.not. logical(abort_decode), 'manual decode stays within the time budget')
call fini_timer()
print '(a)', 'MAP65 Q65 Dphi test passed.'
diff --git a/tests/unit/map65/test_map65_q65_handoff.f90 b/tests/unit/map65/test_map65_q65_handoff.f90
new file mode 100644
index 00000000..268a480a
--- /dev/null
+++ b/tests/unit/map65/test_map65_q65_handoff.f90
@@ -0,0 +1,117 @@
+program test_map65_q65_handoff
+ use iso_fortran_env, only: int16, real64
+ use cacb_mod, only: ca, cb, init_cacb
+ use q65b_mod, only: build_q65_samples
+ implicit none
+
+ integer, parameter :: nfft2 = 336000, nfft1 = 5376000, sample_rate = 12000
+ integer, parameter :: k_auto = 2600000, k_manual = 2600100
+ real, parameter :: df = 96000.0/nfft1
+ real, parameter :: input_amplitude = 100000000.0
+ real(real64), parameter :: pi = acos(-1.0_real64)
+ integer(int16), allocatable :: wave(:), reference(:)
+ real(real64) :: reference_rms
+ integer :: tone_index
+
+ allocate(wave(720000), reference(720000))
+ call init_cacb(nfft1)
+ call set_tone(k_auto, 1000)
+ cb = cmplx(1000000., -1000000.)
+
+ call build_q65_samples(k_auto,nfft2,df,.false.,0,0.0,reference)
+ call require(maxval(abs(int(reference))) < 1000, 'X-only samples do not clip')
+ call require(all(reference(2*nfft2+1:) == 0), '56-second samples have a zero tail')
+ call expect_tone(reference, 1.0_real64, 'automatic center preserves 1000 Hz level and phase')
+ reference_rms = rms(reference)
+
+ cb = cmplx(-2000000., 3000000.)
+ call build_q65_samples(k_auto,nfft2,df,.false.,180,45.0,wave)
+ call require(all(wave == reference), 'single-polarization mode ignores Y')
+
+ cb = ca
+ call build_q65_samples(k_auto,nfft2,df,.true.,0,45.0,wave)
+ call expect_tone(wave, sqrt(2.0_real64), 'equal X and Y combine coherently')
+
+ cb = -ca
+ call build_q65_samples(k_auto,nfft2,df,.true.,0,45.0,wave)
+ call require(rms(wave) < 1.0_real64, 'opposite-phase Y cancels without correction')
+ call build_q65_samples(k_auto,nfft2,df,.true.,180,45.0,wave)
+ call expect_tone(wave, sqrt(2.0_real64), '180-degree correction restores opposite-phase Y')
+
+ tone_index = k_auto + nint(1000.0/df)
+ cb = 0.
+ cb(tone_index) = cmplx(0.0, -input_amplitude)
+ call build_q65_samples(k_auto,nfft2,df,.true.,90,45.0,wave)
+ call expect_tone(wave, sqrt(2.0_real64), '90-degree correction preserves rotation sign')
+
+ call set_tone(k_manual, 1000)
+ call build_q65_samples(k_manual,nfft2,df,.false.,0,0.0,wave)
+ call expect_tone(wave, 1.0_real64, 'shifted extraction offset preserves level and phase')
+
+ call expect_filter(250, 0.5_real64)
+ call expect_filter(2750, 0.5_real64)
+ call expect_filter(3500, 0.0_real64)
+
+ print '(a)', 'MAP65 Q65 sample handoff contracts passed.'
+
+contains
+
+ subroutine set_tone(center, frequency)
+ integer, intent(in) :: center, frequency
+ ca = 0.
+ ca(center+nint(real(frequency)/df)) = cmplx(input_amplitude, 0.0)
+ end subroutine set_tone
+
+ subroutine expect_tone(samples, gain, description)
+ integer(int16), intent(in) :: samples(:)
+ real(real64), intent(in) :: gain
+ character(len=*), intent(in) :: description
+ real(real64) :: expected
+ integer :: i
+
+ do i = 1, sample_rate
+ expected = gain * 2.0_real64 * real(input_amplitude, real64) / real(nfft2, real64) * &
+ cos(2.0_real64*pi*1000.0_real64*real(i, real64)/real(sample_rate, real64))
+ if (abs(int(samples(i))-nint(expected)) > 2) then
+ print '(a,i0)', 'FAIL: '//description//' at sample ', i
+ error stop 1
+ endif
+ enddo
+ end subroutine expect_tone
+
+ subroutine expect_filter(frequency, expected_ratio)
+ integer, intent(in) :: frequency
+ real(real64), intent(in) :: expected_ratio
+ real(real64) :: ratio
+
+ call set_tone(k_auto, frequency)
+ call build_q65_samples(k_auto,nfft2,df,.false.,0,0.0,wave)
+ ratio = rms(wave)/reference_rms
+ call require(abs(ratio-expected_ratio) < 0.01_real64, 'filter response at '//frequency_text(frequency))
+ end subroutine expect_filter
+
+ function frequency_text(frequency) result(value)
+ integer, intent(in) :: frequency
+ character(len=16) :: value
+ write(value, '(i0,a)') frequency, ' Hz'
+ end function frequency_text
+
+ real(real64) function rms(samples)
+ integer(int16), intent(in) :: samples(:)
+ integer :: i
+ rms = 0.0_real64
+ do i=1,sample_rate
+ rms = rms + real(samples(i),real64)**2
+ enddo
+ rms = sqrt(rms/real(sample_rate,real64))
+ end function rms
+
+ 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_q65_handoff
diff --git a/tests/unit/map65/test_map65_symspec_contract.f90 b/tests/unit/map65/test_map65_symspec_contract.f90
new file mode 100644
index 00000000..d9267268
--- /dev/null
+++ b/tests/unit/map65/test_map65_symspec_contract.f90
@@ -0,0 +1,118 @@
+program test_map65_symspec_contract
+ use iso_fortran_env, only: int8, real32
+ use, intrinsic :: ieee_arithmetic, only: ieee_quiet_nan, ieee_value
+ use datcom_ptrs_mod, only: dd, ss, savg
+ use npar_ptrs_mod, only: set_runtime_params
+ use symspec_mod, only: symspec
+ implicit none
+
+ integer, parameter :: bins(5) = [4000, 4300, 4600, 4900, 5200]
+ real(real32), parameter :: pi = 3.14159265358979323846_real32
+ integer :: rate, fft_size, i, k, ihsym, nzap, nkhz
+ integer(int8) :: lstrong(0:1023)
+ character(len=16) :: rate_text
+ real(real32), allocatable :: waterfall(:)
+ real(real32) :: gainx, gainy, phasex, phasey, rejectx, rejecty, pxdb, pydb, slimit
+ real(real32) :: power(4,5), expected(4,3), phase, nan_value
+ complex(real32) :: x, y
+
+ call get_command_argument(1, rate_text)
+ read(rate_text, *) rate
+ call require(rate == 96000 .or. rate == 192000, 'supported sample rate')
+ fft_size = 32768 * (rate / 96000)
+ call set_runtime_params(rate, fft_size, 56 * rate)
+ allocate(dd(4, fft_size + 1024), ss(4, 1, fft_size), savg(4, fft_size), waterfall(fft_size))
+ dd = 0.0
+ ss = 0.0
+ savg = 0.0
+
+ do i = 1, fft_size + 1024
+ x = cmplx(0.0_real32, 0.0_real32, real32)
+ y = x
+ phase = 2.0_real32 * pi * real(i - 1, real32) / real(fft_size, real32)
+ x = x + tone(bins(1), phase)
+ x = x + tone(bins(2), phase)
+ y = y + tone(bins(2), phase)
+ x = x + tone(bins(3), phase)
+ y = y - tone(bins(3), phase)
+ x = x + 2.0_real32 * tone(bins(4), phase)
+ x = x + tone(bins(5), phase + 0.71_real32)
+ dd(1,i) = real(x)
+ dd(2,i) = aimag(x)
+ dd(3,i) = real(y)
+ dd(4,i) = aimag(y)
+ end do
+
+ call spectrum(fft_size + 512, 1)
+ call require(ihsym == 1, 'first completed row')
+ do i = 1, 5
+ call require(abs(maxloc(ss(1,1,:), dim=1) - (bins(4) + 1)) <= 1, &
+ 'strongest X frequency bin')
+ power(:,i) = ss(:,1,bins(i)+1)
+ call require(all(abs(savg(:,bins(i)+1) - power(:,i)) <= 0.0001_real32 * max(1.0_real32, maxval(power(:,i)))), &
+ 'savg accumulates completed row')
+ end do
+ expected(:,1) = [1.0_real32, 0.5_real32, 0.0_real32, 0.5_real32]
+ expected(:,2) = [1.0_real32, 2.0_real32, 1.0_real32, 0.0_real32]
+ expected(:,3) = [1.0_real32, 0.0_real32, 1.0_real32, 2.0_real32]
+ do i = 1, 3
+ call require(all(abs(power(:,i) / power(1,i) - expected(:,i)) < 0.03_real32), &
+ 'polarization relative powers')
+ end do
+ call require(abs(power(1,4) / power(1,1) - 4.0_real32) < 0.12_real32, 'amplitude squared power')
+ call require(abs(power(1,5) / power(1,1) - 1.0_real32) < 0.03_real32, 'phase preserves power')
+
+ nan_value = ieee_value(0.0_real32, ieee_quiet_nan)
+ dd = 0.0
+ do i = 1, fft_size + 1024
+ x = tone(bins(1), 2.0_real32*pi*real(i-1, real32)/real(fft_size, real32))
+ dd(1,i) = real(x)
+ dd(2,i) = aimag(x)
+ end do
+ dd(3:4,:) = nan_value
+ call spectrum(fft_size, 0)
+ call require(ihsym == 1, 'single-polarization reset row')
+ call require(abs(maxloc(ss(1,1,:), dim=1) - (bins(1) + 1)) <= 1, 'single-polarization frequency bin')
+ call require(abs(ss(1,1,bins(1)+1) / power(1,1) - 1.0_real32) < 0.03_real32, &
+ 'poisoned Y cannot affect X power')
+ call require(abs(savg(1,bins(1)+1) / ss(1,1,bins(1)+1) - 1.0_real32) < 0.001_real32, &
+ 'single-polarization savg reset')
+ print '(a,i0)', 'MAP65 symspec contract passed at ', rate
+
+contains
+
+ function tone(bin, angle) result(value)
+ integer, intent(in) :: bin
+ real(real32), intent(in) :: angle
+ complex(real32) :: value
+ real(real32) :: theta
+
+ theta = real(bin, real32) * angle
+ value = 300.0_real32 * cmplx(cos(theta), -sin(theta), real32)
+ end function tone
+
+ subroutine spectrum(sample_count, polarization_count)
+ integer, intent(in) :: sample_count, polarization_count
+
+ k = sample_count
+ ihsym = 0
+ nzap = 0
+ gainx = 1.0
+ gainy = 1.0
+ phasex = 0.0
+ phasey = 0.0
+ slimit = 0.0
+ call symspec(k, polarization_count, 1, 0, 40, 0, 0, 0, gainx, gainy, phasex, phasey, &
+ rejectx, rejecty, pxdb, pydb, waterfall, nkhz, ihsym, nzap, slimit, lstrong)
+ end subroutine spectrum
+
+ 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_map65_symspec_contract
diff --git a/tests/unit/map65/windows_fake/windows.h b/tests/unit/map65/windows_fake/windows.h
new file mode 100644
index 00000000..3c1ab60c
--- /dev/null
+++ b/tests/unit/map65/windows_fake/windows.h
@@ -0,0 +1,29 @@
+#ifndef MAP65_TEST_WINDOWS_H
+#define MAP65_TEST_WINDOWS_H
+
+#include <stdint.h>
+
+typedef void *HANDLE;
+typedef uint32_t DWORD;
+typedef int BOOL;
+
+#define INVALID_HANDLE_VALUE ((HANDLE)(intptr_t)-1)
+#define GENERIC_WRITE 0x40000000
+#define OPEN_EXISTING 3
+#define FILE_ATTRIBUTE_NORMAL 0x80
+#define SETRTS 3
+#define CLRRTS 4
+#define SETDTR 5
+#define CLRDTR 6
+#define CLRBREAK 9
+#define ERROR_ACCESS_DENIED 5
+#define ERROR_GEN_FAILURE 31
+#define ERROR_NO_SUCH_DEVICE 433
+#define ERROR_DEVICE_NOT_CONNECTED 1167
+
+HANDLE CreateFileA(const char *, DWORD, DWORD, void *, DWORD, DWORD, HANDLE);
+BOOL EscapeCommFunction(HANDLE, DWORD);
+BOOL CloseHandle(HANDLE);
+DWORD GetLastError(void);
+
+#endif