Blame src/functional/search_insque.c

Szabolcs Nagy 132e38
#include <stdlib.h>
Szabolcs Nagy 132e38
#include <search.h>
Szabolcs Nagy 132e38
#include "test.h"
Szabolcs Nagy 132e38
Szabolcs Nagy 132e38
struct q {
Szabolcs Nagy 132e38
	struct q *n;
Szabolcs Nagy 132e38
	struct q *p;
Szabolcs Nagy 132e38
	int i;
Szabolcs Nagy 132e38
};
Szabolcs Nagy 132e38
Szabolcs Nagy 132e38
static struct q *new(int i)
Szabolcs Nagy 132e38
{
Szabolcs Nagy 132e38
	struct q *q = malloc(sizeof *q);
Szabolcs Nagy 132e38
	q->i = i;
Szabolcs Nagy 132e38
	return q;
Szabolcs Nagy 132e38
}
Szabolcs Nagy 132e38
Szabolcs Nagy 132e38
int main()
Szabolcs Nagy 132e38
{
Szabolcs Nagy 132e38
	struct q *q = new(0);
Szabolcs Nagy 132e38
	struct q *p;
Szabolcs Nagy 132e38
	int i;
Szabolcs Nagy 132e38
Szabolcs Nagy 132e38
	insque(q, 0);
Szabolcs Nagy 132e38
	for (i = 1; i < 10; i++) {
Szabolcs Nagy 132e38
		insque(new(i), q);
Szabolcs Nagy 132e38
		q = q->n;
Szabolcs Nagy 132e38
	}
Szabolcs Nagy 132e38
	p = q;
Szabolcs Nagy 132e38
	while (q) {
Szabolcs Nagy 132e38
		if (q->i != --i)
Szabolcs Nagy 132e38
			t_error("walking queue: got %d, wanted %d\n", q->i, i);
Szabolcs Nagy 132e38
		q = q->p;
Szabolcs Nagy 132e38
	}
Szabolcs Nagy 132e38
	remque(p->p);
Szabolcs Nagy 132e38
	if (p->p->i != p->i-2)
Szabolcs Nagy 132e38
		t_error("remque: got %d, wanted %d\n", p->p->i, p->i-2);
Szabolcs Nagy 132e38
	if (p->p->n->i != p->i)
Szabolcs Nagy 132e38
		t_error("remque: got %d, wanted %d\n", p->p->n->i, p->i);
Szabolcs Nagy 132e38
	return t_status;
Szabolcs Nagy 132e38
}