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

📄 chk_termios.cpp

📁 一个不错
💻 CPP
字号:
/* Copyright is licensed under GNU LGPL.                 by I.J.Wang 2003   Basic test of WyTermios   Build: make chk_termios*/#include "wyterminal.h"#include <cstdlib>#include <unistd.h>#include <sys/types.h>#include <iostream>#if WYTERMIOS_VERSION!=31#error Test code is for WYTERMIOS_VERSION 31#endifstatic bool chk_equ(const WyTermios& t1,const WyTermios& t2){ if(t1.is_default()!=t2.is_default()) {   return(false); } if(t1.getispeed()!=t2.getispeed()) {   return(false); } if(t1.getospeed()!=t2.getospeed()) {   return(false); } if(t1.iflag()!=t2.iflag()) {   return(false); } if(t1.oflag()!=t2.oflag()) {   return(false); } if(t1.cflag()!=t2.cflag()) {   return(false); } if(t1.lflag()!=t2.lflag()) {   return(false); } if(t1.cc_VSTOP()!=t2.cc_VSTOP()) {   return(false); } if(t1.cc_VSTART()!=t2.cc_VSTART()) {   return(false); } if(t1.cc_VSUSP()!=t2.cc_VSUSP()) {   return(false); } if(t1.cc_VQUIT()!=t2.cc_VQUIT()) {   return(false); } if(t1.cc_VKILL()!=t2.cc_VKILL()) {   return(false); } if(t1.cc_VERASE()!=t2.cc_VERASE()) {   return(false); } if(t1.cc_VEOL()!=t2.cc_VEOL()) {   return(false); } if(t1.cc_VEOF()!=t2.cc_VEOF()) {   return(false); } if(t1.cc_VINTR()!=t2.cc_VINTR()) {   return(false); } if(t1.cc_VTIME()!=t2.cc_VTIME()) {   return(false); } if(t1.cc_VMIN()!=t2.cc_VMIN()) {   return(false); } return(true);};static bool chk_default(const WyTermios& t1){ if(t1.is_default()==false) {   return(false); } if(t1.getispeed()!=speed_t()) {   return(false); } if(t1.getospeed()!=speed_t()) {   return(false); } if(t1.iflag()!=tcflag_t()) {   return(false); } if(t1.oflag()!=tcflag_t()) {   return(false); } if(t1.cflag()!=tcflag_t()) {   return(false); } if(t1.lflag()!=tcflag_t()) {   return(false); } if(t1.cc_VSTOP()!=cc_t()) {   return(false); } if(t1.cc_VSTART()!=cc_t()) {   return(false); } if(t1.cc_VSUSP()!=cc_t()) {   return(false); } if(t1.cc_VQUIT()!=cc_t()) {   return(false); } if(t1.cc_VKILL()!=cc_t()) {   return(false); } if(t1.cc_VERASE()!=cc_t()) {   return(false); } if(t1.cc_VEOL()!=cc_t()) {   return(false); } if(t1.cc_VEOF()!=cc_t()) {   return(false); } if(t1.cc_VINTR()!=cc_t()) {   return(false); } if(t1.cc_VTIME()!=cc_t()) {   return(false); } if(t1.cc_VMIN()!=cc_t()) {   return(false); } return(true);};#define CHK_EQU(expr1,expr2) if(chk_equ(expr1,expr2)==false) { WY_THROW( WyRet() ); };#define CHK_NOT_EQU(expr1,expr2) if(chk_equ(expr1,expr2)==true) { WY_THROW( WyRet() ); };#define CHK_DEFAULT(expr) if(chk_default(expr)==false) { WY_THROW( WyRet() ); };#define CHK_NOT_DEFAULT(expr) if(chk_default(expr)==true) { WY_THROW( WyRet() ); };//// Check basic operations on default object//static void t1(void){ WyRet r; WyTermios NObj;    // non default object for the test CHK_DEFAULT(NObj); if((r=NObj.setispeed(B50))!=Ok) {   WY_THROW(r); } CHK_NOT_DEFAULT(NObj);  // constructor {   WyTermios tmp;   CHK_DEFAULT(tmp);   WyTermios tmp1(tmp);   CHK_DEFAULT(tmp1);   CHK_EQU(tmp,tmp1);   WyTermios tmp2(NObj);   CHK_NOT_DEFAULT(tmp2);   CHK_EQU(tmp2,NObj); } // operator== {   WyTermios tmp1,tmp2;   CHK_DEFAULT(tmp1);   CHK_DEFAULT(tmp2);   if(tmp1==tmp2) {   } else {     WY_THROW( WyRet() );   }   if(tmp1!=tmp2) {     WY_THROW( WyRet() );   }   if(tmp1!=NObj) {   } else {     WY_THROW( WyRet() );   }   if(tmp1==NObj) {     WY_THROW( WyRet() );   }   WyTermios tmp3(NObj);   if(tmp3==NObj) {   } else {     WY_THROW( WyRet() );   }   if(tmp3!=NObj) {     WY_THROW( WyRet() );   } } // reset {   WyTermios tmp;   CHK_DEFAULT(tmp);   tmp.reset();   CHK_DEFAULT(tmp);   WyTermios tmp1(NObj);   CHK_NOT_DEFAULT(tmp1);   tmp1.reset();   CHK_DEFAULT(tmp1); } // swap {  WyTermios tmp0(NObj),tmp1,tmp2;  CHK_NOT_DEFAULT(tmp0);  CHK_DEFAULT(tmp1);  CHK_DEFAULT(tmp2);  CHK_NOT_DEFAULT(NObj);  tmp1.swap(tmp2);  CHK_DEFAULT(tmp1);  CHK_DEFAULT(tmp2);  tmp1.swap(tmp0);  CHK_DEFAULT(tmp0);  CHK_NOT_DEFAULT(tmp1);  CHK_EQU(tmp1,NObj);  tmp0.swap(tmp1);  CHK_DEFAULT(tmp1);  CHK_NOT_DEFAULT(tmp0);  CHK_EQU(tmp0,NObj); } // reset {   WyTermios tmp;   CHK_DEFAULT(tmp);   tmp.reset(WyTermios());   CHK_DEFAULT(tmp);   tmp.reset(NObj);   CHK_EQU(tmp,NObj);   // operator=   WyTermios tmp1(NObj);   CHK_NOT_DEFAULT(tmp1);   tmp1=tmp=WyTermios();   CHK_DEFAULT(tmp);   CHK_DEFAULT(tmp1);      tmp=NObj;   CHK_EQU(tmp,NObj); } // get/set i/o speed {   WyTermios tmp;   CHK_DEFAULT(tmp);   if((r=tmp.setispeed(B134))!=Ok) {     WY_THROW(r);   }   if(tmp.getispeed()!=B134) {     WY_THROW( WyRet() );   }   if((r=tmp.setospeed(B150))!=Ok) {     WY_THROW(r);   }   if(tmp.getospeed()!=B150) {     WY_THROW( WyRet() );   }   errno=0;   if((r=tmp.setispeed(speed_t(-1)))!=Wym_EINVAL) {     WY_THROW(r);   };   errno=0;   if((r=tmp.setospeed(speed_t(-1)))!=Wym_EINVAL) {     WY_THROW(r);   }; } // iflag {   WyTermios tmp;   CHK_DEFAULT(tmp);   tmp.iflag(1);   if(tmp.iflag()!=1) {     WY_THROW( WyRet() );   }   tmp.iflag_or(7);   if(tmp.iflag()!=7) {     WY_THROW( WyRet() );   }   tmp.iflag_and(2);   if(tmp.iflag()!=2) {     WY_THROW( WyRet() );   } } // oflag {   WyTermios tmp;   CHK_DEFAULT(tmp);   tmp.oflag(1);   if(tmp.oflag()!=1) {     WY_THROW( WyRet() );   }   tmp.oflag_or(7);   if(tmp.oflag()!=7) {     WY_THROW( WyRet() );   }   tmp.oflag_and(2);   if(tmp.oflag()!=2) {     WY_THROW( WyRet() );   } } // cflag {   WyTermios tmp;   CHK_DEFAULT(tmp);   tmp.cflag(1);   if(tmp.cflag()!=1) {     WY_THROW( WyRet() );   }   tmp.cflag_or(7);   if(tmp.cflag()!=7) {     WY_THROW( WyRet() );   }   tmp.cflag_and(2);   if(tmp.cflag()!=2) {     WY_THROW( WyRet() );   } } // lflag {   WyTermios tmp;   CHK_DEFAULT(tmp);   tmp.lflag(1);   if(tmp.lflag()!=1) {     WY_THROW( WyRet() );   }   tmp.lflag_or(7);   if(tmp.lflag()!=7) {     WY_THROW( WyRet() );   }   tmp.lflag_and(2);   if(tmp.lflag()!=2) {     WY_THROW( WyRet() );   } } // cc {   WyTermios tmp;   CHK_DEFAULT(tmp);   tmp.cc_VSTOP(5);   if(tmp.cc_VSTOP()!=5) {     WY_THROW( WyRet() );   }   tmp.cc_VSTART(4);   if(tmp.cc_VSTART()!=4) {     WY_THROW( WyRet() );   }   tmp.cc_VSUSP(3);   if(tmp.cc_VSUSP()!=3) {     WY_THROW( WyRet() );   }   tmp.cc_VQUIT(2);   if(tmp.cc_VQUIT()!=2) {     WY_THROW( WyRet() );   }   tmp.cc_VKILL(1);   if(tmp.cc_VKILL()!=1) {     WY_THROW( WyRet() );   }   tmp.cc_VERASE(6);   if(tmp.cc_VERASE()!=6) {     WY_THROW( WyRet() );   }   tmp.cc_VEOL(7);   if(tmp.cc_VEOL()!=7) {     WY_THROW( WyRet() );   }   tmp.cc_VEOF(8);   if(tmp.cc_VEOF()!=8) {     WY_THROW( WyRet() );   }   tmp.cc_VINTR(9);   if(tmp.cc_VINTR()!=9) {     WY_THROW( WyRet() );   }   tmp.cc_VTIME(15);   if(tmp.cc_VTIME()!=15) {     WY_THROW( WyRet() );   }   tmp.cc_VMIN(16);   if(tmp.cc_VMIN()!=16) {     WY_THROW( WyRet() );   } } // chk move ctor {  char raw_buf[2*sizeof(WyTermios)];  ::memset(raw_buf,0,sizeof(raw_buf));  WyTermios* const p1( reinterpret_cast<WyTermios*>(raw_buf) );  WyTermios* const p2(p1+1);  new(p1) WyTermios();  if(p1->is_default()==false) {    p1->~WyTermios();    WY_THROW(r);  }  // move *p1 to p2  try {    new(p2) WyTermios(*p1,Wy::ByMove);  }  catch(...) {    p1->~WyTermios();    throw;  };  if(p2->is_default()==false) {    p2->~WyTermios();    WY_THROW(r);  }  p2->~WyTermios(); }};static void t2(void){};#include "wy_array.h"static void t_array(void){/* // chk struct struct termios // (simple chk) {   typedef struct ::termios ElemType;   Wy_Array<ElemType> tmp;   ElemType ele;   ele.c_cc[0]=45;   ele.c_cc[1]=123;   tmp.push_back(ele);   if(tmp.size()!=1) {     WY_THROW( WyRet() );   }   if(tmp[0].c_cc[0]!=ele.c_cc[0]) {     WY_THROW( WyRet() );   }   if(tmp[0].c_cc[1]!=ele.c_cc[1]) {     WY_THROW( WyRet() );   }   tmp.push_back(ElemType());   if(tmp.size()!=2) {     WY_THROW( WyRet() );   }   if(tmp[1].c_cc[0]!=0) {     WY_THROW( WyRet() );   }   if(tmp[1].c_cc[1]!=0) {     WY_THROW( WyRet() );   } }*/};static WyStr chdr("+---------------+\n"                  "| main() caught:|\n"                  "+---------------+\n");int main(void) throw()try { std::cout << "Checking wytermios.h ...\n"; if(WyStr(WyTermios::class_name)!="WyTermios") {   WY_THROW( WyRet() ); } t1(); t2(); t_array(); std::cout << "Checked Ok\n"; 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 + -