1
0
reconque/demo.c

102 lines
1.7 KiB
C

/* SPDX-License-Identifier: 0BSD */
#include <assert.h>
#include <stdlib.h>
#include "reconque.h"
#define FAKE(x) ((void *)(x))
void
basic(void)
{
struct reconque *q1 = rcq_alloc(5);
assert(q1);
for (size_t i = 0; i < 5; i++) {
assert(rcq_push(q1, FAKE(i + 1)) == 0);
}
assert(rcq_push(q1, FAKE(1000)) < 0);
for (size_t i = 0; i < 5; i++) {
assert(rcq_pop(q1) == FAKE(i + 1));
}
assert(rcq_pop(q1) == NULL);
rcq_free(q1);
}
void
grow(void)
{
struct reconque *q1 = rcq_alloc(5);
assert(q1);
for (size_t i = 0; i < 3; i++) {
assert(rcq_push(q1, FAKE(i + 1)) == 0);
}
struct reconque *q2 = rcq_recon(10, q1);
assert(q2);
q1 = NULL; /* q2 now "owns" q1 so we should drop it */
for (size_t i = 0; i < 8; i++) {
assert(rcq_push(q2, FAKE(i + 3 + 1)) == 0);
}
for (size_t i = 0; i < 11; i++) {
assert(rcq_pop(q2) == FAKE(i + 1));
}
assert(rcq_pop(q2) == NULL);
/*
* Admittedly it's a little weird that we can `rcq_pop` more often than
* the capacity of `q2` would indicate, but those are the breaks.
*/
rcq_free(q2);
}
void
triple(void)
{
struct reconque *q1 = rcq_alloc(7);
assert(q1);
assert(rcq_push(q1, FAKE(1)) == 0);
struct reconque *q2 = rcq_recon(10, q1);
assert(q2);
q1 = NULL;
assert(rcq_push(q2, FAKE(2)) == 0);
struct reconque *q3 = rcq_recon(3, q2);
assert(q3);
q2 = NULL;
for (size_t i = 0; i < 3; i++) {
assert(rcq_push(q3, FAKE(i + 3)) == 0);
}
for (size_t i = 0; i < 5; i++) {
assert(rcq_pop(q3) == FAKE(i + 1));
}
assert(rcq_pop(q3) == NULL);
rcq_free(q3);
}
int
main(void)
{
basic(); /* just basic FIFO and full/empty */
grow(); /* replace with bigger queue */
triple(); /* just showing off at this point */
exit(EXIT_SUCCESS);
}