diff options
| author | Christoph Berg <myon@debian.org> | 2026-10-14 14:39:20 +0200 |
|---|---|---|
| committer | Christoph Berg <myon@debian.org> | 2026-10-14 14:39:20 +0200 |
| commit | c576a4281ef8c87990ae6399393a827dfaccacd5 (patch) | |
| tree | 0f28b7d1fc2ba73ccea5d7513281b0f8cae76fb6 /tests/unit | |
| parent | ffc62c792c9a5196cb4ce7a353287dfe20db6524 (diff) | |
New upstream version 3.3.0~beta2+dfsgupstream/3.3.0_beta2+dfsgupstream
Diffstat (limited to 'tests/unit')
| -rw-r--r-- | tests/unit/decoder_engine/CMakeLists.txt | 56 | ||||
| -rw-r--r-- | tests/unit/decoder_engine/jt4_engine_signal.f90 | 47 | ||||
| -rw-r--r-- | tests/unit/decoder_engine/test_decoder_engine.c | 40 | ||||
| -rw-r--r-- | tests/unit/decoder_engine/test_decoder_engine_jt4.c | 470 | ||||
| -rw-r--r-- | tests/unit/decoder_engine/test_decoder_engine_jt4_host.f90 | 213 | ||||
| -rw-r--r-- | tests/unit/decoder_engine/test_decoder_engine_jt4_state.f90 | 112 | ||||
| -rw-r--r-- | tests/unit/jtty/jtty_cpp_n1mm_smoke.cpp | 37 | ||||
| -rw-r--r-- | tests/unit/jtty/test_jtty_messages.cpp | 48 | ||||
| -rw-r--r-- | tests/unit/jtty/test_jtty_structured_decode.f90 | 29 | ||||
| -rw-r--r-- | tests/unit/map65/test_map65_pass_snapshots.f90 | 100 | ||||
| -rw-r--r-- | tests/unit/streaming/CMakeLists.txt | 6 | ||||
| -rw-r--r-- | tests/unit/streaming/test_streaming_consumers.cpp | 162 |
12 files changed, 1315 insertions, 5 deletions
diff --git a/tests/unit/decoder_engine/CMakeLists.txt b/tests/unit/decoder_engine/CMakeLists.txt index c2546109..82965b4b 100644 --- a/tests/unit/decoder_engine/CMakeLists.txt +++ b/tests/unit/decoder_engine/CMakeLists.txt @@ -10,6 +10,9 @@ target_include_directories (test_decoder_engine PRIVATE ${CMAKE_SOURCE_DIR}) target_link_libraries (test_decoder_engine PRIVATE test_decoder_engine_signal) add_test (NAME test_decoder_engine COMMAND test_decoder_engine) set_tests_properties (test_decoder_engine PROPERTIES TIMEOUT 90) +add_test (NAME prepare_ft8_engine_wav COMMAND test_decoder_engine + --write-wav ${CMAKE_CURRENT_BINARY_DIR}/ft8-engine.wav) +set_tests_properties (prepare_ft8_engine_wav PROPERTIES FIXTURES_SETUP ft8_engine_wav TIMEOUT 15) add_executable (test_decoder_engine_ft4 test_decoder_engine_ft4.c) set_target_properties (test_decoder_engine_ft4 PROPERTIES LINKER_LANGUAGE CXX) @@ -220,3 +223,56 @@ if (TARGET wsjt_fort_omp) set_tests_properties (test_decoder_engine_fst4_omp PROPERTIES TIMEOUT 360 PROCESSORS 2 ENVIRONMENT "OMP_DYNAMIC=FALSE;OMP_NUM_THREADS=2;OMP_THREAD_LIMIT=2;OMP_STACKSIZE=1M") endif () + +add_library (test_decoder_engine_jt4_signal STATIC jt4_engine_signal.f90) +wsjt_link_common_fortran (test_decoder_engine_jt4_signal) +add_executable (test_decoder_engine_jt4 test_decoder_engine_jt4.c) +set_target_properties (test_decoder_engine_jt4 PROPERTIES LINKER_LANGUAGE CXX) +target_include_directories (test_decoder_engine_jt4 PRIVATE ${CMAKE_SOURCE_DIR}) +target_link_libraries (test_decoder_engine_jt4 PRIVATE test_decoder_engine_jt4_signal Threads::Threads) +add_test (NAME test_decoder_engine_jt4 COMMAND test_decoder_engine_jt4 --small-stack) +set_tests_properties (test_decoder_engine_jt4 PROPERTIES TIMEOUT 180 + ENVIRONMENT "OMP_DYNAMIC=FALSE;OMP_NUM_THREADS=1;OMP_THREAD_LIMIT=1;OMP_STACKSIZE=1M") +add_executable (test_decoder_engine_jt4_state test_decoder_engine_jt4_state.f90) +set_target_properties (test_decoder_engine_jt4_state PROPERTIES LINKER_LANGUAGE CXX) +wsjt_link_common_fortran (test_decoder_engine_jt4_state) +add_test (NAME test_decoder_engine_jt4_state COMMAND test_decoder_engine_jt4_state) +set_tests_properties (test_decoder_engine_jt4_state PROPERTIES TIMEOUT 30) +add_test (NAME prepare_jt4_engine_wav COMMAND test_decoder_engine_jt4 + --write-wav ${CMAKE_CURRENT_BINARY_DIR}/jt4-engine.wav) +set_tests_properties (prepare_jt4_engine_wav PROPERTIES FIXTURES_SETUP jt4_engine_wav TIMEOUT 15) +if (TARGET wsjt_fort_omp AND TARGET wsjt_fort) + add_library (test_decoder_engine_jt4_signal_omp STATIC jt4_engine_signal.f90) + target_link_libraries (test_decoder_engine_jt4_signal_omp PUBLIC wsjt_fort_omp wsjt_fortran_openmp) + add_executable (test_decoder_engine_jt4_omp test_decoder_engine_jt4.c) + set_target_properties (test_decoder_engine_jt4_omp PROPERTIES LINKER_LANGUAGE CXX) + target_include_directories (test_decoder_engine_jt4_omp PRIVATE ${CMAKE_SOURCE_DIR}) + target_link_libraries (test_decoder_engine_jt4_omp PRIVATE test_decoder_engine_jt4_signal_omp Threads::Threads) + add_test (NAME test_decoder_engine_jt4_omp COMMAND test_decoder_engine_jt4_omp --smoke) + set_tests_properties (test_decoder_engine_jt4_omp PROPERTIES TIMEOUT 90 + ENVIRONMENT "OMP_DYNAMIC=FALSE;OMP_NUM_THREADS=1;OMP_THREAD_LIMIT=1;OMP_STACKSIZE=1M") +endif () + +add_test (NAME test_jt4_engine_cli COMMAND ${CMAKE_COMMAND} + "-DJT9_EXECUTABLE=$<TARGET_FILE:jt9>" + "-DJT4_WAV=${CMAKE_CURRENT_BINARY_DIR}/jt4-engine.wav" + "-DTEST_DIRECTORY=${CMAKE_CURRENT_BINARY_DIR}/jt4-cli" + -P ${CMAKE_SOURCE_DIR}/tests/integration/jt9/verify_jt4_engine.cmake) +set_tests_properties (test_jt4_engine_cli PROPERTIES FIXTURES_REQUIRED jt4_engine_wav TIMEOUT 60) + +add_executable (test_decoder_engine_jt4_host test_decoder_engine_jt4_host.f90) +set_target_properties (test_decoder_engine_jt4_host PROPERTIES LINKER_LANGUAGE CXX) +target_link_libraries (test_decoder_engine_jt4_host PRIVATE test_decoder_engine_jt4_signal) +wsjt_link_common_fortran (test_decoder_engine_jt4_host) +file (MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/jt4-host) +add_test (NAME test_decoder_engine_jt4_host COMMAND test_decoder_engine_jt4_host) +set_tests_properties (test_decoder_engine_jt4_host PROPERTIES + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/jt4-host TIMEOUT 90) + +add_test (NAME test_jt4_12000_read_extent COMMAND ${CMAKE_COMMAND} + "-DJT9=$<TARGET_FILE:jt9>" + "-DJT4_WAV=${CMAKE_CURRENT_BINARY_DIR}/jt4-engine.wav" + "-DWORK_DIR=${CMAKE_CURRENT_BINARY_DIR}/jt4-read-extent" + -P ${CMAKE_SOURCE_DIR}/tests/integration/jt9/verify_jt4_12000_read_extent.cmake) +set_tests_properties (test_jt4_12000_read_extent PROPERTIES + FIXTURES_REQUIRED jt4_engine_wav LABELS "decoder;jt4;integration" TIMEOUT 60) diff --git a/tests/unit/decoder_engine/jt4_engine_signal.f90 b/tests/unit/decoder_engine/jt4_engine_signal.f90 new file mode 100644 index 00000000..0599b7d5 --- /dev/null +++ b/tests/unit/decoder_engine/jt4_engine_signal.f90 @@ -0,0 +1,47 @@ +subroutine decoder_engine_test_jt4_signal(rate,submode,seed,amplitude,noise,frequency,samples) bind(C) + use iso_c_binding, only: c_int,c_int16_t,c_float + implicit none + integer(c_int), value :: rate,submode,seed + real(c_float), value :: amplitude,noise,frequency + integer(c_int16_t), intent(out) :: samples(60*rate) + call jt4_engine_make_signal('K1ABC W9XYZ FN42',rate,submode,seed,amplitude,noise,frequency,samples) +end subroutine + +subroutine jt4_engine_make_signal(message,rate,submode,seed,amplitude,noise,frequency,samples) + use iso_c_binding, only: c_int,c_int16_t,c_float + use iso_fortran_env, only: int64,real64 + use jt4, only: nch + implicit none + character(len=*), intent(in) :: message + integer(c_int), intent(in) :: rate,submode,seed + real(c_float), intent(in) :: amplitude,noise,frequency + integer(c_int16_t), intent(out) :: samples(60*rate) + real(real64), parameter :: twopi=6.283185307179586476925286766559_real64 + integer(int64) :: state + integer :: tones(206),i,symbol,itype + real(real64) :: phase,value,u1,u2,freq + character(len=22) :: encoded,sent + + if(submode<0.or.submode>6) error stop 'invalid JT4 fixture submode' + if(rate/=11025.and.rate/=12000) error stop 'invalid JT4 fixture sample rate' + encoded=message + call gen4(encoded,0,sent,tones,itype) + state=104729_int64+int(seed,int64) + phase=0.0_real64 + do i=1,size(samples) + state=modulo(16807_int64*state,2147483647_int64) + u1=max(real(state,real64)/2147483647.0_real64,tiny(1.0_real64)) + state=modulo(16807_int64*state,2147483647_int64) + u2=real(state,real64)/2147483647.0_real64 + value=noise*sqrt(-2.0_real64*log(u1))*cos(twopi*u2) + if(i>rate) then + symbol=int(real(i-rate-1,real64)*4.375_real64/rate)+1 + if(symbol<=206) then + freq=frequency+tones(symbol)*4.375_real64*nch(submode+1) + phase=modulo(phase+twopi*freq/rate,twopi) + value=value+amplitude*sin(phase) + endif + endif + samples(i)=int(nint(max(-32768.0_real64,min(32767.0_real64,value))),c_int16_t) + enddo +end subroutine diff --git a/tests/unit/decoder_engine/test_decoder_engine.c b/tests/unit/decoder_engine/test_decoder_engine.c index 218688b3..71d9e3be 100644 --- a/tests/unit/decoder_engine/test_decoder_engine.c +++ b/tests/unit/decoder_engine/test_decoder_engine.c @@ -270,8 +270,44 @@ static void check_mtd_lifecycle(callback_context *context, decoder_attempt_reque free(saved); } -int main(void) +static void write_le(FILE *file, uint32_t value, unsigned bytes) { + for (unsigned i = 0; i < bytes; ++i) CHECK(fputc((value >> (8 * i)) & 255, file) != EOF); +} + +static void write_wav(const char *path) +{ + const int count = 180000; + int16_t *samples = malloc(count * sizeof *samples); + int8_t payload[77], tones[79]; + CHECK(samples); + decoder_engine_test_signal(samples, payload, tones, 6000); + FILE *file = fopen(path, "wb"); + CHECK(file); + CHECK(fwrite("RIFF", 1, 4, file) == 4); + write_le(file, 36 + count * 2, 4); + CHECK(fwrite("WAVEfmt ", 1, 8, file) == 8); + write_le(file, 16, 4); + write_le(file, 1, 2); + write_le(file, 1, 2); + write_le(file, 12000, 4); + write_le(file, 24000, 4); + write_le(file, 2, 2); + write_le(file, 16, 2); + CHECK(fwrite("data", 1, 4, file) == 4); + write_le(file, count * 2, 4); + for (int i = 0; i < count; ++i) write_le(file, (uint16_t)samples[i], 2); + CHECK(fclose(file) == 0); + free(samples); +} + +int main(int argc, char **argv) +{ + if (argc == 3 && !strcmp(argv[1], "--write-wav")) { + write_wav(argv[2]); + return 0; + } + CHECK(argc == 1); decoder_engine_options options = {DECODER_ENGINE_ABI}; decoder_engine_handle engine = NULL, second = NULL; decoder_engine_capabilities capabilities; @@ -293,7 +329,7 @@ int main(void) CHECK(decoder_engine_create(&options, &second) == DECODER_BUSY); CHECK(decoder_engine_get_capabilities(engine, &capabilities) == DECODER_OK); CHECK(capabilities.abi_version == DECODER_ENGINE_ABI); - CHECK(capabilities.supported_modes == (DECODER_SUPPORT_FT8 | DECODER_SUPPORT_FT4 | DECODER_SUPPORT_JT9 | DECODER_SUPPORT_JT65 | DECODER_SUPPORT_Q65 | DECODER_SUPPORT_FST4 | DECODER_SUPPORT_FST4W)); + CHECK(capabilities.supported_modes == (DECODER_SUPPORT_FT8 | DECODER_SUPPORT_FT4 | DECODER_SUPPORT_JT9 | DECODER_SUPPORT_JT65 | DECODER_SUPPORT_Q65 | DECODER_SUPPORT_FST4 | DECODER_SUPPORT_FST4W | DECODER_SUPPORT_JT4)); CHECK(capabilities.cancellation == 0); CHECK(capabilities.concurrent_sessions == 0); CHECK(capabilities.evidence_capacity == 1024); diff --git a/tests/unit/decoder_engine/test_decoder_engine_jt4.c b/tests/unit/decoder_engine/test_decoder_engine_jt4.c new file mode 100644 index 00000000..3b1d9e7d --- /dev/null +++ b/tests/unit/decoder_engine/test_decoder_engine_jt4.c @@ -0,0 +1,470 @@ +#if defined(__linux__) && !defined(_GNU_SOURCE) +#define _GNU_SOURCE +#endif +#include "DecoderEngine.h" +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#ifdef _WIN32 +#include <windows.h> +#else +#include <pthread.h> +#endif +#ifdef __linux__ +#include <link.h> +#endif + +#define CHECK(condition) do { \ + if (!(condition)) { \ + fprintf(stderr, "%s:%d: %s\n", __FILE__, __LINE__, #condition); \ + exit(EXIT_FAILURE); \ + } \ +} while (0) + +enum { SAMPLE_CAPACITY = 60 * 12000 }; +extern void decoder_engine_test_jt4_signal(int, int, int, float, float, float, int16_t *); + +typedef struct { + decoder_engine_handle engine; + decoder_attempt_request request; + decoder_audio_view audio; + int16_t *samples, *before; + int observations, messages, method, average_count, kind; + float sync; +} fixture; + +static void observe(const decoder_observation *record, void *opaque) +{ + fixture *f = opaque; + CHECK(record->input_id == f->request.input_id); + CHECK(record->analysis_id == f->request.analysis_id); + CHECK(record->attempt_no == f->request.attempt_no); + CHECK(record->mode == DECODER_MODE_JT4); + CHECK(record->variant == f->request.jt4.submode); + CHECK(isfinite(record->sync) && isfinite(record->frequency_hz)); + CHECK(isfinite(record->dt_seconds) && isfinite(record->quality)); + CHECK(record->ap_type == 0); + CHECK(record->ft8.payload_origin == DECODER_PAYLOAD_NONE); + CHECK(record->ft4.payload_origin == DECODER_PAYLOAD_NONE); + CHECK(record->jt4.kind >= DECODER_JT4_NO_SYNC && record->jt4.kind <= DECODER_JT4_MESSAGE); + ++f->observations; + f->kind = record->jt4.kind; + f->sync = record->sync; + if (record->jt4.kind == DECODER_JT4_MESSAGE) { + CHECK(!strcmp(record->message, "K1ABC W9XYZ FN42")); + CHECK(record->jt4.method == DECODER_JT4_METHOD_FANO || + record->jt4.method == DECODER_JT4_METHOD_DEEP_SEARCH); + CHECK(record->jt4.average_count >= 0 && record->jt4.average_count <= 64); + CHECK(record->jt4.width_profile >= 0 && record->jt4.width_profile <= f->request.jt4.submode); + CHECK(record->jt4.sync_polarity == 1); + CHECK(fabsf(record->frequency_hz - 1000) < 10); + CHECK(fabsf(record->dt_seconds) < 0.4f); + ++f->messages; + f->method = record->jt4.method; + f->average_count = record->jt4.average_count; + } else { + CHECK(!record->message[0]); + CHECK(record->jt4.method == DECODER_JT4_METHOD_NONE); + } + CHECK(decoder_engine_release_input(f->engine, record->input_id) == DECODER_BUSY); + CHECK(decoder_engine_clear_jt4_averages(f->engine) == DECODER_BUSY); + CHECK(decoder_engine_set_jt4_calls(f->engine, NULL, 0) == DECODER_BUSY); +} + +static void create(fixture *f) +{ + decoder_engine_options options = {DECODER_ENGINE_ABI}; + decoder_engine_capabilities capabilities; + f->samples = malloc(SAMPLE_CAPACITY * sizeof *f->samples); + f->before = malloc(SAMPLE_CAPACITY * sizeof *f->before); + CHECK(f->samples && f->before); + f->audio.samples = f->samples; + CHECK(decoder_engine_create(&options, &f->engine) == DECODER_OK); + CHECK(decoder_engine_get_capabilities(f->engine, &capabilities) == DECODER_OK); + CHECK(capabilities.supported_modes & DECODER_SUPPORT_JT4); +} + +static void prepare(fixture *f, int rate, int submode, int utc) +{ + static int64_t next_id = 100; + memset(&f->request, 0, sizeof f->request); + f->request.input_id = ++next_id; + f->request.analysis_id = next_id; + f->request.attempt_no = 1; + f->request.mode = DECODER_MODE_JT4; + f->request.phase = DECODER_PHASE_NORMAL; + f->request.source = DECODER_SOURCE_FILE; + f->request.jt4.utc = utc; + f->request.jt4.submode = submode; + f->request.jt4.receive_frequency_hz = 1000; + f->request.jt4.tolerance_hz = 50; + f->request.jt4.depth = 1; + memset(f->request.jt4.mycall, ' ', sizeof f->request.jt4.mycall); + memset(f->request.jt4.hiscall, ' ', sizeof f->request.jt4.hiscall); + memset(f->request.jt4.hisgrid, ' ', sizeof f->request.jt4.hisgrid); + f->audio.sample_count = 60 * rate; + f->audio.sample_rate_hz = rate; + decoder_engine_test_jt4_signal(rate, submode, utc + 1, 300, 100, 1000, f->samples); +} + +static void decode(fixture *f, int expected) +{ + decoder_attempt_outcome outcome; + decoder_attempt_request before_request = f->request; + size_t bytes = f->audio.sample_count * sizeof *f->samples; + memcpy(f->before, f->samples, bytes); + f->observations = f->messages = f->average_count = 0; + f->method = DECODER_JT4_METHOD_NONE; + f->kind = -1; + f->sync = NAN; + CHECK(decoder_engine_decode(f->engine, &f->request, &f->audio, observe, f, &outcome) == DECODER_OK); + CHECK(outcome.status == DECODER_OK && outcome.observation_count == f->observations); + CHECK(outcome.evidence_dropped == 0); + CHECK(!memcmp(&before_request, &f->request, sizeof before_request)); + CHECK(!memcmp(f->before, f->samples, bytes)); + if (expected >= 0) { + if (!!f->messages != !!expected) + fprintf(stderr, "JT4%c %d Hz UTC %06d: got %d messages, expected %d\n", + 'A' + f->request.jt4.submode, f->audio.sample_rate_hz, + f->request.jt4.utc, f->messages, expected); + CHECK(!!f->messages == !!expected); + } +} + +static void release(fixture *f) +{ + CHECK(decoder_engine_release_input(f->engine, f->request.input_id) == DECODER_OK); +} + +static void repeat(fixture *f) +{ + f->request.phase = DECODER_PHASE_REPEAT; + ++f->request.analysis_id; + ++f->request.attempt_no; +} + +static void destroy(fixture *f) +{ + CHECK(decoder_engine_destroy(f->engine) == DECODER_OK); + free(f->before); + free(f->samples); +} + +static void invalid(fixture *f) +{ + decoder_attempt_outcome outcome; + f->observations = 0; + CHECK(decoder_engine_decode(f->engine, &f->request, &f->audio, observe, f, &outcome) == DECODER_INVALID); + CHECK(f->observations == 0); +} + +static void lifecycle(fixture *f) +{ + prepare(f, 11025, 0, 120000); + decode(f, 1); + CHECK(f->method == DECODER_JT4_METHOD_FANO && !f->average_count); + repeat(f); + --f->audio.sample_count; + invalid(f); + ++f->audio.sample_count; + f->audio.sample_rate_hz = 12000; + invalid(f); + f->audio.sample_rate_hz = 11025; + f->request.jt4.receive_frequency_hz = 1500; + decode(f, 0); + repeat(f); + f->request.jt4.receive_frequency_hz = 1000; + decode(f, 1); + release(f); + + prepare(f, 12000, 0, 120100); + decode(f, 1); + release(f); + CHECK(decoder_engine_reset_session(f->engine) == DECODER_OK); + prepare(f, 11025, 0, 120200); + f->audio.sample_count = 1; + f->samples[0] = 0; + decode(f, 0); + CHECK(f->observations == 1); + release(f); + prepare(f, 11025, 0, 120300); + decode(f, 1); + release(f); + prepare(f, 11025, 0, 120400); + memset(f->samples, 0, f->audio.sample_count * sizeof *f->samples); + decode(f, 0); + CHECK(f->observations == 1); + release(f); +} + +static void averages(fixture *f, int expected) +{ + decoder_jt4_average_entry entries[DECODER_JT4_AVERAGE_CAPACITY]; + int32_t count; + CHECK(decoder_engine_get_jt4_averages(f->engine, NULL, 0, &count) == DECODER_OK); + CHECK(count == expected); + CHECK(decoder_engine_get_jt4_averages(f->engine, entries, DECODER_JT4_AVERAGE_CAPACITY, &count) == DECODER_OK); + CHECK(count == expected); + for (int i = 0; i < count; ++i) { + CHECK(entries[i].utc == 120000 + i * 200); + CHECK(entries[i].polarity == 1 && entries[i].used == 1); + CHECK(abs(entries[i].frequency_hz - 1000) < 10); + CHECK(isfinite(entries[i].sync) && isfinite(entries[i].dt_seconds)); + } + if (count > 1) { + CHECK(decoder_engine_get_jt4_averages(f->engine, entries, 1, &count) == DECODER_CAPACITY); + CHECK(count == expected); + } +} + +static void weak_averaging(fixture *f) +{ + CHECK(decoder_engine_reset_session(f->engine) == DECODER_OK); + for (int i = 0; i < 2; ++i) { + prepare(f, 11025, 0, 120000 + i * 200); + f->request.jt4.depth = 3; + f->request.jt4.averaging = 1; + decoder_engine_test_jt4_signal(11025, 0, 5 + i, 5.0f, 100, 1000, f->samples); + decode(f, i == 1); + averages(f, i + 1); + if (i == 0) release(f); + } + CHECK(f->messages == 1 && f->method == DECODER_JT4_METHOD_FANO && f->average_count == 2); + repeat(f); + decode(f, 1); + CHECK(f->average_count == 2); + averages(f, 2); + CHECK(decoder_engine_clear_jt4_averages(f->engine) == DECODER_OK); + averages(f, 0); + repeat(f); + decode(f, 0); + int32_t count; + decoder_jt4_average_entry entry; + CHECK(decoder_engine_get_jt4_averages(f->engine, &entry, 1, &count) == DECODER_OK); + CHECK(count == 1 && entry.utc == 120200); + release(f); + prepare(f, 11025, 1, 120400); + decode(f, 1); + averages(f, 0); + release(f); +} + +static void forced_decode(fixture *f) +{ + CHECK(decoder_engine_reset_session(f->engine) == DECODER_OK); + prepare(f, 11025, 0, 120000); + f->request.jt4.tolerance_hz = 0; + f->request.jt4.averaging = 1; + decoder_engine_test_jt4_signal(11025, 0, 1, 0, 100, 1000, f->samples); + decode(f, 0); + CHECK(f->kind == DECODER_JT4_NO_SYNC && f->sync == 0); + int32_t count; + CHECK(decoder_engine_get_jt4_averages(f->engine, NULL, 0, &count) == DECODER_OK && count == 0); + repeat(f); + f->request.jt4.min_sync = -1; + decode(f, 0); + CHECK(f->kind == DECODER_JT4_SYNC && f->sync == 0); + CHECK(decoder_engine_get_jt4_averages(f->engine, NULL, 0, &count) == DECODER_OK && count == 1); + release(f); + + CHECK(decoder_engine_reset_session(f->engine) == DECODER_OK); + prepare(f, 11025, 0, 120200); + f->request.jt4.min_sync = -1; + f->request.jt4.averaging = 1; + memset(f->samples, 0, f->audio.sample_count * sizeof *f->samples); + decode(f, 0); + CHECK(f->kind == DECODER_JT4_NO_SYNC && f->sync == 0); + CHECK(decoder_engine_get_jt4_averages(f->engine, NULL, 0, &count) == DECODER_OK && count == 0); + release(f); +} + +static void deep_search(fixture *f) +{ + decoder_jt4_call call = {"W9XYZ ", "FN42"}; + CHECK(decoder_engine_reset_session(f->engine) == DECODER_OK); + prepare(f, 11025, 0, 120000); + f->request.jt4.depth = 3; + memcpy(f->request.jt4.mycall, "K1ABC ", 12); + decoder_engine_test_jt4_signal(11025, 0, 1, 5.0f, 100, 1000, f->samples); + decode(f, 0); + CHECK(decoder_engine_set_jt4_calls(f->engine, &call, 1) == DECODER_OK); + memset(&call, 0, sizeof call); + repeat(f); + f->request.jt4.deep_search = 1; + decode(f, 1); + CHECK(f->method == DECODER_JT4_METHOD_DEEP_SEARCH && !f->average_count); + memcpy(call.call, "N0CALL ", 12); + memcpy(call.grid, "EM00", 4); + CHECK(decoder_engine_set_jt4_calls(f->engine, &call, 1) == DECODER_OK); + repeat(f); + decode(f, 0); + CHECK(decoder_engine_set_jt4_calls(f->engine, NULL, 0) == DECODER_OK); + memcpy(f->request.jt4.hiscall, "W9XYZ ", 12); + memcpy(f->request.jt4.hisgrid, "FN42 ", 6); + repeat(f); + decode(f, 1); + CHECK(f->method == DECODER_JT4_METHOD_DEEP_SEARCH); + memcpy(f->request.jt4.hiscall, "N0CALL ", 12); + memcpy(f->request.jt4.hisgrid, "EM00 ", 6); + repeat(f); + decode(f, 0); + release(f); + memcpy(call.call, "W9XYZ ", 12); + memcpy(call.grid, "FN42", 4); + CHECK(decoder_engine_set_jt4_calls(f->engine, &call, 1) == DECODER_OK); + CHECK(decoder_engine_reset_session(f->engine) == DECODER_OK); + prepare(f, 11025, 0, 120200); + f->request.jt4.depth = 3; + f->request.jt4.deep_search = 1; + memcpy(f->request.jt4.mycall, "K1ABC ", 12); + decoder_engine_test_jt4_signal(11025, 0, 1, 5.0f, 100, 1000, f->samples); + decode(f, 0); + release(f); +} + +static void run_tests(int smoke) +{ + fixture f = {0}; + create(&f); + for (int submode = 0; submode < (smoke ? 1 : 7); ++submode) { + prepare(&f, submode == 5 ? 12000 : 11025, submode, 0); + decode(&f, 1); + CHECK(f.method == DECODER_JT4_METHOD_FANO && !f.average_count); + release(&f); + } + if (!smoke) { + decoder_jt4_call call = {"W9XYZ ", "FN42"}; + CHECK(decoder_engine_set_jt4_calls(f.engine, &call, 1) == DECODER_OK); + memset(&call, 0, sizeof call); + CHECK(decoder_engine_set_jt4_calls(f.engine, NULL, 1) == DECODER_INVALID); + CHECK(decoder_engine_set_jt4_calls(f.engine, NULL, DECODER_JT4_CALL_CAPACITY + 1) == DECODER_INVALID); + CHECK(decoder_engine_set_jt4_calls(f.engine, NULL, 0) == DECODER_OK); + int32_t count = -1; + CHECK(decoder_engine_get_jt4_averages(f.engine, NULL, 0, &count) == DECODER_OK && count == 0); + prepare(&f, 11025, 0, 0); + f.request.phase = DECODER_PHASE_EARLY; + invalid(&f); + f.request.phase = DECODER_PHASE_NORMAL; + f.request.jt4.minimum_width_profile = 1; + invalid(&f); + f.request.jt4.minimum_width_profile = 0; + f.request.jt4.utc = 126000; + invalid(&f); + f.request.jt4.utc = 0; + f.request.jt4.submode = 7; + invalid(&f); + f.request.jt4.submode = 0; + f.audio.sample_rate_hz = 8000; + decoder_attempt_outcome outcome; + f.observations = 0; + CHECK(decoder_engine_decode(f.engine, &f.request, &f.audio, observe, &f, &outcome) == DECODER_UNSUPPORTED); + CHECK(f.observations == 0); + f.audio.sample_rate_hz = 11025; + ++f.audio.sample_count; + invalid(&f); + --f.audio.sample_count; + f.request.jt4.averaging = 2; + invalid(&f); + } + lifecycle(&f); + if (!smoke) { + weak_averaging(&f); + forced_decode(&f); + deep_search(&f); + } + CHECK(decoder_engine_destroy(f.engine) == DECODER_OK); + decoder_engine_options options = {DECODER_ENGINE_ABI}; + CHECK(decoder_engine_create(&options, &f.engine) == DECODER_OK); + prepare(&f, 11025, 0, 130000); + decode(&f, 1); + destroy(&f); +} + +static void write_le(FILE *file, uint32_t value, unsigned bytes) +{ + for (unsigned i = 0; i < bytes; ++i) CHECK(fputc((value >> (8 * i)) & 255, file) != EOF); +} + +static void write_wav(const char *path) +{ + int16_t *samples = malloc(SAMPLE_CAPACITY * sizeof *samples); + CHECK(samples); + decoder_engine_test_jt4_signal(12000, 0, 1, 300, 100, 1000, samples); + FILE *file = fopen(path, "wb"); + CHECK(file); + CHECK(fwrite("RIFF", 1, 4, file) == 4); + write_le(file, 36 + SAMPLE_CAPACITY * 2, 4); + CHECK(fwrite("WAVEfmt ", 1, 8, file) == 8); + write_le(file, 16, 4); + write_le(file, 1, 2); + write_le(file, 1, 2); + write_le(file, 12000, 4); + write_le(file, 24000, 4); + write_le(file, 2, 2); + write_le(file, 16, 2); + CHECK(fwrite("data", 1, 4, file) == 4); + write_le(file, SAMPLE_CAPACITY * 2, 4); + for (int i = 0; i < SAMPLE_CAPACITY; ++i) write_le(file, (uint16_t)samples[i], 2); + CHECK(fclose(file) == 0); + free(samples); +} + +#ifdef _WIN32 +static DWORD WINAPI run_on_small_stack(LPVOID unused) +#else +static void *run_on_small_stack(void *unused) +#endif +{ + (void)unused; + run_tests(0); + return 0; +} + +#ifdef __linux__ +static int add_tls_size(struct dl_phdr_info *info, size_t size, void *opaque) +{ + (void)size; + size_t *stack_size = opaque; + for (int i = 0; i < info->dlpi_phnum; ++i) { + const ElfW(Phdr) *header = &info->dlpi_phdr[i]; + if (header->p_type == PT_TLS) *stack_size += header->p_memsz + header->p_align; + } + return 0; +} +#endif + +int main(int argc, char **argv) +{ + if (argc == 3 && !strcmp(argv[1], "--write-wav")) write_wav(argv[2]); + else if (argc == 2 && !strcmp(argv[1], "--smoke")) run_tests(1); + else if (argc == 1) run_tests(0); + else { + CHECK(argc == 2 && !strcmp(argv[1], "--small-stack")); +#ifdef _WIN32 + HANDLE thread = CreateThread(NULL, 1024 * 1024, run_on_small_stack, NULL, + STACK_SIZE_PARAM_IS_A_RESERVATION, NULL); + CHECK(thread != NULL); + CHECK(WaitForSingleObject(thread, INFINITE) == WAIT_OBJECT_0); + DWORD status; + CHECK(GetExitCodeThread(thread, &status) && status == 0); + CHECK(CloseHandle(thread)); +#else + pthread_attr_t attributes; + pthread_t thread; + size_t stack_size = 1024 * 1024; +#ifdef __linux__ + /* Linux puts static TLS in the stack mapping; reserve 1 MiB for calls. */ + dl_iterate_phdr(add_tls_size, &stack_size); +#endif + CHECK(pthread_attr_init(&attributes) == 0); + CHECK(pthread_attr_setstacksize(&attributes, stack_size) == 0); + int error = pthread_create(&thread, &attributes, run_on_small_stack, NULL); + if (error) fprintf(stderr, "pthread_create with %zu bytes: %s\n", stack_size, strerror(error)); + CHECK(error == 0); + CHECK(pthread_attr_destroy(&attributes) == 0); + CHECK(pthread_join(thread, NULL) == 0); +#endif + } + return EXIT_SUCCESS; +} diff --git a/tests/unit/decoder_engine/test_decoder_engine_jt4_host.f90 b/tests/unit/decoder_engine/test_decoder_engine_jt4_host.f90 new file mode 100644 index 00000000..2b228894 --- /dev/null +++ b/tests/unit/decoder_engine/test_decoder_engine_jt4_host.f90 @@ -0,0 +1,213 @@ +program test_decoder_engine_jt4_host + use iso_c_binding, only: c_int,c_int8_t,c_int16_t,c_int64_t,c_float,c_sizeof + use decoder_engine, only: engine_host_decode + use decoder_engine_types, only: engine_jt4_options + use ft8_engine_kernel, only: params_block + use jt4_decode, only: jt4_decoder + use jt4_host_support, only: convert_jt4_host_options,load_jt4_calls,write_jt4_averages + use decode_completion_module, only: decode_completion_result + use prog_args, only: data_dir,temp_dir + implicit none + type(params_block) :: params,before_params + type(engine_jt4_options) :: options + type(jt4_decoder), allocatable :: decoder + type(decode_completion_result) :: completion + integer(c_int8_t) :: zeros(c_sizeof(params)) + integer(c_int16_t), allocatable, target :: samples(:) + integer(c_int16_t), allocatable :: before(:) + integer(c_int64_t) :: input_id=700,analysis_id=700 + integer :: npatience,nthreads,unit,ios,attempt=1 + character(len=100) :: line + logical :: exists + common/patience/npatience,nthreads + interface + subroutine signal(rate,submode,seed,amplitude,noise,frequency,samples) bind(C,name='decoder_engine_test_jt4_signal') + import c_int,c_int16_t,c_float + integer(c_int), value :: rate,submode,seed + real(c_float), value :: amplitude,noise,frequency + integer(c_int16_t), intent(out) :: samples(*) + end subroutine + end interface + + data_dir='.' + temp_dir='.' + zeros=0 + params=transfer(zeros,params) + params%nmode=4 + params%ntr=60 + params%nutc=1234 + params%nfqso=1000 + params%ntol=50 + params%ndepth=3+16+32+128 + params%mycall=transfer('K1ABC ',params%mycall) + params%hiscall=transfer('W9XYZ ',params%hiscall) + params%hisgrid=transfer('FN42 ',params%hisgrid) + before_params=params + call convert_jt4_host_options(params,options) + call require(options%utc==123400,'legacy HHMM converts to HHMMSS') + call require(options%depth==3.and.options%averaging==1.and.options%deep_search==1,'packed effort separates feature flags') + call require(all(transfer(params,zeros)==transfer(before_params,zeros)),'conversion does not mutate legacy parameters') + + allocate(decoder) + npatience=0 + call remove_file('CALL3.TXT') + call load_jt4_calls(decoder) + inquire(file='CALL3.TXT',exist=exists) + call require(.not.exists,'missing call list is not created by loading') + call require(size(decoder%calls)==0,'missing call list clears template knowledge') + open(newunit=unit,file='CALL3.TXT',status='replace') + write(unit,'(a)') '// ignored comment','invalid','W9XYZ,FN42,,','K9XYZ,EN50,,','ZZZZ,AA00,,','N0CALL,EM00,,' + close(unit) + call load_jt4_calls(decoder) + call require(size(decoder%calls)==2,'CALL3 terminator stops parsing') + call require(decoder%calls(1)%call=='W9XYZ'.and.decoder%calls(2)%call=='K9XYZ','CALL3 ordering is preserved') + call require(decoder%calls(1)%grid=='FN42','CALL3 grid survives loading') + open(newunit=unit,file='CALL3.TXT',status='replace') + write(unit,'(a)') 'N0CALL,EM00,,' + close(unit) + call load_jt4_calls(decoder) + call require(size(decoder%calls)==1.and.decoder%calls(1)%call=='N0CALL','changed call list replaces host knowledge') + + call decoder%save_average(123400,5.0,0.1,1.0,1000,50) + call decoder%save_average(123600,6.0,0.1,-1.0,1000,50) + decoder%average_used(1)=.true. + open(newunit=unit,file='avemsg.txt',status='replace') + call write_jt4_averages(decoder,unit) + rewind(unit) + read(unit,'(a)') line + call require(line(1:1)=='$'.and.line(3:6)=='1234'.and.index(line,'*')>0,'legacy average row UTC and selection') + read(unit,'(a)') line + call require(line(1:1)=='.'.and.index(line(2:),'$')>0,'legacy inverted-polarity average marker') + call decoder%clear_averages() + call write_jt4_averages(decoder,unit) + rewind(unit) + read(unit,'(a)',iostat=ios) line + call require(ios<0,'clear truncates old average output') + close(unit) + deallocate(decoder) + + allocate(samples(60*11025),before(60*11025)) + params%ndepth=1 + params%newdat=.true. + params%ndiskdat=.true. + params%nclearave=.true. + samples=0 + before=samples + call run(0) + params%nclearave=.false. + call signal(11025,0,1,300.0,100.0,1000.0,samples) + before=samples + call run(1) + call require(output_contains('K1ABC W9XYZ FN42'),'legacy host prints recovered message') + call require(output_contains('1234'),'legacy host retains HHMM output') + params%newdat=.false. + params%nagain=.true. + attempt=2 + analysis_id=analysis_id+1 + call run(1) + call require(output_contains('K1ABC W9XYZ FN42'),'manual repeat decodes retained input') + input_id=input_id+1 + analysis_id=analysis_id+1 + attempt=1 + params%newdat=.true. + params%nagain=.false. + samples=0 + before=0 + call run(0) + call require(.not.output_contains('K1ABC'),'no-sync output cannot reuse prior message') + call new_period(1234) + params%ndepth=3+16 + call signal(11025,0,5,5.0,100.0,1000.0,samples) + before=samples + call run(0) + call require(average_rows()==1,'weak synchronized reception enters average history') + call require(.not.output_contains('K1ABC'),'sync-only observation is not a recovered message') + + params%minsync=100 + params%nagain=.true. + params%newdat=.false. + attempt=2 + analysis_id=analysis_id+1 + call run(0) + call require(output_contains('1234'),'no-sync observation remains visible without counting as decoded') + params%minsync=0 + + call new_period(1235) + params%nclearave=.true. + samples=0 + before=samples + call run(0) + params%nclearave=.false. + + call new_period(1236) + call signal(11025,0,6,5.0,100.0,1000.0,samples) + before=samples + call run(0) + call require(average_rows()==1,'quiet clear prevents reuse of the preceding weak reception') + deallocate(samples,before) + +contains + subroutine require(condition,message) + logical, intent(in) :: condition + character(len=*), intent(in) :: message + if(.not.condition) then + print *, 'JT4 host: ',message + error stop 1 + endif + end subroutine + + subroutine run(expected) + integer, intent(in) :: expected + before_params=params + open(unit=6,file='host-output.txt',status='replace') + call engine_host_decode(samples,params,11025,completion,0,input_id,analysis_id,attempt,size(samples)) + close(6) + call require(completion%available.and.completion%decoded==expected,'host completion counts only recovered messages') + call require(all(before==samples),'host leaves supplied PCM unchanged') + call require(all(transfer(params,zeros)==transfer(before_params,zeros)),'host leaves packed parameters unchanged') + end subroutine + + subroutine new_period(utc) + integer, intent(in) :: utc + params%nutc=utc + params%newdat=.true. + params%nagain=.false. + input_id=input_id+1 + analysis_id=analysis_id+1 + attempt=1 + end subroutine + + integer function average_rows() result(count) + integer :: file_unit,status + character(len=100) :: row + count=0 + open(newunit=file_unit,file='avemsg.txt',status='old') + do + read(file_unit,'(a)',iostat=status) row + if(status/=0) exit + count=count+1 + enddo + close(file_unit) + end function + + logical function output_contains(text) result(found) + character(len=*), intent(in) :: text + integer :: file_unit,status + character(len=256) :: row + found=.false. + open(newunit=file_unit,file='host-output.txt',status='old') + do + read(file_unit,'(a)',iostat=status) row + if(status/=0) exit + if(index(row,text)>0) found=.true. + enddo + close(file_unit) + end function + + subroutine remove_file(path) + character(len=*), intent(in) :: path + integer :: file_unit,status + open(newunit=file_unit,file=path,status='old',iostat=status) + if(status==0) close(file_unit,status='delete') + end subroutine +end program diff --git a/tests/unit/decoder_engine/test_decoder_engine_jt4_state.f90 b/tests/unit/decoder_engine/test_decoder_engine_jt4_state.f90 new file mode 100644 index 00000000..947750ce --- /dev/null +++ b/tests/unit/decoder_engine/test_decoder_engine_jt4_state.f90 @@ -0,0 +1,112 @@ +program test_decoder_engine_jt4_state + use iso_c_binding, only: c_int16_t + use jt4_decode, only: jt4_decoder,jt4_options,jt4_average_entry + implicit none + type(jt4_decoder), allocatable :: decoder,other + type(jt4_average_entry) :: entries(64) + type(jt4_options) :: options + integer(c_int16_t) :: silence(1)=0 + real :: sym(207,7),symbols(207) + integer :: nsum,i,slot,entry_count,ncount,status + character(len=22) :: decoded + interface + subroutine ignore_result(this,snr,dt,freq,have_sync,sync,is_deep,decoded,qual,ich,is_average,ave) + import jt4_decoder + class(jt4_decoder),intent(inout) :: this + integer,intent(in) :: snr,freq,ich,ave + real,intent(in) :: dt,qual + logical,intent(in) :: have_sync,is_deep,is_average + character,intent(in) :: sync + character(len=22),intent(in) :: decoded + end subroutine + end interface + + allocate(decoder,other) + allocate(decoder%rsymbol(207,7),decoder%ppsave(207,7,64)) + decoder%rsymbol=2.0 + call decoder%save_average(120000,4.0,0.0,1.0,1000,20) + call check(decoder%average_count==1,'first reception') + decoder%rsymbol=4.0 + call decoder%save_average(120000,6.0,0.01,1.0,1010,20) + call check(decoder%average_count==1,'same minute and nearby frequency deduplicate') + call decoder%save_average(120200,6.0,0.1,1.0,1010,20) + call decoder%select_average(120200,0.0,1.0,1000,20,sym,nsum) + call check(nsum==2.and.all(abs(sym-3.0)<1.e-6),'matching receptions average symbols') + call check(count(decoder%average_used)==2,'selected reception flags') + + call decoder%save_average(120100,8.0,0.0,1.0,1000,20) + call decoder%save_average(120400,8.0,0.5,1.0,1000,20) + call decoder%save_average(120600,8.0,0.0,-1.0,1000,20) + call decoder%save_average(120800,8.0,0.0,1.0,1100,20) + call decoder%select_average(120200,0.0,1.0,1000,20,sym,nsum) + call check(nsum==2,'parity, DT, polarity and frequency exclude incompatible receptions') + call check(decoder%average_count==6,'excluded receptions remain in history') + call check(other%average_count==0,'decoder instances do not share history') + + call decoder%clear_averages() + call check(decoder%average_count==0.and.all(decoder%iutc<0),'clear removes all entries') + call check(.not.any(decoder%average_used),'clear removes selection flags') + decoder%rsymbol=1.0 + call decoder%save_average(120000,4.0,0.0,1.0,1000,20) + call check(decoder%average_count==1,'cleared reception can be inserted again') + + call decoder%clear_averages() + do i=0,64 + decoder%rsymbol=real(i) + call decoder%save_average((i/60)*10000+mod(i,60)*100,4.0,0.0,1.0,1000,20) + enddo + call check(decoder%average_count==64,'history capacity after ring wrap') + call check(count(decoder%iutc>=0)==64,'all slots remain visible after ring wrap') + call check(.not.any(decoder%iutc==0),'oldest reception evicted') + call check(any(decoder%iutc==10400),'newest reception retained') + call decoder%get_averages(entries,entry_count) + call check(entry_count==64,'snapshot reports all entries after ring wrap') + call check(entries(1)%utc==10400,'snapshot preserves physical slot order') + call decoder%select_average(10400,0.0,1.0,1000,20,sym,nsum) + call check(nsum==32,'parity selection remains correct across hour and ring wrap') + call check(all(abs(sym-33.0)<1.e-6),'wrapped average contains precisely retained even minutes') + slot=decoder%nsave + call decoder%save_average(10400,9.0,0.0,1.0,1001,20) + call check(decoder%nsave==slot.and.decoder%average_count==64,'duplicate does not advance ring') + + options%submode=1 + call decoder%decode_pcm(options,silence,11025,ignore_result,status) + call check(status==0,'prepare changed submode') + decoder%rsymbol=1.0 + call decoder%save_average(120000,4.0,0.0,1.0,1000,20) + call decoder%decode_pcm(options,silence,11025,ignore_result,status) + call check(status==0.and.decoder%average_count==1,'same width profile retains compatible history') + options%minimum_width_profile=1 + call decoder%decode_pcm(options,silence,11025,ignore_result,status) + call check(status==0.and.decoder%average_count==0,'changed width profile clears incompatible history') + deallocate(decoder,other) + + do i=0,1 + symbols=real(i) + decoded='stale decoded message' + ncount=42 + call extract4(symbols,ncount,decoded) + call check(ncount<0.and.len_trim(decoded)==0,'zero-variance symbols fail without stale message') + enddo + +contains + subroutine check(condition,message) + logical, intent(in) :: condition + character(len=*), intent(in) :: message + if(.not.condition) then + print *, 'JT4 average state: ',message + stop 1 + endif + end subroutine +end program + +subroutine ignore_result(this,snr,dt,freq,have_sync,sync,is_deep,decoded,qual,ich,is_average,ave) + use jt4_decode, only: jt4_decoder + implicit none + class(jt4_decoder),intent(inout) :: this + integer,intent(in) :: snr,freq,ich,ave + real,intent(in) :: dt,qual + logical,intent(in) :: have_sync,is_deep,is_average + character,intent(in) :: sync + character(len=22),intent(in) :: decoded +end subroutine diff --git a/tests/unit/jtty/jtty_cpp_n1mm_smoke.cpp b/tests/unit/jtty/jtty_cpp_n1mm_smoke.cpp index c2b3b45b..25fa54b0 100644 --- a/tests/unit/jtty/jtty_cpp_n1mm_smoke.cpp +++ b/tests/unit/jtty/jtty_cpp_n1mm_smoke.cpp @@ -80,3 +80,40 @@ extern "C" int jtty_cpp_rtty_smoke (int tones[], int* nsym) } return status; } + +extern "C" int jtty_cpp_rtty_line_refused () +{ + // 80 characters; RTTY Roundup writes the serial as 005, which makes it 81. + QString const line = QString (73, QLatin1Char {'A'}) + QStringLiteral (" 599 05"); + auto encodesWith = [] (Jtty::NativeExchangeProfile exchange) { + return [profile = static_cast<int> (exchange)] (QString const& text) { + auto frame = Jtty::transmitFrame (text).toLatin1 (); + std::array<int, 16 * 59> tones {}; + std::array<int, 16> frameStarts {}; + int symbols {}; + int const isFinal = 0; + genjtty_profile_ (frame.data (), &profile, tones.data (), &symbols, + frameStarts.data (), &isFinal, frame.size ()); + return symbols > 0; + }; + }; + auto const rtty = Jtty::encodeTransmitLine (line, encodesWith (Jtty::NativeExchangeProfile::RttyRoundup)); + auto const none = Jtty::encodeTransmitLine (line, encodesWith (Jtty::NativeExchangeProfile::None)); + return !rtty && none && none->size () == 1; +} + +extern "C" int jtty_cpp_rtty_line_tones (int tones[], int* nsym) +{ + // 79 characters; with the serial written as 005 it is exactly 80. + QString const line = QString (72, QLatin1Char {'A'}) + QStringLiteral (" 599 05"); + int const profile = static_cast<int> (Jtty::NativeExchangeProfile::RttyRoundup); + auto encode = [&] (QString const& text, int isFinal) { + auto frame = Jtty::transmitFrame (text).toLatin1 (); + std::array<int, 16> frameStarts {}; + *nsym = 0; + genjtty_profile_ (frame.data (), &profile, tones, nsym, frameStarts.data (), &isFinal, frame.size ()); + return *nsym > 0; + }; + auto const segments = Jtty::encodeTransmitLine (line, [&] (QString const& text) { return encode (text, 0); }); + return segments && segments->size () == 1 && encode (segments->front ().text, 1); +} diff --git a/tests/unit/jtty/test_jtty_messages.cpp b/tests/unit/jtty/test_jtty_messages.cpp index 033966e4..f877e1a0 100644 --- a/tests/unit/jtty/test_jtty_messages.cpp +++ b/tests/unit/jtty/test_jtty_messages.cpp @@ -2,6 +2,16 @@ #include "widgets/JttyMessages.hpp" +namespace +{ + // 121 characters: two transmit segments. + QString longLine () + { + return QStringLiteral ("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG AND KEEPS RUNNING ACROSS THE FIELD" + " UNTIL IT REACHES THE OLD BARN BY THE RIVER"); + } +} + class TestJttyMessages final : public QObject { @@ -690,6 +700,44 @@ private slots: Jtty::maxTransmitLength); } + void encodeTransmitLineRefusesRatherThanShortens () + { + QString const line = QString (73, QLatin1Char {'A'}) + QStringLiteral (" 599 05"); + QStringList attempts; + auto const encoded = Jtty::encodeTransmitLine (line, [&] (QString const& text) { + attempts << text; + return text.size () < line.size (); + }); + QVERIFY (!encoded); + QCOMPARE (attempts, QStringList {line}); + + QStringList cuts; + auto const laterSegmentFails = Jtty::encodeTransmitLine (longLine (), [&] (QString const& text) { + cuts << text; + return cuts.size () < 2; + }); + QVERIFY (!laterSegmentFails); + QCOMPARE (cuts.size (), 2); + QCOMPARE (cuts.join (QString {}), longLine ()); + } + + void encodeTransmitLineKeepsTheSegmentCuts () + { + QString const line = longLine (); + QStringList attempts; + auto const encoded = Jtty::encodeTransmitLine (line, [&] (QString const& text) { + attempts << text; + return true; + }); + QVERIFY (encoded); + QCOMPARE (encoded->size (), 2); + QCOMPARE (attempts, (QStringList {encoded->at (0).text, encoded->at (1).text})); + QCOMPARE (encoded->at (0).text + encoded->at (1).text, line); + auto const blank = Jtty::encodeTransmitLine (QString (5, QLatin1Char {' '}), + [] (QString const&) { return false; }); + QVERIFY (blank && blank->isEmpty ()); + } + void wavCaptureValid_data () { QTest::addColumn<qint32> ("k0"); diff --git a/tests/unit/jtty/test_jtty_structured_decode.f90 b/tests/unit/jtty/test_jtty_structured_decode.f90 index 6a55a816..56f9256a 100644 --- a/tests/unit/jtty/test_jtty_structured_decode.f90 +++ b/tests/unit/jtty/test_jtty_structured_decode.f90 @@ -37,6 +37,15 @@ program test_jtty_structured_decode integer(c_int), intent(out) :: tones(*),nsym integer(c_int) :: status end function jtty_cpp_rtty_smoke + function jtty_cpp_rtty_line_refused() result(refused) bind(C) + import :: c_int + integer(c_int) :: refused + end function jtty_cpp_rtty_line_refused + function jtty_cpp_rtty_line_tones(tones,nsym) result(planned) bind(C) + import :: c_int + integer(c_int), intent(out) :: tones(*),nsym + integer(c_int) :: planned + end function jtty_cpp_rtty_line_tones subroutine genjtty_atoms(atoms,natoms,itone,nsym) use jtty_mod, only: jtty_source_atom type(jtty_source_atom), intent(in) :: atoms(:) @@ -63,6 +72,9 @@ program test_jtty_structured_decode call decode_cpp_compiled_n1mm(failures) call decode_compact_text(failures) call decode_profile_text(failures) + call expect(jtty_cpp_rtty_line_refused().eq.1, & + 'an RTTY line whose serial does not fit is refused, not split',failures) + call decode_accepted_rtty_line(failures) if(failures.ne.0) then write(*,'(a,i0)') 'test_jtty_structured_decode: failures=',failures @@ -351,6 +363,23 @@ contains deallocate(pcm,wave,complex_wave) end subroutine decode_waveform + subroutine decode_accepted_rtty_line(count) + integer, intent(inout) :: count + integer(c_int) :: tones(MAX_FRAMES*frame_symbols),nsymbols + character(len=80) :: expected + + expected=repeat('A',72)//' 599 005' + call expect(jtty_cpp_rtty_line_tones(tones,nsymbols).eq.1, & + 'the longest RTTY line that fits is one segment',count) + if(nsymbols.le.0) return + call decode_waveform(tones,int(nsymbols)) + call expect(npending.ge.1,'the accepted RTTY line produces updates',count) + if(npending.lt.1) return + call expect(trim(normalized(pending_updates(npending)%decoded)).eq.trim(expected) .and. & + pending_updates(npending)%complete .and. nactive.eq.0, & + 'the accepted RTTY line arrives whole, serial included',count) + end subroutine decode_accepted_rtty_line + function normalized(value) result(result_value) character(len=*), intent(in) :: value character(len=80) :: result_value diff --git a/tests/unit/map65/test_map65_pass_snapshots.f90 b/tests/unit/map65/test_map65_pass_snapshots.f90 new file mode 100644 index 00000000..cfbd12b7 --- /dev/null +++ b/tests/unit/map65/test_map65_pass_snapshots.f90 @@ -0,0 +1,100 @@ +program test_map65_pass_snapshots + ! Each automatic decode pass must decode data frozen at the moment it was + ! triggered. Simulates the new minute resetting the live buffers while the + ! 302-symbol pass is still queued behind the running 280-symbol pass. + use iso_c_binding, only: c_float + use datcom_ptrs_mod, only: dd, ss, savg, snap, snapshot_buffers + use npar_ptrs_mod, only: snap_latest_idx, set_nhsym, set_nagain, set_ndiskdat, & + publish_decode_request, claim_decode_request, active_request_expired, & + active_snap_idx, snapshot_release, advance_live_input_generation + implicit none + + integer, parameter :: nsmax = 4000, ncol = 12, nfft = 16 + integer(8) :: rid + + allocate(dd(4,nsmax), ss(4,ncol,nfft), savg(4,nfft)) + allocate(snap(1)%dd(4,nsmax), snap(1)%ss(4,ncol,nfft), snap(1)%savg(4,nfft)) + allocate(snap(2)%dd(4,nsmax), snap(2)%ss(4,ncol,nfft), snap(2)%savg(4,nfft)) + snap(1)%dd = -1.0; snap(1)%ss = -1.0; snap(1)%savg = -1.0 + snap(2)%dd = -1.0; snap(2)%ss = -1.0; snap(2)%savg = -1.0 + + ! Early pass (nhsym 280 stand-in): live buffers hold cycle A up to sample 3000 + dd = 0.0; dd(:,1:3000) = 1.0 + ss = 0.0; ss(:,1:10,:) = 1.0 + savg = 10.0 + call snapshot_buffers(1, 3000, 10) + call require(snap_latest_idx == 1, 'latest snapshot is the early one') + + ! Later: live buffers keep filling, then the new minute resets them + dd(:,3001:3800) = 2.0 + ss(:,11:12,:) = 2.0 + savg = 20.0 + call snapshot_buffers(2, 3800, 12) + call require(snap_latest_idx == 2, 'latest snapshot is the final one') + + dd = 7.0; ss = 7.0; savg = 0.0 !Next cycle overwrites / resets live + + call require(all(snap(1)%dd(:,1:3000) == 1.0), 'early dd frozen') + call require(all(snap(1)%dd(:,3513:) == 0.0), 'early dd past lookahead is zero') + call require(all(snap(1)%ss(:,1:10,:) == 1.0), 'early ss frozen') + call require(all(snap(1)%ss(:,11:,:) == 0.0), 'early ss past nhsym is zero') + call require(all(snap(1)%savg == 10.0), 'early savg frozen') + + call require(all(snap(2)%dd(:,1:3000) == 1.0), 'final dd has early part') + call require(all(snap(2)%dd(:,3001:3800) == 2.0), 'final dd has later part') + call require(all(snap(2)%ss(:,11:12,:) == 2.0), 'final ss frozen') + call require(all(snap(2)%savg == 20.0), 'final savg frozen') + + ! ---- request expiry follows the snapshot, not the minute boundary ---- + call set_nagain(0); call set_ndiskdat(0) + + ! 1. A final-pass request claimed after the minute rolled over still runs + ! (its snapshot is intact), and holds its slot while running. + dd = 3.0; ss = 3.0; savg = 3.0 + call set_nhsym(302) + call snapshot_buffers(2, 3800, 12) + rid = publish_decode_request() + call advance_live_input_generation() !new minute begins + call require(claim_decode_request(), 'request claimed') + call require(.not. active_request_expired, 'late request with intact snapshot runs') + call require(active_snap_idx == 2, 'late request reads its own slot') + + ! 2. While that pass is running, the next cycle's fill must not overwrite + ! the slot; the request published for it is then dropped, not mis-decoded. + dd = 4.0 + call snapshot_buffers(2, 3800, 12) + call require(all(snap(2)%dd(:,1:3800) == 3.0), 'busy slot not overwritten') + rid = publish_decode_request() + call snapshot_release() + call require(claim_decode_request(), 'second request claimed') + call require(active_request_expired, 'request whose fill was skipped is dropped') + + ! 3. A queued request whose slot is refilled before it is claimed is dropped. + dd = 5.0 + call snapshot_buffers(2, 3800, 12) + rid = publish_decode_request() + dd = 6.0 + call snapshot_buffers(2, 3800, 12) + call require(claim_decode_request(), 'third request claimed') + call require(active_request_expired, 'overwritten snapshot request is dropped') + + ! 4. More than one generation old (monitoring stopped etc.) is dropped. + call snapshot_buffers(2, 3800, 12) + rid = publish_decode_request() + call advance_live_input_generation() + call advance_live_input_generation() + call require(claim_decode_request(), 'fourth request claimed') + call require(active_request_expired, 'request two generations old is dropped') + print *, 'test_map65_pass_snapshots: PASS' + +contains + + subroutine require(ok, what) + logical, intent(in) :: ok + character(*), intent(in) :: what + if (.not. ok) then + print *, 'FAIL: ', what + error stop 1 + end if + end subroutine require +end program test_map65_pass_snapshots diff --git a/tests/unit/streaming/CMakeLists.txt b/tests/unit/streaming/CMakeLists.txt index 4db80617..6aae5282 100644 --- a/tests/unit/streaming/CMakeLists.txt +++ b/tests/unit/streaming/CMakeLists.txt @@ -1,11 +1,13 @@ add_executable (test_streaming_consumers test_streaming_consumers.cpp) -add_dependencies (test_streaming_consumers test_decoder_engine_q65 test_decoder_engine_fst4) +add_dependencies (test_streaming_consumers test_decoder_engine test_decoder_engine_q65 test_decoder_engine_fst4 test_decoder_engine_jt4) target_link_libraries (test_streaming_consumers Qt5::Test) target_compile_definitions (test_streaming_consumers PRIVATE + JT4_ENGINE_WAV="${CMAKE_BINARY_DIR}/tests/unit/decoder_engine/jt4-engine.wav" JT9_EXECUTABLE="$<TARGET_FILE:jt9>" WSPRD_EXECUTABLE="$<TARGET_FILE:wsprd>" FST4_ENGINE_WAV="${CMAKE_BINARY_DIR}/tests/unit/decoder_engine/fst4-engine.wav" FST4W_ENGINE_WAV="${CMAKE_BINARY_DIR}/tests/unit/decoder_engine/fst4w-engine.wav" + FT8_ENGINE_WAV="${CMAKE_BINARY_DIR}/tests/unit/decoder_engine/ft8-engine.wav" Q65_ENGINE_WAV="${CMAKE_BINARY_DIR}/tests/unit/decoder_engine/q65-engine.wav" ) @@ -13,7 +15,7 @@ add_test (NAME test_streaming_consumers COMMAND test_streaming_consumers) set_tests_properties (test_streaming_consumers PROPERTIES LABELS "streaming;runtime" TIMEOUT 360 - FIXTURES_REQUIRED "q65_engine_wav;fst4_engine_wav;fst4w_engine_wav" + FIXTURES_REQUIRED "ft8_engine_wav;jt4_engine_wav;q65_engine_wav;fst4_engine_wav;fst4w_engine_wav" ) if (WSJT_ENABLE_TSAN) set_tests_properties (test_streaming_consumers PROPERTIES diff --git a/tests/unit/streaming/test_streaming_consumers.cpp b/tests/unit/streaming/test_streaming_consumers.cpp index 91ed0e2e..3d9caf46 100644 --- a/tests/unit/streaming/test_streaming_consumers.cpp +++ b/tests/unit/streaming/test_streaming_consumers.cpp @@ -238,6 +238,52 @@ class TestStreamingConsumers final : public QObject Q_OBJECT private slots: + void jt4DecodesWaveform_data () + { + QTest::addColumn<QJsonObject> ("timeFields"); + QTest::addColumn<QString> ("expected"); + QTest::newRow ("afternoon") << QJsonObject {{"utc", "12:34:00"}} << QString {"123400"}; + QTest::newRow ("legacy-numeric") << QJsonObject {{"nutc", 1234}} << QString {"123400"}; + QTest::newRow ("early-hour-iso-precedence") + << QJsonObject {{"nutc", 1234}, {"utc", "00:12:34"}} << QString {"001234"}; + } + + void jt4DecodesWaveform () + { + QFETCH (QJsonObject, timeFields); + QFETCH (QString, expected); + QFile recording {QString::fromUtf8 (JT4_ENGINE_WAV)}; + QVERIFY2 (recording.open (QIODevice::ReadOnly), qPrintable (recording.errorString ())); + auto const wav = recording.readAll (); + QCOMPARE (wav.size (), 44 + 60 * 12000 * 2); + QCOMPARE (wav.left (4), QByteArray {"RIFF"}); + QCOMPARE (wav.mid (36, 4), QByteArray {"data"}); + QTemporaryDir directory; + QVERIFY (directory.isValid ()); + QJsonObject configuration {{"t", "configure"}, {"mode", "JT4"}, {"trperiod", 60}, + {"submode", 0}, {"depth_level", 1}, {"rxfreq", 1000}, {"ntol", 50}}; + for (auto it = timeFields.constBegin (); it != timeFields.constEnd (); ++it) + { + configuration.insert (it.key (), it.value ()); + } + auto const input = sessionHeader () + controlFrame ( + QJsonDocument {configuration}.toJson (QJsonDocument::Compact)) + + frame (0x01u, wav.mid (44)) + controlFrame (R"({"t":"halt"})"); + auto const result = runProcess (QString::fromUtf8 (JT9_EXECUTABLE), + {"-a", directory.path (), "-t", directory.path (), "--stream"}, input, directory.path ()); + verifyCompleted (result, 0); + auto const events = verifyEvents (result); + QCOMPARE (eventsMatching (events.values, "error").size (), 0); + auto const decodes = eventsMatching (events.values, "decode"); + QCOMPARE (decodes.size (), 1); + QCOMPARE (decodes.first ().value ("mode").toString (), QString {"JT4"}); + QCOMPARE (decodes.first ().value ("time").toString (), expected); + QCOMPARE (decodes.first ().value ("message").toString ().trimmed (), QString {"K1ABC W9XYZ FN42"}); + auto const completions = eventsMatching (events.values, "decode_finished"); + QCOMPARE (completions.size (), 1); + QCOMPARE (completions.first ().value ("period_end").toString (), expected); + } + void fst4DecodesWaveform_data () { QTest::addColumn<QString> ("mode"); @@ -277,7 +323,7 @@ private slots: bool found = false; for (auto const& decode : eventsMatching (events.values, "decode")) { - QCOMPARE (decode.value ("mode").toString (), QString {"FST4"}); + QCOMPARE (decode.value ("mode").toString (), mode); found |= decode.value ("message").toString ().trimmed () == message; } QVERIFY2 (found, result.standardOutput.constData ()); @@ -475,6 +521,120 @@ private slots: QCOMPARE (eventsMatching (events.values, "decode_finished").size (), 2); } + // A period's label is the producer's UTC, whichever key and frame carried it. + void jt9LabelsPeriodsWithTheProducersUtc_data () + { + QTest::addColumn<QByteArray> ("configure"); + QTest::addColumn<int> ("samples"); + QTest::addColumn<QString> ("periodEnd"); + QTest::newRow ("FT8 05:11:15") + << controlFrame (R"({"t":"configure","mode":"FT8","utc":"05:11:15"})") << 15 * 12000 << QString {"051115"}; + QTest::newRow ("FT8 00:45:15") + << controlFrame (R"({"t":"configure","mode":"FT8","utc":"00:45:15"})") << 15 * 12000 << QString {"004515"}; + QTest::newRow ("JT9, utc, then FT8") + << controlFrame (R"({"t":"configure","mode":"JT9"})") + controlFrame (R"({"t":"configure","utc":"05:11:15"})") + + controlFrame (R"({"t":"configure","mode":"FT8"})") + << 15 * 12000 << QString {"051115"}; + QTest::newRow ("FT8 nutc") + << controlFrame (R"({"t":"configure","mode":"FT8","nutc":51115})") << 15 * 12000 << QString {"051115"}; + QTest::newRow ("JT9, nutc 1530, then FT8") + << controlFrame (R"({"t":"configure","mode":"JT9"})") + controlFrame (R"({"t":"configure","nutc":1530})") + + controlFrame (R"({"t":"configure","mode":"FT8"})") + << 15 * 12000 << QString {"001530"}; + QTest::newRow ("FT8 negative nutc") + << controlFrame (R"({"t":"configure","mode":"FT8","nutc":-1})") << 15 * 12000 << QString {"000000"}; + QTest::newRow ("FT4 05:11:15") + << controlFrame (R"({"t":"configure","mode":"FT4","utc":"05:11:15"})") << Ft4PeriodSamples << QString {"051115"}; + QTest::newRow ("MSK144 05:11:15") + << controlFrame (R"({"t":"configure","mode":"MSK144","trperiod":15,"utc":"05:11:15"})") << 15 * 12000 + << QString {"051115"}; + QTest::newRow ("FST4-15 05:11:15") + << controlFrame (R"({"t":"configure","mode":"FST4","trperiod":15,"utc":"05:11:15"})") << 15 * 12000 + << QString {"051115"}; + QTest::newRow ("JT9 05:11:00") + << controlFrame (R"({"t":"configure","mode":"JT9","utc":"05:11:00"})") << 60 * 12000 << QString {"051100"}; + QTest::newRow ("JT9 nutc HHMM") + << controlFrame (R"({"t":"configure","mode":"JT9","nutc":511})") << 60 * 12000 << QString {"051100"}; + QTest::newRow ("JT9 nutc HHMMSS") + << controlFrame (R"({"t":"configure","mode":"JT9","nutc":51100})") << 60 * 12000 << QString {"051100"}; + QTest::newRow ("JT9 nutc 4515") + << controlFrame (R"({"t":"configure","mode":"JT9","nutc":4515})") << 60 * 12000 << QString {"004515"}; + QTest::newRow ("JT9 utc and nutc in one frame") + << controlFrame (R"({"t":"configure","mode":"JT9","utc":"00:15:00","nutc":1530})") << 60 * 12000 + << QString {"001500"}; + QTest::newRow ("utc 00:15:00, then JT9") + << controlFrame (R"({"t":"configure","utc":"00:15:00"})") + controlFrame (R"({"t":"configure","mode":"JT9"})") + << 60 * 12000 << QString {"001500"}; + QTest::newRow ("nutc HHMM, then JT9") + << controlFrame (R"({"t":"configure","nutc":511})") + controlFrame (R"({"t":"configure","mode":"JT9"})") + << 60 * 12000 << QString {"051100"}; + QTest::newRow ("JT9 utc, then nutc") + << controlFrame (R"({"t":"configure","mode":"JT9","utc":"05:11:00"})") + + controlFrame (R"({"t":"configure","nutc":512})") + << 60 * 12000 << QString {"051200"}; + QTest::newRow ("JT65 05:11:00") + << controlFrame (R"({"t":"configure","mode":"JT65","utc":"05:11:00"})") << 60 * 12000 << QString {"051100"}; + QTest::newRow ("JT4 05:11:00") + << controlFrame (R"({"t":"configure","mode":"JT4","utc":"05:11:00"})") << 60 * 12000 << QString {"051100"}; + QTest::newRow ("FST4-60 05:11:00") + << controlFrame (R"({"t":"configure","mode":"FST4","trperiod":60,"utc":"05:11:00"})") << 60 * 12000 + << QString {"051100"}; + QTest::newRow ("FST4W-120 05:12:00") + << controlFrame (R"({"t":"configure","mode":"FST4W","trperiod":120,"utc":"05:12:00"})") << 120 * 12000 + << QString {"051200"}; + QTest::newRow ("Q65-60 05:11:00") + << controlFrame (R"({"t":"configure","mode":"Q65","trperiod":60,"utc":"05:11:00"})") << 60 * 12000 + << QString {"051100"}; + } + + void jt9LabelsPeriodsWithTheProducersUtc () + { + QFETCH (QByteArray, configure); + QFETCH (int, samples); + QFETCH (QString, periodEnd); + auto const result = runJt9 (sessionHeader () + configure + silentAudioFrame (samples)); + verifyCompleted (result, 0); + auto const events = verifyEvents (result); + QCOMPARE (eventsMatching (events.values, "error").size (), 0); + auto const completions = eventsMatching (events.values, "decode_finished"); + QCOMPARE (completions.size (), 1); + QCOMPARE (completions.first ().value ("period_end").toString (), periodEnd); + } + + void ft8DecodeCarriesTheProducersUtc_data () + { + QTest::addColumn<QString> ("utc"); + QTest::addColumn<QString> ("label"); + QTest::newRow ("05:11:15") << QString {"05:11:15"} << QString {"051115"}; + QTest::newRow ("00:45:15") << QString {"00:45:15"} << QString {"004515"}; + } + + void ft8DecodeCarriesTheProducersUtc () + { + QFETCH (QString, utc); + QFETCH (QString, label); + QFile recording {QString::fromUtf8 (FT8_ENGINE_WAV)}; + QVERIFY2 (recording.open (QIODevice::ReadOnly), qPrintable (recording.errorString ())); + auto const wav = recording.readAll (); + QCOMPARE (wav.size (), 44 + 15 * 12000 * 2); + QJsonObject configuration {{"t", "configure"}, {"mode", "FT8"}, {"utc", utc}}; + auto const result = runJt9 (sessionHeader () + controlFrame ( + QJsonDocument {configuration}.toJson (QJsonDocument::Compact)) + frame (0x01u, wav.mid (44))); + verifyCompleted (result, 0); + auto const events = verifyEvents (result); + QCOMPARE (eventsMatching (events.values, "error").size (), 0); + bool found = false; + for (auto const& decode : eventsMatching (events.values, "decode")) + { + QCOMPARE (decode.value ("time").toString (), label); + found |= decode.value ("message").toString ().trimmed () == "CQ K1JT FN20"; + } + QVERIFY2 (found, result.standardOutput.constData ()); + auto const completions = eventsMatching (events.values, "decode_finished"); + QCOMPARE (completions.size (), 1); + QCOMPARE (completions.first ().value ("period_end").toString (), label); + } + void wsprdRejectsOversizedFrames_data () { QTest::addColumn<quint32> ("declaredLength"); |
