📄 chk_select.cpp
字号:
/* Copyright is licensed under GNU LGPL. by I.J.Wang 2003 Basic test of Wy::select The check executable is not in chk_all. Follow the message on the terminal Build: make chk_select*/#include "wyselect.h"#include "wyfifofile.h"#include "wy_uty.h"#if WYSELECT_VERSION!=31#error Test code is for WYSELECT_VERSION 31#endifstatic const WyStr ff1_name("ff1");static const WyStr ff2_name("ff2");static const int ff_open_mode=O_RDONLY|O_NONBLOCK;static void t_fdset(void){ WyRet r; // Wy_FdSet() { Wy_FdSet fs1,fs2(fs1); for(int i=0; i<FD_SETSIZE; ++i) { if(fs1.is_set( WyFileHandle(i) )) { WY_THROW( WyRet() ); } } for(int i=0; i<FD_SETSIZE; ++i) { if(fs2.is_set( WyFileHandle(i) )) { WY_THROW( WyRet() ); } } fs1.set( WyFileHandle(100) ); fs2=fs1; if(fs1.is_set( WyFileHandle(100) )==false) { WY_THROW( WyRet() ); } if(fs2.is_set( WyFileHandle(100) )==false) { WY_THROW( WyRet() ); } fs1.clr( WyFileHandle(100) ); fs2.clr( WyFileHandle(100) ); if(fs1.is_set( WyFileHandle(100) )==true) { WY_THROW( WyRet() ); } if(fs2.is_set( WyFileHandle(100) )==true) { WY_THROW( WyRet() ); } } // swap { Wy_FdSet fs1,fs2(fs1); if(fs2.is_set( WyFileHandle(200) )==true) { WY_THROW( WyRet() ); } fs2.set( WyFileHandle(200) ); if(fs2.is_set( WyFileHandle(200) )==false) { WY_THROW( WyRet() ); } if(fs1.is_set( WyFileHandle(200) )==true) { WY_THROW( WyRet() ); } fs1.swap(fs2); if(fs1.is_set( WyFileHandle(200) )==false) { WY_THROW( WyRet() ); } if(fs2.is_set( WyFileHandle(200) )==true) { WY_THROW( WyRet() ); } } // set()/clr() error { Wy_FdSet fs; WyFileHandle fh; WySysFile sysf; fs.set(fh); if(fs.is_set(fh)==true) { WY_THROW( WyRet() ); } fs.set(sysf); if(fs.is_set(sysf)==true) { WY_THROW( WyRet() ); } fs.clr(fh); if(fs.is_set(fh)==true) { WY_THROW( WyRet() ); } fs.clr(sysf); if(fs.is_set(sysf)==true) { WY_THROW( WyRet() ); } }};static void t_error(void){ WyRet r; // // chk Wy::select(...) Wym_ENODEV error // WyFifoFile tmpf; Wy_FdSet rdfs; rdfs.set(tmpf); const WyTimeSpec Tm0(0,50000000); { const WyTimeSpec tm1(Tm0); if((r=Wy::select(&rdfs,0,0,tm1))!=Wym_ETIMEDOUT) { WY_THROW(r); } if((r=Wy::select(0,&rdfs,0,(WyTimeSpec&)tm1))!=Wym_ENODEV) { WY_THROW(r); } if((r=Wy::select(0,0,&rdfs))!=Wym_ENODEV) { WY_THROW(r); } if(tm1!=Tm0) { WY_THROW(r); } } { WyTimeSpec tm1(Tm0); Wy::select(&rdfs,0,0,tm1); if((r=Wy::select(&rdfs,0,0,tm1))!=Wym_ENODEV) { WY_THROW(r); } }};/* Create two FIFO's (ff1 and ff2) and wait on Wy::select(...) to print input message, timeout=2.0 seconds.*/static void t_select(void)try { WyRet r; Wy::unlink(ff1_name); Wy::unlink(ff2_name); if((r=WyFifoFile::create(ff1_name,S_IRUSR|S_IWUSR))!=Ok) { WY_THROW(r); } if((r=WyFifoFile::create(ff2_name,S_IRUSR|S_IWUSR))!=Ok) { WY_THROW(r); } WyFifoFile ff1_file(ff1_name,ff_open_mode); WyFifoFile ff2_file(ff2_name,ff_open_mode); Wy_FdSet readfds; for(;;) { static bool sflag=false; const WyTimeSpec SDelay(5,Wy_Nano()); WyStr buf; Wy::cout << " -----------------------------------------------------\n" << " Check Wy::select\n" << " Input from other terminal into FIFO files ff1 or ff2\n" << " to check Wy::select(...). e.g.\n\n" << " $echo helo > ff1 (or ff2) ... send 'helo' to ff1 (or ff2)\n" << " $echo exit > ff1 (or ff2) ... end the check\n"; readfds.set(ff1_file); readfds.set(ff2_file); Wy_FdSet rdfs(readfds); if(sflag==true) { const WyTimeSpec tm(SDelay); r=Wy::select(&rdfs,0,0,tm); if(tm!=SDelay) { WY_THROW( WyRet() ); } sflag=false; } else { WyTimeSpec tm(SDelay); r=Wy::select(&rdfs,0,0,tm); if(tm>SDelay) { WY_THROW( WyRet() ); } if(r==Wym_ETIMEDOUT) { if(tm>WyTimeSpec()) { Wy::cout << Wy::wrd(tm) << '\n'; WY_THROW( WyRet() ); } } sflag=true; } if(r==Wym_ETIMEDOUT) { Wy::cout << "Wy::select timedout (5 seconds)...\n\n"; continue; } if(r!=Ok) { WY_THROW(r); } // read ff1 if(rdfs.is_set(ff1_file)) { size_t n_rd=999; Wy::cout << "ff1/"; if((r=ff1_file.read(buf,100,n_rd))!=Ok) { WY_THROW(r); } if(buf.size()==0) { Wy::cout << "... write end closed\n"; if((r=ff1_file.reset())!=Ok) { WY_THROW(r); } if((r=ff1_file.reset(ff1_name,ff_open_mode))!=Ok) { WY_THROW(r); } continue; } Wy::cout << buf.size() << "= " << buf << '\n'; if(buf=="exit\n") { break; } continue; } // read ff2 if(rdfs.is_set(ff2_file)) { size_t n_rd(999); Wy::cout << "ff2/"; if((r=ff2_file.read(buf,100,n_rd))!=Ok) { WY_THROW(r); } if(buf.size()==0) { Wy::cout << "... write end closed\n"; if((r=ff2_file.reset())!=Ok) { WY_THROW(r); } if((r=ff2_file.reset(ff2_name,ff_open_mode))!=Ok) { WY_THROW(r); } continue; } Wy::cout << buf.size() << "= " << buf << '\n'; if(buf=="exit\n") { break; } continue; } WY_THROW( WyRet() ); } Wy::unlink(ff1_name); Wy::unlink(ff2_name);}catch(...) { Wy::unlink(ff1_name); Wy::unlink(ff2_name); throw;};static const WyStr chdr( "+---------------+\n" "| main() caught:|\n" "+---------------+\n");int main(void) throw()try { Wy::cout << "Checking Wy::select ...\n"; t_fdset(); t_error(); t_select(); Wy::cout << "Checked Ok\n"; Wy::cout << FD_SETSIZE << '\n'; return(0);}catch(const WyRet& e) { Wy::cerr << chdr << Wy::wrd(e) << '\n'; return(-1);}catch(const std::exception& e) { Wy::cerr << chdr << "std::exception\n"; return(-1);}catch(...) { Wy::cerr << chdr << "unknown unwind\n"; return(-1);};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -