git-instaweb: Extract configuring web server into configure_httpd
[git] / test-obj-pool.c
1 /*
2  * test-obj-pool.c: code to exercise the svn importer's object pool
3  */
4
5 #include "cache.h"
6 #include "vcs-svn/obj_pool.h"
7
8 enum pool { POOL_ONE, POOL_TWO };
9 obj_pool_gen(one, int, 1)
10 obj_pool_gen(two, int, 4096)
11
12 static uint32_t strtouint32(const char *s)
13 {
14         char *end;
15         uintmax_t n = strtoumax(s, &end, 10);
16         if (*s == '\0' || (*end != '\n' && *end != '\0'))
17                 die("invalid offset: %s", s);
18         return (uint32_t) n;
19 }
20
21 static void handle_command(const char *command, enum pool pool, const char *arg)
22 {
23         switch (*command) {
24         case 'a':
25                 if (!prefixcmp(command, "alloc ")) {
26                         uint32_t n = strtouint32(arg);
27                         printf("%"PRIu32"\n",
28                                 pool == POOL_ONE ?
29                                 one_alloc(n) : two_alloc(n));
30                         return;
31                 }
32         case 'c':
33                 if (!prefixcmp(command, "commit ")) {
34                         pool == POOL_ONE ? one_commit() : two_commit();
35                         return;
36                 }
37                 if (!prefixcmp(command, "committed ")) {
38                         printf("%"PRIu32"\n",
39                                 pool == POOL_ONE ?
40                                 one_pool.committed : two_pool.committed);
41                         return;
42                 }
43         case 'f':
44                 if (!prefixcmp(command, "free ")) {
45                         uint32_t n = strtouint32(arg);
46                         pool == POOL_ONE ? one_free(n) : two_free(n);
47                         return;
48                 }
49         case 'n':
50                 if (!prefixcmp(command, "null ")) {
51                         printf("%"PRIu32"\n",
52                                 pool == POOL_ONE ?
53                                 one_offset(NULL) : two_offset(NULL));
54                         return;
55                 }
56         case 'o':
57                 if (!prefixcmp(command, "offset ")) {
58                         uint32_t n = strtouint32(arg);
59                         printf("%"PRIu32"\n",
60                                 pool == POOL_ONE ?
61                                 one_offset(one_pointer(n)) :
62                                 two_offset(two_pointer(n)));
63                         return;
64                 }
65         case 'r':
66                 if (!prefixcmp(command, "reset ")) {
67                         pool == POOL_ONE ? one_reset() : two_reset();
68                         return;
69                 }
70         case 's':
71                 if (!prefixcmp(command, "set ")) {
72                         uint32_t n = strtouint32(arg);
73                         if (pool == POOL_ONE)
74                                 *one_pointer(n) = 1;
75                         else
76                                 *two_pointer(n) = 1;
77                         return;
78                 }
79         case 't':
80                 if (!prefixcmp(command, "test ")) {
81                         uint32_t n = strtouint32(arg);
82                         printf("%d\n", pool == POOL_ONE ?
83                                 *one_pointer(n) : *two_pointer(n));
84                         return;
85                 }
86         default:
87                 die("unrecognized command: %s", command);
88         }
89 }
90
91 static void handle_line(const char *line)
92 {
93         const char *arg = strchr(line, ' ');
94         enum pool pool;
95
96         if (arg && !prefixcmp(arg + 1, "one"))
97                 pool = POOL_ONE;
98         else if (arg && !prefixcmp(arg + 1, "two"))
99                 pool = POOL_TWO;
100         else
101                 die("no pool specified: %s", line);
102
103         handle_command(line, pool, arg + strlen("one "));
104 }
105
106 int main(int argc, char *argv[])
107 {
108         struct strbuf sb = STRBUF_INIT;
109         if (argc != 1)
110                 usage("test-obj-str < script");
111
112         while (strbuf_getline(&sb, stdin, '\n') != EOF)
113                 handle_line(sb.buf);
114         strbuf_release(&sb);
115         return 0;
116 }