📄 ck_timespec.cpp
字号:
{"-12e+1",6,WyTimeSpec(Wy_Second(-120),Wy_Nano())}, {"-12e-1",6,WyTimeSpec(Wy_Second(-1),Wy_Nano(-200000000))}, {"12.21e1",7,WyTimeSpec(Wy_Second(122),Wy_Nano(100000000))}, {"12.21e+1",8,WyTimeSpec(Wy_Second(122),Wy_Nano(100000000))}, {"12.21e-1",8,WyTimeSpec(Wy_Second(1),Wy_Nano(221000000))}, {"12.010e1",8,WyTimeSpec(Wy_Second(120),Wy_Nano(100000000))}, {"12.010e+1",9,WyTimeSpec(Wy_Second(120),Wy_Nano(100000000))}, {"12.010e-1",9,WyTimeSpec(Wy_Second(1),Wy_Nano(201000000))}, {"12.e1",5,WyTimeSpec(Wy_Second(120),Wy_Nano())}, {"12.e+1",6,WyTimeSpec(Wy_Second(120),Wy_Nano())}, {"12.e-1",6,WyTimeSpec(Wy_Second(1),Wy_Nano(200000000))}, {".12e1",5,WyTimeSpec(Wy_Second(1),Wy_Nano(200000000))}, {".12e+1",6,WyTimeSpec(Wy_Second(1),Wy_Nano(200000000))}, {".12e-1",6,WyTimeSpec(Wy_Second(),Wy_Nano(12000000))}, {".120E1",6,WyTimeSpec(Wy_Second(1),Wy_Nano(200000000))}, {".120E+1",7,WyTimeSpec(Wy_Second(1),Wy_Nano(200000000))}, {".120E-1",7,WyTimeSpec(Wy_Second(),Wy_Nano(12000000))}, // regular use (positive) {"123e-3",6,WyTimeSpec(Wy_Second(),Wy_Nano(123000000))}, {"123e-6",6,WyTimeSpec(Wy_Second(),Wy_Nano(123000))}, {"123e-9",6,WyTimeSpec(Wy_Second(),Wy_Nano(123))}, {"1e-3",4,WyTimeSpec(Wy_Second(),Wy_Nano(1000000))}, {"1e-6",4,WyTimeSpec(Wy_Second(),Wy_Nano(1000))}, {"1e-9",4,WyTimeSpec(Wy_Second(),Wy_Nano(1))}, {"345.678e3",9,WyTimeSpec(Wy_Second(345678),Wy_Nano())}, // regular use (negative) {"-123e-3",7,WyTimeSpec(Wy_Second(),Wy_Nano(-123000000))}, {"-123e-6",7,WyTimeSpec(Wy_Second(),Wy_Nano(-123000))}, {"-123e-9",7,WyTimeSpec(Wy_Second(),Wy_Nano(-123))}, {"-1e-3",5,WyTimeSpec(Wy_Second(),Wy_Nano(-1000000))}, {"-1e-6",5,WyTimeSpec(Wy_Second(),Wy_Nano(-1000))}, {"-1e-9",5,WyTimeSpec(Wy_Second(),Wy_Nano(-1))}, {"-345.678e3",10,WyTimeSpec(Wy_Second(-345678),Wy_Nano())}, // larger number (+/- LONG_INT) {"2147483647",10,WyTimeSpec(Wy_Second(2147483647L),Wy_Nano())}, {"2147483647.0",12,WyTimeSpec(Wy_Second(2147483647L),Wy_Nano())}, {"2147483647.0e-1",15,WyTimeSpec(Wy_Second(214748364L),Wy_Nano(700000000))}, {"2147483647.0e-9",15,WyTimeSpec(Wy_Second(2),Wy_Nano(147483647))}, {"2.1474836470e9",14,WyTimeSpec(Wy_Second(2147483647L),Wy_Nano())}, // digits less than 1/billion are truncated {"2147483647.0e-10",16,WyTimeSpec(Wy_Second(),Wy_Nano(214748364))}, {"2147483647.0e-11",16,WyTimeSpec(Wy_Second(),Wy_Nano(21474836))}, {"12.3456789e-2",13,WyTimeSpec(Wy_Second(),Wy_Nano(123456789))}, {"12.3456789e-9",13,WyTimeSpec(Wy_Second(),Wy_Nano(12))}, {"12.123456789",12,WyTimeSpec(Wy_Second(12),Wy_Nano(123456789))}, {"12.123456789123",15,WyTimeSpec(Wy_Second(12),Wy_Nano(123456789))},};static void verify_sample(const TestSample& sample){ WyRet r; WyTimeSpec res; const char* endptr; try { if(sample.nstr==0) { WY_THROW(r); } if((r=Wy::_strnum(res,&endptr,WyCSeg(sample.nstr,sample.strlen)))!=Ok) { WY_THROW(r); } if(endptr!=sample.nstr+sample.strlen) { WY_THROW( WyRet() ); } if(res!=sample.nvalue) { WY_THROW( WyRet() ); } } catch(...) { std::cerr << "TestSample={\"" << sample.nstr << "\"," << sample.strlen << ",WyTimeSample(" << sample.nvalue.second() << "," << sample.nvalue.nano() << ")}\n"; throw; }};static void t_strnum_samples(void){ for(size_t i=0; i<sizeof(samp_tab)/sizeof(TestSample); ++i) { verify_sample(samp_tab[i]); }};static void t_strnum_error(void){ typedef WyTimeSpec ResType; const ResType Org("56.8923"); const char* endptr; ResType res; WyRet r; // chk errors { res=Org; try { r=Wy::_strnum(res,&endptr,(const char*)0); WY_THROW(r); } catch(const WyCSeg::Reply& e) { if(e!=Wym_EFAULT) { WY_THROW( WyRet(e) ); } }; if(res!=Org) { WY_THROW( WyRet() ); } if((r=Wy::_strnum(res,&endptr,WyCSeg("",(size_t)0)))!=Wym_ENOENT) { WY_THROW(r); } if(res!=Org) { WY_THROW( WyRet() ); } { const char es1[]="233aa"; const char es2[]="+a"; const char es3[]="-a"; const char es4[]="+-.23"; const char es5[]="1.23xa"; res=Org; if((r=Wy::_strnum(res,&endptr,WyCSeg(es1,sizeof(es1)-1)))!=Wym_EBADMSG) { WY_THROW(r); } if(endptr!=es1+3) { WY_THROW( WyRet() ); } if(res!=WyTimeSpec(233,0)) { WY_THROW( WyRet() ); } res=Org; if((r=Wy::_strnum(res,&endptr,WyCSeg(es2,sizeof(es2)-1)))!=Wym_ENOENT) { WY_THROW(r); } if(endptr!=es2+1) { WY_THROW( WyRet() ); } if(res!=Org) { WY_THROW( WyRet() ); } if((r=Wy::_strnum(res,&endptr,WyCSeg(es3,sizeof(es3)-1)))!=Wym_ENOENT) { WY_THROW(r); } if(endptr!=es3+1) { WY_THROW( WyRet() ); } if(res!=Org) { WY_THROW( WyRet() ); } if((r=Wy::_strnum(res,&endptr,WyCSeg(es4,sizeof(es4)-1)))!=Wym_ENOENT) { WY_THROW(r); } if(endptr!=es4+1) { WY_THROW( WyRet() ); } if(res!=Org) { WY_THROW( WyRet() ); } if((r=Wy::_strnum(res,&endptr,WyCSeg(es5,sizeof(es5)-1)))!=Wym_EBADMSG) { WY_THROW(r); } if(endptr!=es5+4) { WY_THROW( WyRet() ); } if(res!=WyTimeSpec("1.23")) { WY_THROW( WyRet() ); } res=Org; } } { const char tstr[]="12.3e1X"; res=Org; if((r=Wy::_strnum(res,&endptr,WyCSeg(tstr,sizeof(tstr)-1)))!=Wym_EBADMSG) { WY_THROW(r); } if(endptr!=tstr+6) { WY_THROW( WyRet() ); } if(res!=WyTimeSpec("12.3E1")) { WY_THROW( WyRet() ); } } { const char tstr[]="12.3e+-1"; res=Org; if((r=Wy::_strnum(res,&endptr,WyCSeg(tstr,sizeof(tstr)-1)))!=Wym_EBADMSG) { WY_THROW(r); } if(endptr!=tstr+4) { WY_THROW( WyRet() ); } if(res!=WyTimeSpec("12.3")) { WY_THROW( WyRet() ); } } { const char tstr[]="12.3e20"; res=Org; if((r=Wy::_strnum(res,&endptr,WyCSeg(tstr,sizeof(tstr)-1)))!=Wym_ERANGE) { WY_THROW(r); } /* Not Pass!!! FIXME if(endptr!=tstr+sizeof(tstr)-1) { WY_THROW( WyRet() ); } if(res!=WyTimeSpec(12.3)) { WY_THROW( WyRet() ); }*/ } { const char tstr[]="12.3e-20"; res=Org; if((r=Wy::_strnum(res,&endptr,WyCSeg(tstr,sizeof(tstr)-1)))!=Wym_ERANGE) { WY_THROW(r); } if(endptr!=tstr+sizeof(tstr)-1) { WY_THROW( WyRet() ); } if(res!=Org) { WY_THROW( WyRet() ); } } { const char s1[]="01234567890"; const char s2[]="0.12345678900001"; if((r=Wy::_strnum(res,&endptr,WyCSeg(s1,11)))!=Ok) { WY_THROW(r); } if(endptr!=s1+sizeof(s1)-1) { WY_THROW( WyRet() ); } if((r=Wy::_strnum(res,&endptr,WyCSeg(s2,11)))!=Ok) { WY_THROW(r); } if((r=Wy::_strnum(res,&endptr,WyCSeg(s2,12)))!=Ok) { WY_THROW(r); } //if(endptr!=s2+sizeof(s2)-1) { _strnum for WyTimeSpec may return Ok // WY_THROW( WyRet() ); while endptr in the middle of input string //} { ResType tmp; if((r=Wy::_strnum(res,&endptr,WyCSeg(s2,15)))!=Ok) { WY_THROW(r); } if((r=Wy::_strnum(tmp,&endptr,WyCSeg(s2,16)))!=Ok) { WY_THROW(r); } if(res!=tmp) { WY_THROW( WyRet() ); } } } // chk error max { WyTimeSpec tmp(WyTimeSpec::max()); tmp+=WyTimeSpec(); tmp-=WyTimeSpec(); } try { WyTimeSpec tmp(WyTimeSpec::max()); tmp+=WyTimeSpec(Wy_Second(),Wy_Nano(1)); WY_THROW( WyRet() ); } catch(const WyTimeSpec::Reply& e) { if(e!=Wym_ERANGE) { throw; } }; // chk error min { WyTimeSpec tmp(WyTimeSpec::min()); tmp-=WyTimeSpec(); tmp+=WyTimeSpec(); } try { WyTimeSpec tmp(WyTimeSpec::min()); tmp-=WyTimeSpec(Wy_Second(),Wy_Nano(1)); WY_THROW( WyRet() ); } catch(const WyTimeSpec::Reply& e) { if(e!=Wym_ERANGE) { throw; } };};static void t_wrd(void){ struct ChkSmp { WyTimeSpec tsp; const char* nstr; void verify(void) const { if(Wy::wrd(tsp)!=nstr) { std::cerr << "WyTimeSpec(" << tsp.second() << "," << tsp.nano() << "), \"" << nstr << "\", wrd()=" << Wy::wrd(tsp).c_str() << std::endl; WY_THROW( WyRet() ); } }; } const chk_tab[]={ {WyTimeSpec(Wy_Second(),Wy_Nano()),"0.0"}, {WyTimeSpec(),"0.0"}, {WyTimeSpec(Wy_Second(),Wy_Nano(1230)),"0.00000123"}, {WyTimeSpec(Wy_Second(),Wy_Nano(-1230)),"-0.00000123"}, {WyTimeSpec(Wy_Second(1),Wy_Nano(1230)),"1.00000123"}, {WyTimeSpec(Wy_Second(1),Wy_Nano(-1230)),"0.99999877"}, {WyTimeSpec(Wy_Second(-1),Wy_Nano(1230)),"-0.99999877"}, {WyTimeSpec(Wy_Second(-1),Wy_Nano(-1230)),"-1.00000123"}, {WyTimeSpec(Wy_Second(1),Wy_Nano(-1)),"0.999999999"}, {WyTimeSpec(120,450),"120.00000045"}, {WyTimeSpec(Wy_Second(),450),"0.00000045"}, {WyTimeSpec(Wy_Second(),-450),"-0.00000045"}, {WyTimeSpec(-1,-450),"-1.00000045"}, {WyTimeSpec(-1,450),"-0.99999955"}, {WyTimeSpec("120.45e-1"),"12.045"}, }; for(size_t i=0; i<sizeof(chk_tab)/sizeof(ChkSmp); ++i) { chk_tab[i].verify(); } // check wd(WyTimeSpec,int,size_t) { WyTimeSpec ts(123,987654321); if(Wy::wrd(ts,10,3)!="123.987") { WY_THROW( WyRet() ); } if(Wy::wrd(ts, 0,3)!="1.239e+2") { WY_THROW( WyRet() ); } // chk error try { Wy::wrd(ts,16,3); WY_THROW( WyRet() ); } catch(const WyStr::Reply& e) { if(e!=Wym_EINVAL) { WY_THROW( WyRet(e) ); } // FALLTHROUGH } }};#include "wy_array.h"#include "ck_array.h"static void t_array(void){ /* // chk struct timespec { time_t tv_sec; long tv_nsec; } // (simple chk) { typedef ::timespec ElemType; Wy_Array<ElemType> tmp; ::timespec ts={ 123,456 }; tmp.push_back(ts); if(tmp.size()!=1) { WY_THROW( WyRet() ); } if(tmp[0].tv_sec!=ts.tv_sec) { WY_THROW( WyRet() ); } if(tmp[0].tv_nsec!=ts.tv_nsec) { WY_THROW( WyRet() ); } tmp.push_back(ElemType()); if(tmp.size()!=2) { WY_THROW( WyRet() ); } if(tmp[1].tv_sec!=0) { WY_THROW( WyRet() ); } if(tmp[1].tv_nsec!=0) { WY_THROW( WyRet() ); } }*/ /* // chk struct timeval { time_t tv_sec; long tv_usec; } // (simple check) { typedef ::timeval ElemType; Wy_Array<ElemType> tmp; ::timeval ts={ 123,456 }; tmp.push_back(ts); if(tmp.size()!=1) { WY_THROW( WyRet() ); } if(tmp[0].tv_sec!=ts.tv_sec) { WY_THROW( WyRet() ); } if(tmp[0].tv_usec!=ts.tv_usec) { WY_THROW( WyRet() ); } tmp.push_back(ElemType()); if(tmp.size()!=2) { WY_THROW( WyRet() ); } if(tmp[1].tv_sec!=0) { WY_THROW( WyRet() ); } if(tmp[1].tv_usec!=0) { WY_THROW( WyRet() ); } }*/};static void t_scanum(void){ WyRet r; // chk _scanum(ValueType&,size_t&,const WyCSeg&) { const char Input1[]=" -12.34 345.67E-1 78.901e1 55.5"; const WyCSeg txt=Input1; size_t fidx=0; WyTimeSpec value;; if((r=Wy::_scanum(value,fidx,txt))!=Ok) { WY_THROW(r); } if(value!=WyTimeSpec("-12.34")) { WY_THROW( WyRet() ); } if((r=Wy::_scanum(value,fidx,txt))!=Ok) { WY_THROW(r); } if(value!=WyTimeSpec("345.67E-1")) { WY_THROW( WyRet() ); } if((r=Wy::_scanum(value,fidx,txt))!=Ok) { WY_THROW(r); } if(value!=WyTimeSpec("78.901e+1")) { WY_THROW( WyRet() ); } if((r=Wy::_scanum(value,fidx,txt))!=Ok) { WY_THROW(r); } if(value!=WyTimeSpec("55.5")) { WY_THROW( WyRet() ); } } // chk _scanum(ValueType&,size_t&,const WyCSeg&,const int&) { const char Input1[]="1.234 5.78e+2 "; const WyCSeg txt=Input1; size_t fidx=0; WyTimeSpec value_f1,value_f2; if((r=Wy::_scanum(value_f1,fidx,txt,0))!=Ok) { WY_THROW(r); } if(value_f1!=WyTimeSpec("1.234")) { WY_THROW( WyRet() ); } if((r=Wy::_scanum(value_f2,fidx,txt,0))!=Ok) { WY_THROW(r); } if(value_f2!=WyTimeSpec("5.78e2")) { WY_THROW( WyRet() ); } } // chk reply { WyCSeg txt; size_t fidx=100; WyTimeSpec value; if((r=Wy::_scanum(value,fidx,txt))!=Wym_ENOENT) { WY_THROW( WyRet() ); } if(fidx!=100) { WY_THROW( WyRet() ); } txt="9876543210"; fidx=0; if((r=Wy::_scanum(value,fidx,txt))!=Wym_ERANGE) { WY_THROW( WyRet() ); } if(fidx!=0) { WY_THROW( WyRet() ); } if((r=Wy::_scanum(value,fidx,txt,37))!=Wym_EINVAL) { WY_THROW( WyRet() ); } if((r=Wy::_scanum(value,fidx,txt,1))!=Wym_EINVAL) { WY_THROW( WyRet() ); } }};void ck_timespec(void){ t1(); t2(); t3(); t4();#ifdef TEST_SLEEP_TILL t5();#endif t_sleep(); t_mkstr(); t_strnum_samples(); t_strnum_error(); t_wrd(); t_array(); t_scanum();};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -