📄 xcrtest.cc
字号:
/************************************************************************************************* * Test cases of Curia for C++ * 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 <xcuria.h>#include <cstdlib>#include <cstdio>#include <cstring>#include <iostream>#include <iomanip>extern "C" {#include <pthread.h>#include <unistd.h>#include <sys/types.h>}using namespace std;using namespace qdbm;/* global constants */const int RECBUFSIZ = 32;const int ALIGNSIZ = 8;/* struct for argument of threads */struct Mission { Curia* curia; bool lob; int rnum; int id; bool printer;};/* global variables */const char* progname;/* function prototypes */int main(int argc, char** argv);void myterminate();void myunexpected();void usage();int runwrite(int argc, char** argv);int runread(int argc, char** argv);int runmulti(int argc, char** argv);int runmisc(int argc, char** argv);void pxdperror(const char* name, DBM_error &e);int dowrite(const char* name, int rnum, int bnum, int dnum, bool lob);int doread(const char* name, bool lob);int domulti(const char* name, int rnum, int bnum, int dnum, int tnum, bool lob);void* mtwriter(void* arg);int domisc(const char* name);/* main routine */int main(int argc, char** argv){ int rv; progname = argv[0]; set_terminate(myterminate); set_unexpected(myunexpected); if(argc < 2) usage(); rv = 0; if(!strcmp(argv[1], "write")){ rv = runwrite(argc, argv); } else if(!strcmp(argv[1], "read")){ rv = runread(argc, argv); } else if(!strcmp(argv[1], "multi")){ rv = runmulti(argc, argv); } else if(!strcmp(argv[1], "misc")){ rv = runmisc(argc, argv); } else { usage(); } return rv;}/* for debug */void myterminate(){ cout << progname << ": terminate" << endl; exit(1);}/* for debug */void myunexpected(){ cout << progname << ": unexpected" << endl; exit(1);}/* print the usage and exit */void usage(){ cerr << progname << ": test cases for Curia for C++" << endl; cerr << endl; cerr << "usage:" << endl; cerr << " " << progname << " write [-lob] name rnum bnum dnum" << endl; cerr << " " << progname << " read [-lob] name" << endl; cerr << " " << progname << " multi [-lob] name rnum bnum dnum tnum" << endl; cerr << " " << progname << " misc name" << endl; exit(1);}/* parse arguments of write command */int runwrite(int argc, char** argv){ char *name, *rstr, *bstr, *dstr; int i, rnum, bnum, dnum, rv; bool lob; name = 0; rstr = 0; bstr = 0; dstr = 0; lob = false; for(i = 2; i < argc; i++){ if(argv[i][0] == '-'){ if(!strcmp(argv[i], "-lob")){ lob = true; } else { usage(); } } else if(!name){ name = argv[i]; } else if(!rstr){ rstr = argv[i]; } else if(!bstr){ bstr = argv[i]; } else if(!dstr){ dstr = argv[i]; } else { usage(); } } if(!name || !rstr || !bstr || !dstr) usage(); rnum = atoi(rstr); bnum = atoi(bstr); dnum = atoi(dstr); if(rnum < 1 || bnum < 1 || dnum < 1) usage(); rv = dowrite(name, rnum, bnum, dnum, lob); return rv;}/* parse arguments of read command */int runread(int argc, char** argv){ char* name; int i, rv; bool lob; name = 0; lob = false; for(i = 2; i < argc; i++){ if(argv[i][0] == '-'){ if(!strcmp(argv[i], "-lob")){ lob = true; } else { usage(); } } else if(!name){ name = argv[i]; } else { usage(); } } if(!name) usage(); rv = doread(name, lob); return rv;}/* parse arguments of multi command */int runmulti(int argc, char** argv){ char *name, *rstr, *bstr, *dstr, *tstr; int i, rnum, bnum, dnum, tnum, rv; bool lob; name = 0; rstr = 0; bstr = 0; dstr = 0; tstr = 0; lob = false; for(i = 2; i < argc; i++){ if(argv[i][0] == '-'){ if(!strcmp(argv[i], "-lob")){ lob = true; } else { usage(); } } else if(!name){ name = argv[i]; } else if(!rstr){ rstr = argv[i]; } else if(!bstr){ bstr = argv[i]; } else if(!dstr){ dstr = argv[i]; } else if(!tstr){ tstr = argv[i]; } else { usage(); } } if(!name || !rstr || !bstr || !dstr || !tstr) usage(); rnum = atoi(rstr); bnum = atoi(bstr); dnum = atoi(dstr); tnum = atoi(tstr); if(rnum < 1 || bnum < 1 || dnum < 1 || tnum < 1) usage(); rv = domulti(name, rnum, bnum, dnum, tnum, lob); return rv;}/* parse arguments of misc command */int runmisc(int argc, char** argv){ char* name; int i, rv; name = 0; for(i = 2; i < argc; i++){ if(argv[i][0] == '-'){ usage(); } else if(!name){ name = argv[i]; } else { usage(); } } if(!name) usage(); rv = domisc(name); return rv;}/* print an error message */void pxdperror(const char* name, DBM_error &e){ cerr << progname << ": " << name << ": " << e << endl;}/* do writing test */int dowrite(const char* name, int rnum, int bnum, int dnum, bool lob){ int i, len; char buf[RECBUFSIZ]; cout << "<Writing Test>" << endl; cout << " name=" << name << " rnum=" << rnum << " bnum=" << bnum << " dnum=" << dnum << " lob=" << lob << endl; cout << endl; Curia* curia = 0; bool err = false; try { // open the database curia = new Curia(name, Curia::OWRITER | Curia::OCREAT | Curia::OTRUNC, bnum, dnum); // roop for storing for(i = 1; i <= rnum; i++){ // set a buffer for a key and a value len = sprintf(buf, "%08d", i); // store a record into the database if(lob){ curia->putlob(buf, len, buf, len); } else { curia->put(buf, len, buf, len); } // print progression if(rnum > 250 && i % (rnum / 250) == 0){ cout << '.'; cout.flush(); if(i == rnum || i % (rnum / 10) == 0){ cout << " (" << setw(8) << setfill('0') << i << ")" << endl; cout.flush(); } } } // close the database curia->close(); cout << "ok" << endl << endl; } catch(Curia_error& e){ err = true; // report an error pxdperror(name, e); } // destroy instance if(curia) delete curia; return err ? 1 : 0;}/* do reading test */int doread(const char* name, bool lob){ int i, rnum, len; char buf[RECBUFSIZ], *val; cout << "<Reading Test>" << endl; cout << " name=" << name << " lob=" << lob << endl; cout << endl; Curia* curia = 0; bool err = false; try { // open the database curia = new Curia(name); // get the number of records if(lob){ rnum = curia->rnumlob();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -