⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chk_cin.cpp

📁 一个不错
💻 CPP
字号:
/* Copyright is licensed under GNU LGPL.                 by I.J.Wang 2003   Check Wy::cin   Build: make chk_cin   Note: Test data files should exist in the working directory         Files may be created in the working directory and unlinked*/#include "wyterminal.h"#include "wyregfile.h"#include "wyfifofile.h"#include "chk_data.h"#include <unistd.h>#include <iostream>#if WYBYTEFLOW_VERSION!=31#error Test code is for WYBYTEFLOW_VERSION 31#endif#if WYREGFILE_VERSION!=31#error Test code is for WYREGFILE_VERSION 31#endif#if WYCHRFILE_VERSION!=31#error Test code is for WYCHRFILE_VERSION 31#endif#if WYTERMINAL_VERSION!=31#error Test code is for WYTERMINAL_VERSION 31#endif//// Assert f1,f2 the same systerm file//static void assert_same_sysf(const WyByteFlow& f1,const WyByteFlow& f2){ if(f1.is_default()&&f2.is_default()) {   return; } WyRet r; WyFileStat stt1,stt2; if((r=f1.stat(stt1))!=Ok) {   WY_THROW( WyRet() ); } if((r=f2.stat(stt2))!=Ok) {   WY_THROW( WyRet() ); } if(stt1.sf_ino()!=stt2.sf_ino()) {   WY_THROW( WyRet() ); } if(stt1.sf_dev()!=stt2.sf_dev()) {   WY_THROW( WyRet() ); }};static void assert_same_sysf(const WyByteFlow& f1,const WyFileHandle f2){ if(f1.is_default()&&f2.is_default()) {   return; } WyRet r; WyFileStat stt1,stt2; if((r=f1.stat(stt1))!=Ok) {   WY_THROW( WyRet() ); } if((r=f2.stat(stt2))!=Ok) {   WY_THROW( WyRet() ); } if(stt1.sf_ino()!=stt2.sf_ino()) {   WY_THROW( WyRet() ); } if(stt1.sf_dev()!=stt2.sf_dev()) {   WY_THROW( WyRet() ); }};#define CHK_SAME_SYSF(p1,p2)\    try { assert_same_sysf(p1,p2); } catch(...) { throw; }//// Setup the test //static void setup_test(void){};//// Undo setup_test//static void close_test(void){};static void t1(void){ WyRegFile TFile("chk_cin.cpp",O_RDONLY); const WyByteFlow Org(Wy::cin); WyRet r; CHK_SAME_SYSF(Wy::cin,Org); if(Wy::cin.wy_is_std_fd()==false) {   WY_THROW( WyRet() ); } if(Org.wy_is_std_fd()==true) {   WY_THROW( WyRet() ); } // reset(const WyByteFlow&) if((r=Wy::cin.reset(static_cast<const WyByteFlow&>(TFile)))!=Ok) {   WY_THROW(r); } if(Wy::cin.fh().fd()!=STDIN_FILENO) {   WY_THROW( WyRet() ); } CHK_SAME_SYSF(Wy::cin,TFile); CHK_SAME_SYSF(Wy::cin,WyFileHandle(STDIN_FILENO));/*  if(Wy::cin.reset(WyFileHandle(STDIN_FILENO))!=Ok) {   WY_THROW(r); } CHK_SAME_SYSF(Wy::cin,TFile); // reset(WyFileHandle) if((r=Wy::cin.reset(TFile.fh()))!=Ok) {   WY_THROW(r); } if(Wy::cin.fh().fd()!=STDIN_FILENO) {   WY_THROW( WyRet() ); } CHK_SAME_SYSF(Wy::cin,TFile); */ // recover Wy::cin if(Wy::cin.is_default()==true) {   WY_THROW( WyRet() ); } if((r=Wy::cin.reset(Org))!=Ok) {   WY_THROW(r); } if(Wy::cin.fh().fd()!=STDIN_FILENO) {   WY_THROW( WyRet() ); } CHK_SAME_SYSF(Wy::cin,Org);  // _swap if((r=Wy::cin._swap(TFile))!=Wym_EBADTYPE) {   WY_THROW(r); } // _alloc if((Wy::cin._alloc(r))!=NULL) {   WY_THROW( WyRet() ); } if(r!=Wym_ENOSYS) {   WY_THROW(r); }};// chk reset()//static void t_last(void){ class RAID {     const WyByteFlow _org_std;   public:     RAID() : _org_std(Wy::cin) {};     ~RAID() {        ::dup2(_org_std.wy_fd(),STDIN_FILENO);      }; } not_use_me; const WyRegFile TFile("chk_cin.cpp",O_RDONLY); WyByteFlow& STDOBJ(Wy::cin); WyRet r; if((r=STDOBJ.reset(TFile))!=Ok) {   WY_THROW(r); } CHK_SAME_SYSF(STDOBJ,TFile); if((r=STDOBJ.reset(TFile.fh()))!=Ok) {   WY_THROW(r); } CHK_SAME_SYSF(STDOBJ,TFile); // chk error if((r=STDOBJ.reset(WyFileHandle()))!=Wym_EBADF) {   WY_THROW(r); } CHK_SAME_SYSF(STDOBJ,TFile); if((r=STDOBJ.reset(WyByteFlow()))!=Wym_EBADF) {   WY_THROW(r); } CHK_SAME_SYSF(STDOBJ,TFile); try {   STDOBJ.reset();   WY_THROW( WyRet() ); } catch(const WyByteFlow::Reply& e) {   WY_THROW( WyRet(e) ); } catch(const WyRet& e) {   if(e!=Wym_EBADF) {     WY_THROW( WyRet(e) );   } }; CHK_SAME_SYSF(STDOBJ,TFile);};//// Read Wy::cin as a regular file (the file have to be TestFile_2A)//static void t_cin_reg(void){  std::cout << "Wy::cin is a regular file\n";  WyRet r;  WyRegFile regf;  if((r=regf.reset(Wy::cin.fh()))!=Ok) {     WY_THROW(r);  }  // read Wy::cin file  char buf[8];  size_t n_rd;  if((r=regf.read(buf,sizeof(buf),n_rd))!=Ok) {    WY_THROW(r);  }  if(n_rd!=2) {    WY_THROW( WyRet() );  }  if((buf[0]!='\n')||(buf[1]!='\n')) {    WY_THROW( WyRet() );  }};//// Read Wy::cin as a character file//static void t_cin_chr(void){   std::cout << "Wy::cin is a character file\n";   WyRet r;   WyChrFile chrf;   if((r=chrf.reset(Wy::cin.fh()))!=Ok) {     WY_THROW(r);   }   // read   std::cout << "Wy::cin (input any string): " << std::flush;   char buf[8];   size_t n_rd;   if((r=chrf.read(buf,sizeof(buf),n_rd))!=Ok) {     WY_THROW(r);   }   if(n_rd<=0) {     std::cout << " Nothing input\n";   } else {     if(buf[n_rd-1]!='\n') {       std::cout << " Check interrupted (input>" << sizeof(buf)-1 << ")\n"                    " type cntrl-D and run again\n";       while(chrf.read(buf,sizeof(buf),n_rd)!=Ok) {         // flush input       }       WY_THROW( WyRet() );  // last char from Wy::cin should be LF.                             // ok if input is more than sizeof(buf)-1     }     --n_rd;     WyStr str;     str.reset( WyCSeg(buf,n_rd) );     std::cout << "Echo the input(" << n_rd << ")=\"" << str.c_str() << '"';     if(n_rd>sizeof(buf)) {       WY_THROW( WyRet() );     }     if(n_rd==sizeof(buf)) {       std::cout << " Only tested " << sizeof(buf) << " characters";     }     std::cout << std::endl;   }};//// Check Wy::cin//static void t_cin(void){ WyRet r; // startup condition check if(Wy::cin.is_default()) {   WY_THROW( WyRet() ); } if(Wy::cin.fh().fd()!=STDIN_FILENO) {   WY_THROW( WyRet() ); } // stat WyFileStat stt; if((r=Wy::cin.stat(stt))!=Ok) {   WY_THROW(r); } if(stt.is_chr()) {   t_cin_chr();   return; } else if(stt.is_reg()) {   t_cin_reg();   return; } else {   WY_THROW( WyRet() );  // neither chr nor reg file }; // chk mkpipe (note: Wy::cin is not WyFifoFile/* {   const WyByteFlow OrgDev(Wy::cin);   WyByteFlow tmp;   if((r=WyFifoFile::mkpipe(Wy::cin,tmp))!=Ok) {     WY_THROW(r);   }   try {     if((r=Wy::cin.stat(stt))!=Ok) {       WY_THROW(r);     }     if(stt.is_fifo()==false) {       WY_THROW( WyRet() );     }     Wy::cin.reset(OrgDev);   }   catch(...) {     Wy::cin.reset(OrgDev);     throw;   }; }*/};static void t_exception(void){ // // Test copy constructor exception // try { } catch(...) {   throw; }; // // Test destructor exception // { }};static WyStr chdr("+---------------+\n"                  "| main() caught:|\n"                  "+---------------+\n");int main(void) throw()try { try {   std::cout << "Checking Wy::cin ...\n";   setup_test();   t1();   t_cin();   t_exception();   close_test();   t_last();   std::cout << "Checked Ok\n"; } catch(...) {   close_test();   throw; } return(0);}catch(const WyRet& e) { std::cerr << chdr.c_str() << Wy::wrd(e).c_str() << std::endl; return(-1);}catch(const std::exception& e) { std::cerr << chdr.c_str() << "std::exception" << std::endl; return(-1);}catch(...) { std::cerr << chdr.c_str() << "unknown unwind" << std::endl; return(-1);};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -