105 lines
1.8 KiB
C
105 lines
1.8 KiB
C
/* SPDX-License-Identifier: 0BSD */
|
|
|
|
#include <assert.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "reconque.h"
|
|
|
|
#define FAKE(x) ((void *)(x))
|
|
|
|
/* Basic FIFO behavior and full/empty conditions. */
|
|
void
|
|
basic(void)
|
|
{
|
|
struct reconque *q = rcq_alloc(5);
|
|
assert(q);
|
|
|
|
for (size_t i = 0; i < 5; i++) {
|
|
assert(rcq_push(q, FAKE(i + 1)) == 0);
|
|
}
|
|
assert(rcq_push(q, FAKE(1000)) < 0); /* full */
|
|
|
|
for (size_t i = 0; i < 5; i++) {
|
|
assert(rcq_pop(q) == FAKE(i + 1));
|
|
}
|
|
assert(rcq_pop(q) == NULL); /* empty */
|
|
|
|
rcq_free(q);
|
|
}
|
|
|
|
/* Replace `q1` with bigger `q2` and show that `rcq_pop` works. */
|
|
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);
|
|
}
|
|
|
|
/* Just showing off at this point... */
|
|
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();
|
|
grow();
|
|
triple();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
}
|