summaryrefslogtreecommitdiff
path: root/ccan/strmap/test/run-prefix.c
blob: 9c5156e6e57dd8c7ba09d85a1ca43138341497b8 (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
#include <ccan/strmap/strmap.h>
#include <ccan/strmap/strmap.c>
#include <ccan/tap/tap.h>
#include <stdio.h>

/* Must be > 100, see below. */
#define NUM 200

static bool in_order(const char *index, char *value, unsigned int *count)
{
	int i = atoi(index);
	ok1(i == atoi(value));
	ok1(*count == i);
	(*count)++;
	return true;
}

static bool find_empty(const char *index, char *value, char *empty)
{
	if (index == empty)
		pass("Found empty entry!");
	return true;
}

int main(void)
{
	struct map {
		STRMAP_MEMBERS(char *);
	};
	struct map map;
	const struct map *sub;
	unsigned int i;
	char *str[NUM], *empty;

	plan_tests(8 + 2 * (1 + 10 + 100) + 1);
	strmap_init(&map);

	for (i = 0; i < NUM; i++) {
		char template[10];
		sprintf(template, "%08u", i);
		str[i] = strdup(template);
	}

	/* All prefixes of an empty map are empty. */
	sub = strmap_prefix(&map, "a");
	ok1(strmap_empty(sub));
	sub = strmap_prefix(&map, "");
	ok1(strmap_empty(sub));

	for (i = 0; i < NUM; i++)
		strmap_add(&map, str[i], str[i]+1);

	/* Nothing */
	sub = strmap_prefix(&map, "a");
	ok1(strmap_empty(sub));

	/* Everything */
	sub = strmap_prefix(&map, "0");
	ok1(sub->raw.u.n == map.raw.u.n);
	sub = strmap_prefix(&map, "");
	ok1(sub->raw.u.n == map.raw.u.n);

	/* Single. */
	sub = strmap_prefix(&map, "00000000");
	i = 0;
	strmap_iterate(sub, in_order, &i);
	ok1(i == 1);

	/* First 10. */
	sub = strmap_prefix(&map, "0000000");
	i = 0;
	strmap_iterate(sub, in_order, &i);
	ok1(i == 10);

	/* First 100. */
	sub = strmap_prefix(&map, "000000");
	i = 0;
	strmap_iterate(sub, in_order, &i);
	ok1(i == 100);

	/* Everything, *plus* empty string. */
	empty = strdup("");
	strmap_add(&map, empty, empty);

	sub = strmap_prefix(&map, "");
	/* Check we get *our* empty string back! */
	strmap_iterate(sub, find_empty, empty);

	strmap_clear(&map);

	for (i = 0; i < NUM; i++)
		free(str[i]);
	free(empty);

	/* This exits depending on whether all tests passed */
	return exit_status();
}