source: rtems/testsuites/sptests/spchain/init.c @ 23de794d

4.11
Last change on this file since 23de794d was 23de794d, checked in by Sebastian Huber <sebastian.huber@…>, on Aug 26, 2013 at 12:27:52 PM

score: Add and use CHAIN_INITIALIZER_ONE_NODE().

Add and use CHAIN_NODE_INITIALIZER_ONE_NODE_CHAIN(),
RTEMS_CHAIN_INITIALIZER_ONE_NODE() and
RTEMS_CHAIN_NODE_INITIALIZER_ONE_NODE_CHAIN().

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