File: stailq.c

package info (click to toggle)
tarantool 1.7.2.385.g952d79e-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 21,556 kB
  • ctags: 28,405
  • sloc: ansic: 180,313; cpp: 26,044; sh: 15,513; python: 4,893; makefile: 1,412
file content (77 lines) | stat: -rw-r--r-- 1,679 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "salad/stailq.h"
#include <stdio.h>
#include <stdarg.h>
#include "unit.h"

#define PLAN		37

#define ITEMS		7

struct test {
	char ch;
	int  no;
	struct stailq_entry next;
};

static struct test items[ITEMS];

static struct stailq head;

int
main(void)
{
	int i;
	struct test *it;
	struct stailq_entry *entry;

	stailq_create(&head);

	plan(PLAN);
	ok(stailq_empty(&head), "list is empty");
	stailq_reverse(&head);
	ok(stailq_empty(&head), "list is empty after reverse");
	for (i = 0; i < ITEMS; i++) {
		items[i].no = i;
		stailq_add_tail(&head, &(items[i].next));
	}
	is(stailq_first(&head), &items[0].next, "first item");
	is(stailq_last(&head), &items[6].next, "last item");
	i = 0;
	stailq_foreach(entry, &head) {
		is(entry, &items[i].next, "element (foreach) %d", i);
		i++;
	}
	isnt(stailq_first(&head), &items[ITEMS - 1].next, "first item");

	ok(!stailq_empty(&head), "head is not empty");

	is(stailq_first_entry(&head, struct test, next),
	   &items[0], "first entry");
	for (i = 0; i < ITEMS; i++)
		is(stailq_shift(&head), &items[i].next, "shift item %d", i);
	ok(stailq_empty(&head), "list is empty after shift");

	stailq_create(&head);
	ok(stailq_empty(&head), "next is empty");
	for (i = 0; i < ITEMS; i++) {
		items[i].no = i;
		stailq_add_entry(&head, &items[i], next);
	}
	stailq_foreach_entry(it, &head, next) {
		i--;
		is(it, items + i, "element (foreach_entry) %d", i);
	}

	stailq_create(&head);
	for (i = 0; i < ITEMS; i++) {
		items[i].no = ITEMS - i;
		stailq_add_tail_entry(&head, &items[i], next);
	}
	i = 0;
	stailq_foreach_entry(it, &head, next) {
		is(it, items + i, "element (foreach_entry) %d", i);
		i++;
	}
	return check_plan();
}