6 #include <tests/tap/basic.h>
12 struct opr_queue entry
;
20 struct charqueue
*entry
;
22 entry
=malloc(sizeof(struct charqueue
));
28 queueAsString(struct opr_queue
*head
) {
29 static char items
[255];
30 struct opr_queue
*cursor
;
33 for(opr_queue_Scan(head
, cursor
)) {
34 items
[pos
] = opr_queue_Entry(cursor
, struct charqueue
, entry
)->item
;
45 stringIntoQueue(char *string
, struct opr_queue
*q
) {
49 while (string
[i
]!='\0') {
50 opr_queue_Append(q
, &(newEntry(string
[i
])->entry
));
58 struct opr_queue q1
, q2
, q3
, q4
, *cursor
;
67 opr_queue_Append(&q1
, &(newEntry('A')->entry
));
68 opr_queue_Append(&q1
, &(newEntry('B')->entry
));
69 opr_queue_Append(&q1
, &(newEntry('C')->entry
));
70 is_string(queueAsString(&q1
), "ABC", "Append works as expected");
72 opr_queue_Prepend(&q1
, &(newEntry('D')->entry
));
73 opr_queue_Prepend(&q1
, &(newEntry('E')->entry
));
74 is_string(queueAsString(&q1
), "EDABC", "Prepend works");
76 opr_queue_Append(&q2
, &(newEntry('1')->entry
));
77 opr_queue_Append(&q2
, &(newEntry('2')->entry
));
79 opr_queue_SpliceAppend(&q1
, &q2
);
80 is_string(queueAsString(&q1
), "EDABC12", "SpliceAppend works");
81 ok(opr_queue_IsEmpty(&q2
),
82 "IsEmpty works (and splice empties queues)");
84 opr_queue_Append(&q2
, &(newEntry('8')->entry
));
85 opr_queue_Append(&q2
, &(newEntry('9')->entry
));
86 is_string(queueAsString(&q2
), "89", "Append works again");
88 opr_queue_SplicePrepend(&q1
, &q2
);
89 is_string(queueAsString(&q1
), "89EDABC12", "SplicePrepend works");
91 /* Now for some trickier stuff */
92 stringIntoQueue("XYZ", &q2
);
94 /* Find the A in the string above */
95 for (opr_queue_Scan(&q1
, cursor
)) {
96 if (opr_queue_Entry(cursor
, struct charqueue
, entry
)->item
== 'A')
100 opr_queue_InsertBefore(cursor
, &(newEntry('M')->entry
));
101 is_string("89EDMABC12", queueAsString(&q1
),
102 "InsertBefore functions correctly");
103 opr_queue_SplitBeforeAppend(&q1
, &q2
, cursor
);
104 is_string("ABC12", queueAsString(&q1
),
105 "SplitBefore leaves old queue in correct state");
106 is_string("XYZ89EDM", queueAsString(&q2
),
107 "SplitBefore correctly appends to new queue");
109 /* Find the 9 in q2 */
110 for (opr_queue_Scan(&q2
, cursor
)) {
111 if (opr_queue_Entry(cursor
, struct charqueue
, entry
)->item
== '9')
114 opr_queue_InsertAfter(cursor
, &(newEntry('N')->entry
));
115 is_string("XYZ89NEDM", queueAsString(&q2
), "InsertAfter");
117 opr_queue_SplitAfterPrepend(&q2
, &q1
, cursor
);
118 is_string("NEDMABC12", queueAsString(&q1
), "SplitAfterPrepend Q1");
119 is_string("XYZ89", queueAsString(&q2
), "SplitAfterPrepend Q2");
122 opr_queue_Swap(&q3
, &q4
);
123 ok(opr_queue_IsEmpty(&q3
) && opr_queue_IsEmpty(&q4
), "Swap empty queues");
125 opr_queue_Append(&q3
, &(newEntry('A')->entry
));
126 opr_queue_Append(&q3
, &(newEntry('B')->entry
));
127 opr_queue_Append(&q3
, &(newEntry('C')->entry
));
128 opr_queue_Swap(&q3
, &q4
);
129 ok(opr_queue_IsEmpty(&q3
), "Swap with one empty queue Q3");
130 is_string("ABC", queueAsString(&q4
), "Swap with one empty queue Q4");
132 opr_queue_Append(&q3
, &(newEntry('1')->entry
));
133 opr_queue_Append(&q3
, &(newEntry('2')->entry
));
134 opr_queue_Append(&q3
, &(newEntry('3')->entry
));
135 opr_queue_Swap(&q3
, &q4
);
136 is_string("ABC", queueAsString(&q3
), "Swap Q3");
137 is_string("123", queueAsString(&q4
), "Swap Q4");
139 /* IsEnd and IsLast handling */
140 ok(opr_queue_IsLast(&q1
, &(opr_queue_Last(&q1
, struct charqueue
, entry
)->entry
)),
141 "IsLast is true for last element of a list");
142 ok(opr_queue_IsEnd(&q1
,
143 opr_queue_Last(&q1
, struct charqueue
, entry
)->entry
.next
),
144 "IsEnd is true for entry after last element");
145 ok(opr_queue_IsEnd(&q1
, &q1
), "IsEnd is true for queue head");