summaryrefslogtreecommitdiff
path: root/tests/unit/jtty/test_jtty_receive_state.f90
blob: 232679ceb8186a877a390beddc4a7d720bf5f657 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
program test_jtty_receive_state
  use iso_fortran_env, only: int64
  use jtty_mdec, only: decode,message_assembly,MAX_ACTIVE_MESSAGES,MAX_RECENT_FRAMES, &
       nactive,active_messages, &
       nrecent,npending,pending_updates,reset_decode_search_state, &
       discard_pending_updates,start_message,append_active_message, &
       remove_active_message,is_recent_frame,remember_recent_frame, &
       prune_receive_state,classify_active_candidate
  implicit none

  real, parameter :: frame_period=2.0
  type(decode) :: candidate,oldest_candidate
  type(message_assembly) :: accepted_message,classification_existing
  integer(int64) :: first_id,last_id
  integer :: i,index,nrecent_before,npending_before,nframes_gap,nmissing
  logical :: accepted,match,is_window_dupe

  call reset_all()

  candidate=make_candidate(1500.0,10.0,'FIRST FRAME',.false.)
  call start_message(candidate,accepted_message,accepted)
  index=nactive
  call expect(accepted .and. index.eq.1 .and. nactive.eq.1, &
       'an open message enters the active store')
  call expect(npending.eq.1 .and. .not.pending_updates(1)%complete, &
       'an open message publishes its initial state')
  call expect(nrecent.eq.1 .and. is_recent_frame(candidate), &
       'an accepted frame enters recent history')
  first_id=active_messages(1)%message_id

  candidate=make_candidate(1500.0,12.0,' COMPLETE',.true.)
  call append_active_message(1,candidate,1,accepted_message,accepted)
  call expect(nactive.eq.0 .and. npending.eq.1, &
       'completion frees active capacity without losing its update')
  call expect(pending_updates(1)%message_id.eq.first_id .and. &
       pending_updates(1)%complete .and. &
       trim(pending_updates(1)%decoded).eq.'FIRST FRAME COMPLETE', &
       'completion coalesces the final state by logical identity')

  call reset_all()
  candidate=make_candidate(1500.0,10.0,'BEFORE',.false.)
  call start_message(candidate,accepted_message,accepted)
  first_id=active_messages(1)%message_id
  candidate=make_candidate(1500.0,16.0,'~AFTER',.true.)
  call append_active_message(1,candidate,3,accepted_message,accepted)
  call expect(accepted .and. nactive.eq.0 .and. npending.eq.1, &
       'a maximum in-range gap completes one logical message')
  call expect(pending_updates(1)%message_id.eq.first_id .and. &
       pending_updates(1)%complete .and. &
       trim(pending_updates(1)%decoded).eq.'BEFORE~~~~~AFTER', &
       'gap assembly inserts one sentinel and strips continuation filler')

  do nmissing=1,3
     classification_existing%f1=1500.0
     classification_existing%tsync=10.0
     candidate=make_candidate(1500.0,10.0+real(nmissing+1)*frame_period, &
          'AFTER',.true.)
     call classify_active_candidate(classification_existing,candidate,frame_period, &
          match,is_window_dupe,nframes_gap)
     if(nmissing.le.2) then
        call expect(match .and. .not.is_window_dupe .and. &
             nframes_gap.eq.nmissing+1, &
             'classification accepts an in-range missing-frame gap')
     else
        call expect(.not.match, &
             'classification rejects a gap beyond the merge limit')
     endif
  enddo

  call reset_decode_search_state()
  call expect(nactive.eq.0 .and. nrecent.eq.0 .and. npending.eq.1, &
       'a search reset preserves undelivered updates')
  candidate=make_candidate(1510.0,20.0,'AFTER RESET',.true.)
  call start_message(candidate,accepted_message,accepted)
  call expect(accepted .and. nactive.eq.0 .and. npending.eq.2, &
       'a single-frame completion never occupies active capacity')
  call expect(pending_updates(2)%message_id.gt.first_id, &
       'logical identities remain monotonic across search resets')

  call reset_all()
  do i=1,MAX_ACTIVE_MESSAGES
     candidate=make_candidate(1000.0+real(i),real(i),'OPEN',.false.)
     call start_message(candidate,accepted_message,accepted)
     call expect(accepted .and. nactive.eq.i, &
          'active messages fill capacity without replacement')
  enddo
  last_id=active_messages(MAX_ACTIVE_MESSAGES)%message_id
  nrecent_before=nrecent
  npending_before=npending
  candidate=make_candidate(2000.0,100.0,'REFUSED',.false.)
  call start_message(candidate,accepted_message,accepted)
  call expect(.not.accepted, &
       'a full active store declines a new open message')
  call expect(nactive.eq.MAX_ACTIVE_MESSAGES .and. &
       nrecent.eq.nrecent_before .and. npending.eq.npending_before, &
       'a declined message leaves every store unchanged')
  call remove_active_message(7)
  call start_message(candidate,accepted_message,accepted)
  index=nactive
  call expect(accepted .and. nactive.eq.MAX_ACTIVE_MESSAGES, &
       'freeing one active entry restores capacity')
  call expect(active_messages(index)%message_id.eq.last_id+1_int64, &
       'a declined message does not consume a logical identity')

  call reset_all()
  candidate=make_candidate(1500.0,10.0,'HISTORY',.true.)
  call start_message(candidate,accepted_message,accepted)
  call expect(is_recent_frame(candidate), &
       'completion does not remove its frame fingerprint')
  call prune_receive_state(11.54,frame_period)
  call expect(is_recent_frame(candidate), &
       'history remains until the oldest retro window passes it')
  call prune_receive_state(11.56,frame_period)
  call expect(.not.is_recent_frame(candidate), &
       'history expires after the rediscovery horizon')

  call reset_all()
  candidate=make_candidate(1500.0,10.0,'HISTORY',.true.)
  call start_message(candidate,accepted_message,accepted)
  candidate=make_candidate(1504.0,10.01,'NEAR SIMULTANEOUS',.true.)
  call expect(is_recent_frame(candidate), &
       'recent history rejects a near-simultaneous 4 Hz rediscovery')
  candidate=make_candidate(1511.9,10.01,'INSIDE FREQUENCY LIMIT',.true.)
  call expect(is_recent_frame(candidate), &
       'recent history catches just inside 12 Hz')
  candidate=make_candidate(1512.1,10.01,'OUTSIDE FREQUENCY LIMIT',.true.)
  call expect(.not.is_recent_frame(candidate), &
       'recent history allows just outside 12 Hz')
  candidate=make_candidate(1505.0,10.049,'INSIDE TIME LIMIT',.true.)
  call expect(is_recent_frame(candidate), &
       'recent history catches just inside 0.05 seconds')
  candidate=make_candidate(1505.0,10.051,'OUTSIDE TIME LIMIT',.true.)
  call expect(.not.is_recent_frame(candidate), &
       'recent history allows just outside 0.05 seconds')
  candidate=make_candidate(1500.0,12.0,'ADJACENT MESSAGE',.true.)
  call expect(.not.is_recent_frame(candidate), &
       'recent history allows a message one full frame later')

  call reset_all()
  do i=1,MAX_RECENT_FRAMES
     candidate=make_candidate(1000.0+real(i),real(i),'HISTORY',.true.)
     call remember_recent_frame(candidate)
  enddo
  oldest_candidate=make_candidate(1001.0,1.0,'HISTORY',.true.)
  candidate=make_candidate(2500.0,1000.0,'AFTER HISTORY CAPACITY',.true.)
  call start_message(candidate,accepted_message,accepted)
  call expect(accepted .and. npending.eq.1, &
       'full duplicate history does not block message delivery')
  call expect(nrecent.eq.MAX_RECENT_FRAMES .and. is_recent_frame(candidate) .and. &
       .not.is_recent_frame(oldest_candidate), &
       'full duplicate history replaces its oldest fingerprint')

  call reset_all()
  candidate=make_candidate(1500.0,10.0,'PARTIAL',.false.)
  call start_message(candidate,accepted_message,accepted)
  index=nactive
  active_messages(index)%decoded='PARTIAL GROWN'
  active_messages(index)%k=len_trim(active_messages(index)%decoded)
  call prune_receive_state(17.59,frame_period)
  call expect(nactive.eq.1, &
       'an assembly remains active through its continuation horizon')
  call prune_receive_state(17.61,frame_period)
  call expect(nactive.eq.0, &
       'an abandoned assembly expires beyond its continuation horizon')
  call expect(npending.eq.1 .and. &
       trim(pending_updates(1)%decoded).eq.'PARTIAL GROWN' .and. &
       .not.pending_updates(1)%complete, &
       'expiration preserves the newest partial state for delivery')

  print *, 'test_jtty_receive_state: all checks passed'

contains

  function make_candidate(frequency,tsync,text,complete) result(value)
    real, intent(in) :: frequency,tsync
    character(len=*), intent(in) :: text
    logical, intent(in) :: complete
    type(decode) :: value

    value%f1=frequency
    value%tsync=tsync
    value%decoded=text
    value%is_last_frame=complete
  end function make_candidate

  subroutine reset_all()
    call reset_decode_search_state()
    call discard_pending_updates()
  end subroutine reset_all

  subroutine expect(condition,message)
    logical, intent(in) :: condition
    character(len=*), intent(in) :: message

    if(.not.condition) then
       write(*,'(a)') message
       error stop 1
    endif
  end subroutine expect

end program test_jtty_receive_state