📄 tcutest.c
字号:
/************************************************************************************************* * The test cases of the utility API * Copyright (C) 2006-2009 Mikio Hirabayashi * This file is part of Tokyo Cabinet. * Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of * the GNU Lesser General Public License as published by the Free Software Foundation; either * version 2.1 of the License or any later version. Tokyo Cabinet 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 Lesser General Public * License for more details. * You should have received a copy of the GNU Lesser General Public License along with Tokyo * Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307 USA. *************************************************************************************************/#include <tcutil.h>#include "myconf.h"#define RECBUFSIZ 48 // buffer for records/* global variables */const char *g_progname; // program nameunsigned int g_randseed; // random seed/* function prototypes */int main(int argc, char **argv);static void usage(void);static void iprintf(const char *format, ...);static void iputchar(int c);static void sysprint(void);static int myrand(int range);static void *pdprocfunc(const void *vbuf, int vsiz, int *sp, void *op);static bool iterfunc(const void *kbuf, int ksiz, const void *vbuf, int vsiz, void *op);static int intcompar(const void *ap, const void *bp);static int runxstr(int argc, char **argv);static int runlist(int argc, char **argv);static int runmap(int argc, char **argv);static int runtree(int argc, char **argv);static int runmdb(int argc, char **argv);static int runndb(int argc, char **argv);static int runmisc(int argc, char **argv);static int runwicked(int argc, char **argv);static int procxstr(int rnum);static int proclist(int rnum, int anum, bool rd);static int procmap(int rnum, int bnum, bool rd, bool tr, bool rnd, int dmode);static int proctree(int rnum, bool rd, bool tr, bool rnd, int dmode);static int procmdb(int rnum, int bnum, bool rd, bool tr, bool rnd, int dmode);static int procndb(int rnum, bool rd, bool tr, bool rnd, int dmode);static int procmisc(int rnum);static int procwicked(int rnum);/* main routine */int main(int argc, char **argv){ g_progname = argv[0]; const char *ebuf = getenv("TCRNDSEED"); g_randseed = ebuf ? tcatoix(ebuf) : tctime() * 1000; srand(g_randseed); if(argc < 2) usage(); int rv = 0; if(!strcmp(argv[1], "xstr")){ rv = runxstr(argc, argv); } else if(!strcmp(argv[1], "list")){ rv = runlist(argc, argv); } else if(!strcmp(argv[1], "map")){ rv = runmap(argc, argv); } else if(!strcmp(argv[1], "tree")){ rv = runtree(argc, argv); } else if(!strcmp(argv[1], "mdb")){ rv = runmdb(argc, argv); } else if(!strcmp(argv[1], "ndb")){ rv = runndb(argc, argv); } else if(!strcmp(argv[1], "misc")){ rv = runmisc(argc, argv); } else if(!strcmp(argv[1], "wicked")){ rv = runwicked(argc, argv); } else { usage(); } return rv;}/* print the usage and exit */static void usage(void){ fprintf(stderr, "%s: test cases of the utility API of Tokyo Cabinet\n", g_progname); fprintf(stderr, "\n"); fprintf(stderr, "usage:\n"); fprintf(stderr, " %s xstr rnum\n", g_progname); fprintf(stderr, " %s list [-rd] rnum [anum]\n", g_progname); fprintf(stderr, " %s map [-rd] [-tr] [-rnd] [-dk|-dc|-dai|-dad|-dpr] rnum [bnum]\n", g_progname); fprintf(stderr, " %s tree [-rd] [-tr] [-rnd] [-dk|-dc|-dai|-dad|-dpr] rnum\n", g_progname); fprintf(stderr, " %s mdb [-rd] [-tr] [-rnd] [-dk|-dc|-dai|-dad|-dpr] rnum [bnum]\n", g_progname); fprintf(stderr, " %s ndb [-rd] [-tr] [-rnd] [-dk|-dc|-dai|-dad|-dpr] rnum\n", g_progname); fprintf(stderr, " %s misc rnum\n", g_progname); fprintf(stderr, " %s wicked rnum\n", g_progname); fprintf(stderr, "\n"); exit(1);}/* print formatted information string and flush the buffer */static void iprintf(const char *format, ...){ va_list ap; va_start(ap, format); vprintf(format, ap); fflush(stdout); va_end(ap);}/* print a character and flush the buffer */static void iputchar(int c){ putchar(c); fflush(stdout);}/* print system information */static void sysprint(void){ TCMAP *info = tcsysinfo(); if(info){ tcmapiterinit(info); const char *kbuf; while((kbuf = tcmapiternext2(info)) != NULL){ iprintf("sys_%s: %s\n", kbuf, tcmapiterval2(kbuf)); } tcmapdel(info); }}/* get a random number */static int myrand(int range){ if(range < 2) return 0; int high = (unsigned int)rand() >> 4; int low = range * (rand() / (RAND_MAX + 1.0)); low &= (unsigned int)INT_MAX >> 4; return (high + low) % range;}/* duplication callback function */static void *pdprocfunc(const void *vbuf, int vsiz, int *sp, void *op){ if(myrand(4) == 0) return (void *)-1; if(myrand(2) == 0) return NULL; int len = myrand(RECBUFSIZ); char buf[RECBUFSIZ]; memset(buf, '*', len); *sp = len; return tcmemdup(buf, len);}/* iterator function */static bool iterfunc(const void *kbuf, int ksiz, const void *vbuf, int vsiz, void *op){ unsigned int sum = 0; while(--ksiz >= 0){ sum += ((char *)kbuf)[ksiz]; } while(--vsiz >= 0){ sum += ((char *)vbuf)[vsiz]; } return myrand(100 + (sum & 0xff)) > 0;}/* compare two integers */static int intcompar(const void *ap, const void *bp){ return *(int *)ap - *(int *)bp;}/* parse arguments of xstr command */static int runxstr(int argc, char **argv){ char *rstr = NULL; for(int i = 2; i < argc; i++){ if(!rstr && argv[i][0] == '-'){ usage(); } else if(!rstr){ rstr = argv[i]; } else { usage(); } } if(!rstr) usage(); int rnum = tcatoix(rstr); if(rnum < 1) usage(); int rv = procxstr(rnum); return rv;}/* parse arguments of list command */static int runlist(int argc, char **argv){ char *rstr = NULL; char *astr = NULL; bool rd = false; for(int i = 2; i < argc; i++){ if(!rstr && argv[i][0] == '-'){ if(!strcmp(argv[i], "-rd")){ rd = true; } else { usage(); } } else if(!rstr){ rstr = argv[i]; } else if(!astr){ astr = argv[i]; } else { usage(); } } if(!rstr) usage(); int rnum = tcatoix(rstr); if(rnum < 1) usage(); int anum = astr ? tcatoix(astr) : -1; int rv = proclist(rnum, anum, rd); return rv;}/* parse arguments of map command */static int runmap(int argc, char **argv){ char *rstr = NULL; char *bstr = NULL; bool rd = false; bool tr = false; bool rnd = false; int dmode = 0; for(int i = 2; i < argc; i++){ if(!rstr && argv[i][0] == '-'){ if(!strcmp(argv[i], "-rd")){ rd = true; } else if(!strcmp(argv[i], "-tr")){ tr = true; } else if(!strcmp(argv[i], "-rnd")){ rnd = true; } else if(!strcmp(argv[i], "-dk")){ dmode = -1; } else if(!strcmp(argv[i], "-dc")){ dmode = 1; } else if(!strcmp(argv[i], "-dai")){ dmode = 10; } else if(!strcmp(argv[i], "-dad")){ dmode = 11; } else if(!strcmp(argv[i], "-dpr")){ dmode = 12; } else { usage(); } } else if(!rstr){ rstr = argv[i]; } else if(!bstr){ bstr = argv[i]; } else { usage(); } } if(!rstr) usage(); int rnum = tcatoix(rstr); if(rnum < 1) usage(); int bnum = bstr ? tcatoix(bstr) : -1; int rv = procmap(rnum, bnum, rd, tr, rnd, dmode); return rv;}/* parse arguments of tree command */static int runtree(int argc, char **argv){ char *rstr = NULL; bool rd = false; bool tr = false; bool rnd = false; int dmode = 0; for(int i = 2; i < argc; i++){ if(!rstr && argv[i][0] == '-'){ if(!strcmp(argv[i], "-rd")){ rd = true; } else if(!strcmp(argv[i], "-tr")){ tr = true; } else if(!strcmp(argv[i], "-rnd")){ rnd = true; } else if(!strcmp(argv[i], "-dk")){ dmode = -1; } else if(!strcmp(argv[i], "-dc")){ dmode = 1; } else if(!strcmp(argv[i], "-dai")){ dmode = 10; } else if(!strcmp(argv[i], "-dad")){ dmode = 11; } else if(!strcmp(argv[i], "-dpr")){ dmode = 12; } else { usage(); } } else if(!rstr){ rstr = argv[i]; } else { usage(); } } if(!rstr) usage(); int rnum = tcatoix(rstr); if(rnum < 1) usage(); int rv = proctree(rnum, rd, tr, rnd, dmode); return rv;}/* parse arguments of mdb command */static int runmdb(int argc, char **argv){ char *rstr = NULL; char *bstr = NULL; bool rd = false; bool tr = false; bool rnd = false; int dmode = 0; for(int i = 2; i < argc; i++){ if(!rstr && argv[i][0] == '-'){ if(!strcmp(argv[i], "-rd")){ rd = true; } else if(!strcmp(argv[i], "-tr")){ tr = true; } else if(!strcmp(argv[i], "-rnd")){ rnd = true; } else if(!strcmp(argv[i], "-dk")){ dmode = -1; } else if(!strcmp(argv[i], "-dc")){ dmode = 1; } else if(!strcmp(argv[i], "-dai")){ dmode = 10; } else if(!strcmp(argv[i], "-dad")){ dmode = 11; } else if(!strcmp(argv[i], "-dpr")){ dmode = 12; } else { usage(); } } else if(!rstr){ rstr = argv[i]; } else if(!bstr){ bstr = argv[i]; } else { usage(); } } if(!rstr) usage(); int rnum = tcatoix(rstr); if(rnum < 1) usage(); int bnum = bstr ? tcatoix(bstr) : -1; int rv = procmdb(rnum, bnum, rd, tr, rnd, dmode); return rv;}/* parse arguments of ndb command */static int runndb(int argc, char **argv){ char *rstr = NULL; bool rd = false; bool tr = false; bool rnd = false; int dmode = 0; for(int i = 2; i < argc; i++){ if(!rstr && argv[i][0] == '-'){ if(!strcmp(argv[i], "-rd")){ rd = true; } else if(!strcmp(argv[i], "-tr")){ tr = true; } else if(!strcmp(argv[i], "-rnd")){ rnd = true; } else if(!strcmp(argv[i], "-dk")){ dmode = -1; } else if(!strcmp(argv[i], "-dc")){ dmode = 1; } else if(!strcmp(argv[i], "-dai")){ dmode = 10; } else if(!strcmp(argv[i], "-dad")){ dmode = 11; } else if(!strcmp(argv[i], "-dpr")){ dmode = 12; } else { usage(); } } else if(!rstr){ rstr = argv[i]; } else { usage(); } } if(!rstr) usage(); int rnum = tcatoix(rstr); if(rnum < 1) usage(); int rv = procndb(rnum, rd, tr, rnd, dmode); return rv;}/* parse arguments of misc command */static int runmisc(int argc, char **argv){ char *rstr = NULL; for(int i = 2; i < argc; i++){ if(!rstr && argv[i][0] == '-'){ usage(); } else if(!rstr){ rstr = argv[i]; } else { usage(); } } if(!rstr) usage(); int rnum = tcatoix(rstr); if(rnum < 1) usage(); int rv = procmisc(rnum); return rv;}/* parse arguments of wicked command */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -