Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / rx / test / testqueue.c
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
4 *
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
8 */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13
14 #include <rx/rx_queue.h>
15
16 struct myq {
17 struct rx_queue queue_header;
18 int value;
19 };
20
21 void
22 qprint(char *s, struct myq *qe)
23 {
24 printf("%s/%x: next:%x, prev:%x, value=%d\n", s, qe, queue_Next(qe, myq),
25 queue_Prev(qe, myq), qe->value);
26 }
27
28 void
29 qremove(char *s, struct myq *q)
30 {
31 struct myq *qe, *nqe;
32 printf("*head* ");
33 qprint(s, q);
34 for (queue_Scan(q, qe, nqe, myq)) {
35 if (qe->value <= 10)
36 queue_Remove(qe);
37 else
38 qprint(s, qe);
39 }
40 }
41
42 /* Separate test for the splice macros */
43 struct rx_queue *
44 createQueue(int n)
45 {
46 int i;
47 struct rx_queue *q;
48 struct myq *qe;
49 q = malloc(sizeof(struct rx_queue));
50 queue_Init(q);
51 for (i = 0; i < 3; i++) {
52 qe = malloc(sizeof(struct myq));
53 qe->value = n * 1000 + i;
54 queue_Append(q, qe);
55 }
56 return q;
57 }
58
59 void
60 testSplice(void)
61 {
62 struct rx_queue *q[10];
63 struct myq *qe, *nqe;
64 int i;
65 for (i = 0; i < 10; i++)
66 q[i] = createQueue(i);
67 for (i = 0; i < 9; i++) {
68 if (i & 1)
69 queue_SplicePrepend(q[0], q[i + 1]);
70 else
71 queue_SpliceAppend(q[0], q[i + 1]);
72 }
73 /* Move the queue to the middle (splice non-empty onto empty) */
74 queue_SpliceAppend(q[7], q[0]);
75 queue_SplicePrepend(q[6], q[0]);
76 /* Splice some empty&non-empty queues onto empty&non-empty queues */
77 for (i = 0; i < 9; i++)
78 queue_SpliceAppend(q[i], q[i + 1]);
79 for (i = 0; i < 9; i++)
80 queue_SplicePrepend(q[i], q[i + 1]);
81 printf("All queues except 5 should be empty\n");
82 for (i = 0; i < 10; i++) {
83 printf("Forwards, i=%d:", i);
84 for (queue_Scan(q[i], qe, nqe, myq))
85 printf(" %d", qe->value);
86 printf("\n");
87 }
88 for (i = 0; i < 10; i++) {
89 printf("Backwards, i=%d:", i);
90 for (queue_ScanBackwards(q[i], qe, nqe, myq))
91 printf(" %d", qe->value);
92 printf("\n");
93 }
94 }
95
96 void
97 testAppend(void)
98 {
99 int i;
100 struct myq x;
101 struct myq xa[20];
102 struct myq y;
103 struct myq ya[20];
104
105 queue_Init(&x);
106 x.value = 100001;
107 for (i = 0; i < 20; i++)
108 queue_Prepend(&x, &xa[i]), xa[i].value = i + 1;
109 queue_Init(&y);
110 y.value = 100002;
111 for (i = 0; i < 20; i++)
112 queue_Append(&y, &ya[i]), ya[i].value = i + 1;
113 qremove("x, first pass", &x);
114 qremove("x, later", &x);
115 qremove("y, first pass", &y);
116 qremove("y, later", &y);
117 }
118
119 int
120 main(int argc, char **argv)
121 {
122
123 if (argc > 1) {
124 testSplice();
125 } else {
126 testAppend();
127 }
128 exit(0);
129 }