📄 tcbtest.c
字号:
} else { char *vbuf = tcbdbget(bdb, kbuf, ksiz, &vsiz); if(!vbuf && !(rnd && tcbdbecode(bdb) == TCENOREC)){ eprint(bdb, "tcbdbget"); err = true; break; } tcfree(vbuf); } if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } iprintf("record number: %llu\n", (unsigned long long)tcbdbrnum(bdb)); iprintf("size: %llu\n", (unsigned long long)tcbdbfsiz(bdb)); mprint(bdb); sysprint(); if(!tcbdbclose(bdb)){ eprint(bdb, "tcbdbclose"); err = true; } tcbdbdel(bdb); iprintf("time: %.3f\n", tctime() - stime); iprintf("%s\n\n", err ? "error" : "ok"); return err ? 1 : 0;}/* perform remove command */static int procremove(const char *path, bool mt, TCCMP cmp, int lcnum, int ncnum, int xmsiz, int omode, bool rnd){ iprintf("<Removing Test>\n seed=%u path=%s mt=%d cmp=%p lcnum=%d ncnum=%d xmsiz=%d" " omode=%d rnd=%d\n\n", g_randseed, path, mt, (void *)(intptr_t)cmp, lcnum, ncnum, xmsiz, omode, rnd); bool err = false; double stime = tctime(); TCBDB *bdb = tcbdbnew(); if(g_dbgfd >= 0) tcbdbsetdbgfd(bdb, g_dbgfd); if(mt && !tcbdbsetmutex(bdb)){ eprint(bdb, "tcbdbsetmutex"); err = true; } if(cmp && !tcbdbsetcmpfunc(bdb, cmp, NULL)){ eprint(bdb, "tcbdbsetcmpfunc"); err = true; } if(!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)){ eprint(bdb, "tcbdbsetcodecfunc"); err = true; } if(!tcbdbsetcache(bdb, lcnum, ncnum)){ eprint(bdb, "tcbdbsetcache"); err = true; } if(xmsiz >= 0 && !tcbdbsetxmsiz(bdb, xmsiz)){ eprint(bdb, "tcbdbsetxmsiz"); err = true; } if(!tcbdbopen(bdb, path, BDBOWRITER | omode)){ eprint(bdb, "tcbdbopen"); err = true; } int rnum = tcbdbrnum(bdb); for(int i = 1; i <= rnum; i++){ char kbuf[RECBUFSIZ]; int ksiz; if(cmp == tccmpdecimal){ ksiz = sprintf(kbuf, "%d", rnd ? myrand(rnum) + 1 : i); } else if(cmp == tccmpint32){ int32_t lnum = rnd ? myrand(rnum) + 1 : i; memcpy(kbuf, &lnum, sizeof(lnum)); ksiz = sizeof(lnum); } else if(cmp == tccmpint64){ int64_t llnum = rnd ? myrand(rnum) + 1 : i; memcpy(kbuf, &llnum, sizeof(llnum)); ksiz = sizeof(llnum); } else { ksiz = sprintf(kbuf, "%08d", rnd ? myrand(rnum) + 1 : i); } if(!tcbdbout(bdb, kbuf, ksiz) && !(rnd && tcbdbecode(bdb) == TCENOREC)){ eprint(bdb, "tcbdbout"); err = true; break; } if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } iprintf("record number: %llu\n", (unsigned long long)tcbdbrnum(bdb)); iprintf("size: %llu\n", (unsigned long long)tcbdbfsiz(bdb)); mprint(bdb); sysprint(); if(!tcbdbclose(bdb)){ eprint(bdb, "tcbdbclose"); err = true; } tcbdbdel(bdb); iprintf("time: %.3f\n", tctime() - stime); iprintf("%s\n\n", err ? "error" : "ok"); return err ? 1 : 0;}/* perform rcat command */static int procrcat(const char *path, int rnum, int lmemb, int nmemb, int bnum, int apow, int fpow, bool mt, TCCMP cmp, int opts, int lcnum, int ncnum, int xmsiz, int lsmax, int capnum, int omode, int pnum, bool dai, bool dad, bool rl, bool ru){ iprintf("<Random Concatenating Test>\n" " seed=%u path=%s rnum=%d lmemb=%d nmemb=%d bnum=%d apow=%d fpow=%d" " mt=%d cmp=%p opts=%d lcnum=%d ncnum=%d xmsiz=%d lsmax=%d capnum=%d" " omode=%d pnum=%d dai=%d dad=%d rl=%d ru=%d\n\n", g_randseed, path, rnum, lmemb, nmemb, bnum, apow, fpow, mt, (void *)(intptr_t)cmp, opts, lcnum, ncnum, xmsiz, lsmax, capnum, omode, pnum, dai, dad, rl, ru); if(pnum < 1) pnum = rnum; bool err = false; double stime = tctime(); TCBDB *bdb = tcbdbnew(); if(g_dbgfd >= 0) tcbdbsetdbgfd(bdb, g_dbgfd); if(mt && !tcbdbsetmutex(bdb)){ eprint(bdb, "tcbdbsetmutex"); err = true; } if(cmp && !tcbdbsetcmpfunc(bdb, cmp, NULL)){ eprint(bdb, "tcbdbsetcmpfunc"); err = true; } if(!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)){ eprint(bdb, "tcbdbsetcodecfunc"); err = true; } if(!tcbdbtune(bdb, lmemb, nmemb, bnum, apow, fpow, opts)){ eprint(bdb, "tcbdbtune"); err = true; } if(!tcbdbsetcache(bdb, lcnum, ncnum)){ eprint(bdb, "tcbdbsetcache"); err = true; } if(xmsiz >= 0 && !tcbdbsetxmsiz(bdb, xmsiz)){ eprint(bdb, "tcbdbsetxmsiz"); err = true; } if(!tcbdbsetlsmax(bdb, lsmax)){ eprint(bdb, "tcbdbsetlsmax"); err = true; } if(!tcbdbsetcapnum(bdb, capnum)){ eprint(bdb, "tcbdbsetcapnum"); err = true; } if(!tcbdbopen(bdb, path, BDBOWRITER | BDBOCREAT | BDBOTRUNC | omode)){ eprint(bdb, "tcbdbopen"); err = true; } for(int i = 1; i <= rnum; i++){ if(ru){ char fmt[RECBUFSIZ]; sprintf(fmt, "%%0%dd", myrand(RECBUFSIZ)); char kbuf[RECBUFSIZ]; int ksiz = sprintf(kbuf, fmt, myrand(pnum)); switch(myrand(10)){ case 0: if(!tcbdbput(bdb, kbuf, ksiz, kbuf, ksiz)){ eprint(bdb, "tcbdbput"); err = true; } break; case 1: if(!tcbdbputkeep(bdb, kbuf, ksiz, kbuf, ksiz) && tcbdbecode(bdb) != TCEKEEP){ eprint(bdb, "tcbdbputkeep"); err = true; } break; case 2: if(!tcbdbputdup(bdb, kbuf, ksiz, kbuf, ksiz) && tcbdbecode(bdb) != TCEKEEP){ eprint(bdb, "tcbdbputkeep"); err = true; } break; case 3: if(!tcbdbputdupback(bdb, kbuf, ksiz, kbuf, ksiz) && tcbdbecode(bdb) != TCEKEEP){ eprint(bdb, "tcbdbputkeep"); err = true; } break; case 4: if(!tcbdbout(bdb, kbuf, ksiz) && tcbdbecode(bdb) != TCENOREC){ eprint(bdb, "tcbdbout"); err = true; } break; case 5: if(tcbdbaddint(bdb, kbuf, ksiz, 1) == INT_MIN && tcbdbecode(bdb) != TCEKEEP){ eprint(bdb, "tcbdbaddint"); err = true; } break; case 6: if(isnan(tcbdbadddouble(bdb, kbuf, ksiz, 1.0)) && tcbdbecode(bdb) != TCEKEEP){ eprint(bdb, "tcbdbadddouble"); err = true; } break; case 7: if(myrand(2) == 0){ if(!tcbdbputproc(bdb, kbuf, ksiz, kbuf, ksiz, pdprocfunc, NULL) && tcbdbecode(bdb) != TCEKEEP){ eprint(bdb, "tcbdbputproc"); err = true; } } else { if(!tcbdbputproc(bdb, kbuf, ksiz, NULL, 0, pdprocfunc, NULL) && tcbdbecode(bdb) != TCEKEEP && tcbdbecode(bdb) != TCENOREC){ eprint(bdb, "tcbdbputproc"); err = true; } } break; default: if(!tcbdbputcat(bdb, kbuf, ksiz, kbuf, ksiz)){ eprint(bdb, "tcbdbputcat"); err = true; } break; } if(err) break; } else { char kbuf[RECBUFSIZ]; int ksiz; if(cmp == tccmpdecimal){ ksiz = sprintf(kbuf, "%d", myrand(pnum)); } else if(cmp == tccmpint32){ int32_t lnum = myrand(pnum); memcpy(kbuf, &lnum, sizeof(lnum)); ksiz = sizeof(lnum); } else if(cmp == tccmpint64){ int64_t llnum = myrand(pnum); memcpy(kbuf, &llnum, sizeof(llnum)); ksiz = sizeof(llnum); } else { ksiz = sprintf(kbuf, "%d", myrand(pnum)); } if(dai){ if(tcbdbaddint(bdb, kbuf, ksiz, myrand(3)) == INT_MIN){ eprint(bdb, "tcbdbaddint"); err = true; break; } } else if(dad){ if(isnan(tcbdbadddouble(bdb, kbuf, ksiz, myrand(30) / 10.0))){ eprint(bdb, "tcbdbadddouble"); err = true; break; } } else if(rl){ char vbuf[PATH_MAX]; int vsiz = myrand(PATH_MAX); for(int j = 0; j < vsiz; j++){ vbuf[j] = myrand(0x100); } if(!tcbdbputcat(bdb, kbuf, ksiz, vbuf, vsiz)){ eprint(bdb, "tcbdbputcat"); err = true; break; } } else { if(!tcbdbputcat(bdb, kbuf, ksiz, kbuf, ksiz)){ eprint(bdb, "tcbdbputcat"); err = true; break; } } } if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } iprintf("record number: %llu\n", (unsigned long long)tcbdbrnum(bdb)); iprintf("size: %llu\n", (unsigned long long)tcbdbfsiz(bdb)); mprint(bdb); sysprint(); if(!tcbdbclose(bdb)){ eprint(bdb, "tcbdbclose"); err = true; } tcbdbdel(bdb); iprintf("time: %.3f\n", tctime() - stime); iprintf("%s\n\n", err ? "error" : "ok"); return err ? 1 : 0;}/* perform queue command */static int procqueue(const char *path, int rnum, int lmemb, int nmemb, int bnum, int apow, int fpow, bool mt, TCCMP cmp, int opts, int lcnum, int ncnum, int xmsiz, int lsmax, int capnum, int omode){ iprintf("<Queueing Test>\n seed=%u path=%s rnum=%d lmemb=%d nmemb=%d bnum=%d apow=%d" " fpow=%d mt=%d cmp=%p opts=%d lcnum=%d ncnum=%d xmsiz=%d lsmax=%d capnum=%d" " omode=%d\n\n", g_randseed, path, rnum, lmemb, nmemb, bnum, apow, fpow, mt, (void *)(intptr_t)cmp, opts, lcnum, ncnum, xmsiz, lsmax, capnum, omode); bool err = false; double stime = tctime(); TCBDB *bdb = tcbdbnew(); if(g_dbgfd >= 0) tcbdbsetdbgfd(bdb, g_dbgfd); if(mt && !tcbdbsetmutex(bdb)){ eprint(bdb, "tcbdbsetmutex"); err = true; } if(cmp && !tcbdbsetcmpfunc(bdb, cmp, NULL)){ eprint(bdb, "tcbdbsetcmpfunc"); err = true; } if(!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)){ eprint(bdb, "tcbdbsetcodecfunc"); err = true; } if(!tcbdbtune(bdb, lmemb, nmemb, bnum, apow, fpow, opts)){ eprint(bdb, "tcbdbtune"); err = true; } if(!tcbdbsetcache(bdb, lcnum, ncnum)){ eprint(bdb, "tcbdbsetcache"); err = true; } if(xmsiz >= 0 && !tcbdbsetxmsiz(bdb, xmsiz)){ eprint(bdb, "tcbdbsetxmsiz"); err = true; } if(!tcbdbsetlsmax(bdb, lsmax)){ eprint(bdb, "tcbdbsetlsmax"); err = true; } if(!tcbdbsetcapnum(bdb, capnum)){ eprint(bdb, "tcbdbsetcapnum"); err = true; } if(!tcbdbopen(bdb, path, BDBOWRITER | BDBOCREAT | BDBOTRUNC)){ eprint(bdb, "tcbdbopen"); err = true; } int deqfreq = (lmemb > 0) ? lmemb * 10 : 256; BDBCUR *cur = tcbdbcurnew(bdb); for(int i = 1; i <= rnum; i++){ char buf[RECBUFSIZ]; int len; if(cmp == tccmpdecimal){ len = sprintf(buf, "%d", i); } else if(cmp == tccmpint32){ int32_t lnum = i; memcpy(buf, &lnum, sizeof(lnum)); len = sizeof(lnum); } else if(cmp == tccmpint64){ int64_t llnum = i; memcpy(buf, &llnum, sizeof(llnum)); len = sizeof(llnum); } else { len = sprintf(buf, "%08d", i); } if(!tcbdbput(bdb, buf, len, buf, len)){ eprint(bdb, "tcbdbput"); err = true; break; } if(myrand(deqfreq) == 0){ if(!tcbdbcurfirst(cur) && tcbdbecode(bdb) != TCENOREC){ eprint(bdb, "tcbdbcurfirst"); err = true; break; } int num = myrand(deqfreq * 2 + 1); while(num >= 0){ if(tcbdbcurout(cur)){ num--; } else { if(tcbdbecode(bdb) != TCENOREC){ eprint(bdb, "tcbdbcurout"); err = true; } break; } } if(err) break; } if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } if(!tcbdbcurfirst(cur) && tcbdbecode(bdb) != TCENOREC){ eprint(bdb, "tcbdbcurfirst"); err = true; } while(true){ if(tcbdbcurout(cur)) continue; if(tcbdbecode(bdb) != TCENOREC){ eprint(bdb, "tcbdbcurout"); err = true; } break; } tcbdbcurdel(cur); iprintf("record number: %llu\n", (unsigned long long)tcbdbrnum(bdb)); iprintf("size: %llu\n", (unsigned long long)tcbdbfsiz(bdb)); mprint(bdb); sysprint(); if(!tcbdbclose(bdb)){ eprint(bdb, "tcbdbclose"); err = true; } tcbdbdel(bdb); iprintf("time: %.3f\n", tctime() - stime); iprintf("%s\n\n", err ? "error" : "ok"); return err ? 1 : 0;}/* perform misc command */static int procmisc(const char *path, int rnum, bool mt, int opts, int omode){ iprintf("<Miscellaneous Test>\n seed=%u path=%s rnum=%d mt=%d opts=%d omode=%d\n\n", g_randseed, path, rnum, mt, opts, omode); bool err = false; double stime = tctime(); TCBDB *bdb = tcbdbnew(); if(g_dbgfd >= 0) tcbdbsetdbgfd(bdb, g_dbgfd); if(mt && !tcbdbsetmutex(bdb)){ eprint(bdb, "tcbdbsetmutex"); err = true; } if(!tcbdbsetcodecfunc(bdb, _tc_recencode, NULL, _tc_recdecode, NULL)){ eprint(bdb, "tcbdbsetcodecfunc"); err = true; } if(!tcbdbtune(bdb, 10, 10, rnum / 50, 100, -1, opts)){ eprint(bdb, "tcbdbtune"); err = true; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -