📄 ck_str.cpp
字号:
/* Copyright is licensed under GNU LGPL. by I.J.Wang 2004*/#include "wystr.h"#include <iostream>#include <cstdlib>#include <cmath>#if WYSTR_VERSION!=31#error Test code is for WYSTR_VERSION 31#endif#ifdef WY_DEBUG#include <string>#define StdNPos std::string::npos#endifstatic bool chk_default(const WyStr& str){ if(str._capacity()<WyStr::min_capacity()) { WY_THROW( WyRet() ); } if(str.c_str()!=str.data()) { WY_THROW( WyRet() ); } static const WyStr S0; if(str.is_default()==false) { return(false); } if(str==S0) { } else { return(false); } if(str!=S0) { return(false); } if(str.size()!=0) { return(false); } if(str.begin()!=&str[0]) { return(false); } if(str.begin()!=str.end()) { return(false); } const char* cptr; cptr=str.data(); if(cptr==0) { WY_THROW(WyRet() ); } if(*cptr!=0) { return(false); } cptr=str.c_str(); if(cptr==0) { WY_THROW(WyRet() ); } if(*cptr!=0) { return(false); } return(true);};static bool chk_not_default(const WyStr& str){ if(str._capacity()<WyStr::min_capacity()) { WY_THROW( WyRet() ); } if(str.c_str()!=str.data()) { WY_THROW( WyRet() ); } static const WyStr S0; if(str.is_default()) { return(false); } if(str!=S0) { } else { return(false); } if(str==S0) { return(false); } if(str.size()<=0) { return(false); } if(str.begin()==str.end()) { return(false); } const char* cptr; cptr=str.data(); if(cptr==0) { WY_THROW(WyRet() ); } if(cptr[str.size()]!=0) { WY_THROW(WyRet() ); } cptr=str.c_str(); if(cptr==0) { WY_THROW(WyRet() ); } if(cptr[str.size()]!=0) { WY_THROW(WyRet() ); } return(true);};#define CHK_DEFAULT(expr) if(chk_default(expr)==false) { WY_THROW(WyRet() ); };#define CHK_NOT_DEFAULT(expr) if(chk_not_default(expr)==false) { WY_THROW(WyRet() ); };static void t1(void){ WyRet r; // default constructor { WyStr tmp; CHK_DEFAULT(tmp); WyStr tmp2(tmp); CHK_DEFAULT(tmp2); WyStr tmp3; CHK_DEFAULT(tmp3); WyStr tmp4(tmp3); CHK_DEFAULT(tmp4); } // copy/from cstr constructor { WyStr tmp("abc"); CHK_NOT_DEFAULT(tmp); if(tmp.size()!=3) { WY_THROW(WyRet() ); } if((tmp[0]!='a')||(tmp[1]!='b')||(tmp[2]!='c')||(tmp[3]!=0)) { WY_THROW(WyRet() ); } WyStr tmp2(tmp); if(tmp==tmp2) { } else { WY_THROW(WyRet() ); } if(tmp!=tmp2) { WY_THROW(WyRet() ); } else { } if(std::memcmp(tmp.data(),tmp.c_str(),tmp.size()+1)!=0) { WY_THROW(WyRet() ); } if(std::memcmp(tmp.data(),tmp2.data(),tmp.size()+1)!=0) { WY_THROW(WyRet() ); } WyStr tmp3(""); CHK_DEFAULT(tmp3); WyStr tmp4(tmp2); if(tmp4==tmp2) { } else { WY_THROW(WyRet() ); } if(tmp4!=tmp2) { WY_THROW(WyRet() ); } else { } } // WyStr(const WyStr&,size_t) { WyStr Frm("12345"); WyStr tmp(Frm.cseg(0,100)); if(tmp!=Frm) { WY_THROW(WyRet() ); } WyStr tmp1(Frm.cseg(1,100)); if(tmp1!="2345") { WY_THROW(WyRet() ); } WyStr tmp2(Frm.cseg(Frm.size(),1)); if(tmp2.is_default()==false) { WY_THROW(WyRet() ); } } // WyStr(const WyStr&,size_t,size_t) { WyStr S_abcd("abcd"); WyStr tmp(S_abcd.cseg(0,0)); CHK_DEFAULT(tmp); WyStr tmp1(S_abcd.cseg(1,2)); if(tmp1!="bc") { WY_THROW(WyRet() ); } WyStr tmp2(S_abcd.cseg(2,5)); if(tmp2!="cd") { WY_THROW(WyRet() ); } } // WyStr(const char*,size_t) { WyStr tmp( WyCSeg("abcd",size_t(0)) ); CHK_DEFAULT(tmp); WyStr tmp1( WyCSeg("abcd",1) ); if(tmp1!="a") { WY_THROW(WyRet() ); } WyStr tmp2( WyCSeg("abcd",2) ); if(tmp2!="ab") { WY_THROW(WyRet() ); } } // WyStr(size_t,char) { WyStr tmp(size_t(0),char('a')); CHK_DEFAULT(tmp); WyStr tmp1(size_t(1),char('b')); if(tmp1!="b") { WY_THROW(WyRet() ); } WyStr tmp2(size_t(100),char('c')); CHK_NOT_DEFAULT(tmp2); for(size_t i=0; i<tmp2.size(); ++i) { if(tmp2[i]!='c') { WY_THROW(WyRet() ); } } if(tmp2[tmp2.size()]!=0) { WY_THROW(WyRet() ); } } // operator ==(const char*) { const char* CPtr="4567"; WyStr tmp(CPtr); if(tmp==(const char*)CPtr) { } else { WY_THROW( WyRet() ); } if(tmp!=(const char*)CPtr) { WY_THROW( WyRet() ); } if(tmp==(const char*)"uuuuu") { WY_THROW( WyRet() ); } if(tmp!=(const char*)"uuuuu") { } else { WY_THROW( WyRet() ); } } // operator[] { WyStr tmp0; CHK_DEFAULT(tmp0); if(tmp0[0]!=0) { WY_THROW(WyRet() ); } CHK_DEFAULT(tmp0); WyStr tmp1("123"); CHK_NOT_DEFAULT(tmp1); char* tp=&tmp1[0]; if((tp[0]!='1')||(tp[1]!='2')||(tp[2]!='3')||(tp[3]!=0)) { WY_THROW(WyRet() ); } CHK_NOT_DEFAULT(tmp1); if((tmp1[0]!='1')||(tmp1[1]!='2')||(tmp1[2]!='3')||(tmp1[3]!=0)) { WY_THROW(WyRet() ); } CHK_NOT_DEFAULT(tmp1); if(std::memcmp(tmp0.data(),tmp0.c_str(),tmp0.size()+1)!=0) { WY_THROW(WyRet() ); } } // at { WyStr tmp0; CHK_DEFAULT(tmp0); WyStr tmp1("123"); CHK_NOT_DEFAULT(tmp1); char* tp=&tmp1.at(0); if((tp[0]!='1')||(tp[1]!='2')||(tp[2]!='3')||(tp[3]!=0)) { WY_THROW(WyRet() ); } CHK_NOT_DEFAULT(tmp1); if((tmp1.at(0)!='1')||(tmp1.at(1)!='2')||(tmp1.at(2)!='3')) { WY_THROW(WyRet() ); } CHK_NOT_DEFAULT(tmp1); } // begin/end { WyStr tmp0; CHK_DEFAULT(tmp0); if(tmp0.begin()!=&tmp0[0]) { WY_THROW(WyRet()); } if(tmp0.begin()!=tmp0.end()) { WY_THROW(WyRet()); } WyStr tmp1("abcx"); CHK_NOT_DEFAULT(tmp1); if(tmp1.end()==&tmp1[0]) { WY_THROW(WyRet()); } if(tmp1.begin()==tmp1.end()) { WY_THROW(WyRet()); } if((tmp1.end()-tmp1.begin())!=(int)tmp1.size()) { WY_THROW(WyRet()); } } // reset(const WyStr&) { WyStr tmp,tmp0; WyStr tmp1("rst"); CHK_DEFAULT(tmp); CHK_DEFAULT(tmp0); CHK_NOT_DEFAULT(tmp1); if((r=tmp.reset(tmp))!=Ok) { WY_THROW(r); } CHK_DEFAULT(tmp); if((r=tmp.reset(tmp0))!=Ok) { WY_THROW(r); } CHK_DEFAULT(tmp); if((r=tmp.reset(tmp1))!=Ok) { WY_THROW(r); } CHK_NOT_DEFAULT(tmp); if(tmp!=WyStr("rst")) { WY_THROW(WyRet() ); } if(tmp!=tmp1) { WY_THROW(WyRet() ); } if((r=tmp.reset(tmp))!=Wym_ELOOP) { WY_THROW(r); } if(tmp!=tmp1) { WY_THROW(WyRet() ); } } // reset(const WyStr&,size_t) { WyStr Frm("12345"),tmp; if((r=tmp.reset(Frm.cseg(0)))!=Ok) { WY_THROW(WyRet() ); } if(tmp!=Frm) { WY_THROW(WyRet() ); } if((r=tmp.reset(Frm.cseg(1,100)))!=Ok) { WY_THROW(WyRet() ); } if(tmp!="2345") { WY_THROW(WyRet() ); } if((r=tmp.reset(Frm.cseg(Frm.size(),1)))!=Ok) { WY_THROW(WyRet() ); } if(tmp.is_default()==false) { WY_THROW(WyRet() ); } try { r=tmp.reset( Frm.cseg(1000,1) ); WY_THROW( WyRet() ); } catch(const WyStr::Reply& e) { if(e!=Wym_EINVAL) { WY_THROW(r); } // FALL_THROUGH }; } // reset(const WyStr&,size_t,size_t) { WyStr tmp,tmp0; WyStr tmp1("rst"); CHK_DEFAULT(tmp); CHK_DEFAULT(tmp0); CHK_NOT_DEFAULT(tmp1); if((r=tmp.reset(tmp0.cseg(0,0)))!=Ok) { WY_THROW(r); } CHK_DEFAULT(tmp); if((r=tmp.reset(tmp0.cseg(0,2)))!=Ok) { WY_THROW(r); } CHK_DEFAULT(tmp); if((r=tmp.reset(tmp1.cseg(0,5)))!=Ok) { WY_THROW(r); } CHK_NOT_DEFAULT(tmp); if(tmp!=WyStr("rst")) { WY_THROW(WyRet() ); } if(tmp!=tmp1) { WY_THROW(WyRet() ); } if((r=tmp.reset(tmp))!=Wym_ELOOP) { WY_THROW(r); } if(tmp!=tmp1) { WY_THROW(WyRet() ); } { const char* TStr="abcdefg1234567"; const WyStr TmpA(TStr); if((r=tmp.reset(TmpA))!=Ok) { WY_THROW(r); } if(tmp!=TStr) { WY_THROW(WyRet() ); } if((r=tmp.reset(tmp.cseg(1,100)))!=Wym_ELOOP) { WY_THROW(r); } if((r=tmp.reset(tmp.cseg(tmp.size()-1,1)))!=Ok) { // This is imp. feature, src/des on the same buffer though WY_THROW(r); } } } // reset(const char*,size_t) { WyStr tmp; const char* cstr("rst"); CHK_DEFAULT(tmp); if((r=tmp.reset( WyCSeg("a",size_t(0))) )!=Ok) { WY_THROW(r); } CHK_DEFAULT(tmp); if((r=tmp.reset( WyCSeg("a",1)) )!=Ok) { WY_THROW(r); } if(tmp!="a") { WY_THROW(r); } if((r=tmp.reset( WyCSeg(cstr,2)) )!=Ok) { WY_THROW(r); } if(tmp!="rs") { WY_THROW(WyRet() ); } if((r=tmp.reset( WyCSeg(cstr,3)) )!=Ok) { WY_THROW(r); } CHK_NOT_DEFAULT(tmp); if(tmp!="rst") { WY_THROW(WyRet() ); } if((r=tmp.reset( WyStr(tmp.cseg(1,1)) ) )!=Ok) { WY_THROW(r); } if(tmp!="s") { WY_THROW(WyRet() ); } if((r=tmp.reset("abc123"))!=Ok) { WY_THROW(r); } if(tmp!="abc123") { WY_THROW(WyRet() ); } if((r=tmp.reset( WyStr(tmp.cseg(1,5))) )!=Ok) { WY_THROW(r); } if(tmp!="bc123") { WY_THROW(WyRet() ); } } // reset(const char*) { WyStr tmp; const char* cstr("rst"); CHK_DEFAULT(tmp); if((r=tmp.reset(""))!=Ok) { WY_THROW(WyRet() ); } CHK_DEFAULT(tmp); if((r=tmp.reset(cstr))!=Ok) { WY_THROW(WyRet() ); } if(tmp!="rst") { WY_THROW(WyRet() ); } if((r=tmp.reset( WyStr(tmp.cseg(0)).c_str()))!=Ok) { WY_THROW(WyRet() ); } if(tmp!="rst") { WY_THROW(WyRet() ); } const char* c2="123455344343g"; if((r=tmp.reset(c2))!=Ok) { WY_THROW(WyRet() ); } if(tmp!=c2) { WY_THROW(WyRet() ); } } // reset(size_t,char) { WyStr tmp; CHK_DEFAULT(tmp); if((r=tmp.reset(0,'a'))!=Ok) { WY_THROW(WyRet() ); } CHK_DEFAULT(tmp); if((r=tmp.reset(1,'a'))!=Ok) { WY_THROW(WyRet() ); } if(tmp!="a") { WY_THROW(WyRet() ); } size_t ca=tmp._capacity()+1; if((r=tmp.reset(ca,'b'))!=Ok) { WY_THROW(WyRet() ); } if(tmp.size()!=ca) { WY_THROW(WyRet() ); } for(size_t i=0; i<ca; ++i) { if(tmp[i]!='b') { WY_THROW(WyRet() ); } } // reallocate { const char* PrePtr=tmp.data(); if((r=tmp.reset(WyStr::min_capacity(),'a'))!=Ok) { WY_THROW( WyRet() ); } if(tmp.data()!=PrePtr) { WY_THROW( WyRet() ); } if((r=tmp.reset(tmp._capacity()+1,'x'))!=Ok) { WY_THROW( WyRet() ); } if(tmp.data()==PrePtr) { WY_THROW( WyRet() ); } } } // operator =(const char*) { WyStr tmp; const char* cstr("rst"); CHK_DEFAULT(tmp); tmp=""; CHK_DEFAULT(tmp); tmp=cstr; if(tmp!="rst") { WY_THROW(WyRet() ); } tmp= WyStr(tmp.cseg(0)).c_str(); if(tmp!="rst") { WY_THROW(WyRet() ); } const char* c2="123455344343g"; tmp=c2; if(tmp!=c2) { WY_THROW(WyRet() ); } } // operator =(const WyStr&) { WyStr tmp,tmp0; WyStr tmp1("rst"); CHK_DEFAULT(tmp); CHK_DEFAULT(tmp0); CHK_NOT_DEFAULT(tmp1); tmp=tmp; CHK_DEFAULT(tmp); tmp=tmp0; CHK_DEFAULT(tmp); tmp=tmp1; CHK_NOT_DEFAULT(tmp); if(tmp!=WyStr("rst")) { WY_THROW(WyRet() ); } if(tmp!=tmp1) { WY_THROW(WyRet() ); } } // swap { WyStr tmp,tmp0; WyStr tmp1("ab"),tmp2("1234"); CHK_DEFAULT(tmp); CHK_DEFAULT(tmp0); CHK_NOT_DEFAULT(tmp1); CHK_NOT_DEFAULT(tmp2); tmp.swap(tmp0); CHK_DEFAULT(tmp); CHK_DEFAULT(tmp0); tmp.swap(tmp0); CHK_DEFAULT(tmp); CHK_DEFAULT(tmp0); tmp.swap(tmp1); if(tmp!="ab") { WY_THROW(WyRet() ); } CHK_DEFAULT(tmp1); tmp.swap(tmp1); if(tmp1!="ab") { WY_THROW(WyRet() ); } CHK_DEFAULT(tmp0); tmp1.swap(tmp2); if(tmp1!="1234") { WY_THROW(WyRet() ); } if(tmp2!="ab") { WY_THROW(WyRet() ); } tmp1.swap(tmp2); if(tmp1!="ab") { WY_THROW(WyRet() ); } if(tmp2!="1234") { WY_THROW(WyRet() ); } } // append(const WyStr&) { WyStr tmp,tmp0,tmp00(""); const WyStr tmp1("ab"),tmp2("1234"); CHK_DEFAULT(tmp); CHK_DEFAULT(tmp0); CHK_NOT_DEFAULT(tmp1); CHK_NOT_DEFAULT(tmp2); if((r=tmp.append(tmp))!=Ok) { WY_THROW(r); } CHK_DEFAULT(tmp); if((r=tmp.append(tmp00))!=Ok) { WY_THROW(r); } CHK_DEFAULT(tmp); if((r=tmp.append(tmp1))!=Ok) { WY_THROW(r); } if(tmp!=tmp1) { WY_THROW(WyRet() ); } CHK_NOT_DEFAULT(tmp); if((r=tmp.append(tmp1))!=Ok) { WY_THROW(r); } if(tmp!="abab") { WY_THROW(WyRet() ); } if((r=tmp.append(tmp2))!=Ok) { WY_THROW(r); } if(tmp!="abab1234") { WY_THROW(WyRet() ); } WyStr tmp5("abc"); if((r=tmp5.append(tmp5))!=Ok) { WY_THROW(r); } if(tmp5!="abcabc") { WY_THROW(WyRet() );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -