chiark / gitweb /
url: Allow `;' to separate key/value pairs in URL-encoded strings.
[mLib] / da-test.c
CommitLineData
d74e2a35 1/* -*-c-*-
2 *
3 * Test driver for darray.
4 */
5
6#include <assert.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10
11#include "darray.h"
12#include "exc.h"
13
14DA_DECL(vec, int);
15
16#ifdef notdef
17
18static void dump(vec *v)
19{
20 printf("# len = %lu, sz = %lu, off = %lu; push = %u, unshift = %u\n",
21 (unsigned long)v->b.len,
22 (unsigned long)v->b.sz,
23 (unsigned long)v->b.off,
24 v->b.push, v->b.unshift);
25 printf("# {");
26 if (DA_LEN(v)) {
27 size_t i;
28 printf(" %i", DA(v)[0]);
29 for (i = 1; i < DA_LEN(v); i++)
30 printf(", %i", DA(v)[i]);
31 }
32 puts(" }");
33 assert(v->b.sz + v->b.off == 128);
34}
35
36#endif
37
38int main(void)
39{
40 char buf[1024];
41 char *p;
42 vec v = DA_INIT;
43
44/* setvbuf(stdout, 0, _IOLBF, BUFSIZ); */
45
46 while (fgets(buf, sizeof(buf), stdin)) {
47 buf[strlen(buf) - 1] = 0;
48/* printf("# %s\n", buf); */
49 p = strtok(buf, " ");
50
51 TRY {
52 if (strcmp(p, "push") == 0) {
53 int n = atoi(strtok(0, " "));
54 DA_PUSH(&v, n);
55 } else if (strcmp(p, "unshift") == 0) {
56 int n = atoi(strtok(0, " "));
57 DA_UNSHIFT(&v, n);
58 } else if (strcmp(p, "pop") == 0) {
59 printf("%i\n", DA_POP(&v));
60 } else if (strcmp(p, "shift") == 0) {
61 printf("%i\n", DA_SHIFT(&v));
62 } else if (strcmp(p, "insert") == 0 ||
63 strcmp(p, "append") == 0) {
64 vec vv;
65 char *q = p;
66 DA_CREATE(&vv);
67/* putchar('#'); */
68 while ((p = strtok(0, " ")) != 0) {
69 int n = atoi(p);
70 DA_PUSH(&vv, n);
71 }
72 if (strcmp(q, "insert") == 0) {
73 DA_SHUNT(&v, DA_LEN(&vv));
74 DA_SLIDE(&v, DA_LEN(&vv));
75 memcpy(DA(&v), DA(&vv), DA_LEN(&vv) * sizeof(int));
76 } else {
77 size_t l = DA_LEN(&v);
78 DA_ENSURE(&v, DA_LEN(&vv));
79 DA_EXTEND(&v, DA_LEN(&vv));
80 memcpy(DA(&v) + l, DA(&vv), DA_LEN(&vv) * sizeof(int));
81 }
82 DA_DESTROY(&vv);
83 } else if (strcmp(p, "delete") == 0) {
84 int n = atoi(strtok(0, " "));
85bb21f7 85 DA_UNSLIDE(&v, n);
d74e2a35 86 } else if (strcmp(p, "reduce") == 0) {
87 int n = atoi(strtok(0, " "));
85bb21f7 88 DA_SHRINK(&v, n);
d74e2a35 89 } else if (strcmp(p, "set") == 0) {
90 size_t i = atoi(strtok(0, " "));
91 int n = atoi(strtok(0, " "));
92 size_t l = DA_LEN(&v);
93 DA_INCLUDE(&v, i);
94 if (i >= l) {
95 size_t j;
96 for (j = l; j < i; j++)
97 DA(&v)[j] = -1;
98 }
99 DA(&v)[i] = n;
100 } else if (strcmp(p, "get") == 0) {
101 size_t i = atoi(strtok(0, " "));
102 if (i >= DA_LEN(&v))
103 puts("*RANGE*");
104 else
105 printf("%i\n", DA(&v)[i]);
706bf01d 106 } else if (strcmp(p, "first") == 0) {
107 printf("%i\n", DA_FIRST(&v));
108 } else if (strcmp(p, "last") == 0) {
109 printf("%i\n", DA_LAST(&v));
d74e2a35 110 } else if (strcmp(p, "show") == 0) {
111 if (DA_LEN(&v) == 0)
112 puts("*EMPTY*");
113 else {
114 size_t i;
115 printf("%i", DA(&v)[0]);
116 for (i = 1; i < DA_LEN(&v); i++)
117 printf(" %i", DA(&v)[i]);
118 putchar('\n');
119 }
120 } else
121 puts("*BAD*");
122 } CATCH switch (exc_type) {
123 case DAEXC_UFLOW:
124 puts("*UFLOW*");
125 break;
126 case DAEXC_OFLOW:
127 puts("*OFLOW*");
128 break;
129 case EXC_NOMEM:
130 puts("*NOMEM*");
131 break;
132 default:
133 puts("*UNKNOWN-EXCEPTION*");
134 break;
135 } END_TRY;
136/* dump(&v); */
137 }
138
139 DA_DESTROY(&v);
140 return (0);
141}