rbtree

diff test/rbshell/test.c @ 6:028a21468abf

added rbshell test
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 12 Oct 2011 07:03:03 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/rbshell/test.c	Wed Oct 12 07:03:03 2011 +0300
     1.3 @@ -0,0 +1,214 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <string.h>
     1.7 +#include <ctype.h>
     1.8 +#include "rbtree.h"
     1.9 +#include "sym.h"
    1.10 +
    1.11 +#define SEP	" \t\r\n,\"\'"
    1.12 +
    1.13 +/* commands */
    1.14 +enum {
    1.15 +	CMD_CREATE,
    1.16 +	CMD_DESTROY,
    1.17 +	CMD_CLEAR,
    1.18 +	CMD_COPY,
    1.19 +	CMD_SIZE,
    1.20 +	CMD_INSERT,
    1.21 +	CMD_DELETE,
    1.22 +	CMD_FIND,
    1.23 +	CMD_PRINT,
    1.24 +
    1.25 +	MAX_CMD
    1.26 +};
    1.27 +
    1.28 +int runcmd(int cmd, int argc, char **argv);
    1.29 +
    1.30 +#define NUM_TREES	8
    1.31 +struct rbtree *trees[NUM_TREES];
    1.32 +
    1.33 +
    1.34 +int main(void)
    1.35 +{
    1.36 +	char buf[512];
    1.37 +
    1.38 +	/* init symbol table */
    1.39 +	if(init_sym() == -1) {
    1.40 +		return 1;
    1.41 +	}
    1.42 +	add_sym("create", CMD_CREATE, 0);
    1.43 +	add_sym("destroy", CMD_DESTROY, 1);
    1.44 +	add_sym("clear", CMD_CLEAR, 1);
    1.45 +	add_sym("copy", CMD_COPY, 2);
    1.46 +	add_sym("size", CMD_SIZE, 1);
    1.47 +	add_sym("insert", CMD_INSERT, 2);
    1.48 +	add_sym("delete", CMD_DELETE, 2);
    1.49 +	add_sym("find", CMD_FIND, 2);
    1.50 +	add_sym("print", CMD_PRINT, 1);
    1.51 +
    1.52 +	for(;;) {
    1.53 +		int argc = 0;
    1.54 +		char *argv[8];
    1.55 +		int cmd_id, cmd_arity;
    1.56 +
    1.57 +		printf("rbshell> ");
    1.58 +		fflush(stdout);
    1.59 +
    1.60 +		if(!fgets(buf, sizeof buf, stdin)) {
    1.61 +			putchar('\n');
    1.62 +			break;
    1.63 +		}
    1.64 +
    1.65 +		if(!(argv[argc] = strtok(buf, SEP)) || argv[argc][0] == '#') {
    1.66 +			return 0;	/* empty / comment */
    1.67 +		}
    1.68 +		argc++;
    1.69 +
    1.70 +		if((cmd_id = sym_lookup(argv[0])) == -1) {
    1.71 +			continue;
    1.72 +		}
    1.73 +		cmd_arity = sym_arity(argv[0]);
    1.74 +
    1.75 +		while((argv[argc] = strtok(0, SEP))) {
    1.76 +			argc++;
    1.77 +			if(argc >= sizeof argv / sizeof *argv) {
    1.78 +				break;
    1.79 +			}
    1.80 +		}
    1.81 +
    1.82 +		if(argc - 1 != cmd_arity) {
    1.83 +			fprintf(stderr, "too many (%d) arguments for %s, expected %d\n", argc - 1, argv[0], cmd_arity);
    1.84 +			continue;
    1.85 +		}
    1.86 +
    1.87 +		runcmd(cmd_id, argc, argv);
    1.88 +	}
    1.89 +
    1.90 +	destroy_sym();
    1.91 +	return 0;
    1.92 +}
    1.93 +
    1.94 +#define CHECKINT(i)		\
    1.95 +	if(!isdigit(argv[i][0])) { \
    1.96 +		fprintf(stderr, "expected number, got: %s\n", argv[i]); \
    1.97 +		return -1; \
    1.98 +	}
    1.99 +
   1.100 +#define CHECKVALID(idx)	\
   1.101 +	if((idx) < 0 || (idx) >= NUM_TREES || !trees[idx]) { \
   1.102 +		fprintf(stderr, "invalid tree specified: %d\n", idx); \
   1.103 +		return -1; \
   1.104 +	}
   1.105 +
   1.106 +
   1.107 +int runcmd(int cmd, int argc, char **argv)
   1.108 +{
   1.109 +	int i, idx, idx2, n;
   1.110 +	struct rbnode *node;
   1.111 +
   1.112 +	switch(cmd) {
   1.113 +	case CMD_CREATE:
   1.114 +		for(i=0; i<NUM_TREES; i++) {
   1.115 +			if(trees[i] == 0) {
   1.116 +				if(!(trees[i] = rb_create(RB_KEY_INT))) {
   1.117 +					fprintf(stderr, "failed to create a new tree\n");
   1.118 +					return -1;
   1.119 +				}
   1.120 +				printf("created new tree: %d\n", i);
   1.121 +				break;
   1.122 +			}
   1.123 +		}
   1.124 +		if(i == NUM_TREES) {
   1.125 +			fprintf(stderr, "can't create more trees\n");
   1.126 +			return -1;
   1.127 +		}
   1.128 +		break;
   1.129 +
   1.130 +	case CMD_DESTROY:
   1.131 +		CHECKINT(1);
   1.132 +		idx = atoi(argv[1]);
   1.133 +		CHECKVALID(idx);
   1.134 +
   1.135 +		rb_free(trees[idx]);
   1.136 +		trees[idx] = 0;
   1.137 +		printf("destroyed tree: %d\n", idx);
   1.138 +		break;
   1.139 +
   1.140 +	case CMD_CLEAR:
   1.141 +		CHECKINT(1);
   1.142 +		idx = atoi(argv[1]);
   1.143 +		CHECKVALID(idx);
   1.144 +
   1.145 +		rb_clear(trees[idx]);
   1.146 +		printf("cleared tree: %d\n", idx);
   1.147 +		break;
   1.148 +
   1.149 +	case CMD_COPY:
   1.150 +		CHECKINT(1);
   1.151 +		CHECKINT(2);
   1.152 +		idx = atoi(argv[1]);
   1.153 +		idx2 = atoi(argv[2]);
   1.154 +		CHECKVALID(idx);
   1.155 +		CHECKVALID(idx2);
   1.156 +
   1.157 +		rb_copy(trees[idx2], trees[idx]);
   1.158 +		printf("copied %d -> %d\n", idx, idx2);
   1.159 +		break;
   1.160 +
   1.161 +	case CMD_SIZE:
   1.162 +		CHECKINT(1);
   1.163 +		idx = atoi(argv[1]);
   1.164 +		CHECKVALID(idx);
   1.165 +
   1.166 +		printf("tree size: %d\n", rb_size(trees[idx]));
   1.167 +		break;
   1.168 +
   1.169 +	case CMD_INSERT:
   1.170 +		CHECKINT(1);
   1.171 +		CHECKINT(2);
   1.172 +		idx = atoi(argv[1]);
   1.173 +		CHECKVALID(idx);
   1.174 +
   1.175 +		n = atoi(argv[2]);
   1.176 +		rb_inserti(trees[idx], n, 0);
   1.177 +		printf("inserted: %d\n", n);
   1.178 +		break;
   1.179 +
   1.180 +	case CMD_DELETE:
   1.181 +	case CMD_FIND:
   1.182 +		CHECKINT(1);
   1.183 +		CHECKINT(2);
   1.184 +		idx = atoi(argv[1]);
   1.185 +		CHECKVALID(idx);
   1.186 +		n = atoi(argv[2]);
   1.187 +
   1.188 +		if(!rb_findi(trees[idx], n)) {
   1.189 +			fprintf(stderr, "%d not found\n", n);
   1.190 +		} else {
   1.191 +			if(cmd == CMD_DELETE) {
   1.192 +				rb_deletei(trees[idx], n);
   1.193 +				printf("deleted %d\n", n);
   1.194 +			} else {
   1.195 +				printf("found %d\n", n);
   1.196 +			}
   1.197 +		}
   1.198 +		break;
   1.199 +
   1.200 +	case CMD_PRINT:
   1.201 +		CHECKINT(1);
   1.202 +		idx = atoi(argv[1]);
   1.203 +		CHECKVALID(idx);
   1.204 +
   1.205 +		rb_begin(trees[idx]);
   1.206 +		while((node = rb_next(trees[idx]))) {
   1.207 +			printf("%d ", rb_node_keyi(node));
   1.208 +		}
   1.209 +		putchar('\n');
   1.210 +		break;
   1.211 +
   1.212 +	default:
   1.213 +		/* can't happen */
   1.214 +		break;
   1.215 +	}
   1.216 +	return 0;
   1.217 +}