source: rtems/testsuites/sptests/spchain/init.c @ ec978d9

4.11
Last change on this file since ec978d9 was ec978d9, checked in by Sebastian Huber <sebastian.huber@…>, on Jun 12, 2013 at 9:29:12 AM

score: Add _Chain_Insert_ordered_unprotected()

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