Welcome to mirror list, hosted at ThFree Co, Russian Federation.

test-list.c « tests - git.openwrt.org/project/libubox.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ea2f3cd63c200a2ceecfe3638bfb3f0db7716ae8 (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "list.h"
#include "utils.h"

struct item {
	const char *name;
	struct list_head list;
};

#define OUT(fmt, ...) do { \
	fprintf(stdout, "%s: " fmt, __func__, ## __VA_ARGS__); \
} while (0);

static void init_list(struct list_head *list)
{
	const char *vals[] = {
		"zero", "one", "two", "three", "four", "five", "six",
		"seven", "eight", "nine", "ten", "eleven", "twelve"
	};

	OUT("list_empty: %s\n", list_empty(list) ? "yes" : "no");
	OUT("list_add_tail: ");
	for (size_t i=0; i<ARRAY_SIZE(vals); i++) {
		struct item *e = malloc(sizeof(struct item));
		e->name = vals[i];
		list_add_tail(&e->list, list);
		fprintf(stdout, "%s ", vals[i]);
	}
	fprintf(stdout, "\n");
	OUT("list_empty: %s\n", list_empty(list) ? "yes" : "no");
}

static void test_basics()
{
	struct item *tmp;
	struct item *item;
	struct item *last;
	struct item *first;
	struct list_head test_list = LIST_HEAD_INIT(test_list);

	init_list(&test_list);

	first = list_first_entry(&test_list, struct item, list);
	last = list_last_entry(&test_list, struct item, list);
	OUT("first=%s last=%s\n", first->name, last->name);
	OUT("'zero' is first, %s\n", list_is_first(&first->list, &test_list) ? "yes" : "no");
	OUT("'twelve' is last, %s\n", list_is_last(&last->list, &test_list) ? "yes" : "no");

	OUT("removing 'twelve' and 'zero'\n");
	list_del(&first->list);
	list_del(&last->list);
	free(first);
	free(last);

	first = list_first_entry(&test_list, struct item, list);
	last = list_last_entry(&test_list, struct item, list);

	if (!first || !last)
		return;

	OUT("first=%s last=%s\n", first->name, last->name);
	OUT("'one' is first, %s\n", list_is_first(&first->list, &test_list) ? "yes" : "no");
	OUT("'eleven' is last, %s\n", list_is_last(&last->list, &test_list) ? "yes" : "no");

	OUT("moving 'one' to the tail\n");
	list_move_tail(&first->list, &test_list);
	first = list_first_entry(&test_list, struct item, list);
	last = list_last_entry(&test_list, struct item, list);
	OUT("first=%s last=%s\n", first->name, last->name);
	OUT("'two' is first, %s\n", list_is_first(&first->list, &test_list) ? "yes" : "no");
	OUT("'one' is last, %s\n", list_is_last(&last->list, &test_list) ? "yes" : "no");

	OUT("list_for_each_entry: ");
	list_for_each_entry(item, &test_list, list) {
		fprintf(stdout, "%s ", item->name);
	}
	fprintf(stdout, "\n");

	OUT("list_for_each_entry_reverse: ");
	list_for_each_entry_reverse(item, &test_list, list) {
		fprintf(stdout, "%s ", item->name);
	}
	fprintf(stdout, "\n");

	OUT("delete all entries\n");
	list_for_each_entry_safe(item, tmp, &test_list, list) {
		list_del(&item->list);
		free(item);
	}
	OUT("list_empty: %s\n", list_empty(&test_list) ? "yes" : "no");
}

static void test_while_list_empty()
{
	struct item *first;
	struct list_head test_list = LIST_HEAD_INIT(test_list);

	init_list(&test_list);

	OUT("delete all entries\n");
	while (!list_empty(&test_list)) {
		first = list_first_entry(&test_list, struct item, list);
		list_del(&first->list);
		free(first);
	}
	OUT("list_empty: %s\n", list_empty(&test_list) ? "yes" : "no");
}

int main()
{
	test_basics();
	test_while_list_empty();
	return 0;
}