aboutsummaryrefslogtreecommitdiffstats
path: root/test/nil/source/test/nil_test_sequence_004.c
blob: 16e86897dd708eda7a465d128370164a92693fed (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
202
203
204
205
206
/*
    ChibiOS - Copyright (C) 2006..2017 Giovanni Di Sirio

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#include "hal.h"
#include "nil_test_root.h"

/**
 * @file    nil_test_sequence_004.c
 * @brief   Test Sequence 004 code.
 *
 * @page nil_test_sequence_004 [4] Suspend/Resume and Event Flags
 *
 * File: @ref nil_test_sequence_004.c
 *
 * <h2>Description</h2>
 * This sequence tests the ChibiOS/NIL functionalities related to
 * threads suspend/resume and event flags.
 *
 * <h2>Test Cases</h2>
 * - @subpage nil_test_004_001
 * - @subpage nil_test_004_002
 * .
 */

/****************************************************************************
 * Shared code.
 ****************************************************************************/

static thread_reference_t tr1;

/****************************************************************************
 * Test cases.
 ****************************************************************************/

/**
 * @page nil_test_004_001 [4.1] Suspend and Resume functionality
 *
 * <h2>Description</h2>
 * The functionality of chThdSuspendTimeoutS() and chThdResumeI() is
 * tested.
 *
 * <h2>Test Steps</h2>
 * - [4.1.1] The function chThdSuspendTimeoutS() is invoked, the thread
 *   is remotely resumed with message @p MSG_OK. On return the message
 *   and the state of the reference are tested.
 * - [4.1.2] The function chThdSuspendTimeoutS() is invoked, the thread
 *   is not resumed so a timeout must occur. On return the message and
 *   the state of the reference are tested.
 * .
 */

static void nil_test_004_001_setup(void) {
  tr1 = NULL;
}

static void nil_test_004_001_execute(void) {
  systime_t time;
  msg_t msg;

  /* [4.1.1] The function chThdSuspendTimeoutS() is invoked, the thread
     is remotely resumed with message @p MSG_OK. On return the message
     and the state of the reference are tested.*/
  test_set_step(1);
  {
    chSysLock();
    msg = chThdSuspendTimeoutS(&gtr1, TIME_INFINITE);
    chSysUnlock();
    test_assert(NULL == gtr1, "not NULL");
    test_assert(MSG_OK == msg,"wrong returned message");
  }

  /* [4.1.2] The function chThdSuspendTimeoutS() is invoked, the thread
     is not resumed so a timeout must occur. On return the message and
     the state of the reference are tested.*/
  test_set_step(2);
  {
    chSysLock();
    time = chVTGetSystemTimeX();
    msg = chThdSuspendTimeoutS(&tr1, TIME_MS2I(1000));
    chSysUnlock();
    test_assert_time_window(chTimeAddX(time, TIME_MS2I(1000)),
                            chTimeAddX(time, TIME_MS2I(1000) + 1),
                            "out of time window");
    test_assert(NULL == tr1, "not NULL");
    test_assert(MSG_TIMEOUT == msg, "wrong returned message");
  }
}

static const testcase_t nil_test_004_001 = {
  "Suspend and Resume functionality",
  nil_test_004_001_setup,
  NULL,
  nil_test_004_001_execute
};

#if (CH_CFG_USE_EVENTS) || defined(__DOXYGEN__)
/**
 * @page nil_test_004_002 [4.2] Events Flags functionality
 *
 * <h2>Description</h2>
 * Event flags functionality is tested.
 *
 * <h2>Conditions</h2>
 * This test is only executed if the following preprocessor condition
 * evaluates to true:
 * - CH_CFG_USE_EVENTS
 * .
 *
 * <h2>Test Steps</h2>
 * - [4.2.1] A set of event flags are set on the current thread then
 *   the function chEvtWaitAnyTimeout() is invoked, the function is
 *   supposed to return immediately because the event flags are already
 *   pending, after return the events mask is tested.
 * - [4.2.2] The pending event flags mask is cleared then the function
 *   chEvtWaitAnyTimeout() is invoked, after return the events mask is
 *   tested. The thread is signaled by another thread.
 * - [4.2.3] The function chEvtWaitAnyTimeout() is invoked, no event
 *   can wakeup the thread, the function must return because timeout.
 * .
 */

static void nil_test_004_002_execute(void) {
  systime_t time;
  eventmask_t events;

  /* [4.2.1] A set of event flags are set on the current thread then
     the function chEvtWaitAnyTimeout() is invoked, the function is
     supposed to return immediately because the event flags are already
     pending, after return the events mask is tested.*/
  test_set_step(1);
  {
    time = chVTGetSystemTimeX();
    chEvtSignal(chThdGetSelfX(), 0x55);
    events = chEvtWaitAnyTimeout(ALL_EVENTS, TIME_MS2I(1000));
    test_assert((eventmask_t)0 != events, "timed out");
    test_assert((eventmask_t)0x55 == events, "wrong events mask");
  }

  /* [4.2.2] The pending event flags mask is cleared then the function
     chEvtWaitAnyTimeout() is invoked, after return the events mask is
     tested. The thread is signaled by another thread.*/
  test_set_step(2);
  {
    time = chVTGetSystemTimeX();
    chThdGetSelfX()->epmask = 0;
    events = chEvtWaitAnyTimeout(ALL_EVENTS, TIME_MS2I(1000));
    test_assert((eventmask_t)0 != events, "timed out");
    test_assert((eventmask_t)0x55 == events, "wrong events mask");
  }

  /* [4.2.3] The function chEvtWaitAnyTimeout() is invoked, no event
     can wakeup the thread, the function must return because timeout.*/
  test_set_step(3);
  {
    time = chVTGetSystemTimeX();
    events = chEvtWaitAnyTimeout(0, TIME_MS2I(1000));
    test_assert_time_window(chTimeAddX(time, TIME_MS2I(1000)),
                            chTimeAddX(time, TIME_MS2I(1000) + 1),
                            "out of time window");
    test_assert((eventmask_t)0 == events, "wrong events mask");
  }
}

static const testcase_t nil_test_004_002 = {
  "Events Flags functionality",
  NULL,
  NULL,
  nil_test_004_002_execute
};
#endif /* CH_CFG_USE_EVENTS */

/****************************************************************************
 * Exported data.
 ****************************************************************************/

/**
 * @brief   Array of test cases.
 */
const testcase_t * const nil_test_sequence_004_array[] = {
  &nil_test_004_001,
#if (CH_CFG_USE_EVENTS) || defined(__DOXYGEN__)
  &nil_test_004_002,
#endif
  NULL
};

/**
 * @brief   Suspend/Resume and Event Flags.
 */
const testsequence_t nil_test_sequence_004 = {
  "Suspend/Resume and Event Flags",
  nil_test_sequence_004_array
};