📄 cbtest.c
字号:
/************************************************************************************************* * Test cases of Cabin * Copyright (C) 2000-2003 Mikio Hirabayashi * This file is part of QDBM, Quick Database Manager. * QDBM 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. QDBM 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 QDBM; if * not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA. *************************************************************************************************/#include <cabin.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg.h>#include <time.h>#undef TRUE#define TRUE 1 /* boolean true */#undef FALSE#define FALSE 0 /* boolean false */#define RECBUFSIZ 32 /* buffer for records *//* for RISC OS */#if defined(__riscos__) || defined(__riscos)#include <unixlib/local.h>int __riscosify_control = __RISCOSIFY_NO_PROCESS;#endif/* global variables */const char *progname; /* program name *//* function prototypes */int main(int argc, char **argv);void usage(void);int runsort(int argc, char **argv);int runlist(int argc, char **argv);int runmap(int argc, char **argv);int runwicked(int argc, char **argv);int runmisc(int argc, char **argv);int printfflush(const char *format, ...);int strpcmp(const void *ap, const void *bp);int myrand(void);int dosort(int rnum, int disp);int dolist(int rnum, int disp);int domap(int rnum, int disp);int dowicked(int rnum);int domisc(void);/* main routine */int main(int argc, char **argv){ int rv; progname = argv[0]; if(argc < 2) usage(); rv = 0; if(!strcmp(argv[1], "sort")){ rv = runsort(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], "wicked")){ rv = runwicked(argc, argv); } else if(!strcmp(argv[1], "misc")){ rv = runmisc(argc, argv); } else { usage(); } return rv;}/* print the usage and exit */void usage(void){ fprintf(stderr, "%s: test cases for Cabin\n", progname); fprintf(stderr, "\n"); fprintf(stderr, "usage:\n"); fprintf(stderr, " %s sort [-d] rnum\n", progname); fprintf(stderr, " %s list [-d] rnum\n", progname); fprintf(stderr, " %s map [-d] rnum\n", progname); fprintf(stderr, " %s wicked rnum\n", progname); fprintf(stderr, " %s misc\n", progname); exit(1);}/* parse arguments of sort command */int runsort(int argc, char **argv){ int i, rnum, disp, rv; char *rstr; rstr = NULL; rnum = 0; disp = FALSE; for(i = 2; i < argc; i++){ if(argv[i][0] == '-'){ if(!strcmp(argv[i], "-d")){ disp = TRUE; } else { usage(); } } else if(!rstr){ rstr = argv[i]; } else { usage(); } } if(!rstr) usage(); rnum = atoi(rstr); if(rnum < 1) usage(); rv = dosort(rnum, disp); return rv;}/* parse arguments of list command */int runlist(int argc, char **argv){ int i, rnum, disp, rv; char *rstr; rstr = NULL; rnum = 0; disp = FALSE; for(i = 2; i < argc; i++){ if(argv[i][0] == '-'){ if(!strcmp(argv[i], "-d")){ disp = TRUE; } else { usage(); } } else if(!rstr){ rstr = argv[i]; } else { usage(); } } if(!rstr) usage(); rnum = atoi(rstr); if(rnum < 1) usage(); rv = dolist(rnum, disp); return rv;}/* parse arguments of map command */int runmap(int argc, char **argv){ int i, rnum, disp, rv; char *rstr; rstr = NULL; rnum = 0; disp = FALSE; for(i = 2; i < argc; i++){ if(argv[i][0] == '-'){ if(!strcmp(argv[i], "-d")){ disp = TRUE; } else { usage(); } } else if(!rstr){ rstr = argv[i]; } else { usage(); } } if(!rstr) usage(); rnum = atoi(rstr); if(rnum < 1) usage(); rv = domap(rnum, disp); return rv;}/* parse arguments of wicked command */int runwicked(int argc, char **argv){ int i, rnum, rv; char *rstr; rstr = NULL; rnum = 0; for(i = 2; i < argc; i++){ if(argv[i][0] == '-'){ usage(); } else if(!rstr){ rstr = argv[i]; } else { usage(); } } if(!rstr) usage(); rnum = atoi(rstr); if(rnum < 1) usage(); rv = dowicked(rnum); return rv;}/* parse arguments of misc command */int runmisc(int argc, char **argv){ int rv; rv = domisc(); return rv;}/* print formatted string and flush the buffer */int prinfflush(const char *format, ...){ va_list ap; int rv; va_start(ap, format); rv = vprintf(format, ap); if(fflush(stdout) == EOF) rv = -1; va_end(ap); return rv;}/* comparing function */int strpcmp(const void *ap, const void *bp){ char *a, *b; a = *(char **)ap; b = *(char **)bp; return strcmp(a, b);}/* pseudo random number generator */int myrand(void){ static int cnt = 0; if(cnt == 0) srand(time(NULL)); return (rand() * rand() + (rand() >> (sizeof(int) * 4)) + (cnt++)) & 0x7FFFFFFF;}/* perform sort command */int dosort(int rnum, int disp){ char **ivector1, **ivector2, **ivector3, **ivector4, **ivector5; char buf[RECBUFSIZ]; int i, len, err; if(!disp) prinfflush("<Sorting Test>\n rnum=%d disp=%d\n\n", rnum, disp); ivector1 = cbmalloc(rnum * sizeof(ivector1[0])); ivector2 = cbmalloc(rnum * sizeof(ivector2[0])); ivector3 = cbmalloc(rnum * sizeof(ivector3[0])); ivector4 = cbmalloc(rnum * sizeof(ivector4[0])); ivector5 = cbmalloc(rnum * sizeof(ivector5[0])); err = FALSE; for(i = 0; i < rnum; i++){ len = sprintf(buf, "%08d", myrand() % rnum + 1); ivector1[i] = cbmemdup(buf, len); ivector2[i] = cbmemdup(buf, len); ivector3[i] = cbmemdup(buf, len); ivector4[i] = cbmemdup(buf, len); ivector5[i] = cbmemdup(buf, len); } if(!disp) prinfflush("Sorting with insert sort ... "); cbisort(ivector1, rnum, sizeof(ivector1[0]), strpcmp); if(!disp) prinfflush("ok\n"); if(!disp) prinfflush("Sorting with shell sort ... "); cbssort(ivector2, rnum, sizeof(ivector2[0]), strpcmp); if(!disp) prinfflush("ok\n"); if(!disp) prinfflush("Sorting with heap sort ... "); cbhsort(ivector3, rnum, sizeof(ivector3[0]), strpcmp); if(!disp) prinfflush("ok\n"); if(!disp) prinfflush("Sorting with quick sort ... "); cbqsort(ivector4, rnum, sizeof(ivector4[0]), strpcmp); if(!disp) prinfflush("ok\n"); for(i = 0; i < rnum; i++){ if(disp) prinfflush("%s\t%s\t%s\t%s\t[%s]\n", ivector1[i], ivector2[i], ivector3[i], ivector4[i], ivector5[i]); if(strcmp(ivector1[i], ivector2[i])) err = TRUE; if(strcmp(ivector1[i], ivector3[i])) err = TRUE; if(strcmp(ivector1[i], ivector4[i])) err = TRUE; free(ivector1[i]); free(ivector2[i]); free(ivector3[i]); free(ivector4[i]); free(ivector5[i]); } free(ivector1); free(ivector2); free(ivector3); free(ivector4); free(ivector5); if(err) fprintf(stderr, "%s: sorting failed\n", progname); if(!disp && !err) prinfflush("all ok\n\n"); return err ? 1 : 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -