OpenBSD: Complete implementation of afs_osi_TimedSleep
[openafs.git] / tests / util / queues-t.c
1 #include <afsconfig.h>
2 #include <afs/param.h>
3
4 #include <stdlib.h>
5
6 #include <tap/basic.h>
7
8 #include <opr/queue.h>
9
10
11 struct charqueue {
12     struct opr_queue entry;
13     char item;
14 };
15
16
17 struct charqueue *
18 newEntry(char string)
19 {
20     struct charqueue *entry;
21
22     entry=malloc(sizeof(struct charqueue));
23     entry->item=string;
24     return(entry);
25 }
26
27 char *
28 queueAsString(struct opr_queue *head) {
29     static char items[255];
30     struct opr_queue *cursor;
31     int pos = 0;
32
33     for(opr_queue_Scan(head, cursor)) {
34         items[pos] = opr_queue_Entry(cursor, struct charqueue, entry)->item;
35         pos++;
36         if (pos==254)
37             break;
38     }
39     items[pos]='\0';
40
41     return items;
42 }
43
44 void
45 stringIntoQueue(char *string, struct opr_queue *q) {
46     int i;
47
48     i = 0;
49     while (string[i]!='\0') {
50         opr_queue_Append(q, &(newEntry(string[i])->entry));
51         i++;
52     }
53 }
54
55 int
56 main(void)
57 {
58     struct opr_queue q1, q2, *cursor;
59
60     plan(12);
61
62     opr_queue_Init(&q1);
63     opr_queue_Init(&q2);
64
65     opr_queue_Append(&q1, &(newEntry('A')->entry));
66     opr_queue_Append(&q1, &(newEntry('B')->entry));
67     opr_queue_Append(&q1, &(newEntry('C')->entry));
68     is_string(queueAsString(&q1), "ABC", "Append works as expected");
69
70     opr_queue_Prepend(&q1, &(newEntry('D')->entry));
71     opr_queue_Prepend(&q1, &(newEntry('E')->entry));
72     is_string(queueAsString(&q1), "EDABC", "Prepend works");
73
74     opr_queue_Append(&q2, &(newEntry('1')->entry));
75     opr_queue_Append(&q2, &(newEntry('2')->entry));
76
77     opr_queue_SpliceAppend(&q1, &q2);
78     is_string(queueAsString(&q1), "EDABC12", "SpliceAppend works");
79     ok(opr_queue_IsEmpty(&q2),
80            "IsEmpty works (and splice empties queues)");
81
82     opr_queue_Append(&q2, &(newEntry('8')->entry));
83     opr_queue_Append(&q2, &(newEntry('9')->entry));
84     is_string(queueAsString(&q2), "89", "Append works again");
85
86     opr_queue_SplicePrepend(&q1, &q2);
87     is_string(queueAsString(&q1), "89EDABC12", "SplicePrepend works");
88
89     /* Now for some trickier stuff */
90     stringIntoQueue("XYZ", &q2);
91
92     /* Find the A in the string above */
93     for (opr_queue_Scan(&q1, cursor)) {
94         if (opr_queue_Entry(cursor, struct charqueue, entry)->item == 'A')
95             break;
96     }
97
98     opr_queue_InsertBefore(cursor, &(newEntry('M')->entry));
99     is_string("89EDMABC12", queueAsString(&q1),
100               "InsertBefore functions correctly");
101     opr_queue_SplitBeforeAppend(&q1, &q2, cursor);
102     is_string("ABC12", queueAsString(&q1),
103               "SplitBefore leaves old queue in correct state");
104     is_string("XYZ89EDM", queueAsString(&q2),
105               "SplitBefore correctly appends to new queue");
106
107     /* Find the 9 in q2 */
108     for (opr_queue_Scan(&q2, cursor)) {
109         if (opr_queue_Entry(cursor, struct charqueue, entry)->item == '9')
110             break;
111     }
112     opr_queue_InsertAfter(cursor, &(newEntry('N')->entry));
113     is_string("XYZ89NEDM", queueAsString(&q2), "InsertAfter");
114
115     opr_queue_SplitAfterPrepend(&q2, &q1, cursor);
116     is_string("NEDMABC12", queueAsString(&q1), "SplitAfterPrepend Q1");
117     is_string("XYZ89", queueAsString(&q2), "SplitAfterPrepend Q2");
118
119     return 0;
120 }
121
122