📄 ck_timespec.cpp
字号:
} CHK_EQU(a2,a3); if((a2.nano()!=100)||(a2.second()!=100)) { WY_THROW( WyRet() ); } if((r=a2.add(a2))!=Ok) { WY_THROW(r); } if((a2.nano()!=200)||(a2.second()!=200)) { WY_THROW( WyRet() ); } } // sub // { WyTimeSpec a2,a3; if((r=a2.reset(-400,-400))!=Ok) { WY_THROW(r); } a3.reset(a2); if((r=a2.sub(500,500))!=Ok) { WY_THROW(r); } if((r=a3.sub(WyTimeSpec(500,500)))!=Ok) { WY_THROW(r); } CHK_EQU(a2,a3); if((a2.nano()!=-900)||(a2.second()!=-900)) { WY_THROW( WyRet() ); } if((r=a2.sub(a2))!=Ok) { WY_THROW(r); } CHK_DEFAULT(a2); a2.reset( WyTimeSpec(MinSecond,0) ); a3=a2; if((r=a2.sub(WyTimeSpec(0,0)))!=Ok) { WY_THROW(r); } if(a2!=a3) { WY_THROW( WyRet() ); } } // mul { const signed long SS(100000), NN(999999999); WyTimeSpec tmp(SS,NN); if((r=tmp.mul((signed long)10000))!=Ok) { WY_THROW(r); } if(tmp!=WyTimeSpec(1000009999,999990000)) { WY_THROW( WyRet() ); } if((r=tmp.div((signed long)10000))!=Ok) { WY_THROW(r); } if(tmp!=WyTimeSpec(SS,NN)) { WY_THROW( WyRet() ); } tmp.reset(-SS,-NN); if((r=tmp.mul((signed long)10000))!=Ok) { WY_THROW(r); } if(tmp!=WyTimeSpec(-1000009999,-999990000)) { WY_THROW( WyRet() ); } if((r=tmp.div((signed long)10000))!=Ok) { WY_THROW(r); } if(tmp!=WyTimeSpec(-SS,-NN)) { WY_THROW( WyRet() ); } tmp.reset(-SS,-NN); if((r=tmp.mul((signed long)1000000000))!=Wym_ERANGE) { WY_THROW(r); } if((r=tmp.div((signed long)0))!=Wym_EDIVZERO) { WY_THROW(r); } const WyTimeSpec CT(MinSecond,0); tmp.reset(CT); if((r=tmp.mul((long)1))!=Ok) { WY_THROW(r); } if(tmp!=CT) { WY_THROW( WyRet() ); } } // mul(const WyTimeSpec&), div(const WyTimeSpec&) { const WyTimeSpec t1(123,456000000L); WyTimeSpec tmp(t1); if((r=tmp.mul(t1))!=Ok) { WY_THROW( WyRet() ); } if(tmp!=WyTimeSpec(15241,383936000L)) { WY_THROW( WyRet() ); } if((r=tmp.mul( WyTimeSpec(0,-500000000L)))!=Ok) { WY_THROW( WyRet() ); } if(tmp!=WyTimeSpec(- 7620, -691968000L)) { WY_THROW( WyRet() ); } tmp.reset(std::numeric_limits<WyTimeSpec>::max()); if((r=tmp.mul( WyTimeSpec(1,0) ))!=Ok) { WY_THROW( WyRet() ); } if(tmp!=std::numeric_limits<WyTimeSpec>::max()) { WY_THROW( WyRet() ); } if((r=tmp.mul( WyTimeSpec(-1,0) ))!=Ok) { WY_THROW( WyRet() ); } if(-tmp!=std::numeric_limits<WyTimeSpec>::max()) { WY_THROW( WyRet() ); } if((r=tmp.mul( WyTimeSpec(-1,0) ))!=Ok) { WY_THROW( WyRet() ); } if(tmp!=std::numeric_limits<WyTimeSpec>::max()) { WY_THROW( WyRet() ); } if((r=tmp.mul( WyTimeSpec(0,999999999) ))!=Ok) { WY_THROW( WyRet() ); } if(tmp>=std::numeric_limits<WyTimeSpec>::max()) { WY_THROW( WyRet() ); }/* if((r=tmp.div( WyTimeSpec(0,-500000000L)))!=Ok) { WY_THROW( WyRet() ); } if(tmp!=WyTimeSpec(15241,383936000L)) { std::cerr << tmp.second() << ", " << tmp.nano() << '\n'; WY_THROW( WyRet() ); } if((r=tmp.div(t1))!=Ok) { WY_THROW( WyRet() ); } if(tmp!=t1) { WY_THROW( WyRet() ); }*/ tmp=t1; if((r=tmp.mul( WyTimeSpec(MaxSecond,0)))!=Wym_ERANGE) { WY_THROW( WyRet() ); } if(tmp!=t1) { WY_THROW( WyRet() ); } if((r=tmp.mul( WyTimeSpec(MinSecond,0)))!=Wym_ERANGE) { WY_THROW( WyRet() ); } if(tmp!=t1) { WY_THROW( WyRet() ); } } // mul/div { WyTimeSpec tmp(1,MinNano); if(tmp.mul((long)999)!=Ok) { WY_THROW(r); } if(tmp.div((long)999)!=Ok) { WY_THROW(r); } if(tmp!=WyTimeSpec(1,MinNano)) { WY_THROW( WyRet() ); } const WyTimeSpec CT(MinSecond,0); tmp.reset(CT); if((r=tmp.div(1))!=Ok) { WY_THROW(r); } if(tmp!=CT) { WY_THROW( WyRet() ); } } // Simple test for of Wy::now // { //std::cout << Wy::wrd(Wy::now()) << std::endl; WyTimeSpec T0(1100966810,Wy_Nano()); // time this code written WyTimeSpec T_3year(T0+WyTimeSpec(3*365L*24*3600,0)); // 3 years life time WyTimeSpec tmp=Wy::now(); if(tmp<=T0) { std::cerr << "\a\aSystem time is long time ago, you might be not care\n"; r=Ok; WY_HERE(r); std::cerr << Wy::wrd(r).c_str() << std::endl; } if(tmp>=T_3year) { std::cerr << "\a\aNot updated for 3 years, might be Ok.\n"; r=Ok; WY_HERE(r); std::cerr << Wy::wrd(r).c_str() << std::endl; } } // check that WyTimeSpec(0,0) relates to 1970-1-1 0000 // { time_t tm_t(0); //struct tm* tsp=::localtime(&tm_t); struct tm* tsp=::gmtime(&tm_t); if(tsp==NULL) { WY_THROW( WyRet() ); } try { if(tsp->tm_year!=70) { WY_THROW( WyRet() ); } if(tsp->tm_mon!=0) { WY_THROW( WyRet() ); } if(tsp->tm_mday!=1) { WY_THROW( WyRet() ); } if(tsp->tm_hour!=0) { WY_THROW( WyRet() ); } if(tsp->tm_min!=0) { WY_THROW( WyRet() ); } if(tsp->tm_sec!=0) { WY_THROW( WyRet() ); } if(tsp->tm_wday!=4) { WY_THROW( WyRet() ); } if(tsp->tm_yday!=0) { WY_THROW( WyRet() ); } } catch(...) { std::cerr << " -- WyTimeSpec(0,0) not relate to 1970-1-1 0000\n"; dump_tm(tsp); throw; }; } // Test set_systime // if(::getuid()==0) { // this is root WyTimeSpec tm0=Wy::now()-WyTimeSpec(Wy_Second(10),Wy_Nano()); // tm to set systime WyTimeSpec tm1; WyTimeSpec tm_now=Wy::now(); if((r=Wy::set_systime(tm0))!=Ok) { // set time 10 secs earlier WY_THROW(r); } tm1=Wy::now(); // read back if((r=Wy::set_systime(tm_now))!=Ok) {// recover time WY_THROW(r); } if(((tm0-tm1)>WyTimeSpec(Wy_Second(),60000000))|| // diff>0.06sec ((tm0-tm1)<WyTimeSpec(Wy_Second(),-60000000))) { // diff>0.06sec std::cerr << Wy::wrd(tm0).c_str() << "!=" << Wy::wrd(tm1).c_str() << std::endl; WY_THROW( WyRet() ); } } else { // non-superuser cannot set systeim time WyTimeSpec tm_now=Wy::now(); if((r=Wy::set_systime(tm_now))!=Wym_EPERM) { WY_THROW(r); } } // double/float conversion /* { WyTimeSpec tm("123.456e1"); double a(tm); if((a<123.4559e1)||(a>123.4561e1)) { std::cerr << "tm= " << Wy::wrd(tm) << std::endl; std::cerr << "a= " << a << std::endl; WY_THROW( WyRet() ); } float b(tm); if((b<123.4559e1)||(b>123.4561e1)) { std::cerr << "tm= " << Wy::wrd(tm) << std::endl; std::cerr << "b= " << b << std::endl; WY_THROW( WyRet() ); } }*/ // chk move ctor { char buf[2*sizeof(WyTimeSpec)]; ::memset(buf,0,sizeof(buf)); WyTimeSpec*p=reinterpret_cast<WyTimeSpec*>(buf); new(p) WyTimeSpec(23,4); new(p+1) WyTimeSpec(*p,Wy::ByMove); if(*(p+1)!=WyTimeSpec(23,4)) { (p+1)->~WyTimeSpec(); WY_THROW( WyRet() ); } (p+1)->~WyTimeSpec(); }};/* Test errors (set_systime is not tested)*/static void t2(void){ WyRet r; // // constructor Wym_ERANGE // try { WyTimeSpec ttm(MaxSecond,LongMax); // should overflow ttm.reset(); // supress compiler waring from above WY_THROW( WyRet() ); } catch(const WyTimeSpec::Reply& e) { if(e!=Wym_ERANGE) { WY_THROW( WyRet() ); } }; // constructor Wym_ERANGE // try { WyTimeSpec ttm(MinSecond,LongMin); // should underflow ttm.reset(); // supress compiler waring from above WY_THROW( WyRet() ); } catch(const WyTimeSpec::Reply& e) { if(e!=Wym_ERANGE) { WY_THROW( WyRet() ); } }; // constructor Wym_EFAULT // try { WyTimeSpec ttm((const char*)0); ttm.reset(); // supress compiler waring from above WY_THROW( WyRet() ); } catch(const WyCSeg::Reply& e) { if(e!=Wym_EFAULT) { WY_THROW( WyRet() ); } }; // constructor Wym_EFAULT // try { WyTimeSpec ttm(WyCSeg((const char*)0,size_t(0))); ttm.reset(); // supress compiler waring from above WY_THROW( WyRet() ); } catch(const WyCSeg::Reply& e) { if(e!=Wym_EFAULT) { WY_THROW( WyRet() ); } }; // ctor(double) // try { WyTimeSpec ttm(double(1e90)); // should overflow ttm.reset(); // supress compiler waring from above WY_THROW( WyRet() ); } catch(const WyTimeSpec::Reply& e) { if(e!=Wym_ERANGE) { WY_THROW( WyRet() ); } }; try { WyTimeSpec ttm(double(-1e90)); // should overflow ttm.reset(); // supress compiler waring from above WY_THROW( WyRet() ); } catch(const WyTimeSpec::Reply& e) { if(e!=Wym_ERANGE) { WY_THROW( WyRet() ); } }; // Test reset(Wy_Second,Wy_Nano) overflow // { WyTimeSpec ttm; if((r=ttm.reset(MaxSecond,999999999L))!=Ok) { // this should be the max value WY_THROW(r); } if((r=ttm.reset(MaxSecond,1000000000L))!=Wym_ERANGE) { // this should overflow WY_THROW( WyRet() ); } if(ttm!=WyTimeSpec(MaxSecond,999999999L)) { WY_THROW( WyRet() ); // error modified } if((r=ttm.reset(MinSecond,-999999999L))!=Ok) { // this should be the min value WY_THROW(r); } if((r=ttm.reset(MinSecond,-2000000001L))!=Wym_ERANGE) { // this should no be Ok WY_THROW( WyRet() ); } } // reset(const char*) error { WyTimeSpec t0("12.34"); WyTimeSpec t1(t0); if((r=t1.reset(""))!=Wym_ENOENT) { WY_THROW(r); } if((r=t1.reset("abc"))!=Wym_ENOENT) { WY_THROW(r); } if(t1!=t0) { WY_THROW( WyRet() ); } if((r=t1.reset("-0.1234567891"))!=Ok) { WY_THROW(r); } if(t1!=WyTimeSpec("-0.123456789")) { // fraction digits > 9 truncated WY_THROW( WyRet() ); } } // reset(const char*,size_t) error { WyTimeSpec t0(WyCSeg("12.34",5)); WyTimeSpec t1(t0); if((r=t1.reset(WyCSeg("",1)))!=Wym_ENOENT) { WY_THROW(r); } if((r=t1.reset("abc"))!=Wym_ENOENT) { WY_THROW(r); } if(t1!=t0) { WY_THROW( WyRet() ); } if((r=t1.reset(WyCSeg("123456789123456789",18)))!=Wym_ERANGE) { WY_THROW(r); } } // operator=(const char*) // try { WyTimeSpec ttm; ttm="helo"; WY_THROW( WyRet() ); } catch(const WyTimeSpec::Reply& e) { if(e!=Wym_ENOENT) { WY_THROW( WyRet() ); } }; // operator-() // try { WyTimeSpec ttm(MinSecond,0); ttm=-ttm; if(ttm<=WyTimeSpec(MinSecond,0)) { WY_THROW( WyRet() ); } } catch(const WyTimeSpec::Reply& e) { if(e!=Wym_EMATHNEG) { WY_THROW( WyRet() ); } }; // operator++() // try { WyTimeSpec ttm(MaxSecond,0); ++ttm; WY_THROW( WyRet() ); } catch(const WyTimeSpec::Reply& e) { if(e!=Wym_ERANGE) { WY_THROW( WyRet() ); } }; // operator--() // try { WyTimeSpec ttm(MinSecond,0); --ttm; WY_THROW( WyRet() ); } catch(const WyTimeSpec::Reply& e) { if(e!=Wym_ERANGE) { WY_THROW( WyRet() ); } }; // operator++(int) // try { WyTimeSpec ttm(MaxSecond,0); ttm++; WY_THROW( WyRet() ); } catch(const WyTimeSpec::Reply& e) { if(e!=Wym_ERANGE) { WY_THROW( WyRet() ); } }; // operator--(int) // try { WyTimeSpec ttm(MinSecond,0); ttm--; WY_THROW( WyRet() ); } catch(const WyTimeSpec::Reply& e) { if(e!=Wym_ERANGE) { WY_THROW( WyRet() ); } }; // operator+=(WyTimeSpec) // try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -