test-l2g.c 5.33 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
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
/*
 * Testbench for the Local_to_global structure.
 * Copyright (C) 1996, 1998-1999  Lysator Academic Computer Association.
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 1, or (at your option) 
 * any later version.
 * 
 * LysKOM is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */

26
27
#include <config.h>

Per Cederqvist's avatar
Per Cederqvist committed
28
29
30
31
32
33
34
35
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
#include <ctype.h>

#include "s-string.h"
Per Cederqvist's avatar
Per Cederqvist committed
36
#include "kom-types.h"
37
#include "../local-to-global.h"
38
39
40
41
#ifdef TRACED_ALLOCATIONS
#  include "../trace-alloc.h"
#endif

Per Cederqvist's avatar
Per Cederqvist committed
42

43
44
45
#define MAXMAPS 10
#define LINSIZE 1024
#define ARGS 3
Per Cederqvist's avatar
Per Cederqvist committed
46
47
48
49
50

/*
  D m	  destruct
  I m     init
  C m	  clear
51
  C d s   copy
Per Cederqvist's avatar
Per Cederqvist committed
52
53
54
55
56
57
  a m l g append
  d m l   delete
  l m l   lookup
  n m l   next-key
  u m     dump
  r m     read
58
59
60
  w m     write
  i m     iterate through all texts
  i m f e iterate through f..e-1
61
  q       quit
Per Cederqvist's avatar
Per Cederqvist committed
62
63
 */
int
64
main(void)
Per Cederqvist's avatar
Per Cederqvist committed
65
66
67
68
69
70
71
72
73
{
    Local_to_global maps[MAXMAPS];
    char input[LINSIZE];
    int num[ARGS];
    int n;
    int got;
    int ok;
    char *s;

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#ifdef TRACED_ALLOCATIONS
    /* We must do this before we allocate any memory... */
    {
      char buf[1024];
      char *nl;

      fputs("Where does the trace want to go today? [stderr]\n", stdout);
      fflush(stdout);
      if (fgets(buf, sizeof(buf), stdin) != buf)
      {
	  fprintf(stderr, "unable to read trace location\n");
	  exit(1);
      }

      if ((nl = strchr(buf, '\n')) != NULL)
	  *nl = '\0';
      trace_alloc_file(buf);
    }
#endif

94
95
    l2g_set_block_size(10);

Per Cederqvist's avatar
Per Cederqvist committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    printf("l2g> ");
    fflush(stdout);
    while (fgets(input, LINSIZE, stdin) != NULL)
    {
	s = strchr(input, '\n');
	assert(s);
	*s = '\0';

	if (*input == '\0')
	{
	    printf("EMPTY LINE\nl2g> ");
	    fflush(stdout);
	    continue;
	}

	for (n = 0; n < ARGS; n++)
	    num[n] = 0;
	n = 0;
	got = 0;

	for (s=input + 1; *s && *s != '\n'; ++s)
	{
118
	    if (isdigit((int)(unsigned char)*s))
Per Cederqvist's avatar
Per Cederqvist committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
	    {
		if (n < ARGS)
		{
		    got = 1;
		    num[n] = 10*num[n] + *s - '0';
		}
		else
		{
		    printf("BAD INPUT (too many numbers)\nl2g> ");
		    fflush(stdout);
		    continue;
		}
	    }
	    else if (*s == ' ')
	    {
		if (got)
		    n++;
		got = 0;
	    }
	    else
	    {
		printf("BAD INPUT (bad character)\nl2g> ");
		fflush(stdout);
		continue;
	    }
	}

	if (got)
	    n++;
	ok = 0;

	switch(*input)
	{
	case 'D':
	    if (n == 1 && num[0] < MAXMAPS)
	    {
		l2g_destruct(&maps[num[0]]);
		ok = 1;
	    }
	    break;
	case 'I':
	    if (n == 1 && num[0] < MAXMAPS)
	    {
		l2g_init(&maps[num[0]]);
		ok = 1;
	    }
	    break;
	case 'C':
	    if (n == 1 && num[0] < MAXMAPS)
	    {
		l2g_clear(&maps[num[0]]);
		ok = 1;
	    }
	    else if (n == 2 && num[0] < MAXMAPS && num[1] < MAXMAPS)
	    {
		l2g_copy(&maps[num[0]], &maps[num[1]]);
		ok = 1;
	    }
	    break;
	case 'a':
	    if (n == 3 && num[0] < MAXMAPS)
	    {
		l2g_append(&maps[num[0]], num[1], num[2]);
		ok = 1;
	    }
	    break;
	case 'd':
	    if (n == 2 && num[0] < MAXMAPS)
	    {
		l2g_delete(&maps[num[0]], num[1]);
		ok = 1;
	    }
	    break;
	case 'l':
	    if (n == 2 && num[0] < MAXMAPS)
	    {
		printf("%lu\n",
		       (unsigned long)l2g_lookup(&maps[num[0]], num[1]));
		ok = 1;
	    }
	    break;
	case 'n':
	    if (n == 2 && num[0] < MAXMAPS)
	    {
		printf("%lu\n",
		       (unsigned long)l2g_next_key(&maps[num[0]], num[1]));
		ok = 1;
	    }
	    break;
208
	case 'i':
Per Cederqvist's avatar
Per Cederqvist committed
209
210
	    if (n == 1 && num[0] < MAXMAPS)
	    {
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
		L2g_iterator iter;
		for (l2gi_searchall(&iter, &maps[num[0]]);
		     !iter.search_ended;
		     l2gi_next(&iter))
		    printf("%lu:%lu\n",
			   (unsigned long)iter.lno,
			   (unsigned long)iter.tno);
		ok = 1;
	    }
	    else if (n == 3 && num[0] < MAXMAPS)
	    {
		L2g_iterator iter;
		for (l2gi_searchsome(&iter, &maps[num[0]],
				     num[1], num[2]);
		     !iter.search_ended;
		     l2gi_next(&iter))
		    printf("%lu:%lu\n",
			   (unsigned long)iter.lno,
			   (unsigned long)iter.tno);
Per Cederqvist's avatar
Per Cederqvist committed
230
231
232
233
234
235
		ok = 1;
	    }
	    break;
	case 'u':
	    if (n == 1 && num[0] < MAXMAPS)
	    {
236
		l2g_dump(stdout, &maps[num[0]]);
Per Cederqvist's avatar
Per Cederqvist committed
237
238
239
240
241
242
		ok = 1;
	    }
	    break;
	case 'r':
	    if (n == 1 && num[0] < MAXMAPS)
	    {
243
244
245
246
247
248
249
		int c;
		l2g_read(stdin, &maps[num[0]]);
		if ((c = getchar()) != '\n')
		{
		    printf("BAD INPUT: got char %d instead of newline\n", c);
		    fflush(stdout);
		}
Per Cederqvist's avatar
Per Cederqvist committed
250
251
252
		ok = 1;
	    }
	    break;
253
	case 'w':
Per Cederqvist's avatar
Per Cederqvist committed
254
255
	    if (n == 1 && num[0] < MAXMAPS)
	    {
256
257
		l2g_write(stdout, &maps[num[0]]);
		putchar('\n');
Per Cederqvist's avatar
Per Cederqvist committed
258
259
260
		ok = 1;
	    }
	    break;
261
262
263
264
	case 'q':
	    printf("test-l2g quitting\n");
	    fflush(stdout);
	    return 0;
Per Cederqvist's avatar
Per Cederqvist committed
265
266
267
268
269
270
271
272
273
274
275
276
277
278
	}

	if (ok == 0)
	{
	    printf("BAD LINE (bad params)\nl2g> ");
	    fflush(stdout);
	    continue;
	}

	printf("l2g> ");
	fflush(stdout);
    }
    return 0;
}