summaryrefslogtreecommitdiffstats
path: root/testsuites/validation/tc-ratemon-timeout.c
blob: dc2c745e61c82d3955e01b58075391a79cd82c26 (plain) (blame)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 * @file
 *
 * @ingroup RtemsRatemonReqTimeout
 */

/*
 * Copyright (C) 2021 embedded brains GmbH & Co. KG
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * This file is part of the RTEMS quality process and was automatically
 * generated.  If you find something that needs to be fixed or
 * worded better please post a report or patch to an RTEMS mailing list
 * or raise a bug report:
 *
 * https://www.rtems.org/bugs.html
 *
 * For information on updating and regenerating please refer to the How-To
 * section in the Software Requirements Engineering chapter of the
 * RTEMS Software Engineering manual.  The manual is provided as a part of
 * a release.  For development sources please refer to the online
 * documentation at:
 *
 * https://docs.rtems.org
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <rtems/test-scheduler.h>
#include <rtems/rtems/ratemonimpl.h>

#include "tx-support.h"

#include <rtems/test.h>

/**
 * @defgroup RtemsRatemonReqTimeout spec:/rtems/ratemon/req/timeout
 *
 * @ingroup TestsuitesValidationNoClock0
 * @ingroup TestsuitesValidationOneCpu0
 *
 * @{
 */

typedef enum {
  RtemsRatemonReqTimeout_Pre_WaitFor_PeriodSelf,
  RtemsRatemonReqTimeout_Pre_WaitFor_PeriodOther,
  RtemsRatemonReqTimeout_Pre_WaitFor_Other,
  RtemsRatemonReqTimeout_Pre_WaitFor_NA
} RtemsRatemonReqTimeout_Pre_WaitFor;

typedef enum {
  RtemsRatemonReqTimeout_Pre_WaitState_Blocked,
  RtemsRatemonReqTimeout_Pre_WaitState_IntendToBlock,
  RtemsRatemonReqTimeout_Pre_WaitState_NA
} RtemsRatemonReqTimeout_Pre_WaitState;

typedef enum {
  RtemsRatemonReqTimeout_Pre_PostponedJobs_Zero,
  RtemsRatemonReqTimeout_Pre_PostponedJobs_NotZeroOrMax,
  RtemsRatemonReqTimeout_Pre_PostponedJobs_Max,
  RtemsRatemonReqTimeout_Pre_PostponedJobs_NA
} RtemsRatemonReqTimeout_Pre_PostponedJobs;

typedef enum {
  RtemsRatemonReqTimeout_Post_PostponedJobs_Nop,
  RtemsRatemonReqTimeout_Post_PostponedJobs_PlusOne,
  RtemsRatemonReqTimeout_Post_PostponedJobs_NA
} RtemsRatemonReqTimeout_Post_PostponedJobs;

typedef enum {
  RtemsRatemonReqTimeout_Post_ReleaseJob_Yes,
  RtemsRatemonReqTimeout_Post_ReleaseJob_No,
  RtemsRatemonReqTimeout_Post_ReleaseJob_NA
} RtemsRatemonReqTimeout_Post_ReleaseJob;

typedef enum {
  RtemsRatemonReqTimeout_Post_Unblock_Yes,
  RtemsRatemonReqTimeout_Post_Unblock_No,
  RtemsRatemonReqTimeout_Post_Unblock_NA
} RtemsRatemonReqTimeout_Post_Unblock;

typedef enum {
  RtemsRatemonReqTimeout_Post_PeriodState_Active,
  RtemsRatemonReqTimeout_Post_PeriodState_Expired,
  RtemsRatemonReqTimeout_Post_PeriodState_NA
} RtemsRatemonReqTimeout_Post_PeriodState;

typedef enum {
  RtemsRatemonReqTimeout_Post_Timer_Active,
  RtemsRatemonReqTimeout_Post_Timer_NA
} RtemsRatemonReqTimeout_Post_Timer;

typedef enum {
  RtemsRatemonReqTimeout_Post_Uptime_Nop,
  RtemsRatemonReqTimeout_Post_Uptime_Set,
  RtemsRatemonReqTimeout_Post_Uptime_NA
} RtemsRatemonReqTimeout_Post_Uptime;

typedef enum {
  RtemsRatemonReqTimeout_Post_CPUUsage_Nop,
  RtemsRatemonReqTimeout_Post_CPUUsage_Set,
  RtemsRatemonReqTimeout_Post_CPUUsage_NA
} RtemsRatemonReqTimeout_Post_CPUUsage;

typedef struct {
  uint32_t Skip : 1;
  uint32_t Pre_WaitFor_NA : 1;
  uint32_t Pre_WaitState_NA : 1;
  uint32_t Pre_PostponedJobs_NA : 1;
  uint32_t Post_PostponedJobs : 2;
  uint32_t Post_ReleaseJob : 2;
  uint32_t Post_Unblock : 2;
  uint32_t Post_PeriodState : 2;
  uint32_t Post_Timer : 1;
  uint32_t Post_Uptime : 2;
  uint32_t Post_CPUUsage : 2;
} RtemsRatemonReqTimeout_Entry;

/**
 * @brief Test context for spec:/rtems/ratemon/req/timeout test case.
 */
typedef struct {
  /**
   * @brief This member contains the period identifier.
   */
  rtems_id period_id;

  /**
   * @brief This member references the period control block.
   */
  Rate_monotonic_Control *period;

  /**
   * @brief This member contains another period identifier.
   */
  rtems_id other_period_id;

  /**
   * @brief This member contains the worker task identifier.
   */
  rtems_id worker_id;

  /**
   * @brief This member contains the call within ISR request.
   */
  CallWithinISRRequest request;

  /**
   * @brief If this member is true, then the worker shall wait for a period.
   */
  bool wait_for_period;

  /**
   * @brief If this member is true, then the worker shall wait for another
   *   period.
   */
  bool period_is_other;

  /**
   * @brief If this member is true, then the worker shall intend to block for a
   *   period.
   */
  bool intend_to_block;

  /**
   * @brief This member contains the postponed jobs count before the timeout.
   */
  uint32_t postponed_jobs;

  /**
   * @brief This member contains the uptime of the period before the timeout.
   */
  Timestamp_Control uptime_before;

  /**
   * @brief This member contains the CPU usage of the period before the
   *   timeout.
   */
  Timestamp_Control cpu_usage_before;

  /**
   * @brief This member contains the release job counter.
   */
  uint32_t release_job_counter;

  /**
   * @brief This member contains the unblock counter.
   */
  uint32_t unblock_counter;

  struct {
    /**
     * @brief This member defines the pre-condition indices for the next
     *   action.
     */
    size_t pci[ 3 ];

    /**
     * @brief This member defines the pre-condition states for the next action.
     */
    size_t pcs[ 3 ];

    /**
     * @brief If this member is true, then the test action loop is executed.
     */
    bool in_action_loop;

    /**
     * @brief This member contains the next transition map index.
     */
    size_t index;

    /**
     * @brief This member contains the current transition map entry.
     */
    RtemsRatemonReqTimeout_Entry entry;

    /**
     * @brief If this member is true, then the current transition variant
     *   should be skipped.
     */
    bool skip;
  } Map;
} RtemsRatemonReqTimeout_Context;

static RtemsRatemonReqTimeout_Context
  RtemsRatemonReqTimeout_Instance;

static const char * const RtemsRatemonReqTimeout_PreDesc_WaitFor[] = {
  "PeriodSelf",
  "PeriodOther",
  "Other",
  "NA"
};

static const char * const RtemsRatemonReqTimeout_PreDesc_WaitState[] = {
  "Blocked",
  "IntendToBlock",
  "NA"
};

static const char * const RtemsRatemonReqTimeout_PreDesc_PostponedJobs[] = {
  "Zero",
  "NotZeroOrMax",
  "Max",
  "NA"
};

static const char * const * const RtemsRatemonReqTimeout_PreDesc[] = {
  RtemsRatemonReqTimeout_PreDesc_WaitFor,
  RtemsRatemonReqTimeout_PreDesc_WaitState,
  RtemsRatemonReqTimeout_PreDesc_PostponedJobs,
  NULL
};

#define EVENT_RESET RTEMS_EVENT_0

#define EVENT_PERIOD_WAIT RTEMS_EVENT_1

#define EVENT_PERIOD_OTHER RTEMS_EVENT_2

typedef RtemsRatemonReqTimeout_Context Context;

static void Tick( void *arg )
{
  Context               *ctx;
  T_scheduler_log_10     scheduler_log_10;
  const T_scheduler_log *scheduler_log;
  size_t                 index;

  ctx = arg;
  ctx->release_job_counter = 0;
  ctx->unblock_counter = 0;
  ctx->uptime_before = ctx->period->time_period_initiated;
  ctx->cpu_usage_before = ctx->period->cpu_usage_period_initiated;
  scheduler_log = T_scheduler_record_10( &scheduler_log_10 );
  T_null( scheduler_log );
  ClockTick();
  scheduler_log = T_scheduler_record( NULL );
  T_eq_ptr( &scheduler_log->header, &scheduler_log_10.header );

  index = 0;

  while ( true ) {
    const T_scheduler_event *event;

    event = T_scheduler_next_any( &scheduler_log_10.header, &index );

    if ( event == &T_scheduler_event_null ) {
      break;
    }

    T_eq_u32( event->thread->Object.id, ctx->worker_id );

    switch ( event->operation ) {
      case T_SCHEDULER_RELEASE_JOB:
        ++ctx->release_job_counter;
        T_eq_u64(
          event->release_job.deadline,
          rtems_clock_get_ticks_since_boot() + 1
        );
        break;
      case T_SCHEDULER_UNBLOCK:
        ++ctx->unblock_counter;
        break;
      default:
        break;
    }
  }
}

static void SchedulerBlock(
  void                    *arg,
  const T_scheduler_event *event,
  T_scheduler_when         when
)
{
  Context *ctx;

  ctx = arg;

  if (
    when == T_SCHEDULER_BEFORE &&
    event->operation == T_SCHEDULER_BLOCK
  ) {
    T_scheduler_set_event_handler( NULL, NULL );
    ctx->request.handler = Tick;
    CallWithinISRSubmit( &ctx->request );
  }
}

static Rate_monotonic_Control *GetControl( rtems_id id )
{
  Rate_monotonic_Control *period;
  ISR_lock_Context        lock_context;

  period = _Rate_monotonic_Get( id, &lock_context );
  T_assert_not_null( period );
  _ISR_lock_ISR_enable( &lock_context );

  return period;
}

static void Worker( rtems_task_argument arg )
{
  Context          *ctx;
  rtems_status_code sc;

  ctx = (Context *) arg;

  sc = rtems_rate_monotonic_create( OBJECT_NAME, &ctx->period_id );
  T_rsc_success( sc );

  sc = rtems_rate_monotonic_create( OBJECT_NAME, &ctx->other_period_id );
  T_rsc_success( sc );

  while ( true ) {
    rtems_event_set events;

    events = ReceiveAnyEvents();

    if ( ( events & EVENT_RESET ) != 0 ) {
      sc = rtems_rate_monotonic_cancel( ctx->period_id );
      T_rsc_success( sc );

      sc = rtems_rate_monotonic_cancel( ctx->other_period_id );
      T_rsc_success( sc );

      sc = rtems_rate_monotonic_period( ctx->period_id, 1 );
      T_rsc_success( sc );

      ctx->period->postponed_jobs = ctx->postponed_jobs;
    }

    if ( ( events & EVENT_PERIOD_WAIT ) != 0 ) {
      if ( ctx->intend_to_block ) {
        T_scheduler_set_event_handler( SchedulerBlock, ctx );
      }

      sc = rtems_rate_monotonic_period( ctx->period_id, 1 );
      T_rsc_success( sc );
    }

    if ( ( events & EVENT_PERIOD_OTHER ) != 0 ) {
      sc = rtems_rate_monotonic_period( ctx->other_period_id, 2 );
      T_rsc_success( sc );

      sc = rtems_rate_monotonic_period( ctx->other_period_id, 2 );
      T_rsc_success( sc );
    }
  }
}

static void RtemsRatemonReqTimeout_Pre_WaitFor_Prepare(
  RtemsRatemonReqTimeout_Context    *ctx,
  RtemsRatemonReqTimeout_Pre_WaitFor state
)
{
  switch ( state ) {
    case RtemsRatemonReqTimeout_Pre_WaitFor_PeriodSelf: {
      /*
       * While the owner task of the period waits for the period.
       */
      ctx->wait_for_period = true;
      ctx->period_is_other = false;
      break;
    }

    case RtemsRatemonReqTimeout_Pre_WaitFor_PeriodOther: {
      /*
       * While the owner task of the period waits for another period.
       */
      ctx->wait_for_period = true;
      ctx->period_is_other = true;
      break;
    }

    case RtemsRatemonReqTimeout_Pre_WaitFor_Other: {
      /*
       * While the owner task of the period does not wait for a period.
       */
      ctx->wait_for_period = false;
      break;
    }

    case RtemsRatemonReqTimeout_Pre_WaitFor_NA:
      break;
  }
}

static void RtemsRatemonReqTimeout_Pre_WaitState_Prepare(
  RtemsRatemonReqTimeout_Context      *ctx,
  RtemsRatemonReqTimeout_Pre_WaitState state
)
{
  switch ( state ) {
    case RtemsRatemonReqTimeout_Pre_WaitState_Blocked: {
      /*
       * While the owner task is in the blocked wait state.
       */
      ctx->intend_to_block = false;
      break;
    }

    case RtemsRatemonReqTimeout_Pre_WaitState_IntendToBlock: {
      /*
       * While the owner task is in the intend to block wait state.
       */
      ctx->intend_to_block = true;
      break;
    }

    case RtemsRatemonReqTimeout_Pre_WaitState_NA:
      break;
  }
}

static void RtemsRatemonReqTimeout_Pre_PostponedJobs_Prepare(
  RtemsRatemonReqTimeout_Context          *ctx,
  RtemsRatemonReqTimeout_Pre_PostponedJobs state
)
{
  switch ( state ) {
    case RtemsRatemonReqTimeout_Pre_PostponedJobs_Zero: {
      /*
       * While the count of postponed jobs is equal to zero.
       */
      ctx->postponed_jobs = 0;
      break;
    }

    case RtemsRatemonReqTimeout_Pre_PostponedJobs_NotZeroOrMax: {
      /*
       * While the count of postponed jobs is not equal to zero or UINT32_MAX.
       */
      ctx->postponed_jobs = 123;
      break;
    }

    case RtemsRatemonReqTimeout_Pre_PostponedJobs_Max: {
      /*
       * While the count of postponed jobs is equal to UINT32_MAX.
       */
      ctx->postponed_jobs = UINT32_MAX;
      break;
    }

    case RtemsRatemonReqTimeout_Pre_PostponedJobs_NA:
      break;
  }
}

static void RtemsRatemonReqTimeout_Post_PostponedJobs_Check(
  RtemsRatemonReqTimeout_Context           *ctx,
  RtemsRatemonReqTimeout_Post_PostponedJobs state
)
{
  switch ( state ) {
    case RtemsRatemonReqTimeout_Post_PostponedJobs_Nop: {
      /*
       * The count of postponed jobs of the period shall not be modified.
       */
      T_eq_u32( ctx->period->postponed_jobs, ctx->postponed_jobs );
      break;
    }

    case RtemsRatemonReqTimeout_Post_PostponedJobs_PlusOne: {
      /*
       * The count of postponed jobs of the period shall be incremented by one.
       */
      T_eq_u32( ctx->period->postponed_jobs, ctx->postponed_jobs + 1 );
      break;
    }

    case RtemsRatemonReqTimeout_Post_PostponedJobs_NA:
      break;
  }
}

static void RtemsRatemonReqTimeout_Post_ReleaseJob_Check(
  RtemsRatemonReqTimeout_Context        *ctx,
  RtemsRatemonReqTimeout_Post_ReleaseJob state
)
{
  switch ( state ) {
    case RtemsRatemonReqTimeout_Post_ReleaseJob_Yes: {
      /*
       * The owner task of the period shall release a job with a deadline equal
       * to the clock tick plus the next period length by the timeout
       * operation.
       */
      T_eq_u32( ctx->release_job_counter, 1 );
      break;
    }

    case RtemsRatemonReqTimeout_Post_ReleaseJob_No: {
      /*
       * The owner task of the period shall not release a job by the timeout
       * operation.
       */
      T_eq_u32( ctx->release_job_counter, 0 );
      break;
    }

    case RtemsRatemonReqTimeout_Post_ReleaseJob_NA:
      break;
  }
}

static void RtemsRatemonReqTimeout_Post_Unblock_Check(
  RtemsRatemonReqTimeout_Context     *ctx,
  RtemsRatemonReqTimeout_Post_Unblock state
)
{
  switch ( state ) {
    case RtemsRatemonReqTimeout_Post_Unblock_Yes: {
      /*
       * The owner task of the period shall be unblocked by the timeout
       * operation.
       */
      T_eq_u32( ctx->unblock_counter, 1 );
      break;
    }

    case RtemsRatemonReqTimeout_Post_Unblock_No: {
      /*
       * The owner task of the period shall not be unblocked by the timeout
       * operation.
       */
      T_eq_u32( ctx->unblock_counter, 0 );
      break;
    }

    case RtemsRatemonReqTimeout_Post_Unblock_NA:
      break;
  }
}

static void RtemsRatemonReqTimeout_Post_PeriodState_Check(
  RtemsRatemonReqTimeout_Context         *ctx,
  RtemsRatemonReqTimeout_Post_PeriodState state
)
{
  switch ( state ) {
    case RtemsRatemonReqTimeout_Post_PeriodState_Active: {
      /*
       * The period state shall be active.
       */
      T_eq_int( ctx->period->state, RATE_MONOTONIC_ACTIVE );
      break;
    }

    case RtemsRatemonReqTimeout_Post_PeriodState_Expired: {
      /*
       * The period state shall be expired.
       */
      T_eq_int( ctx->period->state, RATE_MONOTONIC_EXPIRED );
      break;
    }

    case RtemsRatemonReqTimeout_Post_PeriodState_NA:
      break;
  }
}

static void RtemsRatemonReqTimeout_Post_Timer_Check(
  RtemsRatemonReqTimeout_Context   *ctx,
  RtemsRatemonReqTimeout_Post_Timer state
)
{
  switch ( state ) {
    case RtemsRatemonReqTimeout_Post_Timer_Active: {
      /*
       * The timeout timer of the period shall expired at the current clock
       * tick plus the next period length.
       */
      T_true( _Watchdog_Is_scheduled( &ctx->period->Timer ) );
      T_eq_u64(
        ctx->period->Timer.expire,
        rtems_clock_get_ticks_since_boot() + 1
      );
      break;
    }

    case RtemsRatemonReqTimeout_Post_Timer_NA:
      break;
  }
}

static void RtemsRatemonReqTimeout_Post_Uptime_Check(
  RtemsRatemonReqTimeout_Context    *ctx,
  RtemsRatemonReqTimeout_Post_Uptime state
)
{
  switch ( state ) {
    case RtemsRatemonReqTimeout_Post_Uptime_Nop: {
      /*
       * The period initiated CLOCK_MONOTONIC value shall not be modified.
       */
      T_eq_i64( ctx->uptime_before, ctx->period->time_period_initiated );
      break;
    }

    case RtemsRatemonReqTimeout_Post_Uptime_Set: {
      /*
       * The period initiated CLOCK_MONOTONIC value shall be set to the
       * CLOCK_MONOTONIC at some time point during the timeout operation.
       */
      T_ne_i64( ctx->uptime_before, ctx->period->time_period_initiated );
      break;
    }

    case RtemsRatemonReqTimeout_Post_Uptime_NA:
      break;
  }
}

static void RtemsRatemonReqTimeout_Post_CPUUsage_Check(
  RtemsRatemonReqTimeout_Context      *ctx,
  RtemsRatemonReqTimeout_Post_CPUUsage state
)
{
  switch ( state ) {
    case RtemsRatemonReqTimeout_Post_CPUUsage_Nop: {
      /*
       * The period initiated CPU usage of the owner task value shall not be
       * modified.
       */
      T_eq_i64(
        ctx->cpu_usage_before,
        ctx->period->cpu_usage_period_initiated
      );
      break;
    }

    case RtemsRatemonReqTimeout_Post_CPUUsage_Set: {
      /*
       * The period initiated CPU usage of the owner task value shall be set to
       * the CPU usage of the owner task at some time point during the timeout
       * operation.
       */
      T_ne_i64(
        ctx->cpu_usage_before,
        ctx->period->cpu_usage_period_initiated
      );
      break;
    }

    case RtemsRatemonReqTimeout_Post_CPUUsage_NA:
      break;
  }
}

static void RtemsRatemonReqTimeout_Setup( RtemsRatemonReqTimeout_Context *ctx )
{
  ctx->request.arg = ctx;
  ctx->worker_id = CreateTask( "WORK", GetSelfPriority() );
  StartTask( ctx->worker_id, Worker, ctx );
  Yield();
  ctx->period = GetControl( ctx->period_id );
}

static void RtemsRatemonReqTimeout_Setup_Wrap( void *arg )
{
  RtemsRatemonReqTimeout_Context *ctx;

  ctx = arg;
  ctx->Map.in_action_loop = false;
  RtemsRatemonReqTimeout_Setup( ctx );
}

static void RtemsRatemonReqTimeout_Teardown(
  RtemsRatemonReqTimeout_Context *ctx
)
{
  rtems_status_code sc;

  DeleteTask( ctx->worker_id );

  sc = rtems_rate_monotonic_delete( ctx->period_id );
  T_rsc_success( sc );

  sc = rtems_rate_monotonic_delete( ctx->other_period_id );
  T_rsc_success( sc );
}

static void RtemsRatemonReqTimeout_Teardown_Wrap( void *arg )
{
  RtemsRatemonReqTimeout_Context *ctx;

  ctx = arg;
  ctx->Map.in_action_loop = false;
  RtemsRatemonReqTimeout_Teardown( ctx );
}

static void RtemsRatemonReqTimeout_Action(
  RtemsRatemonReqTimeout_Context *ctx
)
{
  SendEvents( ctx->worker_id, EVENT_RESET );
  Yield();

  if ( ctx->wait_for_period ) {
    if ( ctx->period_is_other ) {
      SendEvents( ctx->worker_id, EVENT_PERIOD_OTHER );
      Yield();
      Tick( ctx );
    } else {
      SendEvents( ctx->worker_id, EVENT_PERIOD_WAIT );
      Yield();

      if ( !ctx->intend_to_block ) {
        Tick( ctx );
      }
    }
  } else {
    Tick( ctx );
  }
}

static void RtemsRatemonReqTimeout_Cleanup(
  RtemsRatemonReqTimeout_Context *ctx
)
{
  ClockTick();
  Yield();
}

static const RtemsRatemonReqTimeout_Entry
RtemsRatemonReqTimeout_Entries[] = {
  { 0, 0, 1, 0, RtemsRatemonReqTimeout_Post_PostponedJobs_PlusOne,
    RtemsRatemonReqTimeout_Post_ReleaseJob_No,
    RtemsRatemonReqTimeout_Post_Unblock_No,
    RtemsRatemonReqTimeout_Post_PeriodState_Expired,
    RtemsRatemonReqTimeout_Post_Timer_Active,
    RtemsRatemonReqTimeout_Post_Uptime_Nop,
    RtemsRatemonReqTimeout_Post_CPUUsage_Nop },
  { 1, 0, 0, 0, RtemsRatemonReqTimeout_Post_PostponedJobs_NA,
    RtemsRatemonReqTimeout_Post_ReleaseJob_NA,
    RtemsRatemonReqTimeout_Post_Unblock_NA,
    RtemsRatemonReqTimeout_Post_PeriodState_NA,
    RtemsRatemonReqTimeout_Post_Timer_NA,
    RtemsRatemonReqTimeout_Post_Uptime_NA,
    RtemsRatemonReqTimeout_Post_CPUUsage_NA },
  { 0, 0, 1, 0, RtemsRatemonReqTimeout_Post_PostponedJobs_Nop,
    RtemsRatemonReqTimeout_Post_ReleaseJob_No,
    RtemsRatemonReqTimeout_Post_Unblock_No,
    RtemsRatemonReqTimeout_Post_PeriodState_Expired,
    RtemsRatemonReqTimeout_Post_Timer_Active,
    RtemsRatemonReqTimeout_Post_Uptime_Nop,
    RtemsRatemonReqTimeout_Post_CPUUsage_Nop },
  { 0, 0, 0, 0, RtemsRatemonReqTimeout_Post_PostponedJobs_Nop,
    RtemsRatemonReqTimeout_Post_ReleaseJob_Yes,
    RtemsRatemonReqTimeout_Post_Unblock_Yes,
    RtemsRatemonReqTimeout_Post_PeriodState_Active,
    RtemsRatemonReqTimeout_Post_Timer_Active,
    RtemsRatemonReqTimeout_Post_Uptime_Set,
    RtemsRatemonReqTimeout_Post_CPUUsage_Set },
  { 0, 0, 0, 0, RtemsRatemonReqTimeout_Post_PostponedJobs_Nop,
    RtemsRatemonReqTimeout_Post_ReleaseJob_Yes,
    RtemsRatemonReqTimeout_Post_Unblock_No,
    RtemsRatemonReqTimeout_Post_PeriodState_Active,
    RtemsRatemonReqTimeout_Post_Timer_Active,
    RtemsRatemonReqTimeout_Post_Uptime_Set,
    RtemsRatemonReqTimeout_Post_CPUUsage_Set }
};

static const uint8_t
RtemsRatemonReqTimeout_Map[] = {
  3, 1, 1, 4, 1, 1, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 2
};

static size_t RtemsRatemonReqTimeout_Scope( void *arg, char *buf, size_t n )
{
  RtemsRatemonReqTimeout_Context *ctx;

  ctx = arg;

  if ( ctx->Map.in_action_loop ) {
    return T_get_scope( RtemsRatemonReqTimeout_PreDesc, buf, n, ctx->Map.pcs );
  }

  return 0;
}

static T_fixture RtemsRatemonReqTimeout_Fixture = {
  .setup = RtemsRatemonReqTimeout_Setup_Wrap,
  .stop = NULL,
  .teardown = RtemsRatemonReqTimeout_Teardown_Wrap,
  .scope = RtemsRatemonReqTimeout_Scope,
  .initial_context = &RtemsRatemonReqTimeout_Instance
};

static inline RtemsRatemonReqTimeout_Entry RtemsRatemonReqTimeout_PopEntry(
  RtemsRatemonReqTimeout_Context *ctx
)
{
  size_t index;

  index = ctx->Map.index;
  ctx->Map.index = index + 1;
  return RtemsRatemonReqTimeout_Entries[
    RtemsRatemonReqTimeout_Map[ index ]
  ];
}

static void RtemsRatemonReqTimeout_SetPreConditionStates(
  RtemsRatemonReqTimeout_Context *ctx
)
{
  ctx->Map.pcs[ 0 ] = ctx->Map.pci[ 0 ];

  if ( ctx->Map.entry.Pre_WaitState_NA ) {
    ctx->Map.pcs[ 1 ] = RtemsRatemonReqTimeout_Pre_WaitState_NA;
  } else {
    ctx->Map.pcs[ 1 ] = ctx->Map.pci[ 1 ];
  }

  ctx->Map.pcs[ 2 ] = ctx->Map.pci[ 2 ];
}

static void RtemsRatemonReqTimeout_TestVariant(
  RtemsRatemonReqTimeout_Context *ctx
)
{
  RtemsRatemonReqTimeout_Pre_WaitFor_Prepare( ctx, ctx->Map.pcs[ 0 ] );
  RtemsRatemonReqTimeout_Pre_WaitState_Prepare( ctx, ctx->Map.pcs[ 1 ] );
  RtemsRatemonReqTimeout_Pre_PostponedJobs_Prepare( ctx, ctx->Map.pcs[ 2 ] );
  RtemsRatemonReqTimeout_Action( ctx );
  RtemsRatemonReqTimeout_Post_PostponedJobs_Check(
    ctx,
    ctx->Map.entry.Post_PostponedJobs
  );
  RtemsRatemonReqTimeout_Post_ReleaseJob_Check(
    ctx,
    ctx->Map.entry.Post_ReleaseJob
  );
  RtemsRatemonReqTimeout_Post_Unblock_Check(
    ctx,
    ctx->Map.entry.Post_Unblock
  );
  RtemsRatemonReqTimeout_Post_PeriodState_Check(
    ctx,
    ctx->Map.entry.Post_PeriodState
  );
  RtemsRatemonReqTimeout_Post_Timer_Check( ctx, ctx->Map.entry.Post_Timer );
  RtemsRatemonReqTimeout_Post_Uptime_Check( ctx, ctx->Map.entry.Post_Uptime );
  RtemsRatemonReqTimeout_Post_CPUUsage_Check(
    ctx,
    ctx->Map.entry.Post_CPUUsage
  );
}

/**
 * @fn void T_case_body_RtemsRatemonReqTimeout( void )
 */
T_TEST_CASE_FIXTURE( RtemsRatemonReqTimeout, &RtemsRatemonReqTimeout_Fixture )
{
  RtemsRatemonReqTimeout_Context *ctx;

  ctx = T_fixture_context();
  ctx->Map.in_action_loop = true;
  ctx->Map.index = 0;

  for (
    ctx->Map.pci[ 0 ] = RtemsRatemonReqTimeout_Pre_WaitFor_PeriodSelf;
    ctx->Map.pci[ 0 ] < RtemsRatemonReqTimeout_Pre_WaitFor_NA;
    ++ctx->Map.pci[ 0 ]
  ) {
    for (
      ctx->Map.pci[ 1 ] = RtemsRatemonReqTimeout_Pre_WaitState_Blocked;
      ctx->Map.pci[ 1 ] < RtemsRatemonReqTimeout_Pre_WaitState_NA;
      ++ctx->Map.pci[ 1 ]
    ) {
      for (
        ctx->Map.pci[ 2 ] = RtemsRatemonReqTimeout_Pre_PostponedJobs_Zero;
        ctx->Map.pci[ 2 ] < RtemsRatemonReqTimeout_Pre_PostponedJobs_NA;
        ++ctx->Map.pci[ 2 ]
      ) {
        ctx->Map.entry = RtemsRatemonReqTimeout_PopEntry( ctx );

        if ( ctx->Map.entry.Skip ) {
          continue;
        }

        RtemsRatemonReqTimeout_SetPreConditionStates( ctx );
        RtemsRatemonReqTimeout_TestVariant( ctx );
        RtemsRatemonReqTimeout_Cleanup( ctx );
      }
    }
  }
}

/** @} */