summaryrefslogtreecommitdiff
path: root/test-obj-pool.c
blob: 5018863ef520426669082e86948e378b93fa161a (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
/*
 * test-obj-pool.c: code to exercise the svn importer's object pool
 */

#include "cache.h"
#include "vcs-svn/obj_pool.h"

enum pool { POOL_ONE, POOL_TWO };
obj_pool_gen(one, int, 1)
obj_pool_gen(two, int, 4096)

static uint32_t strtouint32(const char *s)
{
	char *end;
	uintmax_t n = strtoumax(s, &end, 10);
	if (*s == '\0' || (*end != '\n' && *end != '\0'))
		die("invalid offset: %s", s);
	return (uint32_t) n;
}

static void handle_command(const char *command, enum pool pool, const char *arg)
{
	switch (*command) {
	case 'a':
		if (!prefixcmp(command, "alloc ")) {
			uint32_t n = strtouint32(arg);
			printf("%"PRIu32"\n",
				pool == POOL_ONE ?
				one_alloc(n) : two_alloc(n));
			return;
		}
	case 'c':
		if (!prefixcmp(command, "commit ")) {
			pool == POOL_ONE ? one_commit() : two_commit();
			return;
		}
		if (!prefixcmp(command, "committed ")) {
			printf("%"PRIu32"\n",
				pool == POOL_ONE ?
				one_pool.committed : two_pool.committed);
			return;
		}
	case 'f':
		if (!prefixcmp(command, "free ")) {
			uint32_t n = strtouint32(arg);
			pool == POOL_ONE ? one_free(n) : two_free(n);
			return;
		}
	case 'n':
		if (!prefixcmp(command, "null ")) {
			printf("%"PRIu32"\n",
				pool == POOL_ONE ?
				one_offset(NULL) : two_offset(NULL));
			return;
		}
	case 'o':
		if (!prefixcmp(command, "offset ")) {
			uint32_t n = strtouint32(arg);
			printf("%"PRIu32"\n",
				pool == POOL_ONE ?
				one_offset(one_pointer(n)) :
				two_offset(two_pointer(n)));
			return;
		}
	case 'r':
		if (!prefixcmp(command, "reset ")) {
			pool == POOL_ONE ? one_reset() : two_reset();
			return;
		}
	case 's':
		if (!prefixcmp(command, "set ")) {
			uint32_t n = strtouint32(arg);
			if (pool == POOL_ONE)
				*one_pointer(n) = 1;
			else
				*two_pointer(n) = 1;
			return;
		}
	case 't':
		if (!prefixcmp(command, "test ")) {
			uint32_t n = strtouint32(arg);
			printf("%d\n", pool == POOL_ONE ?
				*one_pointer(n) : *two_pointer(n));
			return;
		}
	default:
		die("unrecognized command: %s", command);
	}
}

static void handle_line(const char *line)
{
	const char *arg = strchr(line, ' ');
	enum pool pool;

	if (arg && !prefixcmp(arg + 1, "one"))
		pool = POOL_ONE;
	else if (arg && !prefixcmp(arg + 1, "two"))
		pool = POOL_TWO;
	else
		die("no pool specified: %s", line);

	handle_command(line, pool, arg + strlen("one "));
}

int main(int argc, char *argv[])
{
	struct strbuf sb = STRBUF_INIT;
	if (argc != 1)
		usage("test-obj-str < script");

	while (strbuf_getline(&sb, stdin, '\n') != EOF)
		handle_line(sb.buf);
	strbuf_release(&sb);
	return 0;
}