1
0
Fork 0
reconque/demo.c

105 lines
1.8 KiB
C
Raw Normal View History

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