📄 tchtest.c
字号:
iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } iprintf("record number: %llu\n", (unsigned long long)tchdbrnum(hdb)); iprintf("size: %llu\n", (unsigned long long)tchdbfsiz(hdb)); mprint(hdb); if(!tchdbclose(hdb)){ eprint(hdb, "tchdbclose"); err = true; } tchdbdel(hdb); iprintf("time: %.3f\n", tctime() - stime); iprintf("%s\n\n", err ? "error" : "ok"); return err ? 1 : 0;}/* perform read command */static int procread(const char *path, bool mt, int rcnum, int xmsiz, int omode, bool wb, bool rnd){ iprintf("<Reading Test>\n path=%s mt=%d rcnum=%d xmsiz=%d omode=%d wb=%d rnd=%d\n\n", path, mt, rcnum, xmsiz, omode, wb, rnd); bool err = false; double stime = tctime(); TCHDB *hdb = tchdbnew(); if(g_dbgfd >= 0) tchdbsetdbgfd(hdb, g_dbgfd); if(mt && !tchdbsetmutex(hdb)){ eprint(hdb, "tchdbsetmutex"); err = true; } if(!tchdbsetcodecfunc(hdb, _tc_recencode, NULL, _tc_recdecode, NULL)){ eprint(hdb, "tchdbsetcodecfunc"); err = true; } if(!tchdbsetcache(hdb, rcnum)){ eprint(hdb, "tchdbsetcache"); err = true; } if(xmsiz >= 0 && !tchdbsetxmsiz(hdb, xmsiz)){ eprint(hdb, "tchdbsetxmsiz"); err = true; } if(!tchdbopen(hdb, path, HDBOREADER | omode)){ eprint(hdb, "tchdbopen"); err = true; } int rnum = tchdbrnum(hdb); for(int i = 1; i <= rnum; i++){ char kbuf[RECBUFSIZ]; int ksiz = sprintf(kbuf, "%08d", rnd ? myrand(rnum) + 1 : i); int vsiz; if(wb){ char vbuf[RECBUFSIZ]; int vsiz = tchdbget3(hdb, kbuf, ksiz, vbuf, RECBUFSIZ); if(vsiz < 0 && !(rnd && tchdbecode(hdb) == TCENOREC)){ eprint(hdb, "tchdbget3"); err = true; break; } } else { char *vbuf = tchdbget(hdb, kbuf, ksiz, &vsiz); if(!vbuf && !(rnd && tchdbecode(hdb) == TCENOREC)){ eprint(hdb, "tchdbget"); 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)tchdbrnum(hdb)); iprintf("size: %llu\n", (unsigned long long)tchdbfsiz(hdb)); mprint(hdb); if(!tchdbclose(hdb)){ eprint(hdb, "tchdbclose"); err = true; } tchdbdel(hdb); 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, int rcnum, int xmsiz, int omode, bool rnd){ iprintf("<Removing Test>\n path=%s mt=%d rcnum=%d xmsiz=%d omode=%d rnd=%d\n\n", path, mt, rcnum, xmsiz, omode, rnd); bool err = false; double stime = tctime(); TCHDB *hdb = tchdbnew(); if(g_dbgfd >= 0) tchdbsetdbgfd(hdb, g_dbgfd); if(mt && !tchdbsetmutex(hdb)){ eprint(hdb, "tchdbsetmutex"); err = true; } if(!tchdbsetcodecfunc(hdb, _tc_recencode, NULL, _tc_recdecode, NULL)){ eprint(hdb, "tchdbsetcodecfunc"); err = true; } if(!tchdbsetcache(hdb, rcnum)){ eprint(hdb, "tchdbsetcache"); err = true; } if(xmsiz >= 0 && !tchdbsetxmsiz(hdb, xmsiz)){ eprint(hdb, "tchdbsetxmsiz"); err = true; } if(!tchdbopen(hdb, path, HDBOWRITER | omode)){ eprint(hdb, "tchdbopen"); err = true; } int rnum = tchdbrnum(hdb); for(int i = 1; i <= rnum; i++){ char kbuf[RECBUFSIZ]; int ksiz = sprintf(kbuf, "%08d", rnd ? myrand(rnum) + 1 : i); if(!tchdbout(hdb, kbuf, ksiz) && !(rnd && tchdbecode(hdb) == TCENOREC)){ eprint(hdb, "tchdbout"); 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)tchdbrnum(hdb)); iprintf("size: %llu\n", (unsigned long long)tchdbfsiz(hdb)); mprint(hdb); if(!tchdbclose(hdb)){ eprint(hdb, "tchdbclose"); err = true; } tchdbdel(hdb); 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 bnum, int apow, int fpow, bool mt, int opts, int rcnum, int xmsiz, int omode, int pnum, bool dai, bool dad, bool rl){ iprintf("<Random Concatenating Test>\n" " path=%s rnum=%d bnum=%d apow=%d fpow=%d mt=%d opts=%d rcnum=%d xmsiz=%d" " omode=%d pnum=%d dai=%d dad=%d rl=%d\n\n", path, rnum, bnum, apow, fpow, mt, opts, rcnum, xmsiz, omode, pnum, dai, dad, rl); if(pnum < 1) pnum = rnum; bool err = false; double stime = tctime(); TCHDB *hdb = tchdbnew(); if(g_dbgfd >= 0) tchdbsetdbgfd(hdb, g_dbgfd); if(mt && !tchdbsetmutex(hdb)){ eprint(hdb, "tchdbsetmutex"); err = true; } if(!tchdbsetcodecfunc(hdb, _tc_recencode, NULL, _tc_recdecode, NULL)){ eprint(hdb, "tchdbsetcodecfunc"); err = true; } if(!tchdbtune(hdb, bnum, apow, fpow, opts)){ eprint(hdb, "tchdbtune"); err = true; } if(!tchdbsetcache(hdb, rcnum)){ eprint(hdb, "tchdbsetcache"); err = true; } if(xmsiz >= 0 && !tchdbsetxmsiz(hdb, xmsiz)){ eprint(hdb, "tchdbsetxmsiz"); err = true; } if(!tchdbopen(hdb, path, HDBOWRITER | HDBOCREAT | HDBOTRUNC | omode)){ eprint(hdb, "tchdbopen"); err = true; } for(int i = 1; i <= rnum; i++){ char kbuf[RECBUFSIZ]; int ksiz = sprintf(kbuf, "%d", myrand(pnum)); if(dai){ if(tchdbaddint(hdb, kbuf, ksiz, myrand(3)) == INT_MIN){ eprint(hdb, "tchdbaddint"); err = true; break; } } else if(dad){ if(isnan(tchdbadddouble(hdb, kbuf, ksiz, myrand(3)))){ eprint(hdb, "tchdbadddouble"); 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(!tchdbputcat(hdb, kbuf, ksiz, vbuf, vsiz)){ eprint(hdb, "tchdbputcat"); err = true; break; } } else { if(!tchdbputcat(hdb, kbuf, ksiz, kbuf, ksiz)){ eprint(hdb, "tchdbputcat"); 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)tchdbrnum(hdb)); iprintf("size: %llu\n", (unsigned long long)tchdbfsiz(hdb)); mprint(hdb); if(!tchdbclose(hdb)){ eprint(hdb, "tchdbclose"); err = true; } tchdbdel(hdb); 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 path=%s rnum=%d mt=%d opts=%d omode=%d\n\n", path, rnum, mt, opts, omode); bool err = false; double stime = tctime(); TCHDB *hdb = tchdbnew(); if(g_dbgfd >= 0) tchdbsetdbgfd(hdb, g_dbgfd); if(mt && !tchdbsetmutex(hdb)){ eprint(hdb, "tchdbsetmutex"); err = true; } if(!tchdbsetcodecfunc(hdb, _tc_recencode, NULL, _tc_recdecode, NULL)){ eprint(hdb, "tchdbsetcodecfunc"); err = true; } if(!tchdbtune(hdb, rnum / 50, 2, -1, opts)){ eprint(hdb, "tchdbtune"); err = true; } if(!tchdbsetcache(hdb, rnum / 10)){ eprint(hdb, "tchdbsetcache"); err = true; } if(!tchdbsetxmsiz(hdb, rnum * sizeof(int))){ eprint(hdb, "tchdbsetxmsiz"); err = true; } if(!tchdbopen(hdb, path, HDBOWRITER | HDBOCREAT | HDBOTRUNC | omode)){ eprint(hdb, "tchdbopen"); err = true; } iprintf("writing:\n"); for(int i = 1; i <= rnum; i++){ char buf[RECBUFSIZ]; int len = sprintf(buf, "%08d", i); if(i % 3 == 0){ if(!tchdbputkeep(hdb, buf, len, buf, len)){ eprint(hdb, "tchdbputkeep"); err = true; break; } } else { if(!tchdbputasync(hdb, buf, len, buf, len)){ eprint(hdb, "tchdbputasync"); err = true; break; } } if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } iprintf("reading:\n"); for(int i = 1; i <= rnum; i++){ char kbuf[RECBUFSIZ]; int ksiz = sprintf(kbuf, "%08d", i); int vsiz; char *vbuf = tchdbget(hdb, kbuf, ksiz, &vsiz); if(!vbuf){ eprint(hdb, "tchdbget"); err = true; break; } else if(vsiz != ksiz || memcmp(vbuf, kbuf, vsiz)){ eprint(hdb, "(validation)"); err = true; tcfree(vbuf); break; } tcfree(vbuf); if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } if(tchdbrnum(hdb) != rnum){ eprint(hdb, "(validation)"); err = true; } iprintf("random writing:\n"); for(int i = 1; i <= rnum; i++){ char kbuf[RECBUFSIZ]; int ksiz = sprintf(kbuf, "%d", myrand(rnum)); char vbuf[RECBUFSIZ]; int vsiz = myrand(RECBUFSIZ); memset(vbuf, '*', vsiz); if(!tchdbput(hdb, kbuf, ksiz, vbuf, vsiz)){ eprint(hdb, "tchdbput"); err = true; break; } int rsiz; char *rbuf = tchdbget(hdb, kbuf, ksiz, &rsiz); if(!rbuf){ eprint(hdb, "tchdbget"); err = true; break; } if(rsiz != vsiz || memcmp(rbuf, vbuf, rsiz)){ eprint(hdb, "(validation)"); err = true; tcfree(rbuf); break; } if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } tcfree(rbuf); } iprintf("word writing:\n"); const char *words[] = { "a", "A", "bb", "BB", "ccc", "CCC", "dddd", "DDDD", "eeeee", "EEEEEE", "mikio", "hirabayashi", "tokyo", "cabinet", "hyper", "estraier", "19780211", "birth day", "one", "first", "two", "second", "three", "third", "four", "fourth", "five", "fifth", "_[1]_", "uno", "_[2]_", "dos", "_[3]_", "tres", "_[4]_", "cuatro", "_[5]_", "cinco", "[\xe5\xb9\xb3\xe6\x9e\x97\xe5\xb9\xb9\xe9\x9b\x84]", "[\xe9\xa6\xac\xe9\xb9\xbf]", NULL }; for(int i = 0; words[i] != NULL; i += 2){ const char *kbuf = words[i]; int ksiz = strlen(kbuf); const char *vbuf = words[i+1]; int vsiz = strlen(vbuf); if(!tchdbputkeep(hdb, kbuf, ksiz, vbuf, vsiz)){ eprint(hdb, "tchdbputkeep"); err = true; break; } if(rnum > 250) iputchar('.'); } if(rnum > 250) iprintf(" (%08d)\n", sizeof(words) / sizeof(*words)); iprintf("random erasing:\n"); for(int i = 1; i <= rnum; i++){ char kbuf[RECBUFSIZ]; int ksiz = sprintf(kbuf, "%d", myrand(rnum)); if(!tchdbout(hdb, kbuf, ksiz) && tchdbecode(hdb) != TCENOREC){ eprint(hdb, "tchdbout"); err = true; break; } if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } iprintf("writing:\n"); for(int i = 1; i <= rnum; i++){ char kbuf[RECBUFSIZ]; int ksiz = sprintf(kbuf, "[%d]", i); char vbuf[RECBUFSIZ]; int vsiz = i % RECBUFSIZ; memset(vbuf, '*', vsiz); if(!tchdbputkeep(hdb, kbuf, ksiz, vbuf, vsiz)){ eprint(hdb, "tchdbputkeep"); err = true; break; } if(vsiz < 1){ char tbuf[PATH_MAX]; for(int j = 0; j < PATH_MAX; j++){ tbuf[j] = myrand(0x100); } if(!tchdbput(hdb, kbuf, ksiz, tbuf, PATH_MAX)){ eprint(hdb, "tchdbput"); err = true; break; } } if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } iprintf("erasing:\n"); for(int i = 1; i <= rnum; i++){ if(i % 2 == 1){ char kbuf[RECBUFSIZ]; int ksiz = sprintf(kbuf, "[%d]", i); if(!tchdbout(hdb, kbuf, ksiz)){ eprint(hdb, "tchdbout"); err = true; break; } if(tchdbout(hdb, kbuf, ksiz) || tchdbecode(hdb) != TCENOREC){ eprint(hdb, "tchdbout"); err = true; break; } } if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } iprintf("random writing and reopening:\n"); for(int i = 1; i <= rnum; i++){ if(myrand(10) == 0){ int ksiz, vsiz; char *kbuf, *vbuf; ksiz = (myrand(5) == 0) ? myrand(UINT16_MAX) : myrand(RECBUFSIZ); kbuf = tcmalloc(ksiz + 1); memset(kbuf, '@', ksiz); vsiz = (myrand(5) == 0) ? myrand(UINT16_MAX) : myrand(RECBUFSIZ); vbuf = tcmalloc(vsiz + 1); for(int j = 0; j < vsiz; j++){ vbuf[j] = myrand(256); } switch(myrand(4)){ case 0: if(!tchdbput(hdb, kbuf, ksiz, vbuf, vsiz)){ eprint(hdb, "tchdbput"); err = true; } break; case 1: if(!tchdbputcat(hdb, kbuf, ksiz, vbuf, vsiz)){ eprint(hdb, "tchdbputcat"); err = true; } break; case 2: if(!tchdbputasync(hdb, kbuf, ksiz, vbuf, vsiz)){ eprint(hdb, "tchdbputasync"); err = true; } break; case 3: if(!tchdbout(hdb, kbuf, ksiz) && tchdbecode(hdb) != TCENOREC){ eprint(hdb, "tchdbout"); err = true; } break; } tcfree(vbuf); tcfree(kbuf); } else { char kbuf[RECBUFSIZ]; int ksiz = myrand(RECBUFSIZ); memset(kbuf, '@', ksiz); char vbuf[RECBUFSIZ]; int vsiz = myrand(RECBUFSIZ); memset(vbuf, '@', vsiz); if(!tchdbput(hdb, kbuf, ksiz, vbuf, vsiz)){ eprint(hdb, "tchdbputcat"); err = true; break; } } if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.'); if(i == rnum || i % (rnum / 10) == 0) iprintf(" (%08d)\n", i); } } if(!tchdbclose(hdb)){ eprint(hdb, "tchdbclose"); err = true; } if(!tchdbopen(hdb, path, HDBOWRITER | omode)){ eprint(hdb, "tchdbopen"); err = true; } iprintf("checking:\n"); for(int i = 1; i <= rnum; i++){ char kbuf[RECBUFSIZ]; int ksiz = sprintf(kbuf, "[%d]", i); int vsiz; char *vbuf = tchdbget(hdb, kbuf, ksiz, &vsiz); if(i % 2 == 0){ if(!vbuf){ eprint(hdb, "tchdbget"); err = true; break; } if(vsiz != i % RECBUFSIZ && vsiz != PATH_MAX){ eprint(hdb, "(validation)"); err = true; tcfree(vbuf); break; } } else { if(vbuf || tchdbecode(hdb) != TCENOREC){ eprint(hdb, "(validation)"); err = true; tcfree(vbuf); break; } } tcfree(vbuf); if(rnum > 250 && i % (rnum / 250) == 0){ iputchar('.');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -