📄 cbtest.c
字号:
/* perform list command */int dolist(int rnum, int disp){ CBLIST *list; int i, err, len; char buf[RECBUFSIZ]; const char *val; if(!disp) prinfflush("<List Writing Test>\n rnum=%d disp=%d\n\n", rnum, disp); list = cblistopen(); err = FALSE; for(i = 1; i <= rnum; i++){ len = sprintf(buf, "%08d", i); cblistpush(list, buf, len); if(!disp && rnum > 250 && i % (rnum / 250) == 0){ putchar('.'); fflush(stdout); if(i == rnum || i % (rnum / 10) == 0){ prinfflush(" (%08d)\n", i); } } } if(disp){ for(i = 0; i < cblistnum(list); i++){ if((val = cblistval(list, i, NULL)) != NULL){ prinfflush("%s\n", val); } else { fprintf(stderr, "%s: val error\n", progname); err = TRUE; break; } } } cblistclose(list); if(!disp && !err) prinfflush("ok\n\n"); return err ? 1 : 0;}/* perform list command */int domap(int rnum, int disp){ CBMAP *map; int i, err, len; char buf[RECBUFSIZ]; const char *val; if(!disp) prinfflush("<Map Writing Test>\n rnum=%d disp=%d\n\n", rnum, disp); map = cbmapopen(); err = FALSE; for(i = 1; i <= rnum; i++){ len = sprintf(buf, "%08d", i); if(!cbmapput(map, buf, len, buf, len, FALSE)){ fprintf(stderr, "%s: put error\n", progname); err = TRUE; break; } if(!disp && rnum > 250 && i % (rnum / 250) == 0){ putchar('.'); fflush(stdout); if(i == rnum || i % (rnum / 10) == 0){ prinfflush(" (%08d)\n", i); } } } if(disp){ for(i = 1; i <= rnum; i++){ len = sprintf(buf, "%08d", i); if((val = cbmapget(map, buf, len, NULL)) != NULL){ prinfflush("%s\n", val); } else { fprintf(stderr, "%s: get error\n", progname); } } } cbmapclose(map); if(!disp && !err) prinfflush("ok\n\n"); return err ? 1 : 0;}/* perform wicked command */int dowicked(int rnum){ CBLIST *list; CBMAP *map; int i, len; char buf[RECBUFSIZ]; prinfflush("<Wicked Writing Test>\n rnum=%d\n\n", rnum); list = cblistopen(); for(i = 1; i <= rnum; i++){ len = sprintf(buf, "%08d", myrand() % rnum + 1); switch(myrand() % 16){ case 0: free(cblistpop(list, NULL)); putchar('O'); break; case 1: cblistunshift(list, buf, len); putchar('U'); break; case 2: free(cblistshift(list, NULL)); putchar('S'); break; case 3: cblistinsert(list, myrand() % (cblistnum(list) + 1), buf, len); putchar('I'); break; case 4: free(cblistremove(list, myrand() % (cblistnum(list) + 1), NULL)); putchar('R'); break; default: cblistpush(list, buf, len); putchar('P'); break; } if(i % 50 == 0) prinfflush(" (%08d)\n", i); } cblistclose(list); map = cbmapopen(); for(i = 1; i <= rnum; i++){ len = sprintf(buf, "%08d", myrand() % rnum + 1); switch(myrand() % 16){ case 0: cbmapput(map, buf, len, buf, len, FALSE); putchar('I'); break; case 1: cbmapget(map, buf, len, NULL); putchar('V'); break; case 2: cbmapout(map, buf, len); putchar('D'); break; case 3: cbmapmove(map, buf, len, myrand() % 2); putchar('M'); break; default: cbmapput(map, buf, len, buf, len, TRUE); putchar('O'); break; } if(i % 50 == 0) prinfflush(" (%08d)\n", i); } cbmapclose(map); prinfflush("ok\n\n"); return 0;}/* perform misc command */int domisc(void){ CBLIST *olist, *nlist, *elems, *glist; CBMAP *omap, *nmap, *pairs, *gmap; int i, j, ssiz, osiz; char kbuf[RECBUFSIZ], vbuf[RECBUFSIZ], *sbuf, spbuf[1024], *tmp, *orig; const char *op, *np; prinfflush("<Miscellaneous Test>\n\n"); prinfflush("Checking serialization of list ... "); olist = cblistopen(); for(i = 1; i <= 1000; i++){ sprintf(vbuf, "%d", i); cblistpush(olist, vbuf, -1); } sbuf = cblistdump(olist, &ssiz); nlist = cblistload(sbuf, ssiz); free(sbuf); for(i = 0; i < cblistnum(olist); i++){ op = cblistval(olist, i, NULL); np = cblistval(nlist, i, NULL); if(!op || !np || strcmp(op, np)){ cblistclose(nlist); cblistclose(olist); fprintf(stderr, "%s: validation failed\n", progname); return 1; } } cblistclose(nlist); cblistclose(olist); prinfflush("ok\n"); prinfflush("Checking serialization of map ... "); omap = cbmapopen(); for(i = 1; i <= 1000; i++){ sprintf(kbuf, "%X", i); sprintf(vbuf, "[%d]", i); } sbuf = cbmapdump(omap, &ssiz); nmap = cbmapload(sbuf, ssiz); free(sbuf); cbmapiterinit(omap); while((op = cbmapiternext(omap, NULL)) != NULL){ if(!(np = cbmapget(nmap, op, -1, NULL))){ cbmapclose(nmap); cbmapclose(omap); fprintf(stderr, "%s: validation failed\n", progname); return 1; } } cbmapclose(nmap); cbmapclose(omap); prinfflush("ok\n"); prinfflush("Checking string utilities ... "); sprintf(spbuf, "[%08d/%08o/%08u/%08x/%08X/%08.3e/%08.3E/%08.3f/%08.3g/%08.3G/%c/%s/%%]", 123456, 123456, 123456, 123456, 123456, 123456.789, 123456.789, 123456.789, 123456.789, 123456.789, 'A', "hoge"); tmp = cbsprintf("[%08d/%08o/%08u/%08x/%08X/%08.3e/%08.3E/%08.3f/%08.3g/%08.3G/%c/%s/%%]", 123456, 123456, 123456, 123456, 123456, 123456.789, 123456.789, 123456.789, 123456.789, 123456.789, 'A', "hoge"); while(strcmp(spbuf, tmp)){ free(tmp); fprintf(stderr, "%s: cbsprintf is invalid\n", progname); return 1; } free(tmp); pairs = cbmapopen(); cbmapput(pairs, "aa", -1, "AAA", -1, TRUE); cbmapput(pairs, "bb", -1, "BBB", -1, TRUE); cbmapput(pairs, "cc", -1, "CCC", -1, TRUE); cbmapput(pairs, "ZZZ", -1, "z", -1, TRUE); tmp = cbreplace("[aaaaabbbbbcccccdddddZZZZ]", pairs); if(strcmp(tmp, "[AAAAAAaBBBBBBbCCCCCCcdddddzZ]")){ free(tmp); cbmapclose(pairs); fprintf(stderr, "%s: cbreplace is invalid\n", progname); return 1; } free(tmp); cbmapclose(pairs); elems = cbsplit("aa bb,ccc-dd,", -1, " ,-"); if(cblistnum(elems) != 5 || strcmp(cblistval(elems, 0, NULL), "aa") || strcmp(cblistval(elems, 1, NULL), "bb") || strcmp(cblistval(elems, 2, NULL), "ccc") || strcmp(cblistval(elems, 3, NULL), "dd") || strcmp(cblistval(elems, 4, NULL), "")){ cblistclose(elems); fprintf(stderr, "%s: cbsplit is invalid\n", progname); return 1; } cblistclose(elems); prinfflush("ok\n"); prinfflush("Checking encoding utilities ... "); strcpy(spbuf, "My name is \xca\xbf\xce\xd3\xb4\xb4\xcd\xba.\n\nGo ahead!\n"); tmp = cburlencode(spbuf, -1); orig = cburldecode(tmp, &osiz); if(osiz != strlen(spbuf) || strcmp(orig, spbuf)){ free(orig); free(tmp); fprintf(stderr, "%s: URL encoding is invalid\n", progname); return 1; } free(orig); free(tmp); tmp = cbbaseencode(spbuf, -1); orig = cbbasedecode(tmp, &osiz); if(osiz != strlen(spbuf) || strcmp(orig, spbuf)){ free(orig); free(tmp); fprintf(stderr, "%s: Base64 encoding is invalid\n", progname); return 1; } free(orig); free(tmp); tmp = cbquoteencode(spbuf, -1); orig = cbquotedecode(tmp, &osiz); if(osiz != strlen(spbuf) || strcmp(orig, spbuf)){ free(orig); free(tmp); fprintf(stderr, "%s: quoted-printable encoding is invalid\n", progname); return 1; } free(orig); free(tmp); prinfflush("ok\n"); prinfflush("Checking the global garbage collector ... "); for(i = 0; i < 256; i++){ glist = cblistopen(); cbglobalgc(glist, (void (*)(void *))cblistclose); for(j = 0; j < 10; j++){ sprintf(kbuf, "%08d", j); cblistpush(glist, kbuf, -1); } gmap = cbmapopen(); cbglobalgc(gmap, (void (*)(void *))cbmapclose); for(j = 0; j < 10; j++){ sprintf(kbuf, "%08d", j); cbmapput(gmap, kbuf, -1, kbuf, -1, TRUE); } } prinfflush("ok\n"); prinfflush("all ok\n\n"); return 0;}/* END OF FILE */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -