diff options
| author | hibby <d@vehibberd.com> | 2026-10-06 21:24:40 +0100 |
|---|---|---|
| committer | hibby <d@vehibberd.com> | 2026-10-06 21:24:40 +0100 |
| commit | c25cbdf6fb6d946175f4335fab77bfea601b56db (patch) | |
| tree | 6f7a94b7b81f999e434f61b73d47e337dfd26bf2 /tests/unit/packjt77 | |
| parent | 71f5689252c9f13e70e1c24856904475b2307809 (diff) | |
| parent | 0685fcada9f95fe90f70b387cd7d757da8209624 (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/packjt77')
20 files changed, 5493 insertions, 0 deletions
diff --git a/tests/unit/packjt77/CMakeLists.txt b/tests/unit/packjt77/CMakeLists.txt new file mode 100644 index 00000000..2ce41135 --- /dev/null +++ b/tests/unit/packjt77/CMakeLists.txt @@ -0,0 +1,132 @@ +# Build the shared helper module once so parallel test targets do not race +# while writing the same Fortran .mod interface file. +add_library (packjt77_test_helpers STATIC packjt77_test_helpers.f90) +set_target_properties (packjt77_test_helpers PROPERTIES + Fortran_MODULE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/packjt77_test_helpers") +target_include_directories (packjt77_test_helpers PUBLIC + "${CMAKE_CURRENT_BINARY_DIR}/packjt77_test_helpers") +wsjt_link_common_fortran (packjt77_test_helpers) + +add_executable (test_packjt77_grammar_gate + test_packjt77_grammar_gate.f90) +target_include_directories (test_packjt77_grammar_gate PRIVATE ${CMAKE_BINARY_DIR}) +wsjt_link_common_fortran (test_packjt77_grammar_gate) + +add_test (NAME test_packjt77_grammar_gate COMMAND test_packjt77_grammar_gate) + +add_executable (test_packjt77_invariants + test_packjt77_invariants.f90) +target_include_directories (test_packjt77_invariants PRIVATE ${CMAKE_BINARY_DIR}) +target_link_libraries (test_packjt77_invariants PRIVATE packjt77_test_helpers) +wsjt_link_common_fortran (test_packjt77_invariants) + +add_test (NAME test_packjt77_invariants COMMAND test_packjt77_invariants) + +add_executable (test_packjt77var_invariants + test_packjt77var_invariants.f90) +target_include_directories (test_packjt77var_invariants PRIVATE ${CMAKE_BINARY_DIR}) +target_link_libraries (test_packjt77var_invariants PRIVATE packjt77_test_helpers) +wsjt_link_common_fortran (test_packjt77var_invariants) + +add_test (NAME test_packjt77var_invariants COMMAND test_packjt77var_invariants) + +add_executable (test_packjt77sdvar_invariants + test_packjt77sdvar_invariants.f90) +target_include_directories (test_packjt77sdvar_invariants PRIVATE ${CMAKE_BINARY_DIR}) +target_link_libraries (test_packjt77sdvar_invariants PRIVATE packjt77_test_helpers) +wsjt_link_common_fortran (test_packjt77sdvar_invariants) + +add_test (NAME test_packjt77sdvar_invariants COMMAND test_packjt77sdvar_invariants) + +add_executable (test_packjt77_protocol_vectors + test_packjt77_protocol_vectors.f90) +target_include_directories (test_packjt77_protocol_vectors PRIVATE ${CMAKE_BINARY_DIR}) +target_link_libraries (test_packjt77_protocol_vectors PRIVATE packjt77_test_helpers) +wsjt_link_common_fortran (test_packjt77_protocol_vectors) + +add_test (NAME test_packjt77_protocol_vectors COMMAND test_packjt77_protocol_vectors) + +add_executable (test_packjt77_schema_encoder + test_packjt77_schema_encoder.f90) +target_include_directories (test_packjt77_schema_encoder PRIVATE ${CMAKE_BINARY_DIR}) +target_link_libraries (test_packjt77_schema_encoder PRIVATE packjt77_test_helpers) +wsjt_link_common_fortran (test_packjt77_schema_encoder) + +add_test (NAME test_packjt77_schema_encoder COMMAND test_packjt77_schema_encoder) + +add_executable (test_packjt77_schema_decoder + test_packjt77_schema_decoder.f90) +target_include_directories (test_packjt77_schema_decoder PRIVATE ${CMAKE_BINARY_DIR}) +target_link_libraries (test_packjt77_schema_decoder PRIVATE packjt77_test_helpers) +wsjt_link_common_fortran (test_packjt77_schema_decoder) + +add_test (NAME test_packjt77_schema_decoder COMMAND test_packjt77_schema_decoder) + +add_executable (test_packjt77_hash_state + test_packjt77_hash_state.f90) +target_include_directories (test_packjt77_hash_state PRIVATE ${CMAKE_BINARY_DIR}) +target_link_libraries (test_packjt77_hash_state PRIVATE packjt77_test_helpers) +wsjt_link_common_fortran (test_packjt77_hash_state) + +add_test (NAME test_packjt77_hash_state COMMAND test_packjt77_hash_state) + +if (WSJT_BUILD_FORTRAN_OPENMP) + add_executable (test_packjt77_concurrent_unpack + test_packjt77_concurrent_unpack.f90) + target_include_directories (test_packjt77_concurrent_unpack PRIVATE + ${WSJT_FORTRAN_OPENMP_MODULE_DIR}) + target_link_libraries (test_packjt77_concurrent_unpack PRIVATE + wsjt_fort_omp wsjt_fortran_openmp) + + add_test (NAME test_packjt77_concurrent_unpack + COMMAND test_packjt77_concurrent_unpack) + set_tests_properties (test_packjt77_concurrent_unpack PROPERTIES + ENVIRONMENT "OMP_DYNAMIC=FALSE;OMP_NUM_THREADS=4;OMP_THREAD_LIMIT=4" + PROCESSORS 4 + TIMEOUT 30) +endif () + +add_executable (test_packjt77_pack28_sweep + test_packjt77_pack28_sweep.f90) +target_include_directories (test_packjt77_pack28_sweep PRIVATE ${CMAKE_BINARY_DIR}) +wsjt_link_common_fortran (test_packjt77_pack28_sweep) + +add_test (NAME test_packjt77_pack28_sweep COMMAND test_packjt77_pack28_sweep) + +add_executable (test_packjt77_domain_sweeps + test_packjt77_domain_sweeps.f90) +target_include_directories (test_packjt77_domain_sweeps PRIVATE ${CMAKE_BINARY_DIR}) +target_link_libraries (test_packjt77_domain_sweeps PRIVATE packjt77_test_helpers) +wsjt_link_common_fortran (test_packjt77_domain_sweeps) + +add_test (NAME test_packjt77_domain_sweeps COMMAND test_packjt77_domain_sweeps) + +add_executable (test_packjt77_mutation_sweep + test_packjt77_mutation_sweep.f90) +target_include_directories (test_packjt77_mutation_sweep PRIVATE ${CMAKE_BINARY_DIR}) +target_link_libraries (test_packjt77_mutation_sweep PRIVATE packjt77_test_helpers) +wsjt_link_common_fortran (test_packjt77_mutation_sweep) + +add_test (NAME test_packjt77_mutation_sweep COMMAND test_packjt77_mutation_sweep) + +find_program (WSJT_SH_EXECUTABLE NAMES sh) +if (NOT WSJT_SH_EXECUTABLE) + message (FATAL_ERROR "sh is required to run lint_packjt77_idioms") +endif () + +add_test (NAME lint_packjt77_idioms + COMMAND "${WSJT_SH_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/lint_packjt77_idioms.sh" + "${CMAKE_SOURCE_DIR}") + +add_executable (test_packjt77_silent_rewrite_corpus + test_packjt77_silent_rewrite_corpus.f90) +target_include_directories (test_packjt77_silent_rewrite_corpus PRIVATE ${CMAKE_BINARY_DIR}) +wsjt_link_common_fortran (test_packjt77_silent_rewrite_corpus) + +add_test (NAME test_packjt77_silent_rewrite_corpus + COMMAND test_packjt77_silent_rewrite_corpus + ${CMAKE_CURRENT_SOURCE_DIR}/silent_rewrite_corpus.txt --assert-zero-silent) +if (WSJT_ENABLE_TSAN) + set_tests_properties (test_packjt77_silent_rewrite_corpus PROPERTIES + ENVIRONMENT "TSAN_OPTIONS=${wsjt_tsan_fortran_options}") +endif () diff --git a/tests/unit/packjt77/encode77_test_messages.txt b/tests/unit/packjt77/encode77_test_messages.txt new file mode 100644 index 00000000..50f45596 --- /dev/null +++ b/tests/unit/packjt77/encode77_test_messages.txt @@ -0,0 +1,3 @@ +CQ K1ABC FN42 +HELLO@WORLD +K1ABC W9XYZ EN37 diff --git a/tests/unit/packjt77/lint_packjt77_idioms.sh b/tests/unit/packjt77/lint_packjt77_idioms.sh new file mode 100755 index 00000000..b221f9f0 --- /dev/null +++ b/tests/unit/packjt77/lint_packjt77_idioms.sh @@ -0,0 +1,88 @@ +#!/bin/sh + +root=${1:-.} +fail=0 + +show_violation() { + label=$1 + lines=$2 + echo "$label" + echo "$lines" + fail=1 +} + +pack_reads=$(grep -En "read *\(.*,\*" "$root/lib/77bit/packjt77.f90" || true) +if [ -n "$pack_reads" ]; then + show_violation "list-directed read in packjt77.f90:" "$pack_reads" +fi + +grammar_reads=$(grep -En "read *\(.*,\*" "$root/lib/77bit/packjt77_grammar.f90" || true) +grammar_read_count=0 +grammar_read_extra= +while IFS= read -r line; do + [ -z "$line" ] && continue + case "$line" in + *": read(token,*,err=10) value") + grammar_read_count=$((grammar_read_count + 1)) + ;; + *) + grammar_read_extra="${grammar_read_extra} +$line" + ;; + esac +done <<EOF +$grammar_reads +EOF +if [ "$grammar_read_count" -ne 2 ]; then + show_violation "unexpected grammar list-directed read count:" "$grammar_reads" +fi +if [ -n "$grammar_read_extra" ]; then + show_violation "unexpected grammar list-directed read:" "$grammar_read_extra" +fi + +bracket_lines=$(grep -En "index *\(.*'[<>]'" "$root/lib/77bit/packjt77.f90" || true) +bracket_extra= +while IFS= read -r line; do + [ -z "$line" ] && continue + case "$line" in + *": i=index(cw,'>')"|\ + *": i=index(cw,'>')"|\ + *": i2=index(c13,'>')"|\ + *": if(index(call_1,'<').le.0) then"|\ + *": if(index(call_2,'<').le.0) then") + ;; + *) + bracket_extra="${bracket_extra} +$line" + ;; + esac +done <<EOF +$bracket_lines +EOF +if [ -n "$bracket_extra" ]; then + show_violation "unexpected bracket probing in packjt77.f90:" "$bracket_extra" +fi + +legacy_lines=$(grep -En "call +(chkcall|split77)|(^|[^A-Za-z0-9_])(chkcall|split77) *\(" \ + "$root/lib/77bit/packjt77.f90" || true) +legacy_extra= +while IFS= read -r line; do + [ -z "$line" ] && continue + case "$line" in + *":subroutine split77(msg,nwords,nw,w)"|\ + *": call chkcall(w(3),bcall_1,ok1)"|\ + *":end subroutine split77") + ;; + *) + legacy_extra="${legacy_extra} +$line" + ;; + esac +done <<EOF +$legacy_lines +EOF +if [ -n "$legacy_extra" ]; then + show_violation "unexpected chkcall or split77 encode-path use:" "$legacy_extra" +fi + +exit "$fail" diff --git a/tests/unit/packjt77/packjt77_test_helpers.f90 b/tests/unit/packjt77/packjt77_test_helpers.f90 new file mode 100644 index 00000000..0373a005 --- /dev/null +++ b/tests/unit/packjt77/packjt77_test_helpers.f90 @@ -0,0 +1,1576 @@ +module packjt77_test_helpers + + use packjt77 + use packjt77_schema + implicit none + private :: reset_packjt77_state, prime_hash_call, prime_hash_call_var, & + store_rx_hash_var, wipe_shared_hash_tables, set_standard_call_state, & + set_var_call_state + + type pack77_result + logical :: encoded=.false. + integer :: status=PACK77_STATUS_NOT_ENCODED + integer :: i3=-1 + integer :: n3=-1 + character(len=77) :: c77=' ' + end type pack77_result + +contains + + subroutine wipe_shared_hash_tables() + calls10='' + calls12='' + calls22='' + recent_calls='' + ihash22=-1 + nzhash=0 + end subroutine wipe_shared_hash_tables + + subroutine set_standard_call_state(mycall,dxcall) + character(len=*), intent(in) :: mycall, dxcall + + mycall13=' ' + dxcall13=' ' + mycall13_configured=' ' + dxcall13_configured=' ' + mycall13=mycall + dxcall13=dxcall + mycall13_configured=mycall + dxcall13_configured=dxcall + end subroutine set_standard_call_state + + subroutine set_var_call_state(mycall,dxcall) + character(len=*), intent(in) :: mycall, dxcall + + queued_calls_by_thread='' + queued_recent_calls_by_thread='' + nqueued_calls_by_thread=0 + nqueued_recent_calls_by_thread=0 + mycall13=' ' + dxcall13=' ' + mycall13_configured_prev=' ' + dxcall13_configured_prev=' ' + mycall13_configured_set=.false. + dxcall13_configured_set=.false. + hashmy10_configured=-1 + hashmy12_configured=-1 + hashmy22_configured=-1 + hashdx10_configured=-1 + mycall13=mycall + dxcall13=dxcall + if(len_trim(mycall).gt.2) then + mycall13_configured_set=.true. + mycall13_configured_prev=mycall13 + hashmy10_configured=ihashcall(mycall13,10) + hashmy12_configured=ihashcall(mycall13,12) + hashmy22_configured=ihashcall(mycall13,22) + endif + if(len_trim(dxcall).gt.2) then + dxcall13_configured_set=.true. + dxcall13_configured_prev=dxcall13 + hashdx10_configured=ihashcall(dxcall13,10) + endif + end subroutine set_var_call_state + + subroutine clear_standard_state(mycall,dxcall) + character(len=*), intent(in) :: mycall, dxcall + + call wipe_shared_hash_tables() + call set_standard_call_state(mycall,dxcall) + end subroutine clear_standard_state + + subroutine clear_var_state(mycall,dxcall) + character(len=*), intent(in) :: mycall, dxcall + + call wipe_shared_hash_tables() + call set_var_call_state(mycall,dxcall) + end subroutine clear_var_state + + subroutine clear_all_state(mycall,dxcall) + character(len=*), intent(in) :: mycall, dxcall + + call wipe_shared_hash_tables() + call set_standard_call_state(mycall,dxcall) + call set_var_call_state(mycall,dxcall) + end subroutine clear_all_state + + subroutine normalize_call(callsign,c13) + character(len=*), intent(in) :: callsign + character(len=13), intent(out) :: c13 + integer :: i + + c13=' ' + c13=callsign + if(c13(1:1).eq.'<') c13=c13(2:) + i=index(c13,'>') + if(i.gt.0) c13(i:)=' ' + end subroutine normalize_call + + subroutine assert_message_type(label,input,want_i3,want_n3,got_i3,got_n3) + character(len=*), intent(in) :: label, input + integer, intent(in) :: want_i3, want_n3, got_i3, got_n3 + + if(got_i3.ne.want_i3 .or. got_n3.ne.want_n3) then + write(*,1000) trim(label), trim(input), want_i3, want_n3, got_i3, got_n3 +1000 format(a,' message type failure for "',a,'"; wanted ',i0,'.',i0, & + ' got ',i0,'.',i0) + error stop 1 + endif + end subroutine assert_message_type + + subroutine assert_binary_payload(label,input,c77) + character(len=*), intent(in) :: label, input + character(len=77), intent(in) :: c77 + + if(.not.is_binary_payload(c77)) then + write(*,1010) trim(label), trim(input), c77 +1010 format(a,' emitted non-binary payload for "',a,'": ',a) + error stop 1 + endif + end subroutine assert_binary_payload + + subroutine assert_pack77_result(label,input,encoded) + character(len=*), intent(in) :: label, input + type(pack77_result), intent(in) :: encoded + + if(.not.encoded%encoded) then + write(*,1015) trim(label), trim(input) +1015 format(a,' failed to encode "',a,'"') + error stop 1 + endif + if(encoded%i3.lt.0 .or. encoded%i3.gt.7 .or. encoded%n3.lt.0 .or. & + encoded%n3.gt.7) then + write(*,1016) trim(label), trim(input), encoded%i3, encoded%n3 +1016 format(a,' emitted invalid type for "',a,'": ',i0,'.',i0) + error stop 1 + endif + call assert_binary_payload(label,input,encoded%c77) + end subroutine assert_pack77_result + + type(pack77_result) function pack77_result_from_api(msg,options) result(encoded) + character(len=*), intent(in) :: msg + type(pack77_options), intent(in), optional :: options + + encoded=pack77_result() + call pack77(msg,encoded%i3,encoded%n3,encoded%c77,options, & + status=encoded%status) + encoded%encoded=encoded%i3.ge.0 + end function pack77_result_from_api + + type(pack77_result) function pack77_legacy_result_from_api(msg,options) & + result(encoded) + character(len=*), intent(in) :: msg + type(pack77_options), intent(in), optional :: options + + encoded=pack77_result() + call pack77_legacy_truncating_fallback(msg,encoded%i3,encoded%n3, & + encoded%c77,options,status=encoded%status) + encoded%encoded=encoded%i3.ge.0 + end function pack77_legacy_result_from_api + + type(pack77_result) function pack77_with_hint(msg,pack_i3,pack_n3) result(encoded) + character(len=*), intent(in) :: msg + integer, intent(in) :: pack_i3, pack_n3 + + encoded=pack77_result() + if(pack_i3.eq.0 .and. pack_n3.eq.5) then + encoded=strict_telemetry_result_from_schema(msg) + else if(pack_i3.eq.0 .and. pack_n3.eq.6) then + encoded=pack77_result_from_api(msg, & + pack77_options(prefer_wspr_50bit=.true.)) + else + encoded=pack77_result_from_api(msg) + endif + end function pack77_with_hint + + type(pack77_result) function strict_telemetry_result_from_schema(msg) & + result(encoded) + character(len=*), intent(in) :: msg + character(len=37) :: text + character(len=18) :: c18 + character(len=1) :: ch + integer :: i,n + integer :: ntel(3) + logical :: ok + + encoded=pack77_result() + text=adjustl(msg) + n=len_trim(text) + if(n.lt.1 .or. n.gt.18) return + do i=1,n + ch=text(i:i) + if(ch.ge.'a' .and. ch.le.'z') ch=char(ichar(ch)-32) + if(index('0123456789ABCDEF',ch).eq.0) return + text(i:i)=ch + enddo + + c18=text(1:n) + c18=adjustr(c18) + ntel=-99 + read(c18,1005,err=6) ntel +1005 format(3z6) +6 if(ntel(1).lt.0 .or. ntel(2).lt.0 .or. ntel(3).lt.0) return + + call encode_pack77_telemetry(ntel(1),ntel(2),ntel(3),encoded%c77,ok) + if(ok) then + encoded%encoded=.true. + encoded%status=PACK77_STATUS_ENCODED + encoded%i3=0 + encoded%n3=5 + endif + end function strict_telemetry_result_from_schema + + logical function is_binary_payload(c77) + character(len=77), intent(in) :: c77 + integer :: i + + is_binary_payload=.true. + do i=1,77 + if(c77(i:i).ne.'0' .and. c77(i:i).ne.'1') then + is_binary_payload=.false. + return + endif + enddo + end function is_binary_payload + + subroutine assert_decode(label,input,expected,decoded,ok) + character(len=*), intent(in) :: label, input, expected, decoded + logical, intent(in) :: ok + + if(.not.ok) then + write(*,1020) trim(label), trim(input) +1020 format(a,' unpack failure for "',a,'"') + error stop 1 + endif + if(trim(decoded).ne.expected) then + write(*,1030) trim(label), trim(input), trim(expected), trim(decoded) +1030 format(a,' decode failure for "',a,'"; expected "',a,'"; got "',a,'"') + error stop 1 + endif + end subroutine assert_decode + + subroutine assert_text_equal(label,left,right) + character(len=*), intent(in) :: label, left, right + + if(trim(left).ne.trim(right)) then + write(*,1040) trim(label), trim(left), trim(right) +1040 format(a,' mismatch; left "',a,'" right "',a,'"') + error stop 1 + endif + end subroutine assert_text_equal + + subroutine assert_canonical_text(label,input,canonical,decoded) + character(len=*), intent(in) :: label, input, canonical, decoded + + if(canonical.ne.decoded) then + write(*,1045) trim(label), trim(input), trim(canonical), trim(decoded) +1045 format(a,' canonical mismatch for "',a,'"; canonical "',a, & + '"; decoded "',a,'"') + error stop 1 + endif + end subroutine assert_canonical_text + + logical function normalized_equal(left,right) result(equal) + character(len=*), intent(in) :: left, right + character(len=37) :: left_norm, right_norm + + call normalize_message(left,left_norm) + call normalize_message(right,right_norm) + equal=trim(left_norm).eq.trim(right_norm) + end function normalized_equal + + logical function canonical_match(left,right) result(matches) + character(len=*), intent(in) :: left, right + character(len=37) :: left_tokens(19), right_tokens(19), wrapped + integer :: left_n, right_n, i + + matches=.false. + call split_tokens(left,left_tokens,left_n) + call split_tokens(right,right_tokens,right_n) + if(left_n.ne.right_n) return + do i=1,left_n + if(trim(left_tokens(i)).eq.trim(right_tokens(i))) cycle + if(plausible_auto_hash_source(left_tokens(i))) then + wrapped=' ' + wrapped='<'//trim(left_tokens(i))//'>' + if(trim(right_tokens(i)).eq.trim(wrapped)) cycle + endif + if(trim(right_tokens(i)).eq.'<...>' .and. & + hash_placeholder_source(left_tokens(i))) cycle + return + enddo + matches=.true. + end function canonical_match + + logical function telemetry_match(left,right) result(matches) + character(len=*), intent(in) :: left, right + character(len=37) :: left_tokens(19), right_tokens(19) + integer :: left_n, right_n + + ! Telemetry messages allow a documented leading-zero canonical form. + matches=.false. + call split_tokens(left,left_tokens,left_n) + call split_tokens(right,right_tokens,right_n) + if(left_n.ne.1 .or. right_n.ne.1) return + matches=telemetry_token_match(left_tokens(1),right_tokens(1)) + end function telemetry_match + + subroutine assert_call(label,got,expected) + character(len=*), intent(in) :: label, got, expected + + if(trim(got).ne.expected) then + write(*,1050) trim(label), trim(expected), trim(got) +1050 format(a,' failure; expected "',a,'"; got "',a,'"') + error stop 1 + endif + end subroutine assert_call + + subroutine assert_int(label,expected,got) + character(len=*), intent(in) :: label + integer, intent(in) :: expected, got + + if(got.ne.expected) then + write(*,1060) trim(label), expected, got +1060 format(a,' failure; expected ',i0,' got ',i0) + error stop 1 + endif + end subroutine assert_int + + subroutine normalize_message(input_msg,normalized) + character(len=*), intent(in) :: input_msg + character(len=37), intent(out) :: normalized + character(len=37) :: tokens(19) + integer :: nwords, i + + normalized=' ' + call split_tokens(input_msg,tokens,nwords) + do i=1,nwords + if(len_trim(normalized).gt.0) normalized=trim(normalized)//' ' + normalized=trim(normalized)//trim(tokens(i)) + enddo + end subroutine normalize_message + + subroutine split_tokens(input_msg,tokens,nwords) + character(len=*), intent(in) :: input_msg + character(len=37), intent(out) :: tokens(19) + integer, intent(out) :: nwords + character(len=1) :: c, previous + integer :: i, n + + tokens=' ' + nwords=0 + n=0 + previous=' ' + do i=1,len_trim(input_msg) + c=input_msg(i:i) + if(ichar(c).eq.0) c=' ' + if(c.ge.'a' .and. c.le.'z') c=char(ichar(c)-32) + if(c.eq.' ' .and. previous.eq.' ') cycle + if(c.ne.' ' .and. previous.eq.' ') then + nwords=nwords+1 + n=0 + if(nwords.gt.size(tokens)) return + endif + if(c.ne.' ') then + n=n+1 + if(n.le.len(tokens(1))) tokens(nwords)(n:n)=c + endif + previous=c + enddo + end subroutine split_tokens + + logical function plausible_auto_hash_source(token) result(ok) + character(len=*), intent(in) :: token + integer :: i,n,slash + + n=len_trim(token) + ok=.false. + if(n.lt.3 .or. n.gt.11) return + slash=index(token(1:n),'/') + if(slash.lt.2 .or. slash.ge.n) return + if(index(token(slash+1:n),'/').gt.0) return + do i=1,n + if(i.eq.slash) cycle + if(.not.((token(i:i).ge.'A' .and. token(i:i).le.'Z') .or. & + (token(i:i).ge.'0' .and. token(i:i).le.'9'))) return + enddo + ok=.true. + end function plausible_auto_hash_source + + logical function hash_placeholder_source(token) result(ok) + character(len=*), intent(in) :: token + integer :: n + + n=len_trim(token) + ok=plausible_auto_hash_source(token) + if(ok) return + if(n.lt.3 .or. n.gt.13) return + ok=token(1:1).eq.'<' .and. token(n:n).eq.'>' .and. n-2.le.11 + end function hash_placeholder_source + + logical function telemetry_token_match(left,right) result(matches) + character(len=*), intent(in) :: left, right + character(len=18) :: left_hex, right_hex + integer :: left_n, right_n + + matches=.false. + left_hex=upper_token(left) + right_hex=upper_token(right) + left_n=len_trim(left_hex) + right_n=len_trim(right_hex) + if(left_n.lt.17 .or. left_n.gt.18) return + if(right_n.lt.17 .or. right_n.gt.18) return + if(.not.is_hex_token(left_hex,left_n)) return + if(.not.is_hex_token(right_hex,right_n)) return + if(left_n.eq.right_n .and. trim(left_hex).eq.trim(right_hex)) then + matches=.true. + else if(left_n.eq.right_n+1 .and. left_hex(1:1).eq.'0' .and. & + left_hex(2:left_n).eq.right_hex(1:right_n)) then + matches=.true. + else if(right_n.eq.left_n+1 .and. right_hex(1:1).eq.'0' .and. & + right_hex(2:right_n).eq.left_hex(1:left_n)) then + matches=.true. + endif + end function telemetry_token_match + + logical function is_hex_token(token,n) result(ok) + character(len=*), intent(in) :: token + integer, intent(in) :: n + integer :: i + + ok=.true. + do i=1,n + if(index('0123456789ABCDEF',token(i:i)).eq.0) then + ok=.false. + return + endif + enddo + end function is_hex_token + + character(len=18) function upper_token(token) result(upper) + character(len=*), intent(in) :: token + integer :: i, n + character :: c + + upper=' ' + n=min(len_trim(token),len(upper)) + do i=1,n + c=token(i:i) + if(c.ge.'a' .and. c.le.'z') c=char(ichar(c)-32) + upper(i:i)=c + enddo + end function upper_token + + subroutine assert_true(label,condition) + character(len=*), intent(in) :: label + logical, intent(in) :: condition + + if(.not.condition) then + write(*,1070) trim(label) +1070 format(a,' failure') + error stop 1 + endif + end subroutine assert_true + + subroutine assert_shared_hash_tables_empty(label) + character(len=*), intent(in) :: label + + call assert_int(trim(label)//' nzhash',0,nzhash) + call assert_true(trim(label)//' calls10',all(calls10.eq.'')) + call assert_true(trim(label)//' calls12',all(calls12.eq.'')) + call assert_true(trim(label)//' calls22',all(calls22.eq.'')) + call assert_true(trim(label)//' recent calls',all(recent_calls.eq.'')) + call assert_true(trim(label)//' ihash22',all(ihash22.eq.-1)) + call assert_true(trim(label)//' queued hash counts', & + all(nqueued_calls_by_thread.eq.0)) + call assert_true(trim(label)//' queued recent counts', & + all(nqueued_recent_calls_by_thread.eq.0)) + call assert_true(trim(label)//' queued calls', & + all(queued_calls_by_thread.eq.'')) + call assert_true(trim(label)//' queued recent calls', & + all(queued_recent_calls_by_thread.eq.'')) + end subroutine assert_shared_hash_tables_empty + + subroutine run_packjt77_common_invariants(configured_var,ntests) + logical, intent(in) :: configured_var + integer, intent(inout) :: ntests + + call expect_round_trip('K1ABC W9XYZ FN42', & + 'K1ABC W9XYZ FN42', 1, 0, 0) + call expect_round_trip('K1ABC W9XYZ R-11', & + 'K1ABC W9XYZ R-11', 1, 0, 0) + call expect_round_trip('K1ABC W9XYZ', & + 'K1ABC W9XYZ', 1, 0, 0) + call expect_round_trip('K1ABC W9XYZ RRR', & + 'K1ABC W9XYZ RRR', 1, 0, 0) + call expect_round_trip('K1ABC W9XYZ RR73', & + 'K1ABC W9XYZ RR73', 1, 0, 0) + call expect_round_trip('K1ABC W9XYZ 73', & + 'K1ABC W9XYZ 73', 1, 0, 0) + call expect_round_trip('K1ABC W9XYZ +00', & + 'K1ABC W9XYZ +00', 1, 0, 0) + call expect_round_trip('K1ABC W9XYZ -30', & + 'K1ABC W9XYZ -30', 1, 0, 0) + call expect_round_trip('K1ABC W9XYZ -31', & + 'K1ABC W9XYZ -31', 1, 0, 0) + call expect_round_trip('K1ABC W9XYZ -50', & + 'K1ABC W9XYZ -50', 1, 0, 0) + call expect_round_trip('K1ABC W9XYZ +50', & + 'K1ABC W9XYZ +50', 1, 0, 0) + call expect_pack77_failure('K1ABC W9XYZ -55') + call expect_pack77_failure('K1ABC W9XYZ +51') + call expect_round_trip('K1ABC W9XYZ R+00', & + 'K1ABC W9XYZ R+00', 1, 0, 0) + call expect_round_trip('K1ABC W9XYZ R-31', & + 'K1ABC W9XYZ R-31', 1, 0, 0) + call expect_round_trip('K1ABC W9XYZ R-50', & + 'K1ABC W9XYZ R-50', 1, 0, 0) + call expect_round_trip('K1ABC W9XYZ R+50', & + 'K1ABC W9XYZ R+50', 1, 0, 0) + call expect_pack77_failure('K1ABC W9XYZ R-55') + call expect_pack77_failure('K1ABC W9XYZ R+51') + call expect_round_trip('K1ABC W9XYZ R FN42', & + 'K1ABC W9XYZ R FN42', 1, 0, 0) + call expect_pack77_failure('K1ABC W9XYZ FN42X') + call expect_pack77_failure('K1ABC W9XYZ R FN42X') + call expect_round_trip('K1ABC/P W9XYZ/P R FN42', & + 'K1ABC/P W9XYZ/P R FN42', 2, 0, 0) + call expect_round_trip('K1ABC/P W9XYZ/P FN42', & + 'K1ABC/P W9XYZ/P FN42', 2, 0, 0) + call expect_not_message_type('K1ABC/R W9XYZ/P FN42', 2, 0) + call expect_not_message_type('K1ABC W9XYZ +3', 1, 0) + call expect_round_trip('HELLO WORLD', & + 'HELLO WORLD', 0, 0, 0) + call expect_round_trip('hello', & + 'HELLO', 0, 0, 0) + call expect_round_trip('FREE TEXT MSG', & + 'FREE TEXT MSG', 0, 0, 0) + call expect_round_trip('ABCDEFGHIJKLM', & + 'ABCDEFGHIJKLM', 0, 0, 0) + call expect_pack77_failure('ABCDEFGHIJKLMN') + call expect_pack77_failure('HELLO WORLD 73') + call expect_pack77_failure('HELLO@WORLD') + call expect_pack77_failure('K1ABC PJ4/K9XYZ') + call expect_free_text_rejection('HELLO@WORLD', & + PACK77_STATUS_FREE_TEXT_INVALID) + call expect_free_text_rejection('THIS MESSAGE IS TOO LONG', & + PACK77_STATUS_FREE_TEXT_TOO_LONG) + call expect_pack77_structured('K1ABC W9XYZ FN42', 1, 0) + call expect_free_text_rejection('K1ABC W9XYZ@', & + PACK77_STATUS_FREE_TEXT_INVALID) + call expect_free_text_rejection('ABCDEFGHIJKLMN', & + PACK77_STATUS_FREE_TEXT_TOO_LONG) + call expect_free_text_rejection('A:B', & + PACK77_STATUS_FREE_TEXT_INVALID) + call expect_free_text_fallback('K1ABC R BOGUS', 'K1ABC R BOGUS') + call expect_legacy_truncating_fallback('ABCDEFGHIJKLMN', & + 'ABCDEFGHIJKLM') + call expect_legacy_truncating_fallback('K1ABCW9XYZ FN42', & + 'K1ABCW9XYZ FN') + call expect_legacy_matches_strict('K1ABC W9XYZ FN42') + call expect_legacy_matches_strict('FREE TEXT MSG') + call expect_legacy_fallback_failure('HELLO@WORLD 73', & + PACK77_STATUS_FREE_TEXT_INVALID) + call expect_round_trip('WA9XYZ KA1ABC R 16A EMA', & + 'WA9XYZ KA1ABC R 16A EMA', 0, 3, 0) + call expect_round_trip('WA9XYZ KA1ABC R 32A EMA', & + 'WA9XYZ KA1ABC R 32A EMA', 0, 4, 0) + call expect_round_trip('WA9XYZ KA1ABC 1A EMA', & + 'WA9XYZ KA1ABC 1A EMA', 0, 3, 0) + call expect_round_trip('WA9XYZ KA1ABC R 1A EMA', & + 'WA9XYZ KA1ABC R 1A EMA', 0, 3, 0) + call expect_round_trip_with_hashes('W1AW/P K1ABC 5A CT', & + '<W1AW/P> K1ABC 5A CT', 0, 3, 0, 'W1AW/P', '', '') + call expect_round_trip_with_hashes('ABCDEFG/HIJ KLMNOPQ/RST R 32A EMA', & + '<ABCDEFG/HIJ> <KLMNOPQ/RST> R 32A EMA', 0, 4, 0, & + 'ABCDEFG/HIJ', 'KLMNOPQ/RST', '') + call expect_round_trip('WA9XYZ KA1ABC 15F DX', & + 'WA9XYZ KA1ABC 15F DX', 0, 3, 0) + call expect_round_trip('WA9XYZ KA1ABC 17A EMA', & + 'WA9XYZ KA1ABC 17A EMA', 0, 4, 0) + call expect_round_trip_with_hashes('Y8W MW8WA/A R 17A ONE', & + 'Y8W <MW8WA/A> R 17A ONE', 0, 4, 0, 'MW8WA/A', '', '') + call expect_round_trip('WA9XYZ KA1ABC R 32A NB', & + 'WA9XYZ KA1ABC R 32A NB', 0, 4, 0) + call expect_pack77_failure('WA9XYZ KA1ABC R 1H EMA') + call expect_round_trip('PJ2/W1AW KA1ABC R 1A EMA', & + '<...> KA1ABC R 1A EMA', 0, 3, 0) + call expect_pack77_failure('WA9XYZ KA1ABC R 0A EMA') + call expect_pack77_failure('WA9XYZ KA1ABC R 33A EMA') + call expect_pack77_failure('WA9XYZ KA1ABC R 1A XYZ') + call expect_not_message_type('WA9XYZ KA1ABC +1A EMA', 0, 3) + call expect_not_message_type('WA9XYZ KA1ABC 01A EMA', 0, 3) + call expect_not_message_type('WA9XYZ KA1ABC R 1A EM', 0, 3) + call expect_not_message_type('WA9XYZ KA1ABC X 1A EMA', 0, 3) + call expect_not_message_type('<WA9XYZ> KA1ABC R 1A EMA', 0, 3) + call expect_pack77_failure('K1ABC/ K1A 1A CT') + call expect_pack77_failure('/K1ABC K1A 1A CT') + call expect_pack77_failure('A//B K1A 1A CT') + call expect_pack77_failure('K1ABC//P K1A 1A CT') + call expect_pack77_failure('ABCDEFGHI/JKL MNOPQRSTU/VWX 1A EMA') + call expect_round_trip('123456789ABCDEF012', & + '123456789ABCDEF012', 0, 5, 0) + call expect_round_trip_with_hint('0de', & + '0000000000000000DE', 0, 5, 0, 5, 0, '', '', '') + call expect_round_trip('TU; W9XYZ K1ABC R 579 MA', & + 'TU; W9XYZ K1ABC R 579 MA', 3, 0, 0) + call expect_round_trip('W9XYZ K1ABC R 579 MA', & + 'W9XYZ K1ABC R 579 MA', 3, 0, 0) + call expect_round_trip('W9XYZ K1ABC 579 MA', & + 'W9XYZ K1ABC 579 MA', 3, 0, 0) + call expect_round_trip('TU; W9XYZ G8ABC R 559 0013', & + 'TU; W9XYZ G8ABC R 559 0013', 3, 0, 0) + call expect_round_trip('W9XYZ G8ABC 559 0013', & + 'W9XYZ G8ABC 559 0013', 3, 0, 0) + call expect_round_trip('W9XYZ K1ABC 529 MA', & + 'W9XYZ K1ABC 529 MA', 3, 0, 0) + call expect_round_trip_with_hashes('W9XYZ W1AW/P R 599 MA', & + 'W9XYZ <W1AW/P> R 599 MA', 3, 0, 0, 'W1AW/P', '', '') + call expect_round_trip('TU; W9XYZ K1ABC 599 MA', & + 'TU; W9XYZ K1ABC 599 MA', 3, 0, 0) + call expect_round_trip('TU; W9XYZ K1ABC R 589 DC', & + 'TU; W9XYZ K1ABC R 589 DC', 3, 0, 0) + call expect_round_trip('W9XYZ VE3ABC 559 ON', & + 'W9XYZ VE3ABC 559 ON', 3, 0, 0) + call expect_round_trip('W9XYZ G8ABC 529 0001', & + 'W9XYZ G8ABC 529 0001', 3, 0, 0) + call expect_round_trip('W9XYZ G8ABC R 599 7999', & + 'W9XYZ G8ABC R 599 7999', 3, 0, 0) + call expect_round_trip('TU; W9XYZ G8ABC 529 0001', & + 'TU; W9XYZ G8ABC 529 0001', 3, 0, 0) + call expect_pack77_failure('W9XYZ K1ABC 509 MA') + call expect_pack77_failure('W9XYZ K1ABC R 519 MA') + call expect_pack77_failure('W9XYZ K1ABC R 609 MA') + call expect_pack77_failure('W9XYZ K1ABC R 579 XYZ') + call expect_pack77_failure('W9XYZ K1ABC 579 MAX') + call expect_pack77_failure('W9XYZ G8ABC R 559 13') + call expect_pack77_failure('W9XYZ G8ABC R 559 0000') + call expect_pack77_failure('W9XYZ G8ABC R 559 8000') + call expect_not_message_type('W9XYZ K1ABC R 579 MA JUNK', 3, 0) + call expect_not_message_type('<W9XYZ> K1ABC R 579 MA', 3, 0) + call expect_not_message_type('TU; GF9/XJ4PIE GP5MZC/1 R 539 1843', 3, 0) + call expect_raw_type3_unpack_failure(0) + call expect_raw_type3_unpack_success(1, & + 'W9XYZ K1ABC R 579 0001') + call expect_raw_type3_unpack_success(7999, & + 'W9XYZ K1ABC R 579 7999') + call expect_raw_type3_unpack_failure(8000) + call expect_raw_type3_unpack_failure(8191) + call expect_nonbinary_unpack_failure() + call expect_not_message_type('<W9XYZ> <K1ABC> R 579 MA', 3, 0) + call expect_round_trip('CQ PJ4/K1ABC', & + 'CQ PJ4/K1ABC', 4, 0, 0) + call expect_pack77_failure('CQ PJ4/K1ABC RR73') + call expect_not_message_type('CQ PJ4/K1ABC FK52', 1, 0) + call expect_round_trip('CQ N0CALL', & + 'CQ N0CALL', 4, 0, 0) + call expect_round_trip('CQ N0CAL FN31', & + 'CQ N0CAL FN31', 1, 0, 0) + call expect_not_message_type('CQ N0CALL FN31', 1, 0) + call expect_not_message_type('CQ N0CALL FN31', 4, 0) + call expect_pack77_failure('CQ N0CALL FN31') + call expect_pack77_failure('K1ABC PJ4/K9XYZ') + call expect_not_message_type('CQ 3DA0RS/W1ABC', 4, 0) + call expect_round_trip('CQ K1ABC FN42', & + 'CQ K1ABC FN42', 1, 0, 0) + call expect_round_trip('CQ 146 K1ABC FN42', & + 'CQ 146 K1ABC FN42', 1, 0, 0) + call expect_round_trip('CQ DX K1ABC FN42', & + 'CQ DX K1ABC FN42', 1, 0, 0) + call expect_round_trip('DE K1ABC FN42', & + 'DE K1ABC FN42', 1, 0, 0) + + call expect_round_trip('K1ABC FN42 37', & + 'K1ABC FN42 37', 0, 6, 0) + call expect_round_trip('PJ2/K1ABC 37', & + 'PJ2/K1ABC 37', 0, 6, 0) + call expect_round_trip('DL/K1ABC 37', & + 'DL/K1ABC 37', 0, 6, 0) + call expect_round_trip('K1ABC/P 37', & + 'K1ABC/P 37', 0, 6, 0) + call expect_not_message_type('00/K1ABC 37', 0, 6) + call expect_not_message_type('K1ABC/00 37', 0, 6) + call expect_round_trip_with_hint('<PJ4/K1ABC> FK52AB', & + '<PJ4/K1ABC> FK52AB', 0, 6, 0, 6, 0, 'PJ4/K1ABC', '', '') + call expect_not_message_type('PJ2/K1ABC FN42 37', 0, 6) + call expect_not_message_type('K1ABC FN4 37', 0, 6) + call expect_not_message_type('Q5A FN20 37', 0, 6) + call expect_not_message_type('VK9X/W1ABC 37', 0, 6) + call expect_not_message_type('A$/K1ABC 37', 0, 6) + call expect_not_message_type('K1ABC/A$ 37', 0, 6) + call expect_not_message_type('K1ABC/ABCD 37', 0, 6) + call expect_not_message_type('PJ2/Q5A 37', 0, 6) + call expect_not_message_type('K1ABC FN42 55', 0, 6) + call expect_not_message_type('K1ABC FN42 03', 0, 6) + call expect_not_message_type('<PJ4/K1ABC> FK52AB', 0, 6) + call expect_strict_telemetry_rejects('K1ABC W9XYZ') + call expect_strict_telemetry_rejects('') + call expect_strict_telemetry_rejects(' ') + call expect_strict_telemetry_rejects('+1') + call expect_strict_telemetry_rejects('-1') + call expect_strict_telemetry_rejects('ABC DEF') + call expect_strict_telemetry_rejects('123G') + call expect_strict_telemetry_rejects('123456789ABCDEF0123') + call expect_not_message_type('6666AAAAAAAA66666661ZZ', 0, 5) + call expect_prefer_wspr_rejects_invalid_type3('<PJ4/K1ABC> FK52A!') + call expect_prefer_wspr_rejects_invalid_type3('<PJ4/K1ABC>X FK52AB') + call expect_legacy_prefer_wspr_rejects('PJ4/K1ABC FK52A!') + + call expect_round_trip_with_hashes('<PJ4/K1ABC> W9XYZ RR73', & + '<PJ4/K1ABC> W9XYZ RR73', 1, 0, 0, 'PJ4/K1ABC', '', '') + call expect_round_trip_with_hashes('<PJ4/K1ABC> W9XYZ RRR', & + '<PJ4/K1ABC> W9XYZ RRR', 1, 0, 0, 'PJ4/K1ABC', '', '') + call expect_round_trip_with_hashes('<PJ4/K1ABC> W9XYZ 73', & + '<PJ4/K1ABC> W9XYZ 73', 1, 0, 0, 'PJ4/K1ABC', '', '') + if(configured_var) then + call expect_type_unpack_failure_with_hashes('PJ2/W1AW <W7ABC>', & + 4, 0, 0, 'W7ABC', '', '') + else + call expect_round_trip_with_hashes('PJ2/W1AW <W7ABC>', & + 'PJ2/W1AW <W7ABC>', 4, 0, 0, 'W7ABC', '', '') + endif + call expect_round_trip_with_hashes('PJ2/W1AW <W7ABC> RRR', & + 'PJ2/W1AW <W7ABC> RRR', 4, 0, 0, 'W7ABC', '', '') + call expect_round_trip_with_hashes('PJ2/W1AW <W7ABC> RR73', & + 'PJ2/W1AW <W7ABC> RR73', 4, 0, 0, 'W7ABC', '', '') + call expect_round_trip_with_hashes('PJ2/W1AW <W7ABC> 73', & + 'PJ2/W1AW <W7ABC> 73', 4, 0, 0, 'W7ABC', '', '') + call expect_round_trip_with_hashes('<W7ABC> PJ2/W1AW RR73', & + '<W7ABC> PJ2/W1AW RR73', 4, 0, 0, 'W7ABC', '', '') + call expect_round_trip_with_context('PJ2/W1AW <W7ABC> 73', & + 'PJ2/W1AW <W7ABC> 73', 4, 0, 0, 'W7ABC', 'N0BBB', '', '', '') + call expect_round_trip_with_context('<PJ4/K1ABC> W9XYZ RR73', & + '<PJ4/K1ABC> W9XYZ RR73', 1, 0, 1, 'PJ4/K1ABC', 'W9XYZ', '', '', '') + call expect_round_trip_with_hashes('<PJ4/K1ABC> W9XYZ -12', & + '<PJ4/K1ABC> W9XYZ -12', 1, 0, 0, 'PJ4/K1ABC', '', '') + call expect_not_message_type('<PJ4/K1ABC> W9XYZ -12', 4, 0) + call expect_not_message_type('<PJ4/K1ABC>X W9XYZ RR73', 1, 0) + call expect_not_message_type('W9XYZ <PJ4/K1ABC>X RR73', 1, 0) + call expect_not_message_type('K1ABC <W7ABC> RR73', 4, 0) + call expect_not_message_type('<W7ABC> K1ABC RR73', 4, 0) + call expect_not_message_type('<W7ABC> <PJ4/K1ABC> RR73', 4, 0) + call expect_pack77_failure('CQ <PJ4/K1ABC>') + + call expect_round_trip_with_hashes('<W3CCX> <K1JT/P> 590001 FN20QI', & + '<W3CCX> <K1JT/P> 590001 FN20QI', 5, 0, 0, 'W3CCX', 'K1JT/P', '') + call expect_round_trip_with_hashes('<W3CCX> <K1JT/P> 520001 FN20QI', & + '<W3CCX> <K1JT/P> 520001 FN20QI', 5, 0, 0, 'W3CCX', 'K1JT/P', '') + call expect_round_trip_with_hashes('<W3CCX> <K1JT/P> R 520001 FN20QI', & + '<W3CCX> <K1JT/P> R 520001 FN20QI', 5, 0, 0, 'W3CCX', 'K1JT/P', '') + call expect_round_trip_with_hashes('<W3CCX> <K1JT/P> R 592047 FN20QI', & + '<W3CCX> <K1JT/P> R 592047 FN20QI', 5, 0, 0, 'W3CCX', 'K1JT/P', '') + call expect_round_trip('<W3CCX> <K1JT/P> 590001 FN20QI', & + '<...> <...> 590001 FN20QI', 5, 0, 0) + call expect_not_message_type('<W3CCX> <K1JT/P> 520000 FN20QI', 5, 0) + call expect_not_message_type('<W3CCX> <K1JT/P> 530000 FN20QI', 5, 0) + call expect_not_message_type('<W3CCX> <K1JT/P> 592048 FN20QI', 5, 0) + call expect_not_message_type('<W3CCX> <K1JT/P> 582048 FN20QI', 5, 0) + call expect_not_message_type('<W3CCX> <K1JT/P> 594095 FN20QI', 5, 0) + call expect_not_message_type('<W3CCX> <K1JT/P> 590001 FN20QY', 5, 0) + call expect_not_message_type('<W3CCX> <K1JT/P> 590001 FN20Q', 5, 0) + call expect_not_message_type('<W3CCX> <K1JT/P> X 590001 FN20QI', 5, 0) + call expect_not_message_type('<W3CCX> <K1JT/P> RR 590001 FN20QI', 5, 0) + call expect_not_message_type('W3CCX <K1JT/P> 590001 FN20QI', 5, 0) + call expect_not_message_type('<W3CCX <K1JT/P> 590001 FN20QI', 5, 0) + call expect_not_message_type('<W3CCX> <K1JT/P 590001 FN20QI', 5, 0) + call expect_not_message_type('<ABCDEFGHIJK>X <K1JT/P> 590001 FN20QI', 5, 0) + call expect_not_message_type('<W3CCX> <K1JT/P> 0590001 FN20QI', 5, 0) + + call expect_round_trip_with_context('K1ABC RR73; W9XYZ <KH1/KH7Z> -12', & + 'K1ABC RR73; W9XYZ <KH1/KH7Z> -12', 0, 1, 1, & + 'N0AAA', 'KH1/KH7Z', '', '', '') + call expect_round_trip_with_context('K1ABC RR73; W9XYZ <KH1/KH7Z> -30', & + 'K1ABC RR73; W9XYZ <KH1/KH7Z> -30', 0, 1, 1, & + 'N0AAA', 'KH1/KH7Z', '', '', '') + call expect_round_trip_with_context('K1ABC RR73; W9XYZ <KH1/KH7Z> +00', & + 'K1ABC RR73; W9XYZ <KH1/KH7Z> +00', 0, 1, 1, & + 'N0AAA', 'KH1/KH7Z', '', '', '') + call expect_round_trip_with_context('K1ABC RR73; W9XYZ <KH1/KH7Z> +32', & + 'K1ABC RR73; W9XYZ <KH1/KH7Z> +32', 0, 1, 1, & + 'N0AAA', 'KH1/KH7Z', '', '', '') + call expect_pack77_failure('K1ABC RR73; W9XYZ <KH1/KH7Z> -11') + call expect_pack77_failure('K1ABC RR73; W9XYZ <KH1/KH7Z> -31') + call expect_pack77_failure('K1ABC RR73; W9XYZ <KH1/KH7Z> +33') + call expect_pack77_failure('K1ABC RR73; W9XYZ <KH1/KH7Z> +34') + call expect_round_trip_with_context('K1ABC RR73; W9XYZ <KH1/KH7Z> -12', & + 'K1ABC RR73; W9XYZ <KH1/KH7Z> -12', 0, 1, 0, & + 'KH1/KH7Z', 'N0BBB', '', '', '') + call expect_not_message_type('K1ABC RR73: W9XYZ <KH1/KH7Z> -12', 0, 1) + call expect_not_message_type('K1ABC RRR; W9XYZ <KH1/KH7Z> -12', 0, 1) + call expect_round_trip_with_context('PJ2/W1AW RR73; W9XYZ <KH1/KH7Z> -12', & + '<...> RR73; W9XYZ <KH1/KH7Z> -12', 0, 1, 1, & + 'N0AAA', 'KH1/KH7Z', '', '', '') + call expect_round_trip_with_context('K1ABC RR73; PJ2/W1AW <KH1/KH7Z> -12', & + 'K1ABC RR73; <...> <KH1/KH7Z> -12', 0, 1, 1, & + 'N0AAA', 'KH1/KH7Z', '', '', '') + call expect_round_trip_with_hashes('PJ2/W1AW RR73; W9XYZ <KH1/KH7Z> -12', & + '<PJ2/W1AW> RR73; W9XYZ <KH1/KH7Z> -12', 0, 1, 0, & + 'PJ2/W1AW', 'KH1/KH7Z', '') + call expect_pack77_failure('PJ2/W1AW RR73; W9/YZ <KH1/KH7Z> -12') + call expect_not_message_type('<K1ABC> RR73; W9XYZ <KH1/KH7Z> -12', 0, 1) + call expect_not_message_type('K1ABC RR73; W9XYZ KH1/KH7Z -12', 0, 1) + call expect_not_message_type('K1ABC RR73; W9XYZ <KH1/KH7Z>X -12', 0, 1) + call expect_not_message_type('K1ABC RR73; W9XYZ <KH1/KH7Z> 3', 0, 1) + call expect_not_message_type('K1ABC RR73; W9XYZ <KH1/KH7Z> -12 JUNK', 0, 1) + + call expect_round_trip(' k1abc w9xyz fn42', & + 'K1ABC W9XYZ FN42', 1, 0, 0) + call expect_round_trip('K1ABC/R W9XYZ/R R FN42', & + 'K1ABC/R W9XYZ/R R FN42', 1, 0, 0) + call expect_round_trip('CQ PJ2/W1AW', & + 'CQ PJ2/W1AW', 4, 0, 0) + call expect_round_trip('CQ TEST/W1AW', & + 'CQ TEST/W1AW', 4, 0, 0) + call expect_round_trip('QRZ PJ4/K1ABC', & + 'QRZ PJ4/K1ABC', 0, 0, 0) + call expect_pack77_failure('CQ K1ABC R FN42') + call expect_not_message_type('CQ K1A W1AW FN20', 1, 0) + call expect_not_message_type('CQ TJ. K1ABC FN20', 1, 0) + call expect_not_message_type('<PJ4/K1ABC> <W7ABC> TEST', 5, 0) + call expect_not_message_type('<A/B> <C/D>', 1, 0) + call expect_not_message_type('<A/B> <C/D>', 5, 0) + + call expect_dual_angle_type1_placeholders('<W1AW> <K1JT>', '<...> <...>') + call expect_dual_angle_type1_placeholders('<W1AW> <K1JT> RR73', & + '<...> <...> RR73') + + contains + + subroutine expect_round_trip(input,expected,want_i3,want_n3,nrx) + character(len=*), intent(in) :: input, expected + integer, intent(in) :: want_i3, want_n3, nrx + + call expect_round_trip_with_context(input,expected,want_i3,want_n3,nrx, & + 'N0AAA', 'N0BBB', '', '', '') + end subroutine expect_round_trip + + subroutine expect_round_trip_with_hashes(input,expected,want_i3,want_n3, & + nrx,hash_call_1,hash_call_2,hash_call_3) + character(len=*), intent(in) :: input, expected + integer, intent(in) :: want_i3, want_n3, nrx + character(len=*), intent(in) :: hash_call_1, hash_call_2, hash_call_3 + + call expect_round_trip_with_context(input,expected,want_i3,want_n3,nrx, & + 'N0AAA', 'N0BBB', hash_call_1, hash_call_2, hash_call_3) + end subroutine expect_round_trip_with_hashes + + subroutine expect_round_trip_with_context(input,expected,want_i3,want_n3, & + nrx,mycall,dxcall,hash_call_1,hash_call_2,hash_call_3) + character(len=*), intent(in) :: input, expected + integer, intent(in) :: want_i3, want_n3, nrx + character(len=*), intent(in) :: mycall, dxcall + character(len=*), intent(in) :: hash_call_1, hash_call_2, hash_call_3 + + call expect_round_trip_with_pack_state(input,expected,want_i3,want_n3, & + -1,-1,nrx,mycall,dxcall,hash_call_1,hash_call_2,hash_call_3) + end subroutine expect_round_trip_with_context + + subroutine expect_round_trip_with_hint(input,expected,want_i3,want_n3, & + pack_i3,pack_n3,nrx,hash_call_1,hash_call_2,hash_call_3) + character(len=*), intent(in) :: input, expected + integer, intent(in) :: want_i3, want_n3, pack_i3, pack_n3, nrx + character(len=*), intent(in) :: hash_call_1, hash_call_2, hash_call_3 + + call expect_round_trip_with_pack_state(input,expected,want_i3,want_n3, & + pack_i3,pack_n3,nrx,'N0AAA','N0BBB',hash_call_1,hash_call_2, & + hash_call_3) + end subroutine expect_round_trip_with_hint + + subroutine expect_round_trip_with_pack_state(input,expected,want_i3,want_n3, & + pack_i3,pack_n3,nrx,mycall,dxcall,hash_call_1,hash_call_2,hash_call_3) + character(len=*), intent(in) :: input, expected + integer, intent(in) :: want_i3, want_n3, pack_i3, pack_n3, nrx + character(len=*), intent(in) :: mycall, dxcall + character(len=*), intent(in) :: hash_call_1, hash_call_2, hash_call_3 + character(len=77) :: c77 + character(len=37) :: canonical, canonical_decoded, decoded, packed_input + integer :: got_i3, got_n3 + logical :: canonical_ok, ok + + packed_input=' ' + packed_input=input + call reset_packjt77_mode_state('N0AAA', 'N0BBB') + + got_i3=pack_i3 + got_n3=pack_n3 + c77='' + block + type(pack77_result) :: encoded + encoded=pack77_with_hint(packed_input,pack_i3,pack_n3) + call assert_pack77_result('pack77',input,encoded) + call assert_true('pack77 gate not rejected',encoded%status.ne.PACK77_STATUS_INTERNAL_ROUNDTRIP_REJECTED) + got_i3=encoded%i3 + got_n3=encoded%n3 + c77=encoded%c77 + end block + + call assert_message_type('pack77',input,want_i3,want_n3,got_i3,got_n3) + canonical_decoded=' ' + canonical_ok=.false. + call unpack77(c77,0,canonical_decoded,canonical_ok) + call assert_decode('pack77 canonical',input,canonical_decoded, & + canonical_decoded,canonical_ok) + canonical=canonical_decoded + call assert_canonical_text('pack77',input,canonical,canonical_decoded) + + call reset_packjt77_mode_state(mycall, dxcall) + call prime_hash_call_mode(hash_call_1) + call prime_hash_call_mode(hash_call_2) + call prime_hash_call_mode(hash_call_3) + + decoded=' ' + ok=.false. + call unpack77_mode(c77,nrx,decoded,ok) + if(.not.ok) then + write(*,1010) trim(input) +1010 format('Unpack failure for "',a,'"') + error stop 1 + endif + if(trim(decoded).ne.expected) then + write(*,1020) trim(input), trim(expected), trim(decoded) +1020 format('Round-trip failure for "',a,'"; expected "',a,'"; got "',a,'"') + error stop 1 + endif + + ntests=ntests+1 + end subroutine expect_round_trip_with_pack_state + + subroutine expect_not_message_type(input,blocked_i3,blocked_n3) + character(len=*), intent(in) :: input + integer, intent(in) :: blocked_i3, blocked_n3 + character(len=37) :: packed_input + integer :: got_i3, got_n3 + + packed_input=' ' + packed_input=input + call reset_packjt77_mode_state('N0AAA', 'N0BBB') + + got_i3=-1 + got_n3=-1 + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(packed_input) + if(.not.encoded%encoded) then + ntests=ntests+1 + return + endif + got_i3=encoded%i3 + got_n3=encoded%n3 + end block + if(got_i3.eq.blocked_i3 .and. got_n3.eq.blocked_n3) then + write(*,1060) trim(input), blocked_i3, blocked_n3 +1060 format('Message "',a,'" packed as blocked type ',i0,'.',i0) + error stop 1 + endif + + ntests=ntests+1 + end subroutine expect_not_message_type + + subroutine expect_raw_type3_unpack_failure(nexch) + integer, intent(in) :: nexch + character(len=77) :: c77 + character(len=37) :: decoded + logical :: ok, unpack_ok + + call encode_pack77_type3(0,12751800,10214965,1,5,nexch,c77,ok) + call assert_true('raw Type 3 payload encoded',ok) + + call reset_packjt77_mode_state('N0AAA', 'N0BBB') + decoded=' ' + unpack_ok=.true. + call unpack77_mode(c77,0,decoded,unpack_ok) + if(unpack_ok) then + write(*,1080) nexch, trim(decoded) +1080 format('Raw Type 3 exchange ',i0,' decoded successfully as "',a,'"') + error stop 1 + endif + + ntests=ntests+1 + end subroutine expect_raw_type3_unpack_failure + + subroutine expect_raw_type3_unpack_success(nexch,expected) + integer, intent(in) :: nexch + character(len=*), intent(in) :: expected + character(len=77) :: c77 + character(len=37) :: decoded + logical :: ok, unpack_ok + + call encode_pack77_type3(0,12751800,10214965,1,5,nexch,c77,ok) + call assert_true('raw Type 3 payload encoded',ok) + + call reset_packjt77_mode_state('N0AAA', 'N0BBB') + decoded=' ' + unpack_ok=.false. + call unpack77_mode(c77,0,decoded,unpack_ok) + call assert_decode('raw Type 3 exchange boundary',expected,expected, & + decoded,unpack_ok) + + ntests=ntests+1 + end subroutine expect_raw_type3_unpack_success + + subroutine expect_nonbinary_unpack_failure() + character(len=77) :: c77 + character(len=37) :: decoded + logical :: ok + + c77=repeat('0',77) + c77(10:10)='x' + c77(75:77)='001' + + call reset_packjt77_mode_state('N0AAA', 'N0BBB') + decoded=' ' + ok=.true. + call unpack77_mode(c77,0,decoded,ok) + if(ok) then + write(*,1082) trim(decoded) +1082 format('Non-binary payload decoded successfully as "',a,'"') + error stop 1 + endif + + ntests=ntests+1 + end subroutine expect_nonbinary_unpack_failure + + subroutine expect_pack77_failure(input) + character(len=*), intent(in) :: input + character(len=37) :: packed_input + + packed_input=' ' + packed_input=input + call reset_packjt77_mode_state('N0AAA', 'N0BBB') + + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(packed_input) + if(encoded%encoded) then + write(*,1095) trim(input), encoded%i3, encoded%n3 +1095 format('Message "',a,'" unexpectedly packed as ',i0,'.',i0) + error stop 1 + endif + end block + + ntests=ntests+1 + end subroutine expect_pack77_failure + + subroutine expect_free_text_rejection(input,want_status) + character(len=*), intent(in) :: input + integer, intent(in) :: want_status + character(len=37) :: packed_input + + packed_input=' ' + packed_input=input + call reset_packjt77_mode_state('N0AAA', 'N0BBB') + + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(packed_input) + if(encoded%encoded) then + write(*,1098) trim(input), encoded%i3, encoded%n3 +1098 format('Free text "',a,'" unexpectedly packed as ',i0, & + '.',i0) + error stop 1 + endif + call assert_int('free text reject status '//trim(input), & + want_status,encoded%status) + end block + + ntests=ntests+1 + end subroutine expect_free_text_rejection + + subroutine expect_strict_telemetry_rejects(input) + character(len=*), intent(in) :: input + character(len=37) :: packed_input + + packed_input=' ' + packed_input=input + call reset_packjt77_mode_state('N0AAA', 'N0BBB') + + block + type(pack77_result) :: encoded + encoded=strict_telemetry_result_from_schema(packed_input) + if(encoded%encoded) then + write(*,1097) trim(input), encoded%i3, encoded%n3 +1097 format('Strict telemetry message "',a,'" unexpectedly packed as ', & + i0,'.',i0) + error stop 1 + endif + end block + + ntests=ntests+1 + end subroutine expect_strict_telemetry_rejects + + subroutine expect_prefer_wspr_rejects_invalid_type3(input) + character(len=*), intent(in) :: input + + call expect_prefer_wspr_rejects_invalid_type3_impl(input) + end subroutine expect_prefer_wspr_rejects_invalid_type3 + + subroutine expect_prefer_wspr_rejects_invalid_type3_impl(input) + character(len=*), intent(in) :: input + character(len=37) :: packed_input + character(len=24) :: prefix + + prefix='Preferred WSPR' + packed_input=' ' + packed_input=input + call reset_packjt77_mode_state('N0AAA', 'N0BBB') + + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(packed_input, & + pack77_options(prefer_wspr_50bit=.true.)) + if(encoded%encoded) then + write(*,1096) trim(prefix), trim(input), encoded%i3, encoded%n3 +1096 format(a,' message "',a,'" unexpectedly packed as ',i0,'.',i0) + error stop 1 + endif + call assert_true('failed preferred WSPR status', & + encoded%status.eq.PACK77_STATUS_PREFERRED_FAMILY_REJECTED) + end block + + ntests=ntests+1 + end subroutine expect_prefer_wspr_rejects_invalid_type3_impl + + subroutine expect_type_unpack_failure_with_hashes(input,want_i3,want_n3, & + nrx,hash_call_1,hash_call_2,hash_call_3) + character(len=*), intent(in) :: input + integer, intent(in) :: want_i3, want_n3, nrx + character(len=*), intent(in) :: hash_call_1, hash_call_2, hash_call_3 + character(len=77) :: c77 + character(len=37) :: decoded, packed_input + integer :: got_i3, got_n3 + logical :: ok + + packed_input=' ' + packed_input=input + call reset_packjt77_mode_state('N0AAA', 'N0BBB') + + c77='' + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(packed_input) + call assert_pack77_result('pack77',packed_input,encoded) + got_i3=encoded%i3 + got_n3=encoded%n3 + c77=encoded%c77 + end block + call assert_message_type('pack77',input,want_i3,want_n3,got_i3,got_n3) + + call reset_packjt77_mode_state('N0AAA', 'N0BBB') + call prime_hash_call_mode(hash_call_1) + call prime_hash_call_mode(hash_call_2) + call prime_hash_call_mode(hash_call_3) + + decoded=' ' + ok=.true. + call unpack77_mode(c77,nrx,decoded,ok) + if(ok) then + write(*,1100) trim(input), trim(decoded) +1100 format('Message "',a,'" decoded successfully as "',a,'"') + error stop 1 + endif + + ntests=ntests+1 + end subroutine expect_type_unpack_failure_with_hashes + + subroutine expect_free_text_fallback(input,expected) + character(len=*), intent(in) :: input, expected + + call expect_free_text_fallback_impl(input,expected) + end subroutine expect_free_text_fallback + + subroutine expect_free_text_fallback_impl(input,expected) + character(len=*), intent(in) :: input, expected + character(len=77) :: c77 + character(len=37) :: canonical, decoded, packed_input + character(len=40) :: pack_label + character(len=16) :: prefix + integer :: got_i3, got_n3 + logical :: ok + + pack_label='pack77' + prefix='Free-text' + packed_input=' ' + packed_input=input + call reset_packjt77_mode_state('N0AAA', 'N0BBB') + + got_i3=-1 + got_n3=-1 + c77='' + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(packed_input) + call assert_pack77_result(trim(pack_label),packed_input,encoded) + got_i3=encoded%i3 + got_n3=encoded%n3 + c77=encoded%c77 + end block + call assert_message_type(trim(pack_label),input,0,0,got_i3,got_n3) + + decoded=' ' + ok=.false. + call unpack77_mode(c77,0,decoded,ok) + if(.not.ok) then + write(*,1070) trim(prefix), trim(input) +1070 format(a,' unpack failure for "',a,'"') + error stop 1 + endif + if(trim(decoded).ne.expected) then + write(*,1085) trim(prefix), trim(input), trim(expected), trim(decoded) +1085 format(a,' failure for "',a,'"; expected "',a,'"; got "',a,'"') + error stop 1 + endif + canonical=decoded + call assert_canonical_text('pack77 free text',input,canonical,decoded) + + ntests=ntests+1 + end subroutine expect_free_text_fallback_impl + + subroutine expect_legacy_truncating_fallback(input,expected) + character(len=*), intent(in) :: input, expected + character(len=37) :: decoded, packed_input + logical :: ok + + packed_input=' ' + packed_input=input + call reset_packjt77_mode_state('N0AAA', 'N0BBB') + + block + type(pack77_result) :: strict_encoded, legacy_encoded + strict_encoded=pack77_result_from_api(packed_input) + if(strict_encoded%encoded) then + write(*,1105) trim(input), strict_encoded%i3, strict_encoded%n3 +1105 format('Strict message "',a,'" unexpectedly packed as ',i0,'.',i0) + error stop 1 + endif + + legacy_encoded=pack77_legacy_result_from_api(packed_input) + call assert_pack77_result('legacy truncating fallback', & + packed_input,legacy_encoded) + call assert_message_type('legacy truncating fallback',input, & + 0,0,legacy_encoded%i3,legacy_encoded%n3) + + decoded=' ' + ok=.false. + call unpack77_mode(legacy_encoded%c77,0,decoded,ok) + call assert_decode('legacy truncating fallback',input,expected, & + decoded,ok) + end block + + ntests=ntests+1 + end subroutine expect_legacy_truncating_fallback + + subroutine expect_legacy_matches_strict(input) + character(len=*), intent(in) :: input + character(len=37) :: packed_input + + packed_input=' ' + packed_input=input + call reset_packjt77_mode_state('N0AAA', 'N0BBB') + + block + type(pack77_result) :: strict_encoded, legacy_encoded + strict_encoded=pack77_result_from_api(packed_input) + legacy_encoded=pack77_legacy_result_from_api(packed_input) + call assert_pack77_result('strict pack77',packed_input,strict_encoded) + call assert_pack77_result('legacy wrapper',packed_input,legacy_encoded) + call assert_int('legacy matches strict i3',strict_encoded%i3, & + legacy_encoded%i3) + call assert_int('legacy matches strict n3',strict_encoded%n3, & + legacy_encoded%n3) + call assert_text_equal('legacy matches strict c77', & + strict_encoded%c77,legacy_encoded%c77) + end block + + ntests=ntests+1 + end subroutine expect_legacy_matches_strict + + subroutine expect_legacy_fallback_failure(input,want_status) + character(len=*), intent(in) :: input + integer, intent(in) :: want_status + character(len=37) :: packed_input + + packed_input=' ' + packed_input=input + call reset_packjt77_mode_state('N0AAA', 'N0BBB') + + block + type(pack77_result) :: encoded + encoded=pack77_legacy_result_from_api(packed_input) + if(encoded%encoded) then + write(*,1110) trim(input), encoded%i3, encoded%n3 +1110 format('Legacy fallback message "',a,'" unexpectedly packed as ', & + i0,'.',i0) + error stop 1 + endif + call assert_int('legacy fallback reject status '//trim(input), & + want_status,encoded%status) + end block + + ntests=ntests+1 + end subroutine expect_legacy_fallback_failure + + subroutine expect_legacy_prefer_wspr_rejects(input) + character(len=*), intent(in) :: input + character(len=37) :: packed_input + + packed_input=' ' + packed_input=input + call reset_packjt77_mode_state('N0AAA', 'N0BBB') + + block + type(pack77_result) :: encoded + encoded=pack77_legacy_result_from_api(packed_input, & + pack77_options(prefer_wspr_50bit=.true.)) + if(encoded%encoded) then + write(*,1115) trim(input), encoded%i3, encoded%n3 +1115 format('Legacy WSPR message "',a,'" unexpectedly packed as ', & + i0,'.',i0) + error stop 1 + endif + call assert_int('legacy preferred WSPR status', & + PACK77_STATUS_PREFERRED_FAMILY_REJECTED,encoded%status) + end block + + ntests=ntests+1 + end subroutine expect_legacy_prefer_wspr_rejects + + subroutine expect_pack77_structured(input,want_i3,want_n3) + character(len=*), intent(in) :: input + integer, intent(in) :: want_i3, want_n3 + character(len=37) :: packed_input + + packed_input=' ' + packed_input=input + call reset_packjt77_mode_state('N0AAA', 'N0BBB') + + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(packed_input) + call assert_pack77_result('pack77 structured', & + packed_input,encoded) + call assert_message_type('pack77 structured',input, & + want_i3,want_n3,encoded%i3,encoded%n3) + end block + + ntests=ntests+1 + end subroutine expect_pack77_structured + + subroutine expect_dual_angle_type1_placeholders(input,expected) + character(len=*), intent(in) :: input, expected + character(len=77) :: c77 + character(len=37) :: canonical, canonical_decoded, decoded, packed_input + integer :: got_i3, got_n3 + logical :: canonical_ok, ok + + packed_input=' ' + packed_input=input + call reset_packjt77_mode_state('N0AAA', 'N0BBB') + + c77='' + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(packed_input) + call assert_pack77_result('pack77',packed_input,encoded) + got_i3=encoded%i3 + got_n3=encoded%n3 + c77=encoded%c77 + end block + call assert_message_type('pack77',input,1,0,got_i3,got_n3) + canonical_decoded=' ' + canonical_ok=.false. + call unpack77(c77,0,canonical_decoded,canonical_ok) + call assert_decode('dual-angle Type 1 canonical',input,canonical_decoded, & + canonical_decoded,canonical_ok) + canonical=canonical_decoded + call assert_canonical_text('dual-angle Type 1',input,canonical, & + canonical_decoded) + + call reset_packjt77_mode_state('N0AAA', 'N0BBB') + decoded=' ' + ok=.false. + call unpack77_mode(c77,0,decoded,ok) + if(.not.ok) then + write(*,1040) trim(input) +1040 format('Dual-angle Type 1 unpack failure for "',a,'"') + error stop 1 + endif + if(trim(decoded).ne.expected) then + write(*,1050) trim(input), trim(expected), trim(decoded) +1050 format('Dual-angle Type 1 placeholder failure for "',a, & + '"; expected "',a,'"; got "',a,'"') + error stop 1 + endif + + ntests=ntests+1 + end subroutine expect_dual_angle_type1_placeholders + + subroutine reset_packjt77_mode_state(mycall,dxcall) + character(len=*), intent(in) :: mycall, dxcall + + if(configured_var) then + call reset_packjt77var_state(mycall,dxcall) + else + call reset_packjt77_state(mycall,dxcall) + endif + end subroutine reset_packjt77_mode_state + + subroutine prime_hash_call_mode(callsign) + character(len=*), intent(in) :: callsign + + if(configured_var) then + call prime_hash_call_var(callsign) + else + call prime_hash_call(callsign) + endif + end subroutine prime_hash_call_mode + + subroutine unpack77_mode(c77,nrx,decoded,ok) + character(len=77), intent(in) :: c77 + integer, intent(in) :: nrx + character(len=37), intent(out) :: decoded + logical, intent(out) :: ok + + if(configured_var) then + call unpack77_configured(c77,nrx,decoded,ok,unpack77_options(thread_index=1)) + else + call unpack77(c77,nrx,decoded,ok) + endif + end subroutine unpack77_mode + + end subroutine run_packjt77_common_invariants + + subroutine run_packjt77_standard_schema_invariants(ntests) + integer, intent(inout) :: ntests + + call expect_type12_qso_tail_mapping() + + contains + + subroutine expect_type12_qso_tail_mapping() + call expect_type12_qso_tail_irpt('Type 1 no tail', & + 1,32400+1,'K1ABC W9XYZ') + call expect_type12_qso_tail_irpt('Type 1 RRR', & + 1,32400+2,'K1ABC W9XYZ RRR') + call expect_type12_qso_tail_irpt('Type 1 RR73', & + 1,32400+3,'K1ABC W9XYZ RR73') + call expect_type12_qso_tail_irpt('Type 1 73', & + 1,32400+4,'K1ABC W9XYZ 73') + + call expect_type12_qso_tail_irpt('Type 2 no tail', & + 2,32400+1,'K1ABC/P W9XYZ/P') + call expect_type12_qso_tail_irpt('Type 2 RRR', & + 2,32400+2,'K1ABC/P W9XYZ/P RRR') + call expect_type12_qso_tail_irpt('Type 2 RR73', & + 2,32400+3,'K1ABC/P W9XYZ/P RR73') + call expect_type12_qso_tail_irpt('Type 2 73', & + 2,32400+4,'K1ABC/P W9XYZ/P 73') + + ntests=ntests+1 + end subroutine expect_type12_qso_tail_mapping + + subroutine expect_type12_qso_tail_irpt(label,want_i3,want_igrid4,expected) + character(len=*), intent(in) :: label, expected + integer, intent(in) :: want_i3, want_igrid4 + character(len=37) :: decoded + character(len=77) :: c77 + type(pack77_type12_fields) :: fields + logical :: ok, unpack_ok + + ok=.false. + if(want_i3.eq.1) then + call encode_pack77_type1(10214965,0,12751800,0,0,want_igrid4,c77,ok) + else + call encode_pack77_type2(10214965,1,12751800,1,0,want_igrid4,c77,ok) + endif + call assert_true(trim(label)//' Type 1/2 schema encode',ok) + + fields=pack77_type12_fields() + if(want_i3.eq.1) then + call decode_pack77_type1(c77,fields,ok) + else + call decode_pack77_type2(c77,fields,ok) + endif + call assert_true(trim(label)//' Type 1/2 schema decode',ok) + call assert_int(trim(label)//' igrid4',want_igrid4,fields%igrid4) + + call reset_packjt77_state('N0AAA', 'N0BBB') + decoded=' ' + unpack_ok=.false. + call unpack77(c77,0,decoded,unpack_ok) + call assert_true(trim(label)//' unpack',unpack_ok) + call assert_text_equal(trim(label)//' decoded tail',expected,decoded) + end subroutine expect_type12_qso_tail_irpt + + end subroutine run_packjt77_standard_schema_invariants + + subroutine reset_packjt77_state(mycall,dxcall) + character(len=*), intent(in) :: mycall, dxcall + + call clear_standard_state(mycall,dxcall) + end subroutine reset_packjt77_state + + subroutine reset_packjt77var_state(mycall,dxcall) + character(len=*), intent(in) :: mycall, dxcall + integer :: n10, n12, n22 + + call clear_all_state(mycall,dxcall) + if(len_trim(mycall).gt.2) then + call save_hash_call(mycall13,hashmy10_configured,hashmy12_configured, & + hashmy22_configured) + endif + if(len_trim(dxcall).gt.2) then + call prime_hash_call_var(dxcall) + call save_hash_call(dxcall13,n10,n12,n22) + endif + end subroutine reset_packjt77var_state + + subroutine prime_hash_call(callsign) + character(len=*), intent(in) :: callsign + integer :: n10, n12, n22 + character(len=13) :: c13 + + if(len_trim(callsign).le.0) return + c13=' ' + c13=callsign + call save_hash_call(c13,n10,n12,n22) + end subroutine prime_hash_call + + subroutine prime_hash_call_var(callsign) + character(len=*), intent(in) :: callsign + integer :: n10, n12, n22 + character(len=13) :: c13 + + if(len_trim(callsign).le.0) return + call normalize_call(callsign,c13) + if(len_trim(c13).le.0) return + call save_hash_call(c13,n10,n12,n22) + call store_rx_hash_var(c13) + end subroutine prime_hash_call_var + + subroutine store_rx_hash_var(c13) + character(len=*), intent(in) :: c13 + integer :: n10, n12, n22, i + + n10=ihashcall(c13,10) + if(n10.ge.0 .and. n10.le.1023) calls10(n10)=c13 + n12=ihashcall(c13,12) + if(n12.ge.0 .and. n12.le.4095) calls12(n12)=c13 + n22=ihashcall(c13,22) + do i=1,nzhash + if(ihash22(i).eq.n22) then + calls22(i)=c13 + return + endif + enddo + if(nzhash.lt.MAXHASH) then + if(nzhash.ge.1) then + ihash22(nzhash+1:2:-1)=ihash22(nzhash:1:-1) + calls22(nzhash+1:2:-1)=calls22(nzhash:1:-1) + endif + nzhash=nzhash+1 + ihash22(1)=n22 + calls22(1)=c13 + endif + end subroutine store_rx_hash_var + +end module packjt77_test_helpers diff --git a/tests/unit/packjt77/silent_rewrite_corpus.txt b/tests/unit/packjt77/silent_rewrite_corpus.txt new file mode 100644 index 00000000..ed668d6d --- /dev/null +++ b/tests/unit/packjt77/silent_rewrite_corpus.txt @@ -0,0 +1,54 @@ +W1AW/P K1ABC 5A CT | compound-call-regression +K1ABC/R W9XYZ/P FN42 | compound-call-regression +CQ PJ4/K1ABC FK52 | compound-call-regression +VK9X/W1ABC 37 | compound-call-regression +K1ABC FN42 55 | compound-call-regression +CQ 3DA0RS/W1ABC | compound-call-regression +CQ PJ4/K1ABC | compound-call-regression +CQ <PJ4/K1ABC> | compound-call-regression +PJ2/W1AW <W7ABC> RR73 | silent-rewrite-regression +<PJ4/K1ABC> W9XYZ RR73 | silent-rewrite-regression +K1ABC RR73; W9XYZ <KH1/KH7Z> -12 | silent-rewrite-regression +<W3CCX> <K1JT/P> 590001 FN20QI | silent-rewrite-regression +PJ2/W1AW KA1ABC R 1A EMA | silent-rewrite-regression +Y8W MW8WA/A R 17A ONE | silent-rewrite-regression +W9XYZ W1AW/P R 599 MA | silent-rewrite-regression +K1ABC FN42 37 | silent-rewrite-regression +PJ2/K1ABC 37 | silent-rewrite-regression +K1ABC/P 37 | silent-rewrite-regression +<PJ4/K1ABC>X W9XYZ RR73 | malformed-bracket +W9XYZ <PJ4/K1ABC>X RR73 | malformed-bracket +<PJ4/K1ABC> FK52A! | malformed-bracket +<PJ4/K1ABC>X FK52AB | malformed-bracket +<W3CCX <K1JT/P> 590001 FN20QI | malformed-bracket +<W3CCX> <K1JT/P 590001 FN20QI | malformed-bracket +<ABCDEFGHIJK>X <K1JT/P> 590001 FN20QI | malformed-bracket +K1ABC RR73; W9XYZ <KH1/KH7Z>X -12 | malformed-bracket +K1ABC/ K1A 1A CT | grammar-edge +/K1ABC K1A 1A CT | grammar-edge +A//B K1A 1A CT | grammar-edge +K1ABC//P K1A 1A CT | grammar-edge +K1ABC <W7ABC> RR73 | grammar-edge +<W7ABC> K1ABC RR73 | grammar-edge +<W7ABC> <PJ4/K1ABC> RR73 | grammar-edge +<A/B> <C/D> | grammar-edge +K1ABC RR73; PJ2/W1AW <KH1/KH7Z> -12 | grammar-edge +PJ2/W1AW RR73; W9XYZ <KH1/KH7Z> -12 | grammar-edge +K1ABC RR73: W9XYZ <KH1/KH7Z> -12 | grammar-edge +K1ABC RRR; W9XYZ <KH1/KH7Z> -12 | grammar-edge +WA9XYZ KA1ABC R 1H EMA | grammar-edge +WA9XYZ KA1ABC R 0A EMA | grammar-edge +WA9XYZ KA1ABC 01A EMA | grammar-edge +W9XYZ K1ABC 579 MAX | grammar-edge +W9XYZ G8ABC R 559 8000 | grammar-edge +K1ABC FN42 03 | grammar-edge +A$/K1ABC 37 | grammar-edge +K1ABC/ABCD 37 | grammar-edge +Q5A FN20 37 | grammar-edge +CQ K1ABC R FN42 | grammar-edge +CQ N0CALL FN31 | grammar-edge +K1ABC PJ4/K9XYZ | grammar-edge +QRZ PJ4/K1ABC | grammar-edge +K1ABC RR73; W9XYZ <KH1/KH7Z> -12 JUNK | overlong-field +<W3CCX> <K1JT/P> 590001 FN20QI EXTRA | overlong-field +PJ2/W1AW RR73; W9/YZ <KH1/KH7Z> -12 | overlong-field diff --git a/tests/unit/packjt77/test_encode77.cmake b/tests/unit/packjt77/test_encode77.cmake new file mode 100644 index 00000000..f614241a --- /dev/null +++ b/tests/unit/packjt77/test_encode77.cmake @@ -0,0 +1,39 @@ +execute_process( + COMMAND "${ENCODE77}" "CQ K1ABC FN42" + RESULT_VARIABLE valid_result + OUTPUT_VARIABLE valid_output + ERROR_VARIABLE valid_error +) +if (NOT valid_result EQUAL 0) + message(FATAL_ERROR "Valid message failed (${valid_result}):\n${valid_output}${valid_error}") +endif () +if (NOT valid_output MATCHES "1[ ]+OK[ ]+CQ K1ABC FN42[ ]+CQ K1ABC FN42") + message(FATAL_ERROR "Valid message output was unexpected:\n${valid_output}") +endif () + +execute_process( + COMMAND "${ENCODE77}" "THIS FREE TEXT IS TOO LONG" + RESULT_VARIABLE rejected_result + OUTPUT_VARIABLE rejected_output + ERROR_VARIABLE rejected_error +) +if (rejected_result EQUAL 0) + message(FATAL_ERROR "Rejected message returned success:\n${rejected_output}") +endif () +if (NOT rejected_output MATCHES "REJECTED[ ]+THIS FREE TEXT IS TOO LONG[ ]+free text too long") + message(FATAL_ERROR "Rejected message output was unexpected:\n${rejected_output}${rejected_error}") +endif () + +execute_process( + COMMAND "${ENCODE77}" -f "${INPUT_FILE}" + WORKING_DIRECTORY "${INPUT_DIR}" + RESULT_VARIABLE batch_result + OUTPUT_VARIABLE batch_output + ERROR_VARIABLE batch_error +) +if (batch_result EQUAL 0) + message(FATAL_ERROR "Batch with a rejection returned success:\n${batch_output}") +endif () +if (NOT batch_output MATCHES "Summary: 2 encoded, 1 rejected, 0 unpack failures, 0 differences") + message(FATAL_ERROR "Batch summary was unexpected:\n${batch_output}${batch_error}") +endif () diff --git a/tests/unit/packjt77/test_pack77_cli_fallbacks.cmake b/tests/unit/packjt77/test_pack77_cli_fallbacks.cmake new file mode 100644 index 00000000..ed3d3aa1 --- /dev/null +++ b/tests/unit/packjt77/test_pack77_cli_fallbacks.cmake @@ -0,0 +1,75 @@ +function(expect_fallback label executable expected_output) + execute_process( + COMMAND "${executable}" ${ARGN} + RESULT_VARIABLE result + OUTPUT_VARIABLE output + ERROR_VARIABLE error + ) + if (NOT result EQUAL 0) + message(FATAL_ERROR "${label} fallback failed (${result}):\n${output}${error}") + endif () + if (NOT output MATCHES "${expected_output}") + message(FATAL_ERROR "${label} fallback output was unexpected:\n${output}") + endif () +endfunction() + +function(expect_rejected label executable) + execute_process( + COMMAND "${executable}" "HELLO@WORLD" ${ARGN} + RESULT_VARIABLE result + OUTPUT_VARIABLE output + ERROR_VARIABLE error + ) + if (result EQUAL 0) + message(FATAL_ERROR "${label} accepted an unrepresentable message:\n${output}${error}") + endif () + if (NOT output MATCHES "Cannot encode message:[ ]+HELLO@WORLD") + message(FATAL_ERROR "${label} rejection output was unexpected:\n${output}${error}") + endif () +endfunction() + +expect_fallback("ft4code" "${FT4CODE}" + "THIS FREE TEX.*0\\.0[ ]+Free text" "THIS FREE TEXT IS TOO LONG") +expect_rejected("ft4code" "${FT4CODE}") + +expect_fallback("ft4sim" "${FT4SIM}" + "THIS FREE TEX.*i3.n3:[ ]+0\\.0" "THIS FREE TEXT IS TOO LONG" + 1500 0.0 0.0 0.0 0 99) +expect_rejected("ft4sim" "${FT4SIM}" 1500 0.0 0.0 0.0 0 99) + +execute_process( + COMMAND "${FST4SIM}" "THIS FREE TEXT IS TOO LONG" 60 1500 0.0 0.0 0.0 0 99 F + RESULT_VARIABLE fst4_result + OUTPUT_VARIABLE fst4_output + ERROR_VARIABLE fst4_error +) +if (NOT fst4_result EQUAL 0) + message(FATAL_ERROR "fst4sim fallback failed (${fst4_result}):\n${fst4_output}${fst4_error}") +endif () +if (NOT fst4_output MATCHES "THIS FREE TEX") + message(FATAL_ERROR "fst4sim did not report the canonical fallback message:\n${fst4_output}") +endif () +if (NOT fst4_output MATCHES "iwspr:[ ]+0") + message(FATAL_ERROR "fst4sim did not select FST4 for the fallback message:\n${fst4_output}") +endif () +expect_rejected("fst4sim" "${FST4SIM}" 60 1500 0.0 0.0 0.0 0 99 F) + +expect_fallback("ldpcsim240_101" "${LDPCSIM240_101}" + "Message sent:[ ]+THIS FREE TEX" 1 -1 0 1.0 101 + "THIS FREE TEXT IS TOO LONG") +execute_process( + COMMAND "${LDPCSIM240_101}" 1 -1 0 1.0 101 "HELLO@WORLD" + RESULT_VARIABLE ldpc_rejected_result + OUTPUT_VARIABLE ldpc_rejected_output + ERROR_VARIABLE ldpc_rejected_error +) +if (ldpc_rejected_result EQUAL 0) + message(FATAL_ERROR "ldpcsim240_101 accepted an unrepresentable message:\n${ldpc_rejected_output}${ldpc_rejected_error}") +endif () +if (NOT ldpc_rejected_output MATCHES "Cannot encode message:[ ]+HELLO@WORLD") + message(FATAL_ERROR "ldpcsim240_101 rejection output was unexpected:\n${ldpc_rejected_output}${ldpc_rejected_error}") +endif () + +expect_fallback("msk144code" "${MSK144CODE}" + "THIS FREE TEX.*0\\.0[ ]+Free text" "THIS FREE TEXT IS TOO LONG") +expect_rejected("msk144code" "${MSK144CODE}") diff --git a/tests/unit/packjt77/test_packjt77_concurrent_unpack.f90 b/tests/unit/packjt77/test_packjt77_concurrent_unpack.f90 new file mode 100644 index 00000000..5f72a4b6 --- /dev/null +++ b/tests/unit/packjt77/test_packjt77_concurrent_unpack.f90 @@ -0,0 +1,101 @@ +program test_packjt77_concurrent_unpack + + use omp_lib + use packjt77 + implicit none + + integer, parameter :: worker_count=4 + integer, parameter :: phase_count=5 + integer, parameter :: iteration_count=1000 + character(len=37) :: source_messages(worker_count,phase_count) + character(len=77) :: payloads(worker_count,phase_count) + character(len=37) :: expected_messages(worker_count,phase_count) + character(len=37) :: decoded + integer :: failures,i3,iteration,n3,observed_threads,phase,status,worker + logical :: ok + + failures=0 + observed_threads=0 + source_messages(1,1)='A92EE F5PSR -14' + source_messages(2,1)='WM3PEN EA6VQ -09' + source_messages(3,1)='W1FC F5BZB -08' + source_messages(4,1)='N1JFU EA6EE R-07' + source_messages(1,2)='K1ABC/P W9XYZ/P R FN42' + source_messages(2,2)='N0AAA/P N0BBB/P -10' + source_messages(3,2)='W3ABC/P K4XYZ/P RR73' + source_messages(4,2)='F1ABC/P G2XYZ/P 73' + source_messages(1,3)='K1ABC RR73; W9XYZ <KH1/KH7Z> -12' + source_messages(2,3)='N0AAA RR73; N0BBB <KH1/KH7Z> -14' + source_messages(3,3)='W3ABC RR73; K4XYZ <KH1/KH7Z> -16' + source_messages(4,3)='F1ABC RR73; G2XYZ <KH1/KH7Z> -18' + source_messages(1,4)='WA9XYZ KA1ABC R 16A EMA' + source_messages(2,4)='N0AAA N0BBB 1B CT' + source_messages(3,4)='W3ABC K4XYZ R 2C WMA' + source_messages(4,4)='F1ABC G2XYZ 3D DX' + source_messages(1,5)='TU; K1ABC W9XYZ R 529 MA' + source_messages(2,5)='TU; W9XYZ K1ABC R 559 CT' + source_messages(3,5)='TU; W9XYZ G8ABC R 559 0013' + source_messages(4,5)='W9XYZ VE3ABC 559 ON' + + do phase=1,phase_count + do worker=1,worker_count + call pack77(source_messages(worker,phase),i3,n3, & + payloads(worker,phase), & + pack77_options(record_tx_hashes=.false.),status) + if(status.ne.PACK77_STATUS_ENCODED) then + write(*,1000) phase,worker,status,trim(source_messages(worker,phase)) +1000 format('failed to encode phase ',i0,' worker ',i0,' status ',i0,': ',a) + error stop 1 + endif + call unpack77_configured(payloads(worker,phase),0, & + expected_messages(worker,phase),ok, & + unpack77_options(thread_index=worker,record_hashes=.false., & + record_recent_calls=.false.)) + if(.not.ok) then + write(*,1010) phase,worker,trim(source_messages(worker,phase)) +1010 format('failed expected decode for phase ',i0,' worker ',i0,': ',a) + error stop 1 + endif + enddo + enddo + + call omp_set_dynamic(.false.) + call omp_set_num_threads(worker_count) + +!$omp parallel default(none) & +!$omp shared(expected_messages,failures,observed_threads,payloads) & +!$omp private(decoded,iteration,ok,phase,worker) + worker=omp_get_thread_num()+1 +!$omp single + observed_threads=omp_get_num_threads() +!$omp end single + do phase=1,phase_count + do iteration=1,iteration_count +!$omp barrier + call unpack77_configured(payloads(worker,phase),0,decoded,ok, & + unpack77_options(thread_index=worker,record_hashes=.false., & + record_recent_calls=.false.)) + if(.not.ok .or. decoded.ne.expected_messages(worker,phase)) then +!$omp atomic update + failures=failures+1 + endif +!$omp barrier + enddo + enddo +!$omp end parallel + + if(observed_threads.ne.worker_count) then + write(*,1020) worker_count,observed_threads +1020 format('expected ',i0,' OpenMP workers, got ',i0) + error stop 1 + endif + if(failures.ne.0) then + write(*,1030) failures,worker_count*phase_count*iteration_count +1030 format('concurrent unpack mismatches: ',i0,' of ',i0) + error stop 1 + endif + + write(*,1040) worker_count*phase_count*iteration_count,worker_count +1040 format('concurrent unpack decodes passed: ',i0,' across ',i0,' threads') + +end program test_packjt77_concurrent_unpack diff --git a/tests/unit/packjt77/test_packjt77_domain_sweeps.f90 b/tests/unit/packjt77/test_packjt77_domain_sweeps.f90 new file mode 100644 index 00000000..eee14cc9 --- /dev/null +++ b/tests/unit/packjt77/test_packjt77_domain_sweeps.f90 @@ -0,0 +1,333 @@ +program test_packjt77_domain_sweeps + use packjt77_grammar + use packjt77_test_helpers, only: assert_true + implicit none + + integer, parameter :: GRID4_TOTAL=18*18*10*10 + integer, parameter :: GRID6_TOTAL=18*18*10*10*24*24 + integer, parameter :: GRID6_WSPR_TOTAL=18*18*10*10*25*25 + integer :: nchecked + + nchecked=0 + call sweep_arrl_sections() + call sweep_rtty_multipliers() + call sweep_rtty_reports() + call sweep_snr_reports() + call sweep_qso_tails() + call sweep_dxpedition_reports() + call sweep_wspr_dbm() + call sweep_field_day_exchanges() + call sweep_wspr_affixes() + call sweep_grid4() + call sweep_grid6_samples() + + print '(a,i0,a)', 'test_packjt77_domain_sweeps: ', nchecked, & + ' values enumerated' + +contains + + subroutine sweep_arrl_sections() + character(len=3) :: section + integer :: i + + call assert_true('ARRL invalid text', & + pack77_arrl_section_index('ZZZ').eq.-1) + do i=1,PACK77_NSEC + section=pack77_arrl_section_name(i) + call assert_true('ARRL inverse', pack77_arrl_section_index(section).eq.i) + if(section(1:1).ge.'A' .and. section(1:1).le.'Z') then + section(1:1)=achar(iachar(section(1:1))+32) + call assert_true('ARRL lowercase reject', & + pack77_arrl_section_index(section).eq.-1) + endif + nchecked=nchecked+1 + enddo + call assert_true('ARRL low index', pack77_arrl_section_name(0).eq.' ') + call assert_true('ARRL high index', & + pack77_arrl_section_name(PACK77_NSEC+1).eq.' ') + end subroutine sweep_arrl_sections + + subroutine sweep_rtty_multipliers() + character(len=3) :: multiplier + integer :: i + + call assert_true('RTTY multiplier invalid text', & + pack77_rtty_multiplier_index('ZZZ').eq.-1) + do i=1,PACK77_NUSCAN + multiplier=pack77_rtty_multiplier_name(i) + call assert_true('RTTY multiplier inverse', & + pack77_rtty_multiplier_index(multiplier).eq.i) + if(multiplier(1:1).ge.'A' .and. multiplier(1:1).le.'Z') then + multiplier(1:1)=achar(iachar(multiplier(1:1))+32) + call assert_true('RTTY multiplier lowercase reject', & + pack77_rtty_multiplier_index(multiplier).eq.-1) + endif + nchecked=nchecked+1 + enddo + call assert_true('RTTY multiplier low index', & + pack77_rtty_multiplier_name(0).eq.' ') + call assert_true('RTTY multiplier high index', & + pack77_rtty_multiplier_name(PACK77_NUSCAN+1).eq.' ') + end subroutine sweep_rtty_multipliers + + subroutine sweep_rtty_reports() + character(len=3) :: report + integer :: i + + do i=0,7 + report=pack77_format_rtty_report(i) + call assert_true('RTTY report inverse', & + pack77_rtty_report_index(report).eq.i) + nchecked=nchecked+1 + enddo + call assert_true('RTTY report low reject', & + pack77_rtty_report_index('519').eq.-1) + call assert_true('RTTY report high reject', & + pack77_rtty_report_index('609').eq.-1) + call assert_true('RTTY report wrong length reject', & + pack77_rtty_report_index('5799').eq.-1) + end subroutine sweep_rtty_reports + + subroutine sweep_snr_reports() + character(len=3) :: report + integer :: isnr,irpt + + do isnr=-50,50 + irpt=pack77_report_index_from_snr(isnr) + call assert_true('SNR report accepts legal value', irpt.ge.0) + call assert_true('SNR report inverse', & + pack77_snr_from_report_index(irpt).eq.isnr) + report=pack77_format_snr_report(isnr) + call assert_true('SNR report format width', len_trim(report).eq.3) + nchecked=nchecked+1 + enddo + do isnr=-75,-51 + call assert_true('SNR report low reject', & + pack77_report_index_from_snr(isnr).eq.-1) + enddo + do isnr=51,75 + call assert_true('SNR report high reject', & + pack77_report_index_from_snr(isnr).eq.-1) + enddo + end subroutine sweep_snr_reports + + subroutine sweep_qso_tails() + character(len=4), parameter :: tails(3)=(/'RRR ','RR73','73 '/) + integer :: i,irpt,nrpt + + do i=1,3 + call assert_true('QSO tail index', pack77_qso_tail_index(tails(i)).eq.i) + irpt=pack77_type12_irpt_from_tail(i) + call assert_true('Type 1/2 tail inverse', & + pack77_tail_from_type12_irpt(irpt).eq.i) + nrpt=pack77_type4_nrpt_from_tail(i) + call assert_true('Type 4 tail inverse', & + pack77_tail_from_type4_nrpt(nrpt).eq.i) + nchecked=nchecked+1 + enddo + call assert_true('QSO tail near miss', pack77_qso_tail_index('RR').eq.0) + call assert_true('Type 1/2 tail low', pack77_tail_from_type12_irpt(1).eq.0) + call assert_true('Type 4 tail high', pack77_tail_from_type4_nrpt(4).eq.0) + end subroutine sweep_qso_tails + + subroutine sweep_dxpedition_reports() + character(len=3) :: report + integer :: isnr,n5 + logical :: ok + + do isnr=-31,33 + call format_signed_report(isnr,report) + call pack77_parse_dxpedition_report(report,n5,ok) + if(isnr.ge.-30 .and. isnr.le.32 .and. mod(isnr,2).eq.0) then + call assert_true('DXpedition report accepts legal value', ok) + call assert_true('DXpedition report inverse', 2*n5-30.eq.isnr) + nchecked=nchecked+1 + else + call assert_true('DXpedition report rejects illegal value', .not.ok) + endif + enddo + end subroutine sweep_dxpedition_reports + + subroutine sweep_wspr_dbm() + character(len=3) :: token + integer :: dbm,idbm,decoded + logical :: ok + + do dbm=-5,65 + write(token,'(i0)') dbm + call pack77_parse_wspr_dbm(token,idbm,ok) + if(ok) then + decoded=nint(idbm*10.0/3.0) + call assert_true('WSPR dBm inverse', decoded.eq.dbm) + nchecked=nchecked+1 + else if(dbm.ge.0 .and. dbm.le.60) then + call assert_true('WSPR dBm legal codebook only', & + nint(nint(0.3*dbm)*10.0/3.0).ne.dbm) + endif + enddo + call pack77_parse_wspr_dbm('03',idbm,ok) + call assert_true('WSPR dBm leading zero reject', .not.ok) + end subroutine sweep_wspr_dbm + + subroutine sweep_field_day_exchanges() + character(len=4) :: token + integer :: ntx,nclass,parsed_ntx,parsed_class + logical :: ok + + do ntx=1,32 + do nclass=0,5 + write(token,'(i0,a1)') ntx, achar(iachar('A')+nclass) + call pack77_parse_field_day_exchange(token,parsed_ntx,parsed_class,ok) + call assert_true('Field Day exchange accepts legal value', ok) + call assert_true('Field Day exchange inverse', & + parsed_ntx.eq.ntx .and. parsed_class.eq.nclass) + nchecked=nchecked+1 + enddo + enddo + call pack77_parse_field_day_exchange('0A',parsed_ntx,parsed_class,ok) + call assert_true('Field Day ntx low reject', .not.ok) + call pack77_parse_field_day_exchange('33A',parsed_ntx,parsed_class,ok) + call assert_true('Field Day ntx high reject', .not.ok) + call pack77_parse_field_day_exchange('1G',parsed_ntx,parsed_class,ok) + call assert_true('Field Day class reject', .not.ok) + call pack77_parse_field_day_exchange('01A',parsed_ntx,parsed_class,ok) + call assert_true('Field Day leading zero reject', .not.ok) + end subroutine sweep_field_day_exchanges + + subroutine sweep_wspr_affixes() + character(len=13) :: token + character(len=3) :: affix,affix_text + integer :: i,npfx,slash_index + logical :: is_prefix,ok + + do i=0,PACK77_WSPR_NZZZ+12959 + call pack77_wspr_affix_text(i,affix,is_prefix,ok) + call assert_true('WSPR affix index renders', ok) + affix_text=adjustl(affix) + if(is_prefix) then + token=trim(affix_text)//'/K1ABC' + slash_index=len_trim(affix_text)+1 + else + token='K1ABC/'//trim(affix_text) + slash_index=6 + endif + npfx=pack77_wspr_affix_index(token,slash_index,len_trim(token),ok) + call assert_true('WSPR affix index inverse', ok .and. npfx.eq.i) + call require_wspr_source(token//' 37', i) + nchecked=nchecked+1 + enddo + + token='ABCD/K1ABC' + npfx=pack77_wspr_affix_index(token,5,len_trim(token),ok) + call assert_true('WSPR four-character prefix reject', .not.ok) + token='K1ABC/ABA' + npfx=pack77_wspr_affix_index(token,6,len_trim(token),ok) + call assert_true('WSPR suffix non-digit third reject', .not.ok) + call require_wspr_source_reject('00/K1ABC 37') + call require_wspr_source_reject('K1ABC/00 37') + end subroutine sweep_wspr_affixes + + subroutine require_wspr_source(msg,want_npfx) + character(len=*), intent(in) :: msg + integer, intent(in) :: want_npfx + type(pack77_wspr_source) :: source + logical :: ok,matched_shape + + call pack77_parse_wspr_source(msg,source,ok,matched_shape) + call assert_true('WSPR source affix parse', & + ok .and. source%npfx.eq.want_npfx) + end subroutine require_wspr_source + + subroutine require_wspr_source_reject(msg) + character(len=*), intent(in) :: msg + type(pack77_wspr_source) :: source + logical :: ok,matched_shape + + call pack77_parse_wspr_source(msg,source,ok,matched_shape) + call assert_true('WSPR source alias reject '//trim(msg), .not.ok) + end subroutine require_wspr_source_reject + + subroutine sweep_grid4() + character(len=4) :: grid4 + integer :: i,narg + logical :: ok + + do i=0,GRID4_TOTAL-1 + narg=i + call to_grid4(narg,grid4,ok) + call assert_true('Grid4 renders', ok) + call assert_true('Grid4 validates', pack77_is_grid4(grid4)) + call assert_true('Grid4 inverse', pack77_grid4_index(grid4).eq.i) + nchecked=nchecked+1 + enddo + narg=-1 + call to_grid4(narg,grid4,ok) + call assert_true('Grid4 low index reject', .not.ok) + narg=GRID4_TOTAL + call to_grid4(narg,grid4,ok) + call assert_true('Grid4 high index reject', .not.ok) + call assert_true('Grid4 short reject', .not.pack77_is_grid4('AA0')) + call assert_true('Grid4 lowercase reject', .not.pack77_is_grid4('aa00')) + end subroutine sweep_grid4 + + subroutine sweep_grid6_samples() + integer, parameter :: NSAMPLE=10000 + integer(kind=8) :: state + integer :: i,n + + call check_grid6_index(0) + call check_grid6_index(GRID6_TOTAL-1) + call check_grid6_index(18*10*10*24*24-1) + call check_grid6_index(18*10*10*24*24) + call check_wspr_grid_index(0) + call check_wspr_grid_index(GRID6_WSPR_TOTAL-1) + call check_wspr_grid_index(18*10*10*25*25-1) + call check_wspr_grid_index(18*10*10*25*25) + + state=123456789_8 + do i=1,NSAMPLE + state=mod(1103515245_8*state+12345_8,2147483648_8) + n=int(mod(state,int(GRID6_TOTAL,kind=8))) + call check_grid6_index(n) + state=mod(1103515245_8*state+12345_8,2147483648_8) + n=int(mod(state,int(GRID6_WSPR_TOTAL,kind=8))) + call check_wspr_grid_index(n) + enddo + end subroutine sweep_grid6_samples + + subroutine check_grid6_index(n) + integer, intent(in) :: n + character(len=6) :: grid6 + integer :: narg + logical :: ok + + narg=n + call to_grid6(narg,grid6,ok) + call assert_true('Grid6 renders', ok) + call assert_true('Grid6 validates', pack77_is_grid6(grid6,.false.)) + call assert_true('Grid6 inverse', pack77_grid6_index(grid6).eq.n) + nchecked=nchecked+1 + end subroutine check_grid6_index + + subroutine check_wspr_grid_index(n) + integer, intent(in) :: n + character(len=6) :: grid6 + integer :: narg + logical :: ok + + narg=n + call to_grid(narg,grid6,ok) + call assert_true('WSPR Grid6 renders', ok) + call assert_true('WSPR Grid6 inverse', pack77_grid6_wspr_index(grid6).eq.n) + nchecked=nchecked+1 + end subroutine check_wspr_grid_index + + subroutine format_signed_report(value,token) + integer, intent(in) :: value + character(len=3), intent(out) :: token + + write(token,'(sp,i3.2)') value + if(value.ge.0 .and. token(1:1).eq.' ') token(1:1)='+' + end subroutine format_signed_report + +end program test_packjt77_domain_sweeps diff --git a/tests/unit/packjt77/test_packjt77_grammar_gate.f90 b/tests/unit/packjt77/test_packjt77_grammar_gate.f90 new file mode 100644 index 00000000..2698baa1 --- /dev/null +++ b/tests/unit/packjt77/test_packjt77_grammar_gate.f90 @@ -0,0 +1,73 @@ +program test_packjt77_grammar_gate + + use packjt77_grammar, only: pack77_gate_messages_match + implicit none + + integer :: ntests + character(len=13) :: no_calls(1), staged_calls(2) + + ntests=0 + no_calls='' + staged_calls='' + staged_calls(1)='PJ4/K1ABC' + staged_calls(2)='W3CCX' + + call expect_match('equal messages', & + ' k1abc w9xyz fn42', 'K1ABC W9XYZ FN42', no_calls, 0) + call expect_match('auto-hash canonicalization', & + 'W1AW/P K1ABC 5A CT', '<W1AW/P> K1ABC 5A CT', no_calls, 0) + call expect_match('staged placeholder', & + '<PJ4/K1ABC> W9XYZ RR73', '<...> W9XYZ RR73', staged_calls, 2) + + call expect_no_match('unstaged placeholder', & + '<PJ4/K1ABC> W9XYZ RR73', '<...> W9XYZ RR73', no_calls, 0) + call expect_no_match('CQ bracketed C11 mismatch', & + 'CQ <PJ4/K1ABC>', 'CQ PJ4/K1ABC', no_calls, 0) + call expect_no_match('wrong bracket body', & + '<K1ABC> W9XYZ RR73', '<W9XYZ> W9XYZ RR73', no_calls, 0) + call expect_no_match('different token count', & + 'K1ABC W9XYZ FN42', 'K1ABC W9XYZ', no_calls, 0) + call expect_no_match('changed first call', & + 'K1ABC W9XYZ FN42', 'N1ABC W9XYZ FN42', no_calls, 0) + call expect_no_match('changed second call', & + 'K1ABC W9XYZ FN42', 'K1ABC W9XYA FN42', no_calls, 0) + call expect_no_match('changed report', & + 'K1ABC W9XYZ -12', 'K1ABC W9XYZ -13', no_calls, 0) + call expect_no_match('changed grid', & + 'K1ABC W9XYZ FN42', 'K1ABC W9XYZ FN43', no_calls, 0) + call expect_no_match('changed serial', & + 'K1ABC FN42 37', 'K1ABC FN42 38', no_calls, 0) + + write(*,1000) ntests +1000 format('packjt77 grammar gate tests passed: ',i0) + +contains + + subroutine expect_match(label,input_msg,decoded_msg,staged_calls,nstaged) + character(len=*), intent(in) :: label,input_msg,decoded_msg + character(len=13), intent(in) :: staged_calls(:) + integer, intent(in) :: nstaged + + if(.not.pack77_gate_messages_match(input_msg,decoded_msg,staged_calls, & + nstaged)) then + write(*,1010) trim(label), trim(input_msg), trim(decoded_msg) +1010 format(a,' expected match for input "',a,'" decoded "',a,'"') + error stop 1 + endif + ntests=ntests+1 + end subroutine expect_match + + subroutine expect_no_match(label,input_msg,decoded_msg,staged_calls,nstaged) + character(len=*), intent(in) :: label,input_msg,decoded_msg + character(len=13), intent(in) :: staged_calls(:) + integer, intent(in) :: nstaged + + if(pack77_gate_messages_match(input_msg,decoded_msg,staged_calls,nstaged)) then + write(*,1020) trim(label), trim(input_msg), trim(decoded_msg) +1020 format(a,' expected mismatch for input "',a,'" decoded "',a,'"') + error stop 1 + endif + ntests=ntests+1 + end subroutine expect_no_match + +end program test_packjt77_grammar_gate diff --git a/tests/unit/packjt77/test_packjt77_hash_state.f90 b/tests/unit/packjt77/test_packjt77_hash_state.f90 new file mode 100644 index 00000000..b122ba46 --- /dev/null +++ b/tests/unit/packjt77/test_packjt77_hash_state.f90 @@ -0,0 +1,1293 @@ +program test_packjt77_hash_state + + use packjt77 + use packjt77_test_helpers + use ft8_mod1, only : mycall, hiscall, lhound, mybcall, hisbcall, hisgrid4 + implicit none + + integer :: ntests + external :: fillhashvar + external :: ft8apsetvar + external :: genft8 + + ntests=0 + + call expect_standard_hash_save_contract() + call expect_var_hash_save_contract() + call expect_var_hash_save_thread_bounds() + call expect_var_thread_queue_capacity() + call expect_var_thread_rx_accumulation() + call expect_type4_literal_call_staged_until_fill() + call expect_configured_type12_addressee_staged_until_fill() + call expect_legacy_type12_addressee_not_hashed() + call expect_service_unpack_does_not_consume_dx_edge() + call expect_configured_call_clear_blanks_stale_state() + call expect_dxpedition_dxcall_reentry_substitution() + call expect_shared_hash_tables_survive_mode_sync() + call expect_default_pack77_tx_hash_recording() + call expect_pack77_record_tx_hashes_option_suppresses_commit() + call expect_pack77_encode_rejects_dx_macro_by_default() + call expect_pack77_free_text_fallback_rejects_unexpanded_dx_macro() + call expect_pack_unpack_hash_state_matches_pack_only() + call expect_genft8_failure_state() + call expect_dx_macro_option_uses_explicit_base() + call expect_failed_pack77_candidates_do_not_record_hashes() + call expect_ft8apsetvar_does_not_record_template_hashes() + call expect_var_tx_rx_separation() + call expect_mycall_dxcall_substitutions() + call expect_invalid_standard_call_decode_failure() + + call expect_hash_resolution_case( & + 'K1ABC RR73; W9XYZ <KH1/KH7Z> -12', 0, 1, & + 'K1ABC RR73; W9XYZ <...> -12', & + 'K1ABC RR73; W9XYZ <KH1/KH7Z> -12', & + 10, 'KH1/KH7Z', 0, '', 0, '') + call expect_hash_resolution_case( & + '<PJ4/K1ABC> W9XYZ RR73', 1, 0, & + '<...> W9XYZ RR73', & + '<PJ4/K1ABC> W9XYZ RR73', & + 22, 'PJ4/K1ABC', 0, '', 0, '') + call expect_hash_resolution_case( & + 'PJ2/W1AW <W7ABC> RR73', 4, 0, & + 'PJ2/W1AW <...> RR73', & + 'PJ2/W1AW <W7ABC> RR73', & + 12, 'W7ABC', 0, '', 0, '') + call expect_hash_resolution_case( & + '<W3CCX> <K1JT/P> 590001 FN20QI', 5, 0, & + '<...> <...> 590001 FN20QI', & + '<W3CCX> <K1JT/P> 590001 FN20QI', & + 12, 'W3CCX', 22, 'K1JT/P', 0, '') + + write(*,1000) ntests +1000 format('packjt77 hash-state tests passed: ',i0) + +contains + + subroutine expect_standard_hash_save_contract() + character(len=13) :: c13 + integer :: n10, n12, n22 + + call clear_all_state('N0AAA','N0BBB') + + c13=' ' + c13='<PJ4/K1ABC>' + call save_hash_call(c13,n10,n12,n22) + call assert_int('standard n10',346,n10) + call assert_int('standard n12',1387,n12) + call assert_int('standard n22',1420834,n22) + call assert_call('standard calls10',calls10(n10),'PJ4/K1ABC') + call assert_call('standard calls12',calls12(n12),'PJ4/K1ABC') + call assert_int('standard nzhash',1,nzhash) + call assert_int('standard ihash22(1)',n22,ihash22(1)) + call assert_call('standard calls22(1)',calls22(1),'PJ4/K1ABC') + + call save_hash_call(c13,n10,n12,n22) + call assert_int('standard duplicate nzhash',1,nzhash) + call assert_call('standard duplicate calls22(1)',calls22(1),'PJ4/K1ABC') + + call clear_all_state('K1ABC','N0BBB') + c13=' ' + c13='K1ABC' + call save_hash_call(c13,n10,n12,n22) + call assert_call('standard mycall calls10 excluded',calls10(n10),'') + call assert_call('standard mycall calls12 excluded',calls12(n12),'') + call assert_call('standard mycall calls22 retained',calls22(1),'K1ABC') + + call clear_all_state('N0AAA','N0BBB') + n10=-99 + n12=-99 + n22=-99 + c13=' ' + c13='<...>' + call save_hash_call(c13,n10,n12,n22) + call assert_int('standard placeholder ignored nzhash',0,nzhash) + call assert_int('standard placeholder ignored n10',-99,n10) + + c13=' ' + c13='K1' + call save_hash_call(c13,n10,n12,n22) + call assert_int('standard short call ignored nzhash',0,nzhash) + + ntests=ntests+1 + end subroutine expect_standard_hash_save_contract + + subroutine expect_var_hash_save_contract() + character(len=13) :: c13 + integer :: n10, n12, n22 + integer :: slot + + call clear_all_state('N0AAA','N0BBB') + + c13=' ' + c13='<PJ4/K1ABC>' + call save_hash_call(c13,n10,n12,n22) + call assert_int('var rx n10',346,n10) + call assert_int('var rx n12',1387,n12) + call assert_int('var rx n22',1420834,n22) + call assert_call('var rx calls10',calls10(n10),'PJ4/K1ABC') + call assert_call('var rx calls12',calls12(n12),'PJ4/K1ABC') + call assert_int('var rx nzhash',1,nzhash) + call assert_int('var rx ihash22(1)',n22,ihash22(1)) + call assert_call('var rx calls22(1)',calls22(1),'PJ4/K1ABC') + + call clear_all_state('N0AAA','N0BBB') + c13=' ' + c13='<PJ4/K1ABC>' + n10=ihashcall('PJ4/K1ABC ',10) + n12=ihashcall('PJ4/K1ABC ',12) + n22=ihashcall('PJ4/K1ABC ',22) + call assert_int('var tx disabled count',0,nzhash) + call assert_call('var tx disabled calls12',calls12(1387),'') + + call save_hash_call(c13,n10,n12,n22) + call assert_int('var tx n10',346,n10) + call assert_int('var tx n12',1387,n12) + call assert_int('var tx n22',1420834,n22) + call assert_call('var tx calls10',calls10(n10),'PJ4/K1ABC') + call assert_call('var tx calls12',calls12(n12),'PJ4/K1ABC') + call assert_int('var tx nzhash',1,nzhash) + call assert_int('var tx ihash22(1)',n22,ihash22(1)) + call assert_call('var tx calls22(1)',calls22(1),'PJ4/K1ABC') + + call clear_all_state('N0AAA','N0BBB') + c13=' ' + c13='<W7ABC>' + call queue_hash_call_for_thread(c13,3) + slot=thread_call_index(3)+1 + call assert_int('var thread 3 count',1,nqueued_calls_by_thread(3)) + call assert_int('var thread 2 count',0,nqueued_calls_by_thread(2)) + call assert_int('var thread 4 count',0,nqueued_calls_by_thread(4)) + call assert_call('var thread 3 call',queued_calls_by_thread(slot),'W7ABC') + call assert_call('var thread call not folded',calls12(ihashcall('W7ABC ',12)),'') + + ntests=ntests+1 + end subroutine expect_var_hash_save_contract + + subroutine expect_var_hash_save_thread_bounds() + character(len=13) :: c13 + + call clear_all_state('N0AAA','N0BBB') + call normalize_call('W7ABC',c13) + + call queue_hash_call_for_thread(c13,0) + call queue_hash_call_for_thread(c13,-1) + call queue_hash_call_for_thread(c13,25) + + call assert_true('var invalid thread indices ignored', & + all(nqueued_calls_by_thread(1:24).eq.0)) + call assert_call('var first queued call untouched',queued_calls_by_thread(1),'') + + ntests=ntests+1 + end subroutine expect_var_hash_save_thread_bounds + + subroutine expect_var_thread_queue_capacity() + character(len=13) :: c13, last_call, overflow_call + integer :: capacity, i, nthr + + call clear_all_state('N0AAA','N0BBB') + + nthr=24 + capacity=thread_call_index(nthr+1)-thread_call_index(nthr) + do i=1,capacity + c13=' ' + write(c13,'("K0",i2.2)') i + call queue_hash_call_for_thread(c13,nthr) + enddo + + overflow_call=' ' + write(overflow_call,'("K0",i2.2)') 99 + call queue_hash_call_for_thread(overflow_call,nthr) + + last_call=' ' + write(last_call,'("K0",i2.2)') capacity + call assert_int('var thread capacity count',capacity, & + nqueued_calls_by_thread(nthr)) + call assert_call('var thread capacity last call', & + queued_calls_by_thread(thread_call_index(nthr)+capacity),last_call) + call assert_true('var thread capacity overflow dropped', & + .not.any(queued_calls_by_thread.eq.overflow_call)) + call assert_int('var thread capacity nzhash before fold',0,nzhash) + + call fillhashvar(nthr,.true.) + + call assert_int('var thread capacity nzhash after fold',capacity,nzhash) + call assert_true('var thread capacity last hash after fold', & + shared_hash22_contains(last_call)) + call assert_true('var thread capacity overflow absent after fold', & + .not.shared_hash22_contains(overflow_call)) + + ntests=ntests+1 + end subroutine expect_var_thread_queue_capacity + + subroutine expect_var_thread_rx_accumulation() + character(len=13) :: rx_call, later_thread_call + integer :: n10, n12, n22 + integer :: later_n10, later_n12 + + call clear_all_state('N0AAA','N0BBB') + + call normalize_call('W7ABC',rx_call) + call normalize_call('K9XYZ',later_thread_call) + + ! queue_hash_call_for_thread queues RX hashes per thread until fillhashvar folds them. + call queue_hash_call_for_thread(rx_call,3) + call queue_hash_call_for_thread(later_thread_call,7) + + n10=ihashcall(rx_call,10) + n12=ihashcall(rx_call,12) + n22=ihashcall(rx_call,22) + later_n10=ihashcall(later_thread_call,10) + later_n12=ihashcall(later_thread_call,12) + + call assert_int('var thread 3 count before fold',1,nqueued_calls_by_thread(3)) + call assert_int('var thread 7 count before fold',1,nqueued_calls_by_thread(7)) + call assert_int('var thread 2 count before fold',0,nqueued_calls_by_thread(2)) + call assert_int('var thread 4 count before fold',0,nqueued_calls_by_thread(4)) + call assert_call('var thread 3 first call',queued_calls_by_thread(thread_call_index(3)+1), & + 'W7ABC') + call assert_call('var thread 7 first call',queued_calls_by_thread(thread_call_index(7)+1), & + 'K9XYZ') + call assert_call('var thread 2 first slot untouched', & + queued_calls_by_thread(thread_call_index(2)+1),'') + call assert_call('var thread 4 first slot untouched', & + queued_calls_by_thread(thread_call_index(4)+1),'') + call assert_call('var rx calls10 before fold',calls10(n10),'') + call assert_call('var rx calls12 before fold',calls12(n12),'') + call assert_int('var rx nzhash before fold',0,nzhash) + + call fillhashvar(3,.true.) + + call assert_call('var rx calls10 after fold',calls10(n10),'W7ABC') + call assert_call('var rx calls12 after fold',calls12(n12),'W7ABC') + call assert_int('var rx nzhash after fold',1,nzhash) + call assert_int('var rx ihash22 after fold',n22,ihash22(1)) + call assert_call('var rx calls22 after fold',calls22(1),'W7ABC') + call assert_call('var later thread not folded calls10',calls10(later_n10),'') + call assert_call('var later thread not folded calls12',calls12(later_n12),'') + call assert_int('var thread 3 count after fold',1,nqueued_calls_by_thread(3)) + call assert_int('var thread 7 count after fold',1,nqueued_calls_by_thread(7)) + call assert_call('var thread 7 call retained after fold', & + queued_calls_by_thread(thread_call_index(7)+1),'K9XYZ') + + ntests=ntests+1 + end subroutine expect_var_thread_rx_accumulation + + subroutine expect_type4_literal_call_staged_until_fill() + character(len=77) :: c77 + character(len=37) :: decoded + character(len=13) :: literal_call + integer :: n10, n12 + logical :: ok + + call pack_and_assert_type('PJ2/W1AW <W7ABC> RR73',4,0,c77) + call clear_all_state('N0AAA','N0BBB') + call normalize_call('PJ2/W1AW',literal_call) + n10=ihashcall(literal_call,10) + n12=ihashcall(literal_call,12) + + call unpack_var_current_state_thread(c77,1,3,decoded,ok) + + call assert_decode('type 4 staged literal decode', & + 'PJ2/W1AW <W7ABC> RR73','PJ2/W1AW <...> RR73',decoded,ok) + call assert_int('type 4 thread 3 queued count',1,nqueued_calls_by_thread(3)) + call assert_call('type 4 thread 3 queued call', & + queued_calls_by_thread(thread_call_index(3)+1),'PJ2/W1AW') + call assert_int('type 4 thread 3 queued recent count',1, & + nqueued_recent_calls_by_thread(3)) + call assert_call('type 4 thread 3 queued recent call', & + queued_recent_calls_by_thread(thread_call_index(3)+1),'PJ2/W1AW') + call assert_call('type 4 calls10 before fill',calls10(n10),'') + call assert_call('type 4 calls12 before fill',calls12(n12),'') + call assert_call('type 4 recent before fill',recent_calls(1),'') + + call fillhashvar(3,.true.) + + call assert_call('type 4 calls10 after fill',calls10(n10),'PJ2/W1AW') + call assert_call('type 4 calls12 after fill',calls12(n12),'PJ2/W1AW') + call assert_true('type 4 calls22 after fill',shared_hash22_contains(literal_call)) + call assert_call('type 4 recent after fill',recent_calls(1),'PJ2/W1AW') + + ntests=ntests+1 + end subroutine expect_type4_literal_call_staged_until_fill + + subroutine expect_configured_type12_addressee_staged_until_fill() + character(len=77) :: c77 + character(len=37) :: decoded + character(len=13) :: call1, call2 + logical :: ok + + call pack_and_assert_type('W9XYZ K1ABC FN42',1,0,c77) + call clear_all_state('N0AAA','N0BBB') + call normalize_call('W9XYZ',call1) + call normalize_call('K1ABC',call2) + + call unpack_var_current_state_thread(c77,1,3,decoded,ok) + + call assert_decode('type 1 staged addressee decode', & + 'W9XYZ K1ABC FN42','W9XYZ K1ABC FN42',decoded,ok) + call assert_int('type 1 thread 3 queued hash count',2, & + nqueued_calls_by_thread(3)) + call assert_call('type 1 thread 3 queued first call', & + queued_calls_by_thread(thread_call_index(3)+1),'W9XYZ') + call assert_call('type 1 thread 3 queued second call', & + queued_calls_by_thread(thread_call_index(3)+2),'K1ABC') + call assert_true('type 1 addressee hash absent before fill', & + .not.shared_hash22_contains(call1)) + + call fillhashvar(3,.true.) + + call assert_true('type 1 addressee hash after fill', & + shared_hash22_contains(call1)) + call assert_true('type 1 second call hash after fill', & + shared_hash22_contains(call2)) + + ntests=ntests+1 + end subroutine expect_configured_type12_addressee_staged_until_fill + + subroutine expect_legacy_type12_addressee_not_hashed() + character(len=77) :: c77 + character(len=37) :: decoded + character(len=13) :: call1, call2 + integer :: n10a, n12a, n10b, n12b + logical :: ok + + call pack_and_assert_type('W9XYZ K1ABC FN42',1,0,c77) + call clear_all_state('N0AAA','N0BBB') + call normalize_call('W9XYZ',call1) + call normalize_call('K1ABC',call2) + n10a=ihashcall(call1,10) + n12a=ihashcall(call1,12) + n10b=ihashcall(call2,10) + n12b=ihashcall(call2,12) + + call unpack_standard_current_state(c77,1,decoded,ok) + + call assert_decode('legacy type 1 addressee decode', & + 'W9XYZ K1ABC FN42','W9XYZ K1ABC FN42',decoded,ok) + call assert_call('legacy type 1 recent sender',recent_calls(1),'K1ABC') + call assert_call('legacy type 1 recent addressee',recent_calls(2),'W9XYZ') + call assert_call('legacy type 1 addressee calls10',calls10(n10a),'') + call assert_call('legacy type 1 addressee calls12',calls12(n12a),'') + call assert_true('legacy type 1 addressee hash22 absent', & + .not.shared_hash22_contains(call1)) + call assert_call('legacy type 1 sender calls10',calls10(n10b),'K1ABC') + call assert_call('legacy type 1 sender calls12',calls12(n12b),'K1ABC') + call assert_true('legacy type 1 sender hash22 present', & + shared_hash22_contains(call2)) + + ntests=ntests+1 + end subroutine expect_legacy_type12_addressee_not_hashed + + subroutine expect_service_unpack_does_not_consume_dx_edge() + character(len=77) :: c77 + character(len=37) :: decoded + logical :: ok + + call pack_and_assert_type('K1ABC W9XYZ RR73',1,0,c77) + call clear_all_state('N0AAA','') + + mycall='N0AAA' + hiscall='KH1/KH7Z' + dxcall13='KH1/KH7Z ' + dxcall13_configured_prev=' ' + dxcall13_configured=' ' + dxcall13_configured_set=.false. + hashdx10_configured=-1 + + decoded=' ' + ok=.false. + call unpack77_configured(c77,1,decoded,ok, & + unpack77_options(record_hashes=.false.,record_recent_calls=.false.)) + + call assert_true('service unpack leaves recent queue empty', & + all(nqueued_recent_calls_by_thread(1:24).eq.0)) + call assert_call('service unpack leaves recent calls empty',recent_calls(1),'') + + call fillhashvar(1,.false.) + + call assert_int('service unpack leaves dx edge queued count',1, & + nqueued_calls_by_thread(1)) + call assert_call('service unpack leaves dx edge queued call', & + queued_calls_by_thread(thread_call_index(1)+1),'KH1/KH7Z') + call assert_true('service unpack dx configured set',dxcall13_configured_set) + call assert_call('service unpack dx configured',dxcall13_configured,'KH1/KH7Z') + call assert_call('service unpack dx configured prev',dxcall13_configured_prev, & + 'KH1/KH7Z') + call assert_int('service unpack dx configured hash', & + ihashcall(dxcall13_configured,10),hashdx10_configured) + + ntests=ntests+1 + end subroutine expect_service_unpack_does_not_consume_dx_edge + + subroutine expect_configured_call_clear_blanks_stale_state() + integer :: old_hash + + call clear_all_state('N0AAA','KH1/KH7Z') + mycall='N0AAA' + hiscall='KH1/KH7Z' + call fillhashvar(1,.false.) + + call assert_true('configured clear seeded dx',dxcall13_configured_set) + call assert_call('configured clear seeded dx call',dxcall13_configured, & + 'KH1/KH7Z') + old_hash=hashdx10_configured + + hiscall='' + call fillhashvar(1,.false.) + + call assert_true('configured clear dx flag',.not.dxcall13_configured_set) + call assert_call('configured clear dx configured blank',dxcall13_configured,'') + call assert_call('configured clear dx previous blank',dxcall13_configured_prev,'') + call assert_int('configured clear dx hash reset',-1,hashdx10_configured) + call assert_int('configured clear dx queue count',0,nqueued_calls_by_thread(1)) + call assert_true('configured clear changed dx hash',hashdx10_configured.ne.old_hash) + + call clear_all_state('KH1/KH7Z','N0BBB') + mycall='KH1/KH7Z' + hiscall='N0BBB' + call fillhashvar(1,.false.) + + call assert_true('configured clear seeded mycall',mycall13_configured_set) + call assert_call('configured clear seeded mycall value',mycall13_configured, & + 'KH1/KH7Z') + + mycall='' + call fillhashvar(1,.false.) + + call assert_true('configured clear mycall flag',.not.mycall13_configured_set) + call assert_call('configured clear mycall configured blank', & + mycall13_configured,'') + call assert_call('configured clear mycall previous blank', & + mycall13_configured_prev,'') + call assert_int('configured clear mycall hash10 reset',-1,hashmy10_configured) + call assert_int('configured clear mycall hash12 reset',-1,hashmy12_configured) + call assert_int('configured clear mycall hash22 reset',-1,hashmy22_configured) + + ntests=ntests+1 + end subroutine expect_configured_call_clear_blanks_stale_state + + subroutine expect_dxpedition_dxcall_reentry_substitution() + character(len=77) :: c77 + character(len=37) :: decoded + logical :: ok + + call pack_and_assert_type('K1ABC RR73; W9XYZ <KH1/KH7Z> -12',0,1,c77) + call clear_all_state('N0AAA','') + + mycall='N0AAA' + hiscall='KH1/KH7Z' + call fillhashvar(1,.false.) + call unpack_var_current_state(c77,1,decoded,ok) + call assert_decode('DXpedition dx configured first entry', & + 'K1ABC RR73; W9XYZ <KH1/KH7Z> -12', & + 'K1ABC RR73; W9XYZ <KH1/KH7Z> -12',decoded,ok) + + hiscall='' + call fillhashvar(1,.false.) + call assert_true('DXpedition dx cleared',.not.dxcall13_configured_set) + + hiscall='KH1/KH7Z' + call fillhashvar(1,.false.) + call unpack_var_current_state(c77,1,decoded,ok) + call assert_decode('DXpedition dx configured re-entry', & + 'K1ABC RR73; W9XYZ <KH1/KH7Z> -12', & + 'K1ABC RR73; W9XYZ <KH1/KH7Z> -12',decoded,ok) + + ntests=ntests+1 + end subroutine expect_dxpedition_dxcall_reentry_substitution + + subroutine expect_shared_hash_tables_survive_mode_sync() + character(len=13) :: c13 + integer :: n10, n12, n22 + + call clear_all_state('N0AAA','KH1/KH7Z') + + call normalize_call('PJ4/K1ABC',c13) + call save_hash_call(c13,n10,n12,n22) + call assert_call('persistent hash calls10 seeded',calls10(n10),'PJ4/K1ABC') + call assert_true('persistent hash calls22 seeded',shared_hash22_contains(c13)) + + mycall='W1AW' + hiscall='W9XYZ' + call fillhashvar(1,.false.) + + call assert_call('persistent hash calls10 after mode sync', & + calls10(n10),'PJ4/K1ABC') + call assert_call('persistent hash calls12 after mode sync', & + calls12(n12),'PJ4/K1ABC') + call assert_true('persistent hash calls22 after mode sync', & + shared_hash22_contains(c13)) + + ntests=ntests+1 + end subroutine expect_shared_hash_tables_survive_mode_sync + + subroutine expect_default_pack77_tx_hash_recording() + character(len=37) :: input, packed_input, decoded + character(len=77) :: c77 + character(len=13) :: call1, call2, call12, call22, forced_call + integer :: n10a, n12a, n10b, n12b, got_i3, got_n3 + integer :: forced_n10, forced_n12, forced_n22 + logical :: ok + + input='K1ABC W9XYZ RR73' + packed_input=' ' + packed_input=input + call normalize_call('K1ABC',call1) + call normalize_call('W9XYZ',call2) + n10a=ihashcall(call1,10) + n12a=ihashcall(call1,12) + n10b=ihashcall(call2,10) + n12b=ihashcall(call2,12) + + call clear_all_state('N0AAA','N0BBB') + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(packed_input) + call assert_pack77_result('default Type 1 tx hashes',packed_input,encoded) + got_i3=encoded%i3 + got_n3=encoded%n3 + end block + call assert_message_type('default Type 1 tx hashes',input,1,0,got_i3,got_n3) + call assert_call('default Type 1 calls10 first call',calls10(n10a),'K1ABC') + call assert_call('default Type 1 calls12 first call',calls12(n12a),'K1ABC') + call assert_call('default Type 1 calls10 second call',calls10(n10b),'W9XYZ') + call assert_call('default Type 1 calls12 second call',calls12(n12b),'W9XYZ') + call assert_int('default Type 1 nzhash after pack',2,nzhash) + call assert_true('default Type 1 hash22 first call',shared_hash22_contains(call1)) + call assert_true('default Type 1 hash22 second call',shared_hash22_contains(call2)) + + input='PJ2/W1AW <W7ABC> RR73' + packed_input=' ' + packed_input=input + call normalize_call('W7ABC',call12) + n10a=ihashcall(call12,10) + n12a=ihashcall(call12,12) + + call clear_all_state('N0AAA','N0BBB') + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(packed_input) + call assert_pack77_result('default Type 4 tx hashes',packed_input,encoded) + got_i3=encoded%i3 + got_n3=encoded%n3 + end block + call assert_message_type('default Type 4 tx hashes',input,4,0,got_i3,got_n3) + call assert_call('default Type 4 calls10 hashed call',calls10(n10a),'W7ABC') + call assert_call('default Type 4 calls12 hashed call',calls12(n12a),'W7ABC') + call assert_int('default Type 4 nzhash after pack',1,nzhash) + call assert_true('default Type 4 hash22 hashed call',shared_hash22_contains(call12)) + + input='<W3CCX> <K1JT/P> 590001 FN20QI' + packed_input=' ' + packed_input=input + call normalize_call('W3CCX',call12) + call normalize_call('K1JT/P',call22) + n10a=ihashcall(call12,10) + n12a=ihashcall(call12,12) + n10b=ihashcall(call22,10) + n12b=ihashcall(call22,12) + + call clear_all_state('N0AAA','N0BBB') + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(packed_input) + call assert_pack77_result('default pack77 tx hashes',packed_input,encoded) + got_i3=encoded%i3 + got_n3=encoded%n3 + c77=encoded%c77 + end block + call assert_message_type('default pack77 tx hashes',input,5,0,got_i3,got_n3) + call assert_call('default tx calls10 first hashed call',calls10(n10a), & + 'W3CCX') + call assert_call('default tx calls12 first hashed call',calls12(n12a), & + 'W3CCX') + call assert_call('default tx calls10 second hashed call',calls10(n10b), & + 'K1JT/P') + call assert_call('default tx calls12 second hashed call',calls12(n12b), & + 'K1JT/P') + call assert_int('default tx nzhash after pack',2,nzhash) + call assert_true('default tx hash22 second hashed call', & + shared_hash22_contains(call22)) + + decoded=' ' + ok=.false. + call unpack77(c77,0,decoded,ok) + call assert_decode('default tx decode uses recorded hashes',input,input,decoded,ok) + + input='<PJ4/K1ABC> FK52AB' + packed_input=' ' + packed_input=input + call normalize_call('PJ4/K1ABC',forced_call) + forced_n10=ihashcall(forced_call,10) + forced_n12=ihashcall(forced_call,12) + forced_n22=ihashcall(forced_call,22) + + call clear_all_state('N0AAA','N0BBB') + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(packed_input,pack77_options(prefer_wspr_50bit=.true.)) + call assert_pack77_result('prefer WSPR tx hashes',packed_input,encoded) + got_i3=encoded%i3 + got_n3=encoded%n3 + c77=encoded%c77 + end block + call assert_message_type('prefer WSPR tx hashes',input,0,6,got_i3,got_n3) + call assert_call('prefer WSPR calls10',calls10(forced_n10),'PJ4/K1ABC') + call assert_call('prefer WSPR calls12',calls12(forced_n12),'PJ4/K1ABC') + call assert_int('prefer WSPR hash22',forced_n22,ihash22(1)) + call assert_call('prefer WSPR calls22',calls22(1),'PJ4/K1ABC') + + decoded=' ' + ok=.false. + call unpack77(c77,0,decoded,ok) + call assert_decode('prefer WSPR decode uses recorded hashes',input,input,decoded,ok) + + ntests=ntests+1 + end subroutine expect_default_pack77_tx_hash_recording + + subroutine expect_pack77_record_tx_hashes_option_suppresses_commit() + character(len=37) :: input, packed_input + + input='K1ABC W9XYZ RR73' + call assert_record_tx_disabled_same_payload(input,1,0) + + input='PJ2/W1AW <W7ABC> RR73' + call assert_record_tx_disabled_same_payload(input,4,0) + + input='<W3CCX> <K1JT/P> 590001 FN20QI' + call assert_record_tx_disabled_same_payload(input,5,0) + + input='<PJ4/K1ABC> FK52AB' + packed_input=' ' + packed_input=input + call clear_all_state('N0AAA','N0BBB') + block + type(pack77_result) :: default_encoded, disabled_encoded + default_encoded=pack77_result_from_api(packed_input,pack77_options(prefer_wspr_50bit=.true.)) + call assert_pack77_result('default preferred WSPR payload',packed_input, & + default_encoded) + call clear_all_state('N0AAA','N0BBB') + disabled_encoded=pack77_result_from_api(packed_input, & + pack77_options(prefer_wspr_50bit=.true.,record_tx_hashes=.false.)) + call assert_pack77_result('disabled preferred WSPR payload',packed_input, & + disabled_encoded) + call assert_text_equal('disabled preferred WSPR c77',default_encoded%c77, & + disabled_encoded%c77) + call assert_message_type('disabled preferred WSPR type',input,0,6, & + disabled_encoded%i3,disabled_encoded%n3) + end block + call assert_shared_hash_tables_empty('disabled preferred WSPR hashes') + + ntests=ntests+1 + end subroutine expect_pack77_record_tx_hashes_option_suppresses_commit + + subroutine expect_pack77_encode_rejects_dx_macro_by_default() + character(len=37) :: shorthand + + shorthand='$DX K1ABC FN42' + call clear_all_state('N0AAA','N0BBB') + + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(shorthand) + call assert_true('pure Pack77 encode rejects DX macro', & + .not.encoded%encoded) + end block + + ntests=ntests+1 + end subroutine expect_pack77_encode_rejects_dx_macro_by_default + + subroutine expect_pack77_free_text_fallback_rejects_unexpanded_dx_macro() + character(len=37) :: input, direct + + direct='W9XYZ K1ABC FN42' + call clear_all_state('N0AAA','N0BBB') + + input='$DX K1ABC FN42' + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(input) + call assert_true('Pack77 free-text fallback rejects DX macro', & + .not.encoded%encoded) + end block + + input='$dx K1ABC FN42' + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(input) + call assert_true('Pack77 free-text fallback rejects lowercase DX macro', & + .not.encoded%encoded) + end block + + input=' $DX K1ABC FN42' + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(input) + call assert_true('Pack77 free-text fallback rejects leading-space DX macro', & + .not.encoded%encoded) + end block + + input='$DXCALL K1ABC FN42' + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(input) + call assert_true('Pack77 free-text fallback rejects DXCALL macro', & + .not.encoded%encoded) + end block + + input='$dxcall K1ABC FN42' + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(input) + call assert_true('Pack77 free-text fallback rejects lowercase DXCALL macro', & + .not.encoded%encoded) + end block + + input='$DXFOO K1ABC FN42' + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(input) + call assert_true('Pack77 free-text fallback rejects invalid DX prefix', & + .not.encoded%encoded) + call assert_true('Pack77 free-text fallback invalid DX prefix status', & + encoded%status.eq.PACK77_STATUS_FREE_TEXT_INVALID) + end block + + input='$DX K1ABC FN42' + block + type(pack77_result) :: shorthand_encoded, direct_encoded + shorthand_encoded=pack77_result_from_api(input, & + pack77_options(expand_dx_macro=.true.,dx_macro_base='W9XYZ')) + direct_encoded=pack77_result_from_api(direct) + call assert_pack77_result('Pack77 free-text fallback explicit DX expansion',input, & + shorthand_encoded) + call assert_text_equal('Pack77 free-text fallback explicit DX expansion c77', & + direct_encoded%c77,shorthand_encoded%c77) + end block + + ntests=ntests+1 + end subroutine expect_pack77_free_text_fallback_rejects_unexpanded_dx_macro + + subroutine expect_pack_unpack_hash_state_matches_pack_only() + call assert_pack_unpack_hash_state_matches_pack_only( & + 'K1ABC RR73; W9XYZ <KH1/KH7Z> -12') + call assert_pack_unpack_hash_state_matches_pack_only('W1AW/P K1A 1A CT') + call assert_pack_unpack_hash_state_matches_pack_only('PJ2/W1AW <W7ABC> RR73') + call assert_pack_unpack_hash_state_matches_pack_only( & + '<W3CCX> <K1JT/P> 590001 FN20QI') + + ntests=ntests+1 + end subroutine expect_pack_unpack_hash_state_matches_pack_only + + subroutine expect_genft8_failure_state() + character(len=37) :: input, msgsent + integer*1 :: msgbits(77) + integer :: itone(79) + integer :: i3,n3 + + input='$DX K1ABC FN42' + call genft8(input,i3,n3,msgsent,msgbits,itone) + + call assert_int('genft8 failure i3',-1,i3) + call assert_int('genft8 failure n3',-1,n3) + call assert_text_equal('genft8 failure msgsent', & + '*** bad message *** ',msgsent) + call assert_true('genft8 failure msgbits',all(msgbits.eq.0)) + call assert_true('genft8 failure itone',all(itone.eq.0)) + + ntests=ntests+1 + end subroutine expect_genft8_failure_state + + subroutine expect_dx_macro_option_uses_explicit_base() + character(len=37) :: shorthand, direct, decoded + logical :: ok + + shorthand='$DX K1ABC FN42' + direct='W9XYZ K1ABC FN42' + call clear_all_state('N0AAA','N0BBB') + + block + type(pack77_result) :: shorthand_encoded, direct_encoded + shorthand_encoded=pack77_result_from_api(shorthand, & + pack77_options(expand_dx_macro=.true.,dx_macro_base='W9XYZ')) + direct_encoded=pack77_result_from_api(direct) + call assert_pack77_result('DX rewrite shorthand',shorthand, & + shorthand_encoded) + call assert_pack77_result('DX rewrite direct',direct,direct_encoded) + call assert_message_type('DX rewrite type',shorthand,1,0, & + shorthand_encoded%i3,shorthand_encoded%n3) + call assert_text_equal('DX rewrite c77',direct_encoded%c77, & + shorthand_encoded%c77) + + decoded=' ' + ok=.false. + call unpack77(shorthand_encoded%c77,0,decoded,ok) + call assert_decode('DX rewrite decode',shorthand,direct,decoded,ok) + end block + + shorthand='$DXCALL K1ABC FN42' + block + type(pack77_result) :: shorthand_encoded, direct_encoded + shorthand_encoded=pack77_result_from_api(shorthand, & + pack77_options(expand_dx_macro=.true.,dx_macro_base='W9XYZ')) + direct_encoded=pack77_result_from_api(direct) + call assert_pack77_result('DXCALL rewrite shorthand',shorthand, & + shorthand_encoded) + call assert_text_equal('DXCALL rewrite c77',direct_encoded%c77, & + shorthand_encoded%c77) + end block + + shorthand='$dx K1ABC FN42' + block + type(pack77_result) :: shorthand_encoded, direct_encoded + shorthand_encoded=pack77_result_from_api(shorthand, & + pack77_options(expand_dx_macro=.true.,dx_macro_base='W9XYZ')) + direct_encoded=pack77_result_from_api(direct) + call assert_pack77_result('lowercase DX rewrite shorthand',shorthand, & + shorthand_encoded) + call assert_text_equal('lowercase DX rewrite c77',direct_encoded%c77, & + shorthand_encoded%c77) + end block + + shorthand=' $DX K1ABC FN42' + block + type(pack77_result) :: shorthand_encoded, direct_encoded + shorthand_encoded=pack77_result_from_api(shorthand, & + pack77_options(expand_dx_macro=.true.,dx_macro_base='W9XYZ')) + direct_encoded=pack77_result_from_api(direct) + call assert_pack77_result('DX rewrite leading whitespace',shorthand, & + shorthand_encoded) + call assert_text_equal('DX rewrite leading whitespace c77', & + direct_encoded%c77,shorthand_encoded%c77) + end block + + ntests=ntests+1 + end subroutine expect_dx_macro_option_uses_explicit_base + + subroutine expect_failed_pack77_candidates_do_not_record_hashes() + call assert_rejected_candidate_does_not_record('W9XYZ K1ABC R BAD MA') + call assert_rejected_candidate_does_not_record('<PJ2/W1AW> K1ABC BOGUS') + call assert_rejected_candidate_does_not_record('CQ PJ2/W1AW BOGUS') + call assert_rejected_candidate_does_not_record('<PJ4/K1ABC>X W9XYZ RR73') + call assert_rejected_candidate_does_not_record( & + 'K1ABC RR73; W9XYZ <KH1/KH7Z>X -12') + call assert_rejected_candidate_does_not_record( & + '<W3CCX <K1JT/P> 590001 FN20QI') + call assert_rejected_candidate_does_not_record( & + '<W3CCX> <K1JT/P 590001 FN20QI') + + ntests=ntests+1 + end subroutine expect_failed_pack77_candidates_do_not_record_hashes + + subroutine expect_ft8apsetvar_does_not_record_template_hashes() + logical(1) :: lmycallstd, lhiscallstd + + call clear_all_state('N0AAA','PJ2/W1AW') + mycall='N0AAA ' + hiscall='PJ2/W1AW ' + mybcall='N0AAA ' + hisbcall='PJ2/W1AW ' + hisgrid4=' ' + lhound=.false. + lmycallstd=.true. + lhiscallstd=.false. + + call ft8apsetvar(lmycallstd,lhiscallstd,1) + call assert_shared_hash_tables_empty('ft8apsetvar template hashes') + + ntests=ntests+1 + end subroutine expect_ft8apsetvar_does_not_record_template_hashes + + subroutine expect_var_tx_rx_separation() + character(len=37) :: input + character(len=77) :: c77 + character(len=37) :: decoded + character(len=13) :: call12, call22 + integer :: n10a, n12a, n10b, n12b + logical :: ok + + input='<W3CCX> <K1JT/P> 590001 FN20QI' + call normalize_call('W3CCX',call12) + call normalize_call('K1JT/P',call22) + n10a=ihashcall(call12,10) + n12a=ihashcall(call12,12) + n10b=ihashcall(call22,10) + n12b=ihashcall(call22,12) + + call clear_all_state('N0AAA','N0BBB') + ! record_tx_hashes writes accepted pack hashes into the shared lookup table. + call pack_var_current_state(input,5,0,1,c77) + + call assert_call('var tx calls10 first hashed call',calls10(n10a), & + 'W3CCX') + call assert_call('var tx calls12 first hashed call',calls12(n12a), & + 'W3CCX') + call assert_call('var tx calls10 second hashed call',calls10(n10b), & + 'K1JT/P') + call assert_call('var tx calls12 second hashed call',calls12(n12b), & + 'K1JT/P') + call assert_int('var tx nzhash after pack',2,nzhash) + call assert_true('var tx hash22 second hashed call', & + shared_hash22_contains(call22)) + + call unpack_var_current_state_thread(c77,1,26,decoded,ok) + call assert_decode('var tx-state decode',input,input,decoded,ok) + call unpack_var_current_state_thread(c77,1,1,decoded,ok) + call assert_decode('var shared-state decodes tx hashes',input,input,decoded,ok) + + call clear_all_state('N0AAA','N0BBB') + call pack_var_current_state(input,5,0,0,c77) + + call assert_int('var tx nzhash disabled',0,nzhash) + call assert_call('var tx disabled calls10 first',calls10(n10a),'') + call assert_call('var tx disabled calls12 first',calls12(n12a),'') + call assert_call('var tx disabled calls10 second',calls10(n10b),'') + call assert_call('var tx disabled calls12 second',calls12(n12b),'') + call assert_true('var tx disabled hash22 empty', & + .not.shared_hash22_contains(call22)) + + call unpack_var_current_state_thread(c77,1,26,decoded,ok) + call assert_decode('var tx-state disabled decode',input, & + '<...> <...> 590001 FN20QI',decoded,ok) + + ntests=ntests+1 + end subroutine expect_var_tx_rx_separation + + subroutine expect_mycall_dxcall_substitutions() + character(len=77) :: c77 + character(len=37) :: decoded_standard, decoded_var + logical :: ok_standard, ok_var + + ! These decode paths substitute local my/dx calls without hash-table priming. + call pack_and_assert_type('<PJ4/K1ABC> W9XYZ RR73',1,0,c77) + call clear_all_state('PJ4/K1ABC','N0BBB') + call unpack_standard_current_state(c77,1,decoded_standard,ok_standard) + call clear_all_state('PJ4/K1ABC','N0BBB') + call unpack_var_current_state(c77,1,decoded_var,ok_var) + call assert_decode('standard Type 1 mycall hash22','<PJ4/K1ABC> W9XYZ RR73', & + '<PJ4/K1ABC> W9XYZ RR73',decoded_standard,ok_standard) + call assert_decode('var Type 1 mycall hash22','<PJ4/K1ABC> W9XYZ RR73', & + '<PJ4/K1ABC> W9XYZ RR73',decoded_var,ok_var) + + call pack_and_assert_type('K1ABC RR73; W9XYZ <KH1/KH7Z> -12',0,1,c77) + call clear_all_state('N0AAA','KH1/KH7Z') + call unpack_standard_current_state(c77,1,decoded_standard,ok_standard) + call clear_all_state('N0AAA','KH1/KH7Z') + call unpack_var_current_state(c77,1,decoded_var,ok_var) + call assert_decode('standard DXpedition dxcall hash10', & + 'K1ABC RR73; W9XYZ <KH1/KH7Z> -12', & + 'K1ABC RR73; W9XYZ <KH1/KH7Z> -12',decoded_standard,ok_standard) + call assert_decode('var DXpedition dxcall hash10', & + 'K1ABC RR73; W9XYZ <KH1/KH7Z> -12', & + 'K1ABC RR73; W9XYZ <KH1/KH7Z> -12',decoded_var,ok_var) + + call clear_all_state('KH1/KH7Z','N0BBB') + call unpack_standard_current_state(c77,0,decoded_standard,ok_standard) + call clear_all_state('KH1/KH7Z','N0BBB') + call unpack_var_current_state(c77,0,decoded_var,ok_var) + call assert_decode('standard DXpedition mycall hash10', & + 'K1ABC RR73; W9XYZ <KH1/KH7Z> -12', & + 'K1ABC RR73; W9XYZ <KH1/KH7Z> -12',decoded_standard,ok_standard) + call assert_decode('var DXpedition mycall hash10', & + 'K1ABC RR73; W9XYZ <KH1/KH7Z> -12', & + 'K1ABC RR73; W9XYZ <KH1/KH7Z> -12',decoded_var,ok_var) + + call pack_and_assert_type('<W3CCX> <K1JT/P> 590001 FN20QI',5,0,c77) + call clear_all_state('W3CCX','N0BBB') + call unpack_standard_current_state(c77,1,decoded_standard,ok_standard) + call clear_all_state('W3CCX','N0BBB') + call unpack_var_current_state(c77,1,decoded_var,ok_var) + call assert_decode('standard Type 5 mycall hash12', & + '<W3CCX> <K1JT/P> 590001 FN20QI', & + '<W3CCX> <...> 590001 FN20QI',decoded_standard,ok_standard) + call assert_decode('var Type 5 mycall hash12', & + '<W3CCX> <K1JT/P> 590001 FN20QI', & + '<W3CCX> <...> 590001 FN20QI',decoded_var,ok_var) + + ntests=ntests+1 + end subroutine expect_mycall_dxcall_substitutions + + subroutine expect_invalid_standard_call_decode_failure() + character(len=13) :: decoded_standard, decoded_var + integer, parameter :: n28_q1abc = 11395945 + logical :: ok_standard, ok_var + + call unpack28(n28_q1abc,decoded_standard,ok_standard) + call unpack28(n28_q1abc,decoded_var,ok_var) + + call assert_true('standard invalid raw call fails',.not.ok_standard) + call assert_true('var invalid raw call fails',.not.ok_var) + + ntests=ntests+1 + end subroutine expect_invalid_standard_call_decode_failure + + subroutine expect_hash_resolution_case(input,want_i3,want_n3,blank_expected, & + primed_expected,width1,call1,width2,call2,width3,call3) + character(len=*), intent(in) :: input, blank_expected, primed_expected + character(len=*), intent(in) :: call1, call2, call3 + integer, intent(in) :: want_i3, want_n3, width1, width2, width3 + character(len=77) :: c77 + character(len=37) :: decoded_standard, decoded_var + logical :: ok_standard, ok_var + + call pack_and_assert_type(input,want_i3,want_n3,c77) + + call decode_standard(c77,1,decoded_standard,ok_standard) + call decode_var(c77,1,decoded_var,ok_var) + call assert_decode('standard blank',input,blank_expected,decoded_standard,ok_standard) + call assert_decode('var blank',input,blank_expected,decoded_var,ok_var) + call assert_text_equal('blank standard/var',decoded_standard,decoded_var) + + call prime_hash_width(width1,call1,'standard') + call prime_hash_width(width2,call2,'standard') + call prime_hash_width(width3,call3,'standard') + call unpack_standard_current_state(c77,1,decoded_standard,ok_standard) + + call prime_hash_width(width1,call1,'var') + call prime_hash_width(width2,call2,'var') + call prime_hash_width(width3,call3,'var') + call unpack_var_current_state(c77,1,decoded_var,ok_var) + + call assert_decode('standard primed',input,primed_expected,decoded_standard,ok_standard) + call assert_decode('var primed',input,primed_expected,decoded_var,ok_var) + call assert_text_equal('primed standard/var',decoded_standard,decoded_var) + + ntests=ntests+1 + end subroutine expect_hash_resolution_case + + subroutine pack_and_assert_type(input,want_i3,want_n3,c77) + character(len=*), intent(in) :: input + integer, intent(in) :: want_i3, want_n3 + character(len=77), intent(out) :: c77 + character(len=37) :: packed_input + integer :: got_i3, got_n3 + + call clear_all_state('N0AAA','N0BBB') + packed_input=' ' + packed_input=input + got_i3=-1 + got_n3=-1 + c77='' + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(packed_input) + call assert_pack77_result('pack77',packed_input,encoded) + got_i3=encoded%i3 + got_n3=encoded%n3 + c77=encoded%c77 + end block + call assert_message_type('pack77',input,want_i3,want_n3,got_i3,got_n3) + end subroutine pack_and_assert_type + + subroutine decode_standard(c77,nrx,decoded,ok) + character(len=77), intent(in) :: c77 + integer, intent(in) :: nrx + character(len=37), intent(out) :: decoded + logical, intent(out) :: ok + + call clear_all_state('N0AAA','N0BBB') + call unpack_standard_current_state(c77,nrx,decoded,ok) + end subroutine decode_standard + + subroutine decode_var(c77,nrx,decoded,ok) + character(len=77), intent(in) :: c77 + integer, intent(in) :: nrx + character(len=37), intent(out) :: decoded + logical, intent(out) :: ok + + call clear_all_state('N0AAA','N0BBB') + call unpack_var_current_state(c77,nrx,decoded,ok) + end subroutine decode_var + + subroutine unpack_standard_current_state(c77,nrx,decoded,ok) + character(len=77), intent(in) :: c77 + integer, intent(in) :: nrx + character(len=37), intent(out) :: decoded + logical, intent(out) :: ok + + decoded=' ' + ok=.false. + call unpack77(c77,nrx,decoded,ok) + end subroutine unpack_standard_current_state + + subroutine unpack_var_current_state(c77,nrx,decoded,ok) + character(len=77), intent(in) :: c77 + integer, intent(in) :: nrx + character(len=37), intent(out) :: decoded + logical, intent(out) :: ok + + call unpack_var_current_state_thread(c77,nrx,1,decoded,ok) + end subroutine unpack_var_current_state + + subroutine unpack_var_current_state_thread(c77,nrx,nthr,decoded,ok) + character(len=77), intent(in) :: c77 + integer, intent(in) :: nrx, nthr + character(len=37), intent(out) :: decoded + logical, intent(out) :: ok + + decoded=' ' + ok=.false. + call unpack77_configured(c77,nrx,decoded,ok,unpack77_options(thread_index=nthr)) + end subroutine unpack_var_current_state_thread + + subroutine pack_var_current_state(input,want_i3,want_n3,ntxhash,c77) + character(len=*), intent(in) :: input + integer, intent(in) :: want_i3, want_n3, ntxhash + character(len=77), intent(out) :: c77 + character(len=37) :: packed_input + integer :: got_i3, got_n3 + + packed_input=' ' + packed_input=input + got_i3=-1 + got_n3=-1 + c77='' + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(packed_input,pack77_options(record_tx_hashes=ntxhash.eq.1)) + call assert_pack77_result('pack77',packed_input,encoded) + got_i3=encoded%i3 + got_n3=encoded%n3 + c77=encoded%c77 + end block + call assert_message_type('pack77',input,want_i3,want_n3,got_i3,got_n3) + end subroutine pack_var_current_state + + subroutine assert_record_tx_disabled_same_payload(input,want_i3,want_n3) + character(len=*), intent(in) :: input + integer, intent(in) :: want_i3, want_n3 + character(len=37) :: packed_input + + packed_input=' ' + packed_input=input + call clear_all_state('N0AAA','N0BBB') + block + type(pack77_result) :: default_encoded, disabled_encoded + default_encoded=pack77_result_from_api(packed_input) + call assert_pack77_result('default record_tx payload',packed_input, & + default_encoded) + call clear_all_state('N0AAA','N0BBB') + disabled_encoded=pack77_result_from_api(packed_input,pack77_options(record_tx_hashes=.false.)) + call assert_pack77_result('disabled record_tx payload',packed_input, & + disabled_encoded) + call assert_text_equal('disabled record_tx c77',default_encoded%c77, & + disabled_encoded%c77) + call assert_message_type('disabled record_tx type',input,want_i3,want_n3, & + disabled_encoded%i3,disabled_encoded%n3) + end block + call assert_shared_hash_tables_empty('disabled record_tx hashes') + end subroutine assert_record_tx_disabled_same_payload + + subroutine assert_rejected_candidate_does_not_record(input) + character(len=*), intent(in) :: input + character(len=37) :: packed_input + + packed_input=' ' + packed_input=input + call clear_all_state('N0AAA','N0BBB') + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(packed_input) + if(encoded%encoded) then + call assert_message_type('rejected candidate fallback type',input,0,0, & + encoded%i3,encoded%n3) + endif + end block + call assert_shared_hash_tables_empty('rejected candidate hashes') + end subroutine assert_rejected_candidate_does_not_record + + subroutine assert_pack_unpack_hash_state_matches_pack_only(input) + character(len=*), intent(in) :: input + character(len=37) :: packed_input, decoded + character(len=77) :: c77 + character(len=13) :: calls10_after_unpack(0:1023) + character(len=13) :: calls12_after_unpack(0:4095) + character(len=13) :: calls22_after_unpack(1:MAXHASH) + integer :: ihash22_after_unpack(1:MAXHASH) + integer :: nzhash_after_unpack + integer :: i3,n3 + logical :: ok + + packed_input=' ' + packed_input=input + + call clear_all_state('N0AAA','N0BBB') + i3=-1 + n3=-1 + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(packed_input) + i3=encoded%i3 + n3=encoded%n3 + c77=encoded%c77 + end block + call assert_true('pack/unpack hash-state packed '//trim(input),i3.ge.0) + decoded=' ' + ok=.false. + call unpack77(c77,0,decoded,ok) + call assert_true('pack/unpack hash-state unpacked '//trim(input),ok) + nzhash_after_unpack=nzhash + calls10_after_unpack=calls10 + calls12_after_unpack=calls12 + calls22_after_unpack=calls22 + ihash22_after_unpack=ihash22 + + call clear_all_state('N0AAA','N0BBB') + i3=-1 + n3=-1 + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(packed_input) + i3=encoded%i3 + n3=encoded%n3 + c77=encoded%c77 + end block + call assert_true('pack-only hash-state packed '//trim(input),i3.ge.0) + + call assert_int('pack/unpack nzhash '//trim(input),nzhash_after_unpack,nzhash) + call assert_true('pack/unpack calls10 '//trim(input), & + all(calls10_after_unpack.eq.calls10)) + call assert_true('pack/unpack calls12 '//trim(input), & + all(calls12_after_unpack.eq.calls12)) + call assert_true('pack/unpack calls22 '//trim(input), & + all(calls22_after_unpack.eq.calls22)) + call assert_true('pack/unpack ihash22 '//trim(input), & + all(ihash22_after_unpack.eq.ihash22)) + end subroutine assert_pack_unpack_hash_state_matches_pack_only + + subroutine prime_hash_width(width,callsign,label) + integer, intent(in) :: width + character(len=*), intent(in) :: callsign, label + character(len=13) :: c13 + integer :: n + + if(width.le.0 .or. len_trim(callsign).le.0) return + call normalize_call(callsign,c13) + if(len_trim(c13).le.0) return + n=ihashcall(c13,width) + if(width.eq.10) then + calls10(n)=c13 + else if(width.eq.12) then + calls12(n)=c13 + else if(width.eq.22) then + if(nzhash.lt.MAXHASH) nzhash=nzhash+1 + ihash22(nzhash)=n + calls22(nzhash)=c13 + else + write(*,1010) trim(label), width +1010 format('Unsupported ',a,' hash width ',i0) + error stop 1 + endif + end subroutine prime_hash_width + + logical function shared_hash22_contains(callsign) + character(len=*), intent(in) :: callsign + character(len=13) :: c13 + integer :: n22 + + call normalize_call(callsign,c13) + n22=ihashcall(c13,22) + shared_hash22_contains=any(ihash22(1:nzhash).eq.n22) + end function shared_hash22_contains + +end program test_packjt77_hash_state diff --git a/tests/unit/packjt77/test_packjt77_invariants.f90 b/tests/unit/packjt77/test_packjt77_invariants.f90 new file mode 100644 index 00000000..3fd14493 --- /dev/null +++ b/tests/unit/packjt77/test_packjt77_invariants.f90 @@ -0,0 +1,150 @@ +program test_packjt77_invariants + + use packjt77_test_helpers + implicit none + + integer :: ntests + external :: genft8 + external :: genft4 + external :: genfst4 + external :: genmsk_128_90 + external :: genq65 + + ntests=0 + + call run_packjt77_common_invariants(.false.,ntests) + call run_packjt77_standard_schema_invariants(ntests) + call expect_callok_suffix_bounds(ntests) + call expect_tx_generators_use_legacy_fallback(ntests) + call expect_tx_generators_reject_invalid_fallback(ntests) + call expect_fst4w_keeps_preferred_wspr_strict(ntests) + + write(*,1000) ntests +1000 format('packjt77 invariant tests passed: ',i0) + +contains + + subroutine expect_callok_suffix_bounds(ntests) + integer, intent(inout) :: ntests + logical :: callok + + call assert_true('callok accepts normal suffix length',callok('K1ABC')) + call assert_true('callok rejects overlength suffix',.not.callok('N0CALL')) + + ntests=ntests+1 + end subroutine expect_callok_suffix_bounds + + subroutine expect_tx_generators_use_legacy_fallback(ntests) + integer, intent(inout) :: ntests + character(len=37) :: input, msgsent + integer :: i3, n3, iwspr, itype + integer :: itone79(79), itone85(85), itone105(105), itone144(144) + integer :: itone160(160) + integer*1 :: msgbits77(77), msgbits101(101) + + input='ABCDEFGHIJKLMN' + + call genft8(input,i3,n3,msgsent,msgbits77,itone79) + call assert_int('FT8 legacy fallback i3',0,i3) + call assert_int('FT8 legacy fallback n3',0,n3) + call assert_text_equal('FT8 legacy fallback message', & + 'ABCDEFGHIJKLM',msgsent) + call assert_true('FT8 legacy fallback tones',any(itone79.ne.0)) + + call genft4(input,0,msgsent,msgbits77,itone105) + call assert_text_equal('FT4 legacy fallback message', & + 'ABCDEFGHIJKLM',msgsent) + call assert_true('FT4 legacy fallback bits',any(msgbits77.ne.0)) + call assert_true('FT4 legacy fallback tones',any(itone105.ne.0)) + + iwspr=0 + call genfst4(input,0,msgsent,msgbits101,itone160,iwspr) + call assert_text_equal('FST4 legacy fallback message', & + 'ABCDEFGHIJKLM',msgsent) + call assert_int('FST4 legacy fallback iwspr',0,iwspr) + call assert_true('FST4 legacy fallback bits',any(msgbits101.ne.0)) + call assert_true('FST4 legacy fallback tones',any(itone160.ne.0)) + + itype=1 + call genmsk_128_90(input,0,msgsent,itone144,itype) + call assert_int('MSK144 legacy fallback type',1,itype) + call assert_text_equal('MSK144 legacy fallback message', & + 'ABCDEFGHIJKLM',msgsent) + call assert_true('MSK144 legacy fallback tones',any(itone144.ne.0)) + + call genq65(input,0,msgsent,itone85,i3,n3,0) + call assert_int('Q65 legacy fallback i3',0,i3) + call assert_int('Q65 legacy fallback n3',0,n3) + call assert_text_equal('Q65 legacy fallback message', & + 'ABCDEFGHIJKLM',msgsent) + call assert_true('Q65 legacy fallback tones',any(itone85.ne.0)) + + ntests=ntests+1 + end subroutine expect_tx_generators_use_legacy_fallback + + subroutine expect_fst4w_keeps_preferred_wspr_strict(ntests) + integer, intent(inout) :: ntests + character(len=37) :: input, msgsent + integer :: iwspr + integer :: itone160(160) + integer*1 :: msgbits101(101) + + input='PJ4/K1ABC FK52A!' + iwspr=1 + msgsent=' ' + msgbits101=1 + itone160=1 + + call genfst4(input,0,msgsent,msgbits101,itone160,iwspr) + + call assert_int('FST4W strict iwspr',1,iwspr) + call assert_text_equal('FST4W strict message','*** bad message ***', & + msgsent) + call assert_true('FST4W strict bits',all(msgbits101.eq.0)) + call assert_true('FST4W strict tones',all(itone160.eq.0)) + + ntests=ntests+1 + end subroutine expect_fst4w_keeps_preferred_wspr_strict + + subroutine expect_tx_generators_reject_invalid_fallback(ntests) + integer, intent(inout) :: ntests + character(len=37) :: input, msgsent + integer :: i3, n3, itype + integer :: itone85(85), itone105(105), itone144(144) + integer*1 :: msgbits77(77) + + input='$DX K1ABC FN42' + + msgsent=' ' + msgbits77=1 + itone105=1 + call genft4(input,1,msgsent,msgbits77,itone105) + call assert_text_equal('FT4 invalid fallback message', & + '*** bad message ***',msgsent) + call assert_true('FT4 invalid fallback bits',all(msgbits77.eq.0)) + call assert_true('FT4 invalid fallback tones',all(itone105(1:103).eq.0)) + + itype=1 + msgsent=' ' + itone144=1 + call genmsk_128_90(input,0,msgsent,itone144,itype) + call assert_int('MSK144 invalid fallback type',-1,itype) + call assert_text_equal('MSK144 invalid fallback message', & + '*** bad message ***',msgsent) + call assert_true('MSK144 invalid fallback tones',all(itone144.eq.0)) + + i3=0 + n3=0 + msgsent=' ' + itone85=1 + call genq65(input,0,msgsent,itone85,i3,n3,0) + call assert_int('Q65 invalid fallback i3',-1,i3) + call assert_int('Q65 invalid fallback n3',-1,n3) + call assert_text_equal('Q65 invalid fallback message', & + '*** bad message ***',msgsent) + call assert_true('Q65 invalid fallback tones',all(itone85.eq.0)) + + ntests=ntests+1 + end subroutine expect_tx_generators_reject_invalid_fallback + +end program test_packjt77_invariants diff --git a/tests/unit/packjt77/test_packjt77_mutation_sweep.f90 b/tests/unit/packjt77/test_packjt77_mutation_sweep.f90 new file mode 100644 index 00000000..58abdb60 --- /dev/null +++ b/tests/unit/packjt77/test_packjt77_mutation_sweep.f90 @@ -0,0 +1,280 @@ + program test_packjt77_mutation_sweep + use packjt77 + use packjt77_test_helpers + implicit none + + integer, parameter :: MAX_SEEDS=400,MAX_MUTANTS=50000 + character(len=37) :: seeds(MAX_SEEDS) + integer :: nseeds,nmutants,nexact,ncanon,nreject + + nseeds=0 + nmutants=0 + nexact=0 + ncanon=0 + nreject=0 + + call check_canonical_matcher() + call generate_seeds() + call assert_true('seed generator coverage', nseeds.ge.200) + call sweep_mutations() + + print '(a,i0,a,i0,a,i0,a,i0)', & + 'test_packjt77_mutation_sweep: mutants=', nmutants, & + ' encoded-exact=', nexact, ' encoded-canonical=', ncanon, & + ' rejected=', nreject + +contains + + subroutine check_canonical_matcher() + call assert_true('canonical compound bracket', & + canonical_match('W1AW/P','<W1AW/P>')) + call assert_true('canonical compound placeholder', & + canonical_match('PJ2/W1AW','<...>')) + call assert_true('canonical bracket placeholder', & + canonical_match('<PJ4/K1ABC>','<...>')) + call assert_true('canonical grid bracket reject', & + .not.canonical_match('FN42','<FN42>')) + call assert_true('canonical trailing slash reject', & + .not.canonical_match('K1ABC/','<K1ABC/>')) + call assert_true('canonical leading slash reject', & + .not.canonical_match('/K1ABC','</K1ABC>')) + call assert_true('canonical double slash reject', & + .not.canonical_match('A//B','<A//B>')) + end subroutine check_canonical_matcher + + subroutine generate_seeds() + character(len=13), parameter :: calls(6)=(/'K1ABC ', & + 'W9XYZ ','KA1ABC ','WA9XYZ ','PJ2/W1AW ', & + 'W1AW/P '/) + character(len=4), parameter :: grids(4)=(/'FN42','EM10','DM79','IO91'/) + character(len=3), parameter :: snrs(4)=(/'-12','+00','+20','R-7'/) + character(len=3), parameter :: rtty_reports(4)=(/'529','559','579','599'/) + character(len=3), parameter :: mults(4)=(/'MA ','CT ','DX ','EMA'/) + character(len=6), parameter :: vhf_exch(4)=(/'520001','590001', & + '592047','582047'/) + integer :: i,j + character(len=13) :: free_seed + + do i=1,4 + do j=1,4 + call add_seed(trim(calls(i))//' '//trim(calls(j))//' '//grids(j)) + call add_seed(trim(calls(i))//' '//trim(calls(j))//' '//snrs(i)) + call add_seed(trim(calls(i))//' '//trim(calls(j))//' R '//grids(j)) + call add_seed(trim(calls(i))//' '//trim(calls(j))//' RRR') + enddo + enddo + + do i=1,4 + call add_seed(trim(calls(i))//' RR73; W9XYZ <KH1/KH7Z> -12') + call add_seed(trim(calls(5))//' RR73; W9XYZ <KH1/KH7Z> -12') + call add_seed(trim(calls(i))//' '//trim(calls(j_index(i)))//' R 1A EMA') + call add_seed(trim(calls(6))//' '//trim(calls(i))//' 5A CT') + call add_seed('TU; '//trim(calls(i))//' W9XYZ R '//rtty_reports(i)// & + ' '//trim(mults(i))) + call add_seed(trim(calls(i))//' W9XYZ '//rtty_reports(i)// & + ' '//trim(mults(i))) + call add_seed('<PJ4/K1ABC> '//trim(calls(i))//' RR73') + call add_seed(trim(calls(5))//' <W7ABC> RR73') + call add_seed('<W3CCX> <K1JT/P> '//vhf_exch(i)//' FN20QI') + call add_seed('<W3CCX> <K1JT/P> R '//vhf_exch(i)//' FN20QI') + call add_seed(trim(calls(i))//' '//grids(i)//' 37') + call add_seed('0/'//trim(calls(i))//' 37') + call add_seed(trim(calls(i))//'/1 37') + enddo + + call add_seed('CQ PJ2/W1AW') + call add_seed('CQ K1ABC FN42') + call add_seed('123456789ABCDEF012') + call add_seed('0000000000000000DE') + call add_seed('HELLO WORLD 73') + call add_seed('FREE TEXT MSG') + + do i=1,80 + call add_seed('K1ABC W9XYZ +'//digit2(mod(i,50))) + call add_seed('K1ABC W9XYZ R+'//digit2(mod(i,50))) + call add_seed('WA9XYZ KA1ABC '//field_day_exchange(i)) + enddo + + do i=1,160 + write(free_seed,'("TXT",i3.3," MSG")') i + call add_seed(free_seed) + enddo + end subroutine generate_seeds + + integer function j_index(i) result(j) + integer, intent(in) :: i + + j=mod(i,4)+1 + end function j_index + + character(len=2) function digit2(n) result(text) + integer, intent(in) :: n + + write(text,'(i2.2)') n + end function digit2 + + character(len=3) function field_day_exchange(i) result(text) + integer, intent(in) :: i + + write(text,'(i0,a1)') mod(i-1,32)+1, achar(iachar('A')+mod(i,6)) + end function field_day_exchange + + subroutine add_seed(text) + character(len=*), intent(in) :: text + character(len=37) :: msg,decoded + logical :: ok + type(pack77_result) :: encoded + + if(nseeds.ge.MAX_SEEDS) return + msg=' ' + msg=text + call clear_all_state('N0AAA','N0BBB') + encoded=pack77_result_from_api(msg) + if(.not.encoded%encoded) return + decoded=' ' + ok=.false. + call unpack77(encoded%c77,0,decoded,ok) + call assert_true('seed decodes', ok) + nseeds=nseeds+1 + seeds(nseeds)=msg + end subroutine add_seed + + subroutine sweep_mutations() + integer :: i + + do i=1,nseeds + call mutate_seed(seeds(i)) + if(nmutants.ge.MAX_MUTANTS) exit + enddo + end subroutine sweep_mutations + + subroutine mutate_seed(seed) + character(len=*), intent(in) :: seed + character(len=8), parameter :: probes='A0/<>+. ' + character(len=37) :: mutant + integer :: i,j,n + + n=len_trim(seed) + do i=1,n + do j=1,len(probes) + mutant=seed + mutant(i:i)=probes(j:j) + call check_mutant(mutant) + if(nmutants.ge.MAX_MUTANTS) return + enddo + enddo + + do i=1,n + mutant=' ' + if(i.gt.1) mutant(1:i-1)=seed(1:i-1) + if(i.lt.n) mutant(i:n-1)=seed(i+1:n) + call check_mutant(mutant) + if(nmutants.ge.MAX_MUTANTS) return + enddo + + call mutate_tokens(seed) + end subroutine mutate_seed + + subroutine mutate_tokens(seed) + character(len=*), intent(in) :: seed + character(len=37) :: tokens(19),mutant + integer :: ntokens,i,j + + call split_tokens(seed,tokens,ntokens) + do i=1,ntokens + mutant=join_tokens(tokens,ntokens,i,i) + call check_mutant(mutant) + if(nmutants.ge.MAX_MUTANTS) return + enddo + do i=1,ntokens-1 + do j=i+1,ntokens + mutant=join_tokens(tokens,ntokens,i,j) + call check_mutant(mutant) + if(nmutants.ge.MAX_MUTANTS) return + enddo + enddo + end subroutine mutate_tokens + + character(len=37) function join_tokens(tokens,ntokens,a,b) result(msg) + character(len=37), intent(in) :: tokens(:) + integer, intent(in) :: ntokens,a,b + integer :: i + character(len=37) :: work(19) + + work=tokens + if(a.eq.b) then + msg=' ' + do i=1,ntokens + call append_token(msg,tokens(i)) + if(i.eq.a) call append_token(msg,tokens(i)) + enddo + else + work(a)=tokens(b) + work(b)=tokens(a) + msg=' ' + do i=1,ntokens + call append_token(msg,work(i)) + enddo + endif + end function join_tokens + + subroutine append_token(msg,token) + character(len=37), intent(inout) :: msg + character(len=*), intent(in) :: token + integer :: n,m + + n=len_trim(msg) + m=len_trim(token) + if(m.eq.0) return + if(n.eq.0) then + if(m.le.37) msg(1:m)=token(1:m) + else if(n+m+1.le.37) then + msg(n+1:n+1)=' ' + msg(n+2:n+m+1)=token(1:m) + endif + end subroutine append_token + + subroutine check_mutant(mutant) + character(len=*), intent(in) :: mutant + character(len=37) :: msg,decoded + logical :: ok,is_exact,is_canonical,is_telemetry + type(pack77_result) :: encoded + + if(nmutants.ge.MAX_MUTANTS) return + nmutants=nmutants+1 + msg=' ' + msg=mutant + call clear_all_state('N0AAA','N0BBB') + encoded=pack77_result_from_api(msg) + if(.not.encoded%encoded) then + nreject=nreject+1 + return + endif + call assert_true('encoded gate rejected', encoded%status.ne.PACK77_STATUS_INTERNAL_ROUNDTRIP_REJECTED) + decoded=' ' + ok=.false. + call unpack77(encoded%c77,0,decoded,ok) + if(.not.ok) then + write(*,'(a,a,a,a,a)') 'silent rewrite: input="', trim(msg), & + '" c77="', trim(encoded%c77), '"' + error stop 1 + endif + is_exact=normalized_equal(msg,decoded) + is_canonical=canonical_match(msg,decoded) + is_telemetry=telemetry_match(msg,decoded) + if(.not.is_exact .and. .not.is_canonical .and. .not.is_telemetry) then + write(*,'(a,a,a,a,a,a,a)') 'silent rewrite: input="', trim(msg), & + '" decoded="', trim(decoded), '" c77="', trim(encoded%c77), '"' + error stop 1 + endif + if(is_exact) then + nexact=nexact+1 + else if(is_telemetry) then + ncanon=ncanon+1 + else + call assert_true('canonical mutation decode', is_canonical) + ncanon=ncanon+1 + endif + end subroutine check_mutant + +end program test_packjt77_mutation_sweep diff --git a/tests/unit/packjt77/test_packjt77_pack28_sweep.f90 b/tests/unit/packjt77/test_packjt77_pack28_sweep.f90 new file mode 100644 index 00000000..66433692 --- /dev/null +++ b/tests/unit/packjt77/test_packjt77_pack28_sweep.f90 @@ -0,0 +1,231 @@ +program test_packjt77_pack28_sweep + + use packjt77 + use packjt77_grammar, only: PACK77_NTOKENS,PACK77_MAX22 + implicit none + + integer, parameter :: ncat=20 + integer, parameter :: nsweep=260 + integer, parameter :: expected_n28(nsweep) = (/ & + 5950126, 5950126, 5121162, 5121162, 3532835, 5950126, 5950126, & + 5950126, 2063592, 5950126, 4386540, 3225066, 3225066, 3959688, & + 5121162, 5002078, 5002078, 3532835, 5950126, 4907736, 4396757, & + 4636842, 3214849, 5375614, 6173770, 4396757, 4396757, 4396757, & + 2063592, 4396757, 4993936, 2, 2, 0, 3454934, & + 4168964, 4776360, 4253090, 4636842, 5755217, 8246608, 86387389, & + 11395888, 204642853, 22417639, 84419089, 8246608, 8246608, 2063592, & + 8246608, 194269912, 2195446, 2195446, 2111640, 1, 2913946, & + 3521341, 4940621, 86387389, 10214908, 8246635, 86388118, 11395915, & + 204661807, 22418368, 84419818, 4873537, 5750231, 2063592, 5750231, & + 6256420, 5239844, 1023, 4175765, 3318227, 2338993, 5010514, & + 2065856, 86388118, 10214962, 8246636, 86388145, 11395916, 204662509, & + 22418395, 84419872, 5574985, 2257374, 2063592, 2257374, 3991096, & + 5415206, 1548, 6216860, 5359321, 4380087, 4372313, 2416579, & + 86388172, 10214965, 6022918, 86388146, 5193953, 204662535, 22418396, & + 84419875, 3165162, 4041856, 2363507, 4041856, 5686611, 126, & + 15737, 2959280, 2101742, 6164569, 3835940, 4156578, 86388175, & + 4447486, 2074017, 3976499, 5439357, 4585084, 5513427, 2444633, & + 3432875, 4309569, 2063592, 4309569, 5932014, 4857335, 398841, & + 3204683, 2347145, 2260287, 4594460, 4335053, 4845483, 5206005, & + 2080475, 3982957, 5445815, 4591542, 5519885, 2464594, 3439920, & + 4316614, 2063592, 4316614, 5951388, 4863793, 2695765, 3211141, & + 2353603, 2280248, 4614421, 4340337, 4865444, 5225967, 2080645, & + 3983127, 5445985, 4591712, 5520055, 2465120, 3440106, 4316799, & + 2063592, 4316799, 5951558, 4863963, 2695935, 3211311, 2353773, & + 2280774, 4614946, 4340491, 4865970, 5226492, 2080650, 3983132, & + 5445989, 4591716, 5520059, 2465134, 3440111, 4316804, 2063592, & + 4316804, 5951563, 4863968, 2695939, 3211316, 2353778, 2280788, & + 4614960, 4340492, 4865984, 5226506, 2080650, 3983132, 5445989, & + 4591716, 5520059, 2465134, 3440111, 4316804, 2063592, 4316804, & + 5951563, 4863968, 2695940, 3211316, 2353778, 2280788, 4614960, & + 4340492, 4865984, 5226506, 2080650, 3983132, 5445989, 4591716, & + 5520059, 2465134, 3440111, 4316804, 2063592, 4316804, 5951563, & + 4863968, 2695940, 3211316, 2353778, 2280788, 4614960, 4340492, & + 4865984, 5226506, 2080650, 3983132, 5445989, 4591716, 5520059, & + 2465134, 3440111, 4316804, 2063592, 4316804, 5951563, 4863968, & + 2695940, 3211316, 2353778, 2280788, 4614960, 4340492, 4865984, & + 5226506 /) + integer :: i,n28 + character(len=13) :: decoded, token + logical :: ok + + call check_cq_modifier_edges() + + do i=1,nsweep + token=sweep_token(i) + call pack28(token,n28) + if(n28.ne.expected_n28(i)) then + write(*,1000) i, trim(token), expected_n28(i), n28 +1000 format('pack28 sweep case ',i0,' "',a,'" expected ',i0,' got ',i0) + error stop 1 + endif + decoded=' ' + ok=.false. + call unpack28(n28,decoded,ok) + enddo + + print '(a,i0,a)', 'test_packjt77_pack28_sweep: ', nsweep, ' checks passed' + +contains + + character(len=13) function sweep_token(i) result(token) + integer, intent(in) :: i + integer :: category,n + + category=mod(i-1,ncat)+1 + n=(i-1)/ncat+1 + token=' ' + select case(category) + case(1) + if(n.lt.3) then + token=take_prefix('AAAAAAAAAAAAA',n) + else + token=take_prefix('A1AAAAAAAAAAA',n) + endif + case(2) + token=take_prefix('AB1AAAAAAAAAA',n) + case(3) + if(n.lt.3) then + token=take_prefix('QQQQQQQQQQQQQ',n) + else + token=take_prefix('Q1AAAAAAAAAAA',n) + endif + case(4) + token=take_prefix('QZ9ZZZAAAAAAA',n) + case(5) + token=take_prefix('1A1AAAAAAAAAA',n) + case(6) + if(n.lt.3) then + token=take_prefix('AAAAAAAAAAAAA',n) + else + token=take_prefix('A11ABCXXXXXXX',n) + endif + case(7) + if(n.lt.3) then + token=take_prefix('AAAAAAAAAAAAA',n) + else + token=take_prefix('A1A/BBBBBBBBB',n) + endif + case(8) + if(n.lt.3) then + token=take_prefix('AAAAAAAAAAAAA',n) + else + token=take_prefix('A1A$BBBBBBBBB',n) + endif + case(9) + token=take_prefix('<W1AW>AAAAAAA',n) + case(10) + if(n.lt.3) then + token=take_prefix('AAAAAAAAAAAAA',n) + else + token=take_prefix('A1A>BBBBBBBBB',n) + endif + case(11) + token=take_prefix('PJ2/W1AWAAAAA',n) + case(12) + token=take_prefix('CQ_123AAAAAAA',n) + case(13) + token=take_prefix('CQ_TESTAAAAAA',n) + case(14) + token=take_prefix('DEAAAAAAAAAAA',n) + case(15) + token=take_prefix('QRZAAAAAAAAAA',n) + case(16) + token=take_prefix('3DA0ABCXXXXXX',n) + case(17) + token=take_prefix('3XABCXXXXXXXX',n) + case(18) + token=take_prefix('1234567890123',n) + case(19) + token=take_prefix('AB1ABCXXXXXXX',n) + case(20) + if(n.lt.3) then + token=take_prefix('KKKKKKKKKKKKK',n) + else + token=take_prefix('K1ABCXXXXXXXX',n) + endif + end select + end function sweep_token + + character(len=13) function take_prefix(seed,n) result(token) + character(len=*), intent(in) :: seed + integer, intent(in) :: n + + token=' ' + token(1:n)=seed(1:n) + end function take_prefix + + subroutine check_cq_modifier_edges() + call assert_cq_modifier_special('CQ_A') + call assert_cq_modifier_special('CQ_AB') + call assert_cq_modifier_special('CQ_ABC') + call assert_cq_modifier_special('CQ_ABCD') + call assert_cq_modifier_special('CQ_000') + call assert_cq_modifier_special('CQ_123') + call assert_cq_modifier_special('CQ_999') + + call assert_cq_modifier_hash('CQ_') + call assert_cq_modifier_hash('CQ_1') + call assert_cq_modifier_hash('CQ_12') + call assert_cq_modifier_hash('CQ_1234') + call assert_cq_modifier_hash('CQ_ABCDE') + call assert_cq_modifier_hash('CQ_A1') + call assert_cq_modifier_hash('CQ_1A') + call assert_cq_modifier_hash('CQ_AB12') + call assert_cq_modifier_hash('CQ_A/B') + call assert_cq_modifier_hash('CQ_A-B') + end subroutine check_cq_modifier_edges + + subroutine assert_cq_modifier_special(input) + character(len=*), intent(in) :: input + character(len=13) :: c13 + integer :: got + + call clear_hash_state() + c13=' ' + c13=input + call pack28(c13,got) + if(got.lt.3 .or. got.ge.PACK77_NTOKENS) then + write(*,1010) trim(input), got +1010 format('CQ modifier "',a,'" expected special token, got ',i0) + error stop 1 + endif + if(nzhash.ne.0) then + write(*,1011) trim(input), nzhash +1011 format('CQ modifier "',a,'" unexpectedly staged ',i0,' hashes') + error stop 1 + endif + end subroutine assert_cq_modifier_special + + subroutine assert_cq_modifier_hash(input) + character(len=*), intent(in) :: input + character(len=13) :: c13 + integer :: got + + call clear_hash_state() + c13=' ' + c13=input + call pack28(c13,got) + if(got.lt.PACK77_NTOKENS .or. got.ge.PACK77_NTOKENS+PACK77_MAX22) then + write(*,1020) trim(input), got +1020 format('CQ modifier "',a,'" expected hash token, got ',i0) + error stop 1 + endif + if(nzhash.ne.1 .or. calls22(1).ne.c13) then + write(*,1021) trim(input), nzhash, trim(calls22(1)) +1021 format('CQ modifier "',a,'" expected one committed hash, got nzhash=', & + i0,' calls22(1)="',a,'"') + error stop 1 + endif + end subroutine assert_cq_modifier_hash + + subroutine clear_hash_state() + calls10='' + calls12='' + calls22='' + recent_calls='' + ihash22=-1 + nzhash=0 + end subroutine clear_hash_state + +end program test_packjt77_pack28_sweep diff --git a/tests/unit/packjt77/test_packjt77_protocol_vectors.f90 b/tests/unit/packjt77/test_packjt77_protocol_vectors.f90 new file mode 100644 index 00000000..70930127 --- /dev/null +++ b/tests/unit/packjt77/test_packjt77_protocol_vectors.f90 @@ -0,0 +1,228 @@ +program test_packjt77_protocol_vectors + + use packjt77 + use packjt77_test_helpers + implicit none + + integer :: ntests + + ntests=0 + + call expect_hash_vector('K1ABC', 712, 2851, 2920267) + call expect_hash_vector('W9XYZ', 972, 3889, 3982604) + call expect_hash_vector('PJ4/K1ABC', 346, 1387, 1420834) + call expect_hash_vector('KH1/KH7Z', 201, 806, 825805) + call expect_hash_vector('K1JT/P', 959, 3839, 3931158) + call expect_hash_vector('W3CCX', 665, 2662, 2726483) + call expect_hash_vector('3DA0ABC', 48, 192, 196695) + call expect_hash_vector('3XABC', 563, 2254, 2308721) + call expect_hash_vector('///////////', 671, 2685, 2749801) + call expect_hash_vector('ZZZZZZZZZZZ', 902, 3609, 3695718) + call expect_hash_vector('99999999999', 762, 3050, 3123740) + call expect_hash_vector('A23456789Z/', 122, 488, 499902) + call expect_hash_vector(' ', 0, 0, 0) + + call expect_pack_bits('K1ABC W9XYZ FN42', 1, 0, -1, -1, & + '00001001101111011110001101010000011000010100100111011100000010100001100110001') + call expect_pack_bits('<PJ4/K1ABC> W9XYZ RR73', 1, 0, -1, -1, & + '00000011010100101011000010100000011000010100100111011100000111111001110101001') + call expect_pack_bits('K1ABC RR73; W9XYZ <KH1/KH7Z> -12', 0, 1, -1, -1, & + '00001001101111011110001101010000110000101001001110111000001100100101001001000') + call expect_pack_bits('PJ2/W1AW <W7ABC> RR73', 4, 0, -1, -1, & + '00100100001100000000000000001011000010101110000110010100011010110100111100100') + call expect_pack_bits('<W3CCX> <K1JT/P> 592047 RR99XX', 5, 0, -1, -1, & + '10100110011011101111111100000101100111111111111111000111001100001111111111101') + call expect_pack_bits('K1ABC FN42 37', 0, 6, -1, -1, & + '00001001101111011110001101010101000011001100101100000000000000000000000110000') + ! WSPR Type 2 prefix/suffix vectors need both exact bits and decoded text. + call expect_pack_bits('PJ2/K1ABC 37', 0, 6, -1, -1, & + '00001001101111011110001101011000000100111110010111000000000000000000000110000', & + 'PJ2/K1ABC 37') + call expect_pack_bits('<PJ4/K1ABC> FK52AB', 0, 6, 0, 6, & + '01010110101110001000100010111111101110100000101010000000000000000000000110000') + + call expect_unified_codec_bits('K1ABC W9XYZ FN42', -1, -1) + call expect_unified_codec_bits('FREE TEXT MSG', -1, -1) + call expect_unified_codec_bits('WA9XYZ KA1ABC R 16A EMA', -1, -1) + call expect_unified_codec_bits('WA9XYZ KA1ABC R 32A EMA', -1, -1) + call expect_unified_codec_bits('TU; W9XYZ G8ABC R 559 0013', -1, -1) + call expect_unified_codec_bits('K1ABC RR73; W9XYZ <KH1/KH7Z> -12', -1, -1) + call expect_unified_codec_bits('<PJ4/K1ABC> W9XYZ RR73', -1, -1) + call expect_unified_codec_bits('PJ2/W1AW <W7ABC> RR73', -1, -1) + call expect_unified_codec_bits('<W3CCX> <K1JT/P> 592047 RR99XX', -1, -1) + call expect_unified_codec_bits('K1ABC FN42 37', -1, -1) + call expect_unified_codec_bits('<PJ4/K1ABC> FK52AB', 0, 6) + + call expect_binary_payload_for_wspr_message('PJ4/K1ABC 37') + call expect_not_wspr_payload('K1ABC/ABCD 37') + + write(*,1000) ntests +1000 format('packjt77 protocol vector tests passed: ',i0) + +contains + + subroutine expect_hash_vector(callsign,want10,want12,want22) + character(len=*), intent(in) :: callsign + integer, intent(in) :: want10, want12, want22 + character(len=13) :: c13 + integer :: got10, got12, got22 + + c13=' ' + c13=callsign + got10=ihashcall(c13,10) + got12=ihashcall(c13,12) + got22=ihashcall(c13,22) + if(got10.ne.want10 .or. got12.ne.want12 .or. got22.ne.want22) then + write(*,1010) trim(callsign), got10, got12, got22, want10, want12, want22 +1010 format('Hash vector failure for "',a,'"; got ',i0,1x,i0,1x,i0, & + ' wanted ',i0,1x,i0,1x,i0) + error stop 1 + endif + + if(got10.lt.0 .or. got10.gt.1023 .or. got12.lt.0 .or. got12.gt.4095 .or. & + got22.lt.0 .or. got22.gt.4194303) then + write(*,1040) trim(callsign), got10, got12, got22 +1040 format('Hash range failure for "',a,'"; got ',i0,1x,i0,1x,i0) + error stop 1 + endif + + ntests=ntests+1 + end subroutine expect_hash_vector + + subroutine expect_pack_bits(input,want_i3,want_n3,pack_i3,pack_n3,expected_c77, & + expected_decode) + character(len=*), intent(in) :: input, expected_c77 + character(len=*), intent(in), optional :: expected_decode + integer, intent(in) :: want_i3, want_n3, pack_i3, pack_n3 + character(len=77) :: c77 + character(len=37) :: decoded + integer :: got_i3, got_n3 + logical :: ok + + call pack_standard(input,pack_i3,pack_n3,got_i3,got_n3,c77) + call assert_message_type('pack77',input,want_i3,want_n3,got_i3,got_n3) + call assert_binary_payload('pack77',input,c77) + if(c77.ne.expected_c77) then + write(*,1030) trim(input), c77, expected_c77 +1030 format('77-bit vector failure for "',a,'"; got ',a,' wanted ',a) + error stop 1 + endif + + if(present(expected_decode)) then + call reset_packjt77_state() + decoded=' ' + ok=.false. + call unpack77(c77,0,decoded,ok) + call assert_decode('pack77 vector',input,expected_decode,decoded,ok) + endif + + ntests=ntests+1 + end subroutine expect_pack_bits + + subroutine expect_unified_codec_bits(input,pack_i3,pack_n3) + character(len=*), intent(in) :: input + integer, intent(in) :: pack_i3, pack_n3 + character(len=77) :: first_c77, second_c77 + integer :: first_i3, first_n3, second_i3, second_n3 + + call pack_standard(input,pack_i3,pack_n3,first_i3,first_n3,first_c77) + call pack_standard(input,pack_i3,pack_n3,second_i3,second_n3,second_c77) + if(first_i3.ne.second_i3 .or. first_n3.ne.second_n3) then + write(*,1040) trim(input), first_i3, first_n3, second_i3, second_n3 +1040 format('Unified codec type failure for "',a,'"; first pack77 got ',i0,'.',i0, & + ' second pack77 got ',i0,'.',i0) + error stop 1 + endif + call assert_binary_payload('pack77',input,first_c77) + call assert_binary_payload('pack77',input,second_c77) + if(first_c77.ne.second_c77) then + write(*,1050) trim(input), first_c77, second_c77 +1050 format('Unified codec bit failure for "',a,'"; first pack77 ',a, & + ' second pack77 ',a) + error stop 1 + endif + + ntests=ntests+1 + end subroutine expect_unified_codec_bits + + subroutine expect_binary_payload_for_wspr_message(input) + character(len=*), intent(in) :: input + character(len=77) :: c77 + character(len=37) :: packed_input + integer :: got_i3, got_n3 + + packed_input=' ' + packed_input=input + call reset_packjt77_state() + + got_i3=-1 + got_n3=-1 + c77='' + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(packed_input) + call assert_pack77_result('pack77',packed_input,encoded) + got_i3=encoded%i3 + got_n3=encoded%n3 + c77=encoded%c77 + end block + call assert_message_type('pack77',input,0,6,got_i3,got_n3) + call assert_binary_payload('pack77',input,c77) + + ntests=ntests+1 + end subroutine expect_binary_payload_for_wspr_message + + subroutine expect_not_wspr_payload(input) + character(len=*), intent(in) :: input + character(len=37) :: packed_input + type(pack77_result) :: encoded + + packed_input=' ' + packed_input=input + call reset_packjt77_state() + + encoded=pack77_result_from_api(packed_input) + if(encoded%encoded .and. encoded%i3.eq.0 .and. encoded%n3.eq.6) then + write(*,1060) trim(input) +1060 format('WSPR reject failure for "',a,'"; still packed as 0.6') + error stop 1 + endif + + ntests=ntests+1 + end subroutine expect_not_wspr_payload + + subroutine pack_standard(input,pack_i3,pack_n3,got_i3,got_n3,c77) + character(len=*), intent(in) :: input + integer, intent(in) :: pack_i3, pack_n3 + integer, intent(out) :: got_i3, got_n3 + character(len=77), intent(out) :: c77 + character(len=37) :: packed_input + + packed_input=' ' + packed_input=input + call reset_packjt77_state() + got_i3=pack_i3 + got_n3=pack_n3 + c77='' + block + type(pack77_result) :: encoded + encoded=pack77_with_hint(packed_input,pack_i3,pack_n3) + call assert_pack77_result('pack77',packed_input,encoded) + call assert_true('pack77 gate not rejected',encoded%status.ne.PACK77_STATUS_INTERNAL_ROUNDTRIP_REJECTED) + got_i3=encoded%i3 + got_n3=encoded%n3 + c77=encoded%c77 + end block + end subroutine pack_standard + + subroutine reset_packjt77_state() + integer :: n10, n12, n22 + + call clear_all_state('N0AAA','N0BBB') + call save_hash_call(mycall13,hashmy10_configured,hashmy12_configured,hashmy22_configured) + hashdx10_configured=ihashcall(dxcall13,10) + call save_hash_call(dxcall13,n10,n12,n22) + call queue_hash_call_for_thread(dxcall13,1) + end subroutine reset_packjt77_state + +end program test_packjt77_protocol_vectors diff --git a/tests/unit/packjt77/test_packjt77_schema_decoder.f90 b/tests/unit/packjt77/test_packjt77_schema_decoder.f90 new file mode 100644 index 00000000..7c9816f5 --- /dev/null +++ b/tests/unit/packjt77/test_packjt77_schema_decoder.f90 @@ -0,0 +1,205 @@ +program test_packjt77_schema_decoder + + use packjt77_schema + use packjt77 + use packjt77_test_helpers + implicit none + + integer :: ntests + + ntests=0 + + call expect_type1_exact_fields() + call expect_type2_tag_decode() + call expect_invalid_binary_and_tag_fail() + call expect_type1_grid_boundary() + call expect_wspr_selector_padding_and_power_checks() + call expect_type5_grid_range_check() + call expect_type5_raw_invalid_serial_rejected() + call expect_failure_has_no_shared_state_side_effects() + + write(*,1000) ntests +1000 format('packjt77 schema decoder tests passed: ',i0) + +contains + + subroutine expect_type1_exact_fields() + character(len=77) :: c77 + type(pack77_type12_fields) :: fields + integer :: i3,n3 + logical :: ok + + c77='00001001101111011110001101010000011000010100100111011100000010100001100110001' + + call decode_pack77_tag(c77,i3,n3,ok) + call assert_true('Type 1 tag decode',ok) + call assert_int('Type 1 i3',1,i3) + call assert_int('Type 1 n3',0,n3) + + call decode_pack77_type1(c77,fields,ok) + call assert_true('Type 1 field decode',ok) + call assert_int('Type 1 n28a',10214965,fields%n28a) + call assert_int('Type 1 ipa',0,fields%ipa) + call assert_int('Type 1 n28b',12751800,fields%n28b) + call assert_int('Type 1 ipb',0,fields%ipb) + call assert_int('Type 1 ir',0,fields%ir) + call assert_int('Type 1 igrid4',10342,fields%igrid4) + + ntests=ntests+1 + end subroutine expect_type1_exact_fields + + subroutine expect_type2_tag_decode() + character(len=77) :: c77 + type(pack77_type12_fields) :: fields + integer :: i3,n3 + logical :: ok + + c77='00001001101111011110001101010000011000010100100111011100000010100001100110010' + + call decode_pack77_tag(c77,i3,n3,ok) + call assert_true('Type 2 tag decode',ok) + call assert_int('Type 2 i3',2,i3) + call assert_int('Type 2 n3',0,n3) + call decode_pack77_type2(c77,fields,ok) + call assert_true('Type 2 field decode',ok) + call assert_int('Type 2 n28a',10214965,fields%n28a) + call assert_int('Type 2 igrid4',10342,fields%igrid4) + + ntests=ntests+1 + end subroutine expect_type2_tag_decode + + subroutine expect_invalid_binary_and_tag_fail() + character(len=77) :: c77 + type(pack77_type12_fields) :: fields + integer :: i3,n3 + logical :: ok + + c77='00001001101111011110001101010000011000010100100111011100000010100001100110001' + c77(10:10)='x' + call decode_pack77_tag(c77,i3,n3,ok) + call assert_true('non-binary tag rejected',.not.ok) + + c77='00001001101111011110001101010000011000010100100111011100000010100001100110001' + c77(75:77)='010' + call decode_pack77_type1(c77,fields,ok) + call assert_true('wrong fixed i3 rejected',.not.ok) + + ntests=ntests+1 + end subroutine expect_invalid_binary_and_tag_fail + + subroutine expect_type1_grid_boundary() + character(len=37) :: input, decoded + character(len=77) :: c77 + logical :: ok + + input='K1ABC W9XYZ FN42' + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(input) + call assert_pack77_result('pack77',input,encoded) + call assert_int('Type 1 boundary i3',1,encoded%i3) + call assert_int('Type 1 boundary n3',0,encoded%n3) + c77=encoded%c77 + end block + + write(c77(60:74),'(b15.15)') 32399 + decoded=' ' + ok=.false. + call unpack77_configured(c77,0,decoded,ok, & + unpack77_options(record_hashes=.false.,record_recent_calls=.false.)) + call assert_true('Type 1 grid boundary succeeds',ok) + call assert_text_equal('Type 1 grid boundary decode', & + 'K1ABC W9XYZ RR99',decoded) + + write(c77(60:74),'(b15.15)') 32400 + decoded=' ' + ok=.true. + call unpack77_configured(c77,0,decoded,ok, & + unpack77_options(record_hashes=.false.,record_recent_calls=.false.)) + call assert_true('Type 1 invalid grid boundary fails',.not.ok) + + ntests=ntests+1 + end subroutine expect_type1_grid_boundary + + subroutine expect_wspr_selector_padding_and_power_checks() + character(len=77) :: c77 + type(pack77_wspr_type1_fields) :: type1_fields + type(pack77_wspr_type3_fields) :: type3_fields + logical :: ok + + c77=repeat('0',77) + c77(72:77)='110000' + c77(48:50)='110' + call decode_pack77_wspr_type1(c77,type1_fields,ok) + call assert_true('invalid WSPR Type 1 selector rejected',.not.ok) + call decode_pack77_wspr_type3(c77,type3_fields,ok) + call assert_true('invalid WSPR Type 3 selector rejected',.not.ok) + + c77=repeat('0',77) + c77(72:77)='110000' + c77(48:50)='010' + c77(51:51)='1' + call decode_pack77_wspr_type3(c77,type3_fields,ok) + call assert_true('nonzero WSPR padding rejected',.not.ok) + + c77=repeat('0',77) + c77(44:48)='11111' + c77(49:50)='00' + c77(72:77)='110000' + call decode_pack77_wspr_type1(c77,type1_fields,ok) + call assert_true('invalid WSPR raw power rejected',.not.ok) + + ntests=ntests+1 + end subroutine expect_wspr_selector_padding_and_power_checks + + subroutine expect_type5_grid_range_check() + character(len=77) :: c77 + type(pack77_type5_fields) :: fields + logical :: ok + + c77=repeat('0',77) + c77(50:74)=repeat('1',25) + c77(75:77)='101' + call decode_pack77_type5(c77,fields,ok) + call assert_true('Type 5 invalid grid rejected',.not.ok) + + ntests=ntests+1 + end subroutine expect_type5_grid_range_check + + subroutine expect_type5_raw_invalid_serial_rejected() + character(len=77) :: c77 + character(len=37) :: decoded + logical :: ok, unpack_ok + + call encode_pack77_type5(0,0,0,0,1,0,c77,ok) + call assert_true('Type 5 raw serial one encoded',ok) + write(c77(39:49),'(b11.11)') 0 + + decoded=' ' + unpack_ok=.true. + call unpack77(c77,0,decoded,unpack_ok) + call assert_true('Type 5 raw serial zero rejected',.not.unpack_ok) + + ntests=ntests+1 + end subroutine expect_type5_raw_invalid_serial_rejected + + subroutine expect_failure_has_no_shared_state_side_effects() + character(len=77) :: c77 + type(pack77_wspr_type3_fields) :: fields + logical :: ok + + call clear_all_state('','') + c77=repeat('0',77) + c77(48:50)='010' + c77(51:51)='1' + c77(72:77)='110000' + + call decode_pack77_wspr_type3(c77,fields,ok) + + call assert_true('schema decode failure rejected',.not.ok) + call assert_shared_hash_tables_empty('decoder failure') + + ntests=ntests+1 + end subroutine expect_failure_has_no_shared_state_side_effects + +end program test_packjt77_schema_decoder diff --git a/tests/unit/packjt77/test_packjt77_schema_encoder.f90 b/tests/unit/packjt77/test_packjt77_schema_encoder.f90 new file mode 100644 index 00000000..439f5e76 --- /dev/null +++ b/tests/unit/packjt77/test_packjt77_schema_encoder.f90 @@ -0,0 +1,99 @@ +program test_packjt77_schema_encoder + + use packjt77_schema + use packjt77_test_helpers + implicit none + + integer :: ntests + + ntests=0 + + call expect_type1_exact_bits() + call expect_type2_tag_bits() + call expect_range_failures() + call expect_bitstring_validation() + call expect_failure_has_no_shared_state_side_effects() + + write(*,1000) ntests +1000 format('packjt77 schema encoder tests passed: ',i0) + +contains + + subroutine expect_type1_exact_bits() + character(len=77) :: c77 + logical :: ok + + call encode_pack77_type1(10214965,0,12751800,0,0,10342,c77,ok) + + call assert_true('Type 1 schema encode',ok) + call assert_binary_payload('Type 1 schema','K1ABC W9XYZ FN42',c77) + call assert_text_equal('Type 1 c77',c77, & + '00001001101111011110001101010000011000010100100111011100000010100001100110001') + call assert_text_equal('Type 1 i3 bits',c77(75:77),'001') + ntests=ntests+1 + end subroutine expect_type1_exact_bits + + subroutine expect_type2_tag_bits() + character(len=77) :: c77 + logical :: ok + + call encode_pack77_type2(10214965,0,12751800,0,0,10342,c77,ok) + + call assert_true('Type 2 schema encode',ok) + call assert_binary_payload('Type 2 schema','tag-only portable layout',c77) + call assert_text_equal('Type 2 c77',c77, & + '00001001101111011110001101010000011000010100100111011100000010100001100110010') + call assert_text_equal('Type 2 i3 bits',c77(75:77),'010') + ntests=ntests+1 + end subroutine expect_type2_tag_bits + + subroutine expect_range_failures() + character(len=77) :: c77 + logical :: ok + + call encode_pack77_type1(-1,0,12751800,0,0,10342,c77,ok) + call assert_true('negative field rejected',.not.ok) + + call encode_pack77_type1(10214965,0,12751800,0,0,32768,c77,ok) + call assert_true('too-wide field rejected',.not.ok) + + call encode_pack77_type5(0,0,0,0,0,0,c77,ok) + call assert_true('Type 5 serial zero rejected',.not.ok) + + ntests=ntests+1 + end subroutine expect_range_failures + + subroutine expect_bitstring_validation() + character(len=77) :: c77 + logical :: ok + + call encode_pack77_free_text(repeat('1',71),c77,ok) + call assert_true('free-text bitstring accepted',ok) + call assert_text_equal('free-text tag bits',c77(72:77),'000000') + + call encode_pack77_free_text(repeat('1',70),c77,ok) + call assert_true('short bitstring rejected',.not.ok) + + call encode_pack77_free_text(repeat('1',72),c77,ok) + call assert_true('long bitstring rejected',.not.ok) + + call encode_pack77_free_text(repeat('1',70)//'X',c77,ok) + call assert_true('non-binary bitstring rejected',.not.ok) + + ntests=ntests+1 + end subroutine expect_bitstring_validation + + subroutine expect_failure_has_no_shared_state_side_effects() + character(len=77) :: c77 + logical :: ok + + call clear_all_state('','') + call encode_pack77_type1(10214965,0,12751800,0,0,32768,c77,ok) + + call assert_true('side-effect failure rejected',.not.ok) + call assert_shared_hash_tables_empty('schema failure') + + ntests=ntests+1 + end subroutine expect_failure_has_no_shared_state_side_effects + +end program test_packjt77_schema_encoder diff --git a/tests/unit/packjt77/test_packjt77_silent_rewrite_corpus.f90 b/tests/unit/packjt77/test_packjt77_silent_rewrite_corpus.f90 new file mode 100644 index 00000000..0fb12d82 --- /dev/null +++ b/tests/unit/packjt77/test_packjt77_silent_rewrite_corpus.f90 @@ -0,0 +1,206 @@ +program test_packjt77_silent_rewrite_corpus + + use packjt77 + implicit none + + integer, parameter :: max_line=256 + character(len=max_line) :: corpus_path, arg, line, input, origin + character(len=37) :: decoded + character(len=77) :: c77 + integer :: unit, ios, sep, i3, n3 + integer :: total, exact, canon, reject, silent + logical :: success, assert_zero + + total=0 + exact=0 + canon=0 + reject=0 + silent=0 + assert_zero=.false. + + corpus_path='tests/unit/packjt77/silent_rewrite_corpus.txt' + if(command_argument_count().ge.1) call get_command_argument(1,corpus_path) + if(command_argument_count().ge.2) then + call get_command_argument(2,arg) + assert_zero=trim(arg).eq.'--assert-zero-silent' + endif + + open(newunit=unit,file=trim(corpus_path),status='old',action='read',iostat=ios) + if(ios.ne.0) then + write(*,1000) trim(corpus_path), ios +1000 format('failed to open silent-rewrite corpus "',a,'": ',i0) + error stop 1 + endif + + do + read(unit,'(a)',iostat=ios) line + if(ios.ne.0) exit + if(len_trim(line).eq.0) cycle + sep=index(line,'|') + if(sep.le.0) cycle + input=' ' + origin=' ' + input=adjustl(line(1:sep-1)) + origin=adjustl(line(sep+1:)) + call classify_case(trim(input),trim(origin),exact,canon,reject,silent) + total=total+1 + enddo + close(unit) + + write(*,1010) total, exact, canon, reject, silent +1010 format('SILENT_REWRITE_SUMMARY total=',i0,' exact=',i0,' canon=',i0, & + ' reject=',i0,' silent_rewrite=',i0) + if(assert_zero .and. silent.ne.0) error stop 1 + + ! Self-contained on purpose: this driver must also compile against older + ! source snapshots where shared grammar/test helper APIs may not exist. +contains + + subroutine classify_case(input_msg,origin,exact,canon,reject,silent) + character(len=*), intent(in) :: input_msg, origin + integer, intent(inout) :: exact, canon, reject, silent + character(len=37) :: packed_input + character(len=16) :: class + + packed_input=' ' + packed_input=input_msg + call reset_hash_state() + call pack77(packed_input,i3,n3,c77) + success=i3.ge.0 + if(.not.success) then + reject=reject+1 + class='REJECT' + call print_case(input_msg,origin,.false.,i3,n3,' ',class) + return + endif + + decoded=' ' + success=.false. + call reset_hash_state() + call unpack77(c77,0,decoded,success) + if(.not.success) then + silent=silent+1 + class='SILENT_REWRITE' + call print_case(input_msg,origin,.true.,i3,n3,decoded,class) + return + endif + + if(normalized_equal(input_msg,decoded)) then + exact=exact+1 + class='EXACT' + else if(canonical_match(input_msg,decoded)) then + canon=canon+1 + class='CANON' + else + silent=silent+1 + class='SILENT_REWRITE' + endif + call print_case(input_msg,origin,.true.,i3,n3,decoded,class) + end subroutine classify_case + + subroutine print_case(input_msg,origin,encoded,i3,n3,decoded_msg,class) + character(len=*), intent(in) :: input_msg, origin, decoded_msg, class + logical, intent(in) :: encoded + integer, intent(in) :: i3, n3 + + write(*,1020) trim(input_msg), trim(origin), encoded, i3, n3, & + trim(decoded_msg), trim(class) +1020 format(a,' | ',a,' | encoded=',l1,' | type=',i0,'.',i0, & + ' | decoded=',a,' | ',a) + end subroutine print_case + + logical function normalized_equal(left,right) result(equal) + character(len=*), intent(in) :: left, right + character(len=37) :: left_norm, right_norm + + call normalize_message(left,left_norm) + call normalize_message(right,right_norm) + equal=trim(left_norm).eq.trim(right_norm) + end function normalized_equal + + logical function canonical_match(left,right) result(matches) + character(len=*), intent(in) :: left, right + character(len=37) :: left_tokens(19), right_tokens(19), wrapped + integer :: left_n, right_n, i + + matches=.false. + call split_tokens(left,left_tokens,left_n) + call split_tokens(right,right_tokens,right_n) + if(left_n.ne.right_n) return + do i=1,left_n + if(trim(left_tokens(i)).eq.trim(right_tokens(i))) cycle + wrapped=' ' + wrapped='<'//trim(left_tokens(i))//'>' + if(trim(right_tokens(i)).eq.trim(wrapped)) cycle + if(trim(right_tokens(i)).eq.'<...>' .and. hash_like(left_tokens(i))) cycle + return + enddo + matches=.true. + end function canonical_match + + logical function hash_like(token) result(ok) + character(len=*), intent(in) :: token + integer :: n + + n=len_trim(token) + ok=.false. + if(n.le.0) return + ok=index(token(1:n),'/').gt.0 + if(n.ge.2) ok=ok .or. (token(1:1).eq.'<' .and. token(n:n).eq.'>') + end function hash_like + + subroutine normalize_message(input_msg,normalized) + character(len=*), intent(in) :: input_msg + character(len=37), intent(out) :: normalized + character(len=37) :: tokens(19) + integer :: nwords, i + + normalized=' ' + call split_tokens(input_msg,tokens,nwords) + do i=1,nwords + if(len_trim(normalized).gt.0) normalized=trim(normalized)//' ' + normalized=trim(normalized)//trim(tokens(i)) + enddo + end subroutine normalize_message + + subroutine split_tokens(input_msg,tokens,nwords) + character(len=*), intent(in) :: input_msg + character(len=37), intent(out) :: tokens(19) + integer, intent(out) :: nwords + character(len=1) :: c, previous + integer :: i, n + + tokens=' ' + nwords=0 + n=0 + previous=' ' + do i=1,len_trim(input_msg) + c=input_msg(i:i) + if(ichar(c).eq.0) c=' ' + if(c.ge.'a' .and. c.le.'z') c=char(ichar(c)-32) + if(c.eq.' ' .and. previous.eq.' ') cycle + if(c.ne.' ' .and. previous.eq.' ') then + nwords=nwords+1 + n=0 + if(nwords.gt.size(tokens)) return + endif + if(c.ne.' ') then + n=n+1 + if(n.le.len(tokens(1))) tokens(nwords)(n:n)=c + endif + previous=c + enddo + end subroutine split_tokens + + subroutine reset_hash_state() + calls10='' + calls12='' + calls22='' + recent_calls='' + ihash22=-1 + nzhash=0 + mycall13=' ' + dxcall13=' ' + end subroutine reset_hash_state + +end program test_packjt77_silent_rewrite_corpus diff --git a/tests/unit/packjt77/test_packjt77sdvar_invariants.f90 b/tests/unit/packjt77/test_packjt77sdvar_invariants.f90 new file mode 100644 index 00000000..d0ffac93 --- /dev/null +++ b/tests/unit/packjt77/test_packjt77sdvar_invariants.f90 @@ -0,0 +1,234 @@ +program test_packjt77sdvar_invariants + + use packjt77 + use packjt77_test_helpers + use ft8_mod1, only : idtone56_valid,idtone56,itone56,msg,lastrxmsg,mycall, & + hiscall,nlasttx,csyncsd_valid,csyncsdcq_valid + implicit none + + integer :: ntests + external :: genft8sdvar + external :: ft8sdvar + external :: ft8svar + external :: tonesdvar + + ntests=0 + + call expect_genft8sdvar_accepts_type1_template() + call expect_genft8sdvar_requires_expanded_dx_macro() + call expect_genft8sdvar_rejects_free_text_template() + call expect_invalid_sd_template_is_not_scored() + call expect_ft8svar_last_grid_template_is_valid() + call expect_failed_qso_template_invalidates_sync() + call expect_failed_cq_template_invalidates_sync() + call expect_genft8sdvar_rejects_hash_call_without_recording_hashes() + + write(*,1000) ntests +1000 format('packjt77 unified SD-var invariant tests passed: ',i0) + +contains + + subroutine expect_genft8sdvar_accepts_type1_template() + character(len=37) :: input, msgsent + integer :: i3, n3 + integer :: itone(79) + integer*1 :: msgbits(77) + + input='K1ABC W9XYZ RR73' + msgsent=' ' + msgbits=0 + itone=0 + i3=-1 + n3=-1 + + call genft8sdvar(input,i3,n3,msgsent,msgbits,itone) + + call assert_int('sdvar Type 1 template i3',1,i3) + call assert_int('sdvar Type 1 template n3',0,n3) + call assert_text_equal('sdvar Type 1 template decode','K1ABC W9XYZ RR73',msgsent) + call assert_true('sdvar Type 1 template message bits',any(msgbits.ne.0)) + call assert_true('sdvar Type 1 template tones',any(itone.ne.0)) + + ntests=ntests+1 + end subroutine expect_genft8sdvar_accepts_type1_template + + subroutine expect_genft8sdvar_requires_expanded_dx_macro() + character(len=37) :: input, direct, msgsent + integer :: i3, n3 + integer :: itone(79) + integer*1 :: msgbits(77) + + input='$DX K1ABC FN42' + direct='W9XYZ K1ABC FN42' + msgsent=' ' + msgbits=0 + itone=0 + i3=-1 + n3=-1 + + call genft8sdvar(input,i3,n3,msgsent,msgbits,itone) + + call assert_int('sdvar unexpanded DX macro i3',-1,i3) + call assert_int('sdvar unexpanded DX macro n3',-1,n3) + call assert_text_equal('sdvar unexpanded DX macro message', & + '*** bad message ***',msgsent) + call assert_true('sdvar unexpanded DX macro message bits', & + all(msgbits.eq.0)) + call assert_true('sdvar unexpanded DX macro tones',all(itone.eq.0)) + + call genft8sdvar(direct,i3,n3,msgsent,msgbits,itone) + + call assert_int('sdvar pre-expanded DX macro i3',1,i3) + call assert_int('sdvar pre-expanded DX macro n3',0,n3) + call assert_text_equal('sdvar pre-expanded DX macro decode',direct, & + msgsent) + call assert_true('sdvar pre-expanded DX macro message bits', & + any(msgbits.ne.0)) + call assert_true('sdvar pre-expanded DX macro tones',any(itone.ne.0)) + + ntests=ntests+1 + end subroutine expect_genft8sdvar_requires_expanded_dx_macro + + subroutine expect_invalid_sd_template_is_not_scored() + real :: s8(0:7,79) + integer :: itone(79) + character(len=37) :: msgd + character(len=37) :: msg37 + logical(1) :: lft8sd, lcq + + msgd='CQ PJ4/K1ABC' + s8=0.0 + s8(0,:)=1.0 + itone=-1 + msg37='' + lft8sd=.false. + lcq=.true. + + call ft8sdvar(s8,0.0,itone,msgd,msg37,lft8sd,lcq) + + call assert_true('invalid SD template not scored',.not.logical(lft8sd)) + call assert_text_equal('invalid SD template message unchanged','',msg37) + + ntests=ntests+1 + end subroutine expect_invalid_sd_template_is_not_scored + + subroutine expect_ft8svar_last_grid_template_is_valid() + real :: s8(0:7,79) + integer :: itone(79) + character(len=37) :: decoded + logical(1) :: lft8s + + call clear_all_state('','') + + mycall='N0AAA' + hiscall='W9XYZ' + nlasttx=1 + msg(53)='N0AAA W9XYZ AA00' + lastrxmsg(1)%lstate=.true. + lastrxmsg(1)%lastmsg='N0AAA W9XYZ FN42' + idtone56(53,1:58)=0 + itone56(53,1:79)=0 + idtone56_valid(53)=.false. + s8=0.0 + itone=0 + decoded='' + lft8s=.false. + + call ft8svar(s8,0.0,itone,decoded,lft8s,1,.false.) + + call assert_true('ft8svar last grid row valid',logical(idtone56_valid(53))) + call assert_text_equal('ft8svar last grid message', & + 'N0AAA W9XYZ FN42',msg(53)) + call assert_true('ft8svar last grid tones',any(itone56(53,1:79).ne.0)) + + ntests=ntests+1 + end subroutine expect_ft8svar_last_grid_template_is_valid + + subroutine expect_failed_qso_template_invalidates_sync() + character(len=37) :: msgd + + csyncsd_valid=.true. + msgd='<PJ4/K1ABC> W9XYZ -01' + + call tonesdvar(msgd,.false.) + + call assert_true('failed QSO template invalidates sync', & + .not.logical(csyncsd_valid)) + + ntests=ntests+1 + end subroutine expect_failed_qso_template_invalidates_sync + + subroutine expect_failed_cq_template_invalidates_sync() + character(len=37) :: msgd + + csyncsdcq_valid=.true. + msgd='CQ PJ4/K1ABC' + + call tonesdvar(msgd,.true.) + + call assert_true('failed CQ template invalidates sync', & + .not.logical(csyncsdcq_valid)) + + ntests=ntests+1 + end subroutine expect_failed_cq_template_invalidates_sync + + subroutine expect_genft8sdvar_rejects_free_text_template() + character(len=37) :: input, msgsent + integer :: i3, n3 + integer :: itone(79) + integer*1 :: msgbits(77) + + input='FREE TEXT MSG' + msgsent=' ' + msgbits=1 + itone=1 + i3=-1 + n3=-1 + + call genft8sdvar(input,i3,n3,msgsent,msgbits,itone) + + call assert_bad_sd_template('sdvar free-text template',i3,n3,msgsent,msgbits,itone) + + ntests=ntests+1 + end subroutine expect_genft8sdvar_rejects_free_text_template + + subroutine expect_genft8sdvar_rejects_hash_call_without_recording_hashes() + character(len=37) :: input, msgsent + integer :: i3, n3, itone(79) + integer*1 :: msgbits(77) + + call clear_all_state('','') + + input='<PJ4/K1ABC> W9XYZ RR73' + msgsent=' ' + msgbits=0 + itone=0 + i3=-1 + n3=-1 + + call genft8sdvar(input,i3,n3,msgsent,msgbits,itone) + + call assert_bad_sd_template('sdvar hash-call template',i3,n3,msgsent,msgbits,itone) + call assert_int('sdvar generator does not record nzhash',0,nzhash) + call assert_true('sdvar generator leaves calls10 empty',all(calls10.eq.'')) + call assert_true('sdvar generator leaves calls12 empty',all(calls12.eq.'')) + call assert_true('sdvar generator leaves calls22 empty',all(calls22.eq.'')) + call assert_true('sdvar generator leaves ihash22 empty',all(ihash22.eq.-1)) + + ntests=ntests+1 + end subroutine expect_genft8sdvar_rejects_hash_call_without_recording_hashes + + subroutine assert_bad_sd_template(label,i3,n3,msgsent,msgbits,itone) + character(len=*), intent(in) :: label + integer, intent(in) :: i3, n3, itone(79) + integer*1, intent(in) :: msgbits(77) + character(len=37), intent(in) :: msgsent + + call assert_int(trim(label)//' rejected i3',-1,i3) + call assert_int(trim(label)//' rejected n3',-1,n3) + call assert_text_equal(trim(label)//' rejected message','*** bad message ***',msgsent) + call assert_true(trim(label)//' zero message bits',all(msgbits.eq.0)) + call assert_true(trim(label)//' zero tones',all(itone.eq.0)) + end subroutine assert_bad_sd_template + +end program test_packjt77sdvar_invariants diff --git a/tests/unit/packjt77/test_packjt77var_invariants.f90 b/tests/unit/packjt77/test_packjt77var_invariants.f90 new file mode 100644 index 00000000..a2395c9c --- /dev/null +++ b/tests/unit/packjt77/test_packjt77var_invariants.f90 @@ -0,0 +1,93 @@ +program test_packjt77var_invariants + + use packjt77 + use packjt77_test_helpers + implicit none + + integer :: ntests + external :: genft8var + + ntests=0 + + call expect_genft8var_rejects_unexpanded_dx_macro() + call expect_configured_var_rejects_unqualified_hash_only_message() + + write(*,1000) ntests +1000 format('packjt77var invariant tests passed: ',i0) + +contains + + subroutine expect_genft8var_rejects_unexpanded_dx_macro() + character(len=37) :: input, direct, msgsent + integer :: i3, n3 + integer :: itone(79) + integer*1 :: msgbits(77) + + input='$DX K1ABC FN42' + direct='W9XYZ K1ABC FN42' + msgsent=' ' + msgbits=0 + itone=0 + i3=-1 + n3=-1 + + call genft8var(input,i3,n3,0,msgsent,msgbits,itone) + + call assert_int('var unexpanded DX macro i3',-1,i3) + call assert_int('var unexpanded DX macro n3',-1,n3) + call assert_text_equal('var unexpanded DX macro message', & + '*** bad message ***',msgsent) + call assert_true('var unexpanded DX macro message bits', & + all(msgbits.eq.0)) + call assert_true('var unexpanded DX macro tones',all(itone.eq.0)) + + call genft8var(direct,i3,n3,0,msgsent,msgbits,itone) + + call assert_int('var pre-expanded DX macro i3',1,i3) + call assert_int('var pre-expanded DX macro n3',0,n3) + call assert_text_equal('var pre-expanded DX macro decode',direct,msgsent) + call assert_true('var pre-expanded DX macro message bits', & + any(msgbits.ne.0)) + call assert_true('var pre-expanded DX macro tones',any(itone.ne.0)) + + ntests=ntests+1 + end subroutine expect_genft8var_rejects_unexpanded_dx_macro + + subroutine expect_configured_var_rejects_unqualified_hash_only_message() + character(len=77) :: c77 + character(len=13) :: c13 + character(len=37) :: decoded, input + integer :: n10, n12, n22 + logical :: ok + + input='PJ2/W1AW <W7ABC>' + call reset_packjt77var_state('N0AAA', 'N0BBB') + block + type(pack77_result) :: encoded + encoded=pack77_result_from_api(input) + call assert_pack77_result('configured-var delta',input,encoded) + call assert_int('configured-var delta i3',4,encoded%i3) + call assert_int('configured-var delta n3',0,encoded%n3) + c77=encoded%c77 + end block + + call reset_packjt77var_state('N0AAA', 'N0BBB') + call normalize_call('W7ABC',c13) + n10=ihashcall(c13,10) + n12=ihashcall(c13,12) + n22=ihashcall(c13,22) + call save_hash_call(c13,n10,n12,n22) + + decoded=' ' + ok=.true. + call unpack77_configured(c77,0,decoded,ok,unpack77_options(thread_index=1)) + if(ok) then + write(*,1010) trim(decoded) +1010 format('Configured-var hash-only delta decoded as "',a,'"') + error stop 1 + endif + + ntests=ntests+1 + end subroutine expect_configured_var_rejects_unqualified_hash_only_message + +end program test_packjt77var_invariants |
