summaryrefslogtreecommitdiff
path: root/tests/unit/map65/test_map65_result_selection.f90
blob: dd76ea4c6dbdb796bd1b5c28fdcf9080491e9805 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
program test_map65_result_selection
  use trimlist_mod, only: select_unique_decodes
  implicit none

  integer, parameter :: MAXMSG = 1000
  real :: sig(MAXMSG, 30)
  character(len=22) :: msg(MAXMSG)
  integer :: indx(MAXMSG), nz

  call test_result_identity
  call test_empty_and_full_lists
  print '(a)', 'MAP65 result selection tests passed.'

contains

  subroutine test_result_identity
    sig = 0.0
    msg = ' '

    call set_result(1, 1, 100.000, 0.20, 1, 'K1ABC W9XYZ FN42', 1.0)
    call set_result(2, 1, 100.005, 0.25, 1, 'K1ABC W9XYZ FN42', 2.0)
    call set_result(3, 1, 100.005, 0.20, 1, 'W1AAA K2BBB EM00')
    call set_result(4, 1, 100.005, 0.50, 1, 'K1ABC W9XYZ FN42')
    call set_result(5, 1, 100.005, 0.20, -1, 'K1ABC W9XYZ FN42')
    call set_result(6, 1, 100.020, 0.20, 1, 'K1ABC W9XYZ FN42')
    call set_result(7, 1, 100.005, 0.20, 1, ' ')
    call set_result(8, 2, 100.005, 0.20, 1, 'K1ABC W9XYZ FN42')

    call select_unique_decodes(sig, msg, 8, 0.010, 0.2, indx, nz)
    call require(nz == 6, 'only a matching message and signal identity is deduplicated')
    call require(selected_count('W1AAA K2BBB EM00', 1, 1) == 1, &
                 'a different message at the same frequency survives')
    call require(selected_count('K1ABC W9XYZ FN42', 1, -1) == 1, &
                 'opposite-sync results survive')
    call require(selected_count('K1ABC W9XYZ FN42', 2, 1) == 1, &
                 'results from another UTC survive')
    call require(any(indx(1:nz) == 2) .and. .not. any(indx(1:nz) == 1), &
                 'the stronger duplicate supplies the displayed metrics')
  end subroutine test_result_identity

  subroutine test_empty_and_full_lists
    integer :: i

    sig = 0.0
    msg = ' '
    call select_unique_decodes(sig, msg, 0, 0.010, 0.2, indx, nz)
    call require(nz == 0, 'an empty result list stays empty')

    do i = 1, MAXMSG
      sig(i, 2) = 1.0
      sig(i, 3) = 0.020*i
      sig(i, 5) = 0.2
      sig(i, 7) = 1.0
      write(msg(i), '("MSG",I4.4)') i
    end do
    call select_unique_decodes(sig, msg, MAXMSG, 0.010, 0.2, indx, nz)
    call require(nz == MAXMSG, 'the maximum result list does not overrun selection storage')
  end subroutine test_empty_and_full_lists

  subroutine set_result(index, utc, frequency, dt, flip, message, strength)
    integer, intent(in) :: index, utc, flip
    real, intent(in) :: frequency, dt
    character(len=*), intent(in) :: message
    real, intent(in), optional :: strength

    sig(index, 2) = utc
    sig(index, 3) = frequency
    sig(index, 5) = dt
    sig(index, 7) = flip
    if (present(strength)) sig(index, 8) = strength
    msg(index) = message
  end subroutine set_result

  integer function selected_count(message, utc, flip)
    character(len=*), intent(in) :: message
    integer, intent(in) :: utc, flip
    integer :: i, source

    selected_count = 0
    do i = 1, nz
      source = indx(i)
      if (msg(source) == message .and. nint(sig(source, 2)) == utc .and. &
          nint(sig(source, 7)) == flip) selected_count = selected_count + 1
    end do
  end function selected_count

  subroutine require(condition, description)
    logical, intent(in) :: condition
    character(len=*), intent(in) :: description

    if (.not. condition) then
      print '(a)', 'FAIL: '//description
      error stop 1
    end if
  end subroutine require
end program test_map65_result_selection