File: test-fanout.c

package info (click to toggle)
nagios4 4.4.6-4.1
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 22,336 kB
  • sloc: ansic: 97,631; sh: 12,619; javascript: 5,483; perl: 2,624; makefile: 1,265; php: 381; ruby: 95
file content (106 lines) | stat: -rw-r--r-- 2,435 bytes parent folder | download | duplicates (3)
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
#define _GNU_SOURCE 1
#include <stdio.h>
#include "fanout.c"
#include "t-utils.h"

struct tcase {
	unsigned long key;
	unsigned long value;
};

static int destroyed;

static void destructor(void *ptr)
{
	destroyed++;
}

static void run_tests(int ntests, int fo_size)
{
	struct tcase *tc;
	unsigned long last_ptr, *ptr;
	int i, added = 0, removed = 0;
	fanout_table *fo;

	last_ptr = ntests;

	fo = fanout_create(fo_size);
	tc = calloc(ntests, sizeof(*tc));
	for (i = 0; i < ntests; i++) {
		tc[i].value = i;
		if (!fanout_add(fo, tc[i].key, &tc[i].value))
			added++;
	}
	ok_int(added, ntests, "Adding stuff must work");

	while ((ptr = (unsigned long *)fanout_remove(fo, 0))) {
		ok_int((int)*ptr, (int)last_ptr - 1, "Removing a bunch of them");
		removed++;
		last_ptr = *ptr;
	}
	ok_int(added, removed, "Removing should work as well as adding");
	fanout_destroy(fo, destructor);
	ok_int(destroyed, 0, "Expected no entries in destructor");

	fo = fanout_create(fo_size);
	for (i = 0; i < ntests; i++) {
		tc[i].value = i;
		if (!fanout_add(fo, tc[i].key, &tc[i].value))
			added++;
	}
	fanout_destroy(fo, destructor);
	ok_int(destroyed, ntests, "Expected ntest entries in destructor");
	destroyed = 0;
	free(tc);
}

struct test_data {
	unsigned long key;
	char *name;
};

static fanout_table *fot;
static void pdest(void *arg_)
{
	struct test_data *td = (struct test_data *)arg_;
	fanout_remove(fot, td->key);
	free(td->name);
	free(td);
	destroyed++;
}

int main(int argc, char **argv)
{
	unsigned long k;
	t_set_colors(0);
	t_start("fanout tests");
	struct test_data *td;

	run_tests(10, 64);
	run_tests(512, 64);
	run_tests(64, 64);
	run_tests(511, 17);

	destroyed = 0;
	fot = fanout_create(512);
	ok_int(fanout_remove(fot, 12398) == NULL, 1,
		"remove on empty table must yield NULL");
	ok_int(fanout_get(fot, 123887987) == NULL, 1,
		"get on empty table must yield NULL");
	for (k = 0; k < 16385; k++) {
		struct test_data *tdata = calloc(1, sizeof(*td));
		tdata->key = k;
		asprintf(&tdata->name, "%lu", k);
		fanout_add(fot, k, tdata);
	}
	td = fanout_get(fot, k - 1);
	ok_int(td != NULL, 1, "get must get what add inserts");
	ok_int(fanout_remove(fot, k + 1) == NULL, 1,
		"remove on non-inserted key must yield NULL");
	ok_int(fanout_get(fot, k + 1) == NULL, 1,
		"get on non-inserted must yield NULL");
	fanout_destroy(fot, pdest);
	ok_int((int)destroyed, (int)k, "destroy counter while free()'ing");

	return t_end();
}