📄 ck_regfile.cpp
字号:
if((r=WyRegFile::create(tmp,Tmp_Name.c_str(),O_WRONLY,0))!=Ok) { WY_THROW(r); } Wy_AtDestroy<WyRet,const WyStr&> rrid(Wy::unlink,Tmp_Name); CHK_NOT_DEFAULT(tmp); if((r=tmp.write(buf,sizeof(buf),n_wr))!=Ok) { WY_THROW(r); } if(n_wr!=sizeof(buf)) { WY_THROW( WyRet() ); } if(tmp.pos()!=sizeof(buf)) { WY_THROW( WyRet() ); } if((tmp.fdatasync())!=Ok) { WY_THROW( WyRet() ); } // resize to 1 if((r=tmp.resize(1))!=Ok) { WY_THROW(r); } if((r=tmp.seek_end())!=Ok) { WY_THROW(r); } if(tmp.pos()!=1) { WY_THROW( WyRet() ); } // resize to 10 if((r=tmp.resize(10))!=Ok) { WY_THROW(r); } if(tmp.pos()==10) { WY_THROW( WyRet() ); } if((r=tmp.seek_end())!=Ok) { WY_THROW(r); } if(tmp.pos()!=10) { WY_THROW( WyRet() ); } } // reset(WyStr..) Wym_ENOENT { WyRegFile tmp(TFile1A); CHK_NOT_DEFAULT(tmp); if((r=tmp.reset(WyStr(Tmp_Name),O_RDONLY))!=Wym_ENOENT) { WY_THROW(r); } } // reset(WyFileHandle()) // { WyRegFile tmp(TFile1A); CHK_NOT_DEFAULT(tmp); if((r=tmp.reset(WyFileHandle()))!=Ok) { WY_THROW(r); } CHK_DEFAULT(tmp); } // reset(WyRegFile()) // { WyRegFile tmp(TFile1A); CHK_NOT_DEFAULT(tmp); if((r=tmp.reset(WyRegFile()))!=Ok) { WY_THROW(r); } CHK_DEFAULT(tmp); } // reset() from base { WyRegFile tmp; CHK_DEFAULT(tmp); WyByteFlow *p=&tmp; if((r=p->reset(TFile1A))!=Ok) { WY_THROW(r); } CHK_SAME_SYSF(tmp,TFile1A); } // chk move ctor { char raw_buf[2*sizeof(WyRegFile)]; ::memset(raw_buf,0,sizeof(raw_buf)); WyRegFile * const p1( reinterpret_cast<WyRegFile*>(raw_buf) ); WyRegFile * const p2(p1+1); new(p1) WyRegFile(__FILE__,O_RDONLY); { try { CHK_NOT_DEFAULT(*p1); } catch(...) { p1->~WyRegFile(); throw; }; } // move *p1 to p2 WyFileHandle ofh( p1->fh() ); try { new(p2) WyRegFile(*p1,Wy::ByMove); } catch(...) { p1->~WyRegFile(); throw; }; WyFileHandle nfh( p2->fh() ); p2->~WyRegFile(); if(nfh!=ofh) { WY_THROW( WyRet() ); } }};// Test read operation (interleave read)//// Two objects are designated to read tfile alternatively.// tfile points to any file that can be read.//static void interleaved_read(const WyStr& tfile){ WyRet r; WyRegFile f0; // tfile if((r=f0.reset(tfile,O_RDONLY))!=Ok) { WY_THROW(r); } WyRegFile f1(f0); // f1 will read with the former half part of tfile WyRegFile f2(f0); // f2 will read with the latter half part of tfile if((r=f0.seek_end())!=Ok) { WY_THROW(r); } //Wy::cout << tfile << "= " << // f0.pos() << " bytes, "; // file size of f0 and the start position of f2, also the defined file size of f1 // const off_t FSize=f0.pos(); const off_t MidPos=FSize>>1; f1.set_pos(0); // set f1 to begin from 0 f2.set_pos(MidPos); // set f2 to begin from MidPos // Set str0 with the whole file of f0 // WyStr str0; { size_t n_rd; char* text0=(char*)::malloc(FSize); if(text0==0) { WY_THROW( WyRet(Wym_ENOMEM) ); } Wy_AtDestroy<void,char*> rrid((void(*)(char*))::free,text0); f0.set_pos(0); if((r=f0.read(text0,FSize,n_rd))!=Ok) { WY_THROW(r); } if((off_t)n_rd!=FSize) { WY_THROW( WyRet() ); } if(f0.pos()!=FSize) { WY_THROW( WyRet() ); // value of seek_end not identical to read EOF } str0.reset( WyCSeg(text0,FSize) ); // transfer text0 to str0 } // Read f1 to str1 and f2 to str2 in a loop, each loop read one byte // WyStr str1,str2; for(;;) { char ch; size_t n_rd; bool all_eof=true; if(str1.size()<(size_t)MidPos) { // f1 read up to MidPos characters if((r=f1.read(&ch,1,n_rd))!=Ok) { WY_THROW(r); } if(n_rd==1) { str1+=ch; // append the read all_eof=false; } else { if(n_rd!=0) { WY_THROW( WyRet() ); // not expected value of n_rd } WY_THROW( WyRet() ); // not expected value of n_rd(0) } } if((r=f2.read(&ch,1,n_rd))!=Ok) { WY_THROW(r); } if(n_rd==1) { str2+=ch; // append the read all_eof=false; } else { if(n_rd!=0) { WY_THROW( WyRet() ); // not expected value } if(f2.pos()!=FSize) { WY_THROW( WyRet() ); // not expected value } // FALL_THROUGH } if(all_eof==true) { break; } } if(f1.pos()!=MidPos) { WY_THROW( WyRet() ); // not expected value } if(f2.pos()!=FSize) { WY_THROW( WyRet() ); // not expected value } str1+=str2; // append str2 to str1 if(str0!=str1) { WY_THROW( WyRet() ); // assertion failed }};static void t_throw(void){ // WyRegFile(const WySysFile&) // if(exaustfd_flag) { try { WyByteFlow tmp(__FILE__,O_RDONLY); exaust_cpy_ctor(tmp); WY_THROW( WyRet() ); // not expected } catch(const WyRegFile::Reply& e) { if((e!=Wym_EMFILE)&&(e!=Wym_ENOMEM)) { WY_THROW( WyRet(e) ); } // FALL_THROUGH }; } // WyRegFile(const WyRegFile&) // if(exaustfd_flag) { try { WyRegFile tmp(__FILE__,O_RDONLY); exaust_cpy_ctor(tmp); WY_THROW( WyRet() ); // not expected } catch(const WyRegFile::Reply& e) { if((e!=Wym_EMFILE)&&(e!=Wym_ENOMEM)) { WY_THROW( WyRet(e) ); } // FALL_THROUGH }; } // WyRegFile(const char*,int) // try { const char pn[]="nosuchfilename"; WyRegFile tmp(pn,0); WY_THROW( WyRet() ); } catch(const WyRegFile::Reply& e) { if(e!=Wym_ENOENT) { WY_THROW( WyRet(e) ); } // FALL_THROUGH }; // WyRegFile(const WyStr&,int) // try { WyStr pn("nosuchfilename"); WyRegFile tmp(pn,0); WY_THROW( WyRet() ); } catch(const WyRegFile::Reply& e) { if(e!=Wym_ENOENT) { WY_THROW( WyRet(e) ); } // FALL_THROUGH }; // WyRegFile(WyFileHandle) // try { WyRegFile tmp( WyFileHandle(STDERR_FILENO)); WY_THROW( WyRet() ); } catch(const WyRegFile::Reply& e) { if(e!=Wym_EBADF) { WY_THROW( WyRet(e) ); } // FALL_THROUGH }; // Test destructor throw // { WyRegFile tmp; //std::memset(&tmp,-2,sizeof(tmp)); // destructor of tmp is now not to thow }};static void t_error(void){ WyRegFile EmptyFile(TestFile_Empty,O_RDONLY); WyRet r; CHK_NOT_DEFAULT(EmptyFile); // stat { WyRegFile tmp; WyFileStat stt; if((r=tmp.stat(stt))!=Wym_EBADF) { WY_THROW(r); } CHK_DEFAULT(tmp); } // reset(const WyRegFile&) { WyRegFile tmp(EmptyFile); const WyFileHandle TmpFh(tmp.fh()); if((r=tmp.reset(tmp))!=Ok) { WY_THROW(r); } CHK_SAME_SYSF(tmp,EmptyFile); if(tmp.fh()!=TmpFh) { // ok either way } } // reset(const char*,int) { WyRegFile tmp; const char pn[]="nosuchfilename"; if((r=tmp.reset(pn,O_RDONLY))!=Wym_ENOENT) { WY_THROW(r); } CHK_DEFAULT(tmp); } // reset(const WyStr&,int) { WyRegFile tmp; WyStr pn("nosuchfilename"); if((r=tmp.reset(pn,O_RDONLY))!=Wym_ENOENT) { WY_THROW(r); } CHK_DEFAULT(tmp); } // reset(WyFileHandle) { WyRegFile tmp; WyFileHandle h(STDERR_FILENO); if((r=tmp.reset(h))!=Wym_EBADF) { WY_THROW(r); } CHK_DEFAULT(tmp); } // chk _alloc not override { class T : public WyRegFile { } tmp; WyByteFlow* p=tmp._alloc(r); if(r!=Wym_ENOSYS) { if(r==Ok) { delete p; } WY_THROW(r); } }};// verify _read_till(..) feature//static void t_read_line1(void){ // check _read_till(..) does not append the buffer with a trailing zero // { WyRegFile tfile("wyregfile.cpp",O_RDONLY); char rbuf[256]; // enough for the first line of tfile +2 WyRet r; size_t n_rd; ::memset(rbuf,-1,sizeof(rbuf)); // character to detect overwritten if((r=tfile._read_till(rbuf,4,n_rd,'\n'))!=Ok) { // read 4 char of the first line WY_THROW(r); } if(n_rd!=4) { WY_THROW( WyRet() ); } if(rbuf[4]==0) { WY_THROW( WyRet() ); } tfile.set_pos(0); if((r=tfile._read_till(rbuf,sizeof(rbuf)-1,n_rd,'\n'))!=Ok) { WY_THROW(r); } if(n_rd>=sizeof(rbuf)-1) { WY_THROW( WyRet() ); // rbuf is small for the check } if(rbuf[n_rd]==0) { WY_THROW( WyRet() ); // chk not appended with zero } }};// Test read tfile (read_line against read)//static void t_read_line2(const WyStr& tfile){ WyRet r; // Set str0 with the whole file of tfile // WyStr str0; { WyRegFile f0; size_t n_rd; if((r=f0.reset(tfile,O_RDONLY))!=Ok) { WY_THROW(r); // file open failure } if((r=f0.seek_end())!=Ok) { WY_THROW(r); } const size_t FSize=f0.pos(); char* text0=(char*)::malloc(FSize); if(text0==0) { WY_THROW( WyRet(Wym_ENOMEM) ); } Wy_AtDestroy<void,char*> rrid((void(*)(char*))::free,text0); f0.set_pos(0); if((r=f0.read(text0,FSize,n_rd))!=Ok) { WY_THROW(r); } if(n_rd!=FSize) { WY_THROW( WyRet() ); } if(f0.pos()!=(off_t)FSize) { WY_THROW( WyRet() ); // value of seek_end not identical to read EOF } str0.reset( WyCSeg(text0,FSize) ); // transfer text0 to str0 } // Read tfile line by line(1), and append to str1, check against str0 // { WyRegFile f0; const size_t MaxLineLen=str0.size(); size_t n_rd; char* buf=(char*)::malloc(MaxLineLen); if(buf==0) { WY_THROW( WyRet(Wym_ENOMEM) ); } Wy_AtDestroy<void,char*> rrid((void(*)(char*))::free,buf); WyStr str1; if((r=f0.reset(tfile,O_RDONLY))!=Ok) { WY_THROW(r); // file open failure } for(;;) { if((r=f0._read_till(buf,MaxLineLen,n_rd,'\n'))!=Ok) { WY_THROW(r); } if(n_rd==0) { // eof if(str1!=str0) { WY_THROW( WyRet() ); } break; } str1.append( WyCSeg(buf,n_rd) ); // append the line read } } // Read tfile line by line(2), and append to str1, check against str0 // { WyRegFile f0; size_t n_rd; WyStr str1; if((r=f0.reset(tfile,O_RDONLY))!=Ok) { WY_THROW(r); // file open failure } for(;;) { if((r=f0._read_till(str1,size_t(65535),n_rd,'\n'))!=Ok) { WY_THROW(r); } if(n_rd==0) { // eof if(str1!=str0) { WY_THROW( WyRet() ); } break; } } }};static void t_writr(void){ WyRet r; WyRegFile tmp; const char buf[2]={-2,-2}; size_t count=5; // anything not zero CHK_DEFAULT(tmp); if((r=WyRegFile::create(tmp,WyStr(Tmp_Name),O_RDWR,0))!=Ok) { WY_THROW(r); } Wy_AtDestroy<WyRet,const WyStr&> rrid(Wy::unlink,Tmp_Name); CHK_NOT_DEFAULT(tmp); if((r=tmp.write(buf,0,count))!=Ok) { WY_THROW(r); } if(count!=0) { WY_THROW( WyRet() ); } if(tmp.pos()!=0) { WY_THROW( WyRet() ); } if((r=tmp.write(buf,1,count))!=Ok) { WY_THROW(r); } if(count!=1) { WY_THROW( WyRet() ); } if(tmp.pos()!=1) { WY_THROW( WyRet() ); } if((r=tmp.write(WyStr( WyCSeg(buf,sizeof(buf)) ),count))!=Ok) { WY_THROW(r); } if(count!=sizeof(buf)) { WY_THROW( WyRet() ); } if(tmp.pos()!=sizeof(buf)+1) { WY_THROW( WyRet() ); } if((tmp.fdatasync())!=Ok) { WY_THROW( WyRet() ); } // read back tmp.set_pos(0); if(tmp.pos()!=0) { WY_THROW( WyRet() ); } char rbuf[sizeof(buf)+2]; if((r=tmp.read(rbuf,sizeof(rbuf),count))!=Ok) { WY_THROW(r); } if(count!=sizeof(buf)+1) { WY_THROW( WyRet() ); } if(tmp.pos()!=sizeof(buf)+1) { WY_THROW( WyRet() ); } if(rbuf[0]!=buf[0]) { WY_THROW( WyRet() ); } if((rbuf[1]!=buf[0])||(rbuf[2]!=buf[1])) { WY_THROW( WyRet() ); }};static void t3(void) { interleaved_read(TestFile_Empty); interleaved_read(TestFile_1A); interleaved_read(TestFile_2A); interleaved_read(WyStr("ck_regfile.cpp")); interleaved_read(WyStr("ck_regfile.o")); t_read_line1(); t_read_line2(TestFile_1A); t_read_line2(TestFile_2A); //Wy::cout << "Check _read_till(" << arg_rtfile<< ") ...\n"; t_read_line2(arg_rtfile); t_writr();};// exaustfd: exaust testing fd or not// rtfile : file name for _read_till test//void ck_regfile(bool exaustfd,const char* rtfile)try { exaustfd_flag=exaustfd; arg_rtfile=rtfile;#ifdef WY_DEBUG initial_fdcnt=wydbg_get_fdcnt();#endif Wy::unlink(Tmp_Name); t1(); t2(); t_throw(); t_error(); t3();#ifdef WY_DEBUG // fd may be lost. report is only valid in single thread test if(wydbg_get_fdcnt()!=initial_fdcnt) { WY_THROW( WyRet() ); } Wy::cout << "At most " << wydbg_get_fdcnt_max() << " fd's allocated\n";#endif Wy::unlink(Tmp_Name);}catch(...) {#ifdef WY_DEBUG // fd may be lost. report is only valid in single thread test if(wydbg_get_fdcnt()!=initial_fdcnt) { WY_THROW( WyRet() ); }#endif Wy::unlink(Tmp_Name); throw;};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -