⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcutest.c

📁 Tokyo Cabinet的Tokyo Cabinet 是一个DBM的实现。这里的数据库由一系列key-value对的记录构成。key和value都可以是任意长度的字节序列,既可以是二进制也可以是字符
💻 C
📖 第 1 页 / 共 4 页
字号:
static int runwicked(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 = procwicked(rnum);  return rv;}/* perform xstr command */static int procxstr(int rnum){  iprintf("<Extensible String Writing Test>\n  seed=%u  rnum=%d\n\n", g_randseed, rnum);  double stime = tctime();  TCXSTR *xstr = tcxstrnew();  for(int i = 1; i <= rnum; i++){    char buf[RECBUFSIZ];    int len = sprintf(buf, "%08d", i);    tcxstrcat(xstr, buf, len);    if(rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }  }  iprintf("size: %u\n", tcxstrsize(xstr));  sysprint();  tcxstrdel(xstr);  iprintf("time: %.3f\n", tctime() - stime);  iprintf("ok\n\n");  return 0;}/* perform list command */static int proclist(int rnum, int anum, bool rd){  iprintf("<List Writing Test>\n  seed=%u  rnum=%d  anum=%d  rd=%d\n\n",          g_randseed, rnum, anum, rd);  double stime = tctime();  TCLIST *list = (anum > 0) ? tclistnew2(anum) : tclistnew();  for(int i = 1; i <= rnum; i++){    char buf[RECBUFSIZ];    int len = sprintf(buf, "%08d", i);    tclistpush(list, buf, len);    if(rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }  }  if(rd){    double itime = tctime();    iprintf("time: %.3f\n", itime - stime);    stime = itime;    for(int i = 1; i <= rnum; i++){      int len;      tclistval(list, i, &len);      if(rnum > 250 && i % (rnum / 250) == 0){        iputchar('.');        if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);      }    }  }  iprintf("record number: %u\n", tclistnum(list));  sysprint();  tclistdel(list);  iprintf("time: %.3f\n", tctime() - stime);  iprintf("ok\n\n");  return 0;}/* perform map command */static int procmap(int rnum, int bnum, bool rd, bool tr, bool rnd, int dmode){  iprintf("<Map Writing Test>\n  seed=%u  rnum=%d  bnum=%d  rd=%d  tr=%d  rnd=%d  dmode=%d\n\n",          g_randseed, rnum, bnum, rd, tr, rnd, dmode);  double stime = tctime();  TCMAP *map = (bnum > 0) ? tcmapnew2(bnum) : tcmapnew();  for(int i = 1; i <= rnum; i++){    char buf[RECBUFSIZ];    int len = sprintf(buf, "%08d", rnd ? myrand(rnum) + 1 : i);    switch(dmode){    case -1:      tcmapputkeep(map, buf, len, buf, len);      break;    case 1:      tcmapputcat(map, buf, len, buf, len);      break;    case 10:      tcmapaddint(map, buf, len, myrand(3));      break;    case 11:      tcmapadddouble(map, buf, len, myrand(3));      break;    case 12:      tcmapputproc(map, buf, len, buf, len, pdprocfunc, NULL);      break;    default:      tcmapput(map, buf, len, buf, len);      break;    }    if(rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }  }  if(rd){    double itime = tctime();    iprintf("time: %.3f\n", itime - stime);    stime = itime;    for(int i = 1; i <= rnum; i++){      char buf[RECBUFSIZ];      int len = sprintf(buf, "%08d", rnd ? myrand(rnum) + 1 : i);      tcmapget(map, buf, len, &len);      if(rnum > 250 && i % (rnum / 250) == 0){        iputchar('.');        if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);      }    }  }  if(tr){    double itime = tctime();    iprintf("time: %.3f\n", itime - stime);    stime = itime;    tcmapiterinit(map);    int ksiz;    const char *kbuf;    int inum = 1;    while((kbuf = tcmapiternext(map, &ksiz)) != NULL){      tcmapiterval2(kbuf);      if(rnum > 250 && inum % (rnum / 250) == 0){        iputchar('.');        if(inum == rnum || inum % (rnum / 10) == 0) iprintf(" (%08d)\n", inum);      }      inum++;    }    if(rnd && rnum > 250) iprintf(" (%08d)\n", inum);  }  iprintf("record number: %llu\n", (unsigned long long)tcmaprnum(map));  iprintf("size: %llu\n", (unsigned long long)tcmapmsiz(map));  sysprint();  tcmapdel(map);  iprintf("time: %.3f\n", tctime() - stime);  iprintf("ok\n\n");  return 0;}/* perform tree command */static int proctree(int rnum, bool rd, bool tr, bool rnd, int dmode){  iprintf("<Tree Writing Test>\n  seed=%u  rnum=%d  rd=%d  tr=%d  rnd=%d  dmode=%d\n\n",          g_randseed, rnum, rd, tr, rnd, dmode);  double stime = tctime();  TCTREE *tree = tctreenew();  for(int i = 1; i <= rnum; i++){    char buf[RECBUFSIZ];    int len = sprintf(buf, "%08d", rnd ? myrand(rnum) + 1 : i);    switch(dmode){    case -1:      tctreeputkeep(tree, buf, len, buf, len);      break;    case 1:      tctreeputcat(tree, buf, len, buf, len);      break;    case 10:      tctreeaddint(tree, buf, len, myrand(3));      break;    case 11:      tctreeadddouble(tree, buf, len, myrand(3));      break;    case 12:      tctreeputproc(tree, buf, len, buf, len, pdprocfunc, NULL);      break;    default:      tctreeput(tree, buf, len, buf, len);      break;    }    if(rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }  }  if(rd){    double itime = tctime();    iprintf("time: %.3f\n", itime - stime);    stime = itime;    for(int i = 1; i <= rnum; i++){      char buf[RECBUFSIZ];      int len = sprintf(buf, "%08d", rnd ? myrand(rnum) + 1 : i);      tctreeget(tree, buf, len, &len);      if(rnum > 250 && i % (rnum / 250) == 0){        iputchar('.');        if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);      }    }  }  if(tr){    double itime = tctime();    iprintf("time: %.3f\n", itime - stime);    stime = itime;    tctreeiterinit(tree);    int ksiz;    const char *kbuf;    int inum = 1;    while((kbuf = tctreeiternext(tree, &ksiz)) != NULL){      tctreeiterval2(kbuf);      if(rnum > 250 && inum % (rnum / 250) == 0){        iputchar('.');        if(inum == rnum || inum % (rnum / 10) == 0) iprintf(" (%08d)\n", inum);      }      inum++;    }    if(rnd && rnum > 250) iprintf(" (%08d)\n", inum);  }  iprintf("record number: %llu\n", (unsigned long long)tctreernum(tree));  iprintf("size: %llu\n", (unsigned long long)tctreemsiz(tree));  sysprint();  tctreedel(tree);  iprintf("time: %.3f\n", tctime() - stime);  iprintf("ok\n\n");  return 0;}/* perform mdb command */static int procmdb(int rnum, int bnum, bool rd, bool tr, bool rnd, int dmode){  iprintf("<On-memory Hash Database Writing Test>\n  seed=%u  rnum=%d  bnum=%d  rd=%d  tr=%d"          "  rnd=%d  dmode=%d\n\n", g_randseed, rnum, bnum, rd, tr, rnd, dmode);  double stime = tctime();  TCMDB *mdb = (bnum > 0) ? tcmdbnew2(bnum) : tcmdbnew();  for(int i = 1; i <= rnum; i++){    char buf[RECBUFSIZ];    int len = sprintf(buf, "%08d", rnd ? myrand(rnum) + 1 : i);    switch(dmode){    case -1:      tcmdbputkeep(mdb, buf, len, buf, len);      break;    case 1:      tcmdbputcat(mdb, buf, len, buf, len);      break;    case 10:      tcmdbaddint(mdb, buf, len, myrand(3));      break;    case 11:      tcmdbadddouble(mdb, buf, len, myrand(3));      break;    case 12:      tcmdbputproc(mdb, buf, len, buf, len, pdprocfunc, NULL);      break;    default:      tcmdbput(mdb, buf, len, buf, len);      break;    }    if(rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }  }  if(rd){    double itime = tctime();    iprintf("time: %.3f\n", itime - stime);    stime = itime;    for(int i = 1; i <= rnum; i++){      char buf[RECBUFSIZ];      int len = sprintf(buf, "%08d", rnd ? myrand(rnum) + 1 : i);      tcfree(tcmdbget(mdb, buf, len, &len));      if(rnum > 250 && i % (rnum / 250) == 0){        iputchar('.');        if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);      }    }  }  if(tr){    double itime = tctime();    iprintf("time: %.3f\n", itime - stime);    stime = itime;    tcmdbiterinit(mdb);    int ksiz;    char *kbuf;    int inum = 1;    while((kbuf = tcmdbiternext(mdb, &ksiz)) != NULL){      tcfree(kbuf);      if(rnum > 250 && inum % (rnum / 250) == 0){        iputchar('.');        if(inum == rnum || inum % (rnum / 10) == 0) iprintf(" (%08d)\n", inum);      }      inum++;    }    if(rnd && rnum > 250) iprintf(" (%08d)\n", inum);  }  iprintf("record number: %llu\n", (unsigned long long)tcmdbrnum(mdb));  iprintf("size: %llu\n", (unsigned long long)tcmdbmsiz(mdb));  sysprint();  tcmdbdel(mdb);  iprintf("time: %.3f\n", tctime() - stime);  iprintf("ok\n\n");  return 0;}/* perform ndb command */static int procndb(int rnum, bool rd, bool tr, bool rnd, int dmode){  iprintf("<On-memory Tree Database Writing Test>\n  seed=%u  rnum=%d  rd=%d  tr=%d"          "  rnd=%d  dmode=%d\n\n", g_randseed, rnum, rd, tr, rnd, dmode);  double stime = tctime();  TCNDB *ndb = tcndbnew();  for(int i = 1; i <= rnum; i++){    char buf[RECBUFSIZ];    int len = sprintf(buf, "%08d", rnd ? myrand(rnum) + 1 : i);    switch(dmode){    case -1:      tcndbputkeep(ndb, buf, len, buf, len);      break;    case 1:      tcndbputcat(ndb, buf, len, buf, len);      break;    case 10:      tcndbaddint(ndb, buf, len, myrand(3));      break;    case 11:      tcndbadddouble(ndb, buf, len, myrand(3));      break;    case 12:      tcndbputproc(ndb, buf, len, buf, len, pdprocfunc, NULL);      break;    default:      tcndbput(ndb, buf, len, buf, len);      break;    }    if(rnum > 250 && i % (rnum / 250) == 0){      iputchar('.');      if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);    }  }  if(rd){    double itime = tctime();    iprintf("time: %.3f\n", itime - stime);    stime = itime;    for(int i = 1; i <= rnum; i++){      char buf[RECBUFSIZ];      int len = sprintf(buf, "%08d", rnd ? myrand(rnum) + 1 : i);      tcfree(tcndbget(ndb, buf, len, &len));      if(rnum > 250 && i % (rnum / 250) == 0){        iputchar('.');        if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i);      }    }  }  if(tr){    double itime = tctime();    iprintf("time: %.3f\n", itime - stime);    stime = itime;    tcndbiterinit(ndb);    int ksiz;    char *kbuf;    int inum = 1;    while((kbuf = tcndbiternext(ndb, &ksiz)) != NULL){      tcfree(kbuf);      if(rnum > 250 && inum % (rnum / 250) == 0){        iputchar('.');        if(inum == rnum || inum % (rnum / 10) == 0) iprintf(" (%08d)\n", inum);      }      inum++;    }    if(rnd && rnum > 250) iprintf(" (%08d)\n", inum);  }  iprintf("record number: %llu\n", (unsigned long long)tcndbrnum(ndb));  iprintf("size: %llu\n", (unsigned long long)tcndbmsiz(ndb));  sysprint();  tcndbdel(ndb);  iprintf("time: %.3f\n", tctime() - stime);  iprintf("ok\n\n");  return 0;}/* perform misc command */static int procmisc(int rnum){  iprintf("<Miscellaneous Test>\n  seed=%u  rnum=%d\n\n", g_randseed, rnum);  double stime = tctime();  bool err = false;  for(int i = 1; i <= rnum && !err; i++){    const char *str = "5%2+3-1=4 \"Yes/No\" <a&b>";    int slen = strlen(str);    char *buf, *dec;    int bsiz, dsiz, jl;    time_t date, ddate;    TCXSTR *xstr;    TCLIST *list;    TCMAP *map;    TCTREE *tree;    TCPTRLIST *ptrlist;    buf = tcmemdup(str, slen);    xstr = tcxstrfrommalloc(buf, slen);    buf = tcxstrtomalloc(xstr);    if(strcmp(buf, str)) err = true;    tcfree(buf);    if(tclmax(1, 2) != 2) err = true;    if(tclmin(1, 2) != 1) err = true;    tclrand();    if(tcdrand() >= 1.0) err = true;    tcdrandnd(50, 10);    if(tcstricmp("abc", "ABC") != 0) err = true;    if(!tcstrfwm("abc", "ab") || !tcstrifwm("abc", "AB")) err = true;    if(!tcstrbwm("abc", "bc") || !tcstribwm("abc", "BC")) err = true;    if(tcstrdist("abcde", "abdfgh") != 4 || tcstrdist("abdfgh", "abcde") != 4) err = true;    if(tcstrdistutf("abcde", "abdfgh") != 4 || tcstrdistutf("abdfgh", "abcde") != 4) err = true;    buf = tcmemdup("abcde", 5);    tcstrtoupper(buf);    if(strcmp(buf, "ABCDE")) err = true;    tcstrtolower(buf);    if(strcmp(buf, "abcde")) err = true;    tcfree(buf);    buf = tcmemdup("  ab  cd  ", 10);    tcstrtrim(buf);    if(strcmp(buf, "ab  cd")) err = true;    tcstrsqzspc(buf);    if(strcmp(buf, "ab cd")) err = true;    tcstrsubchr(buf, "cd", "C");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -