1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2012. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.org/license/LICENSE. |
---|
8 | */ |
---|
9 | |
---|
10 | #ifdef HAVE_CONFIG_H |
---|
11 | #include "config.h" |
---|
12 | #endif |
---|
13 | |
---|
14 | #include <tmacros.h> |
---|
15 | #include <rtems/chain.h> |
---|
16 | |
---|
17 | const char rtems_test_name[] = "SPCHAIN"; |
---|
18 | |
---|
19 | /* forward declarations to avoid warnings */ |
---|
20 | rtems_task Init(rtems_task_argument argument); |
---|
21 | |
---|
22 | #define EVENT RTEMS_EVENT_13 |
---|
23 | #define TIMEOUT 1 |
---|
24 | |
---|
25 | typedef struct { |
---|
26 | rtems_chain_node Node; |
---|
27 | int id; |
---|
28 | } test_node; |
---|
29 | |
---|
30 | static rtems_chain_control one_node_chain; |
---|
31 | |
---|
32 | static rtems_chain_node node_of_one_node_chain = |
---|
33 | RTEMS_CHAIN_NODE_INITIALIZER_ONE_NODE_CHAIN( &one_node_chain ); |
---|
34 | |
---|
35 | static rtems_chain_control one_node_chain = |
---|
36 | RTEMS_CHAIN_INITIALIZER_ONE_NODE( &node_of_one_node_chain ); |
---|
37 | |
---|
38 | static void test_chain_control_initializer(void) |
---|
39 | { |
---|
40 | rtems_chain_control chain = RTEMS_CHAIN_INITIALIZER_EMPTY( chain ); |
---|
41 | |
---|
42 | puts( "INIT - Verify rtems_chain_control initializer" ); |
---|
43 | |
---|
44 | rtems_test_assert( rtems_chain_is_empty( &chain ) ); |
---|
45 | |
---|
46 | rtems_test_assert( rtems_chain_has_only_one_node( &one_node_chain ) ); |
---|
47 | rtems_test_assert( |
---|
48 | rtems_chain_immutable_first( &one_node_chain ) == &node_of_one_node_chain |
---|
49 | ); |
---|
50 | rtems_test_assert( |
---|
51 | rtems_chain_immutable_last( &one_node_chain ) == &node_of_one_node_chain |
---|
52 | ); |
---|
53 | rtems_test_assert( |
---|
54 | rtems_chain_immutable_head( &one_node_chain ) |
---|
55 | == rtems_chain_immutable_previous( &node_of_one_node_chain ) |
---|
56 | ); |
---|
57 | rtems_test_assert( |
---|
58 | rtems_chain_immutable_tail( &one_node_chain ) |
---|
59 | == rtems_chain_immutable_next( &node_of_one_node_chain ) |
---|
60 | ); |
---|
61 | } |
---|
62 | |
---|
63 | static void test_chain_control_layout(void) |
---|
64 | { |
---|
65 | Chain_Control chain; |
---|
66 | |
---|
67 | puts( "INIT - Verify rtems_chain_control layout" ); |
---|
68 | |
---|
69 | rtems_test_assert( |
---|
70 | sizeof(Chain_Control) |
---|
71 | == sizeof(Chain_Node) + sizeof(Chain_Node *) |
---|
72 | ); |
---|
73 | rtems_test_assert( |
---|
74 | sizeof(Chain_Control) |
---|
75 | == 3 * sizeof(Chain_Node *) |
---|
76 | ); |
---|
77 | rtems_test_assert( |
---|
78 | _Chain_Previous( _Chain_Head( &chain ) ) |
---|
79 | == _Chain_Next( _Chain_Tail( &chain ) ) |
---|
80 | ); |
---|
81 | |
---|
82 | #if !defined( RTEMS_SMP ) |
---|
83 | rtems_test_assert( |
---|
84 | sizeof(Chain_Control) |
---|
85 | == sizeof(rtems_chain_control) |
---|
86 | ); |
---|
87 | #endif |
---|
88 | } |
---|
89 | |
---|
90 | static void test_chain_get_with_wait(void) |
---|
91 | { |
---|
92 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
93 | rtems_chain_control chain; |
---|
94 | rtems_chain_node *p = (rtems_chain_node *) 1; |
---|
95 | |
---|
96 | puts( "INIT - Verify rtems_chain_get_with_wait" ); |
---|
97 | rtems_chain_initialize_empty( &chain ); |
---|
98 | sc = rtems_chain_get_with_wait( &chain, EVENT, TIMEOUT, &p ); |
---|
99 | rtems_test_assert( sc == RTEMS_TIMEOUT ); |
---|
100 | rtems_test_assert( p == NULL ); |
---|
101 | } |
---|
102 | |
---|
103 | static void test_chain_first_and_last(void) |
---|
104 | { |
---|
105 | rtems_chain_control chain; |
---|
106 | rtems_chain_node node1, node2; |
---|
107 | rtems_chain_node *cnode; |
---|
108 | |
---|
109 | rtems_chain_initialize_empty( &chain ); |
---|
110 | rtems_chain_append( &chain, &node1 ); |
---|
111 | rtems_chain_insert( &node1, &node2 ); |
---|
112 | |
---|
113 | puts( "INIT - Verify rtems_chain_is_first" ); |
---|
114 | cnode = rtems_chain_first(&chain); |
---|
115 | rtems_test_assert( rtems_chain_is_first( cnode ) ); |
---|
116 | |
---|
117 | puts( "INIT - Verify rtems_chain_is_last" ); |
---|
118 | cnode = rtems_chain_last(&chain); |
---|
119 | rtems_test_assert( rtems_chain_is_last( cnode ) ); |
---|
120 | |
---|
121 | cnode = rtems_chain_get_first_unprotected( &chain ); |
---|
122 | rtems_test_assert( cnode == &node1 ); |
---|
123 | cnode = rtems_chain_first( &chain ); |
---|
124 | rtems_test_assert( cnode == &node2 ); |
---|
125 | cnode = rtems_chain_last( &chain ); |
---|
126 | rtems_test_assert( cnode == &node2 ); |
---|
127 | } |
---|
128 | |
---|
129 | static void test_chain_with_notification(void) |
---|
130 | { |
---|
131 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
132 | rtems_chain_control chain; |
---|
133 | rtems_chain_node a; |
---|
134 | rtems_chain_node b; |
---|
135 | rtems_chain_node *p = (rtems_chain_node *) 1; |
---|
136 | rtems_event_set out = 0; |
---|
137 | |
---|
138 | puts( "INIT - Verify rtems_chain_append_with_notification" ); |
---|
139 | rtems_chain_initialize_empty( &chain ); |
---|
140 | sc = rtems_chain_append_with_notification( |
---|
141 | &chain, |
---|
142 | &a, |
---|
143 | rtems_task_self(), |
---|
144 | EVENT |
---|
145 | ); |
---|
146 | rtems_test_assert( sc == RTEMS_SUCCESSFUL ); |
---|
147 | sc = rtems_chain_get_with_wait( &chain, EVENT, TIMEOUT, &p ); |
---|
148 | rtems_test_assert( sc == RTEMS_SUCCESSFUL ); |
---|
149 | rtems_test_assert( p == &a ); |
---|
150 | |
---|
151 | rtems_chain_initialize_empty( &chain ); |
---|
152 | |
---|
153 | rtems_chain_append( &chain, &b ); |
---|
154 | sc = rtems_chain_append_with_notification( |
---|
155 | &chain, |
---|
156 | &a, |
---|
157 | rtems_task_self(), |
---|
158 | EVENT |
---|
159 | ); |
---|
160 | rtems_test_assert( sc == RTEMS_SUCCESSFUL ); |
---|
161 | rtems_test_assert( p == &a ); |
---|
162 | |
---|
163 | puts( "INIT - Verify rtems_chain_prepend_with_notification" ); |
---|
164 | rtems_chain_initialize_empty( &chain ); |
---|
165 | sc = rtems_chain_prepend_with_notification( |
---|
166 | &chain, |
---|
167 | &a, |
---|
168 | rtems_task_self(), |
---|
169 | EVENT |
---|
170 | ); |
---|
171 | rtems_test_assert( sc == RTEMS_SUCCESSFUL ); |
---|
172 | sc = rtems_chain_get_with_wait( &chain, EVENT, TIMEOUT, &p ); |
---|
173 | rtems_test_assert( sc == RTEMS_SUCCESSFUL ); |
---|
174 | rtems_test_assert( p == &a ); |
---|
175 | |
---|
176 | rtems_chain_prepend( &chain, &b ); |
---|
177 | sc = rtems_chain_prepend_with_notification( |
---|
178 | &chain, |
---|
179 | &a, |
---|
180 | rtems_task_self(), |
---|
181 | EVENT |
---|
182 | ); |
---|
183 | rtems_test_assert( sc == RTEMS_SUCCESSFUL ); |
---|
184 | rtems_test_assert( p == &a ); |
---|
185 | |
---|
186 | puts( "INIT - Verify rtems_chain_get_with_notification" ); |
---|
187 | rtems_chain_initialize_empty( &chain ); |
---|
188 | |
---|
189 | rtems_chain_append( &chain, &b ); |
---|
190 | rtems_chain_append( &chain, &a ); |
---|
191 | |
---|
192 | sc = rtems_chain_get_with_notification(&chain, rtems_task_self(), EVENT, &p); |
---|
193 | rtems_test_assert( sc == RTEMS_SUCCESSFUL ); |
---|
194 | rtems_test_assert( p == &b ); |
---|
195 | |
---|
196 | sc = rtems_chain_get_with_notification(&chain, rtems_task_self(), EVENT, &p); |
---|
197 | rtems_test_assert( sc == RTEMS_SUCCESSFUL ); |
---|
198 | rtems_test_assert( p == &a ); |
---|
199 | sc = rtems_event_receive( |
---|
200 | EVENT, |
---|
201 | RTEMS_EVENT_ALL | RTEMS_WAIT, |
---|
202 | TIMEOUT, |
---|
203 | &out |
---|
204 | ); |
---|
205 | rtems_test_assert( sc == RTEMS_SUCCESSFUL ); |
---|
206 | rtems_test_assert( out == EVENT ); |
---|
207 | } |
---|
208 | |
---|
209 | static void test_chain_with_empty_check(void) |
---|
210 | { |
---|
211 | rtems_chain_control chain; |
---|
212 | rtems_chain_node a; |
---|
213 | rtems_chain_node b; |
---|
214 | rtems_chain_node *p; |
---|
215 | bool empty; |
---|
216 | |
---|
217 | puts( "INIT - Verify rtems_chain_append_with_empty_check" ); |
---|
218 | rtems_chain_initialize_empty( &chain ); |
---|
219 | empty = rtems_chain_append_with_empty_check( &chain, &a ); |
---|
220 | rtems_test_assert( empty ); |
---|
221 | empty = rtems_chain_append_with_empty_check( &chain, &a ); |
---|
222 | rtems_test_assert( !empty ); |
---|
223 | |
---|
224 | puts( "INIT - Verify rtems_chain_prepend_with_empty_check" ); |
---|
225 | rtems_chain_initialize_empty( &chain ); |
---|
226 | empty = rtems_chain_prepend_with_empty_check( &chain, &a ); |
---|
227 | rtems_test_assert( empty ); |
---|
228 | empty = rtems_chain_prepend_with_empty_check( &chain, &a ); |
---|
229 | rtems_test_assert( !empty ); |
---|
230 | empty = rtems_chain_prepend_with_empty_check( &chain, &b ); |
---|
231 | rtems_test_assert( !empty ); |
---|
232 | |
---|
233 | puts( "INIT - Verify rtems_chain_get_with_empty_check" ); |
---|
234 | rtems_chain_initialize_empty( &chain ); |
---|
235 | empty = rtems_chain_get_with_empty_check( &chain, &p ); |
---|
236 | rtems_test_assert( empty ); |
---|
237 | |
---|
238 | rtems_chain_append( &chain, &a ); |
---|
239 | rtems_chain_append( &chain, &b ); |
---|
240 | empty = rtems_chain_get_with_empty_check( &chain, &p ); |
---|
241 | rtems_test_assert( !empty ); |
---|
242 | rtems_test_assert( p == &a ); |
---|
243 | empty = rtems_chain_get_with_empty_check( &chain, &p ); |
---|
244 | rtems_test_assert( empty ); |
---|
245 | rtems_test_assert( p == &b ); |
---|
246 | } |
---|
247 | |
---|
248 | static void test_chain_node_count(void) |
---|
249 | { |
---|
250 | rtems_chain_control chain; |
---|
251 | rtems_chain_node nodes[3]; |
---|
252 | size_t count; |
---|
253 | size_t i; |
---|
254 | |
---|
255 | puts( "INIT - Verify rtems_chain_node_count_unprotected" ); |
---|
256 | |
---|
257 | rtems_chain_initialize_empty( &chain ); |
---|
258 | count = rtems_chain_node_count_unprotected( &chain ); |
---|
259 | rtems_test_assert( count == 0 ); |
---|
260 | |
---|
261 | for (i = 0; i < RTEMS_ARRAY_SIZE( nodes ); ++i) { |
---|
262 | rtems_chain_append_unprotected( &chain, &nodes[i] ); |
---|
263 | count = rtems_chain_node_count_unprotected( &chain ); |
---|
264 | rtems_test_assert( count == i + 1 ); |
---|
265 | } |
---|
266 | } |
---|
267 | |
---|
268 | static bool test_order( const Chain_Node *left, const Chain_Node *right ) |
---|
269 | { |
---|
270 | return left < right; |
---|
271 | } |
---|
272 | |
---|
273 | static void test_chain_insert_ordered( void ) |
---|
274 | { |
---|
275 | Chain_Control chain = CHAIN_INITIALIZER_EMPTY(chain); |
---|
276 | Chain_Node nodes[5]; |
---|
277 | const Chain_Node *tail; |
---|
278 | const Chain_Node *node; |
---|
279 | size_t n = RTEMS_ARRAY_SIZE( nodes ); |
---|
280 | size_t i = 0; |
---|
281 | |
---|
282 | puts( "INIT - Verify _Chain_Insert_ordered_unprotected" ); |
---|
283 | |
---|
284 | _Chain_Insert_ordered_unprotected( &chain, &nodes[4], test_order ); |
---|
285 | _Chain_Insert_ordered_unprotected( &chain, &nodes[2], test_order ); |
---|
286 | _Chain_Insert_ordered_unprotected( &chain, &nodes[0], test_order ); |
---|
287 | _Chain_Insert_ordered_unprotected( &chain, &nodes[3], test_order ); |
---|
288 | _Chain_Insert_ordered_unprotected( &chain, &nodes[1], test_order ); |
---|
289 | |
---|
290 | tail = _Chain_Immutable_tail( &chain ); |
---|
291 | node = _Chain_Immutable_first( &chain ); |
---|
292 | while ( node != tail && i < n ) { |
---|
293 | rtems_test_assert( node == &nodes[ i ] ); |
---|
294 | ++i; |
---|
295 | node = _Chain_Immutable_next( node ); |
---|
296 | } |
---|
297 | |
---|
298 | rtems_test_assert( i == n ); |
---|
299 | } |
---|
300 | |
---|
301 | rtems_task Init( |
---|
302 | rtems_task_argument ignored |
---|
303 | ) |
---|
304 | { |
---|
305 | rtems_chain_control chain1; |
---|
306 | rtems_chain_node *p; |
---|
307 | test_node node1, node2; |
---|
308 | int id; |
---|
309 | |
---|
310 | TEST_BEGIN(); |
---|
311 | |
---|
312 | puts( "Init - Initialize chain empty" ); |
---|
313 | rtems_chain_initialize_empty( &chain1 ); |
---|
314 | |
---|
315 | /* verify that the chain append and insert work */ |
---|
316 | puts( "INIT - Verify rtems_chain_insert" ); |
---|
317 | node1.id = 1; |
---|
318 | node2.id = 2; |
---|
319 | rtems_chain_append( &chain1, &node1.Node ); |
---|
320 | rtems_chain_insert( &node1.Node, &node2.Node ); |
---|
321 | |
---|
322 | for ( p = rtems_chain_first(&chain1), id = 1 ; |
---|
323 | !rtems_chain_is_tail(&chain1, p) ; |
---|
324 | p = p->next , id++ ) { |
---|
325 | test_node *t = (test_node *)p; |
---|
326 | if ( id > 2 ) { |
---|
327 | puts( "INIT - TOO MANY NODES ON CHAIN" ); |
---|
328 | rtems_test_exit(0); |
---|
329 | } |
---|
330 | if ( t->id != id ) { |
---|
331 | puts( "INIT - ERROR ON CHAIN ID MISMATCH" ); |
---|
332 | rtems_test_exit(0); |
---|
333 | } |
---|
334 | } |
---|
335 | |
---|
336 | test_chain_first_and_last(); |
---|
337 | test_chain_with_empty_check(); |
---|
338 | test_chain_with_notification(); |
---|
339 | test_chain_get_with_wait(); |
---|
340 | test_chain_control_layout(); |
---|
341 | test_chain_control_initializer(); |
---|
342 | test_chain_node_count(); |
---|
343 | test_chain_insert_ordered(); |
---|
344 | |
---|
345 | TEST_END(); |
---|
346 | rtems_test_exit(0); |
---|
347 | } |
---|
348 | |
---|
349 | /* configuration information */ |
---|
350 | |
---|
351 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
352 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
353 | |
---|
354 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
355 | |
---|
356 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
357 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
358 | |
---|
359 | #define CONFIGURE_INIT |
---|
360 | #include <rtems/confdefs.h> |
---|
361 | |
---|
362 | /* global variables */ |
---|