📄 chk_array.cpp
字号:
/* Copyright is licensed under GNU LGPL. by IJ. Wang 2006 Build: make chk_array*/#include "wy_array.h"#include "wy_uty.h"#include "wystr.h"#include "wytimespec.h"#include "wydate.h"#include "wyfilestat.h"#include "wydirent.h"#include "wytermios.h"#include "wyselect.h"#include "wy_thread.h"#include "ck_array.h"// Test object as element of Wy_Arrayclass TestBase { const int& _ref; public: TestBase(const int& v) : _ref(v) {}; TestBase(TestBase& src) : _ref(src._ref) {}; TestBase(TestBase& src, Wy::ByMove_t) : _ref(src._ref) {}; const int& ref(void) const { return _ref; };};class Element : TestBase { int _v; const int _cpycnt; // number that *this is copy constructed static int _objcnt; public: Element() : TestBase(_v),_v(0),_cpycnt(1) { ++_objcnt; }; Element(const Element& s) : TestBase(_v), _v(s._v), _cpycnt(s._cpycnt+1) { ++_objcnt; }; // [P] when base is move constructed, s is move destructed from the base, // which is the reverse order of normal object destruction // Element(Element& s, Wy::ByMove_t) : TestBase(s,Wy::ByMove), _v(s._v), _cpycnt(s._cpycnt) { /* No ctor/dtor semantics: ++_objcnt; s.~Element(); */ }; Element(int v) : TestBase(_v),_v(v),_cpycnt(1) { ++_objcnt; }; ~Element() { --_objcnt; }; bool is_default(void) const { return _v==0; }; int value(void) const { return _v; }; int copy_count(void) const { return _cpycnt; }; void reset(int v) { _v=v; }; const Element& operator =(int v) { _v=v; return *this; }; const Element& operator =(const Element& rhs) { _v=rhs._v; return *this; }; bool operator <(const Element& rhs) const { return this->_v<rhs._v; }; bool operator ==(const Element& rhs) const { return this->_v==rhs._v; }; bool operator !=(const Element& rhs) const { return this->_v!=rhs._v; }; void swap(Element& v) { int tmp(_v); _v=v._v; v._v=tmp; }; static int obj_cnt(void) { return _objcnt; };};int Element::_objcnt=0;// Copy constructor throw Reply for test//class ErrCpyCtor { int _v; public: WY_THROW_REPLY; ErrCpyCtor() : _v(1000) {}; ErrCpyCtor(const ErrCpyCtor& rhs) : _v(rhs._v) { throw Reply(); }; ErrCpyCtor(ErrCpyCtor& rhs,Wy::ByMove_t) : _v(rhs._v) {}; bool operator== (const ErrCpyCtor& rhs) const { return _v==rhs._v; }; bool operator!= (const ErrCpyCtor& rhs) const { return _v!=rhs._v; };};#define CHK_DEFAULT WY__CK_ARRAY_CHK_DEFAULTstatic void t0(void){};static void t1(){ if(Element::obj_cnt()!=0) { WY_THROW( WyRet() ); } if((Wy_Array<Element>::min_capacity()<1) || (Wy_Array<Element>::min_capacity()>=Wy_Array<Element>::max_capacity())) { WY_THROW( WyRet() ); } if(Wy_Array<Element>::max_capacity()>INT_MAX/sizeof(Element)-1) { WY_THROW( WyRet() ); } WyRet r; Wy_Array<Element> tmp; if(Element::obj_cnt()!=0) { WY_THROW( WyRet() ); } if(tmp._capacity()!=4) { // predefined value WY_THROW( WyRet() ); } if(tmp.is_default()==false) { WY_THROW( WyRet() ); } if(tmp!=tmp) { WY_THROW( WyRet() ); } if(tmp==tmp) { } else { WY_THROW( WyRet() ); } CHK_DEFAULT(tmp); // chk ctor(size_t,const Element&) { Wy_Array<Element> tmp(1,Element()); if(tmp._capacity()<tmp.min_capacity()) { WY_THROW( WyRet() ); }; if(tmp.size()!=1) { WY_THROW( WyRet() ); }; for(size_t i=0; i<tmp.size(); ++i) { if(tmp[i].is_default()==false) { WY_THROW( WyRet() ); }; } Wy_Array<Element> tmp2(tmp.min_capacity()+1,Element()); if(tmp2._capacity()<tmp2.min_capacity()+1) { WY_THROW( WyRet() ); }; if(tmp2.size()<tmp2.min_capacity()+1) { WY_THROW( WyRet() ); }; for(size_t i=0; i<tmp2.size(); ++i) { if(tmp2[i].is_default()==false) { WY_THROW( WyRet() ); }; } size_t ncap( 2*Wy_Array<Element>::min_capacity() ); Wy_Array<Element> tmp3(ncap,Element(100)); if(tmp3._capacity()<ncap) { WY_THROW( WyRet() ); }; for(size_t i=0; i<tmp3.size(); ++i) { if(tmp3[i].is_default()==true) { WY_THROW( WyRet() ); }; if(tmp3[i]!=100) { WY_THROW( WyRet() ); }; } try { Wy_Array<Element> tmp( Wy_Array<Element>::max_capacity()+1,Element()); WY_THROW( WyRet() ); } catch(const Wy_Array<Element>::Reply& e) { if(e!=Wym_EFBIG) { throw; } }; } // chk ctor(const WySeg<T>&) { const Element ele_arr[3]; Wy_Array<Element> tmp( WySeg<const Element>(ele_arr,3) ); if(tmp._capacity()<tmp.min_capacity()) { WY_THROW( WyRet() ); }; if(tmp.size()!=3) { WY_THROW( WyRet() ); }; for(size_t i=0; i<tmp.size(); ++i) { if(tmp[i].is_default()==false) { WY_THROW( WyRet() ); }; } Element ele2_arr[8]; Wy_Array<Element> tmp2( WySeg<Element>(ele2_arr,8) ); if(tmp2._capacity()<tmp2.min_capacity()) { WY_THROW( WyRet() ); }; if(tmp2.size()!=8) { WY_THROW( WyRet() ); }; for(size_t i=0; i<tmp2.size(); ++i) { if(tmp2[i].is_default()==false) { WY_THROW( WyRet() ); }; } } // chk copy ctor (1) { Wy_Array<Element> tmp2(tmp); CHK_DEFAULT(tmp2); } // chk error push_back() { Wy_Array<ErrCpyCtor> tmp1; Wy_Array<ErrCpyCtor> tmp2(tmp1); if(tmp2!=tmp1) { WY_THROW( WyRet() ); } try { tmp2.push_back( ErrCpyCtor() ); WY_THROW( WyRet() ); } catch(const ErrCpyCtor::Reply& e) { if(tmp2!=tmp1) { WY_THROW( WyRet() ); } if(e.is_default()==false) { WY_THROW( WyRet(e) ); } } catch(...) { WY_THROW( WyRet() ); }; } // chk push_back { tmp.push_back( 53 ); if(tmp.is_default()==true) { WY_THROW( WyRet() ); } if(tmp.size()!=1) { WY_THROW( WyRet() ); } if(tmp[0].value()!=53) { WY_THROW( WyRet() ); } if(Element::obj_cnt()!=1) { Wy::cout << "objcnt= " << Element::obj_cnt() << '\n'; WY_THROW( WyRet() ); } tmp.push_back( 22 ); if(tmp.is_default()==true) { WY_THROW( WyRet() ); } if(tmp.size()!=2) { WY_THROW( WyRet() ); } if(tmp[1].value()!=22) { WY_THROW( WyRet() ); } tmp.push_back(tmp.back()); if(tmp.is_default()==true) { WY_THROW( WyRet() ); } if(tmp.size()!=3) { WY_THROW( WyRet() ); } if(tmp[0].value()!=53) { WY_THROW( WyRet() ); } if(tmp[1].value()!=22) { WY_THROW( WyRet() ); } if(tmp[2].value()!=22) { WY_THROW( WyRet() ); } tmp.push_back(tmp[0]); if(tmp.back()!=tmp[0]) { WY_THROW( WyRet() ); } tmp.pop_back(); tmp.pop_back(); //if(Element::obj_cnt()!=0) { // Wy::cout << "objcnt= " << Element::obj_cnt() << '\n'; // WY_THROW( WyRet() ); //} const size_t org_size=tmp.size(); const size_t new_size=tmp._capacity()+1; const int o_cpycnt= tmp[0].copy_count(); while(tmp.size()<new_size) { tmp.push_back(44); } if(o_cpycnt!=tmp[0].copy_count()) { WY_THROW( WyRet() ); // tmp[0] was modified by the copy ctor } tmp.resize(org_size); } // chk copy ctor (2) { Wy_Array<Element> tmp2(tmp); if(tmp2.size()!=tmp.size()) { WY_THROW( WyRet() ); } for(size_t i=0; i<tmp.size(); ++i) { if(tmp[i]!=tmp2[i]) { WY_THROW( WyRet() ); } } } if(Element::obj_cnt()!=2) { Wy::cout << "objcnt= " << Element::obj_cnt() << '\n'; WY_THROW( WyRet() ); } // chk push_back/copy constructor (reallocate) tmp.reset(); CHK_DEFAULT(tmp); for(int i=0; i<1000000; ++i) { tmp.push_back(i); if(tmp._capacity()<tmp.size()) { WY_THROW( WyRet() ); } if((int)tmp.size()!=i+1) { WY_THROW( WyRet() ); } if(tmp[i].value()!=i) { WY_THROW( WyRet() ); } } { const Element* p=tmp.end(); if(p!=tmp.begin()) { do { --p; } while(p!=tmp.begin()); } Element* vp=tmp.end(); if(vp!=tmp.begin()) { do { --vp; } while(vp!=tmp.begin()); } } { Wy_Array<Element> t2(tmp); if(t2.size()!=tmp.size()) { WY_THROW( WyRet() ); } for(size_t i=0; i<t2.size(); ++i) { if(tmp[i].value()!=t2[i].value()) { WY_THROW( WyRet() ); } } } while(tmp.size()) { tmp.pop_back(); }; CHK_DEFAULT(tmp); // chk front/back/ { tmp.reset(); CHK_DEFAULT(tmp); tmp.push_back(44); if(tmp.front()!=44) { WY_THROW( WyRet() ); } if(tmp.back()!=44) { WY_THROW( WyRet() ); } tmp.push_back(55); if(tmp.front()!=44) { WY_THROW( WyRet() ); } if(tmp.back()!=55) { WY_THROW( WyRet() ); } tmp.front()=1; tmp.back()=2; if(tmp.front()!=1) { WY_THROW( WyRet() ); } if(tmp.back()!=2) { WY_THROW( WyRet() ); } } // chk subseg { Wy_Array<Element> tmp; CHK_DEFAULT(tmp); try { tmp.subseg(1).size(); WY_THROW( WyRet() ); } catch(const Wy_Array<Element>::Reply& e) { if(e!=Wym_EINVAL) { WY_THROW( WyRet(e) ); } }; try { tmp.subseg(1,1).size(); WY_THROW( WyRet() ); } catch(const Wy_Array<Element>::Reply& e) { if(e!=Wym_EINVAL) { WY_THROW( WyRet(e) ); } }; tmp.resize(3); if(tmp.size()!=3) { WY_THROW( WyRet() ); } } // chk _reserve { Wy_Array<Element> tmp; const size_t NCap=1000; CHK_DEFAULT(tmp); if(tmp._capacity()==NCap) { WY_THROW( WyRet() ); // may be ok (default capacity not specified) } if((r=tmp._reserve(NCap))!=Ok) { WY_THROW(r); } if(tmp._capacity()!=NCap) { WY_THROW( WyRet() ); } CHK_DEFAULT(tmp); if((r=tmp._reserve(NCap-1))!=Ok) { WY_THROW(r); } if(tmp._capacity()!=NCap) { WY_THROW( WyRet() ); } CHK_DEFAULT(tmp); } // chk erase(size_t,size_t) { Wy_Array<Element> tmp; CHK_DEFAULT(tmp); tmp.push_back(10); tmp.push_back(11); tmp.push_back(12); tmp.push_back(13); if(tmp.size()!=4) { WY_THROW( WyRet() ); } Wy_Array<Element> tmp_cpy(tmp); if(tmp!=tmp_cpy) { WY_THROW( WyRet() ); } try { tmp.erase(tmp.size()+1,1); WY_THROW( WyRet() ); } catch(const Wy_Array<Element>::Reply& e) { if(tmp!=tmp_cpy) { WY_THROW( WyRet() ); } if(e!=Wym_EINVAL) { WY_THROW( WyRet(e) ); } } catch(...) { WY_THROW( WyRet() ); }; tmp.erase(tmp.size(),1); tmp.erase(3,0); if(tmp.size()!=4) { WY_THROW( WyRet() ); } tmp.erase(3,1); tmp_cpy.reset(tmp); if(tmp!=tmp_cpy) { WY_THROW( WyRet() ); } try { tmp.erase(4,1); WY_THROW( WyRet() ); } catch(const Wy_Array<Element>::Reply& e) { if(tmp!=tmp_cpy) { WY_THROW( WyRet() ); } if(e!=Wym_EINVAL) { WY_THROW( WyRet(e) ); } } catch(...) { WY_THROW( WyRet() ); }; tmp.erase(1,2); if(tmp.size()!=1) { WY_THROW( WyRet() ); } if(tmp[0]!=10) { WY_THROW( WyRet() ); } } // chk erase(size_t, BigNum) { Wy_Array<Element> tmp; CHK_DEFAULT(tmp); tmp.push_back(10); tmp.push_back(11); tmp.push_back(12); tmp.push_back(13); if(tmp.size()!=4) { WY_THROW( WyRet() ); } Wy_Array<Element> tmp_cpy(tmp); if(tmp!=tmp_cpy) { WY_THROW( WyRet() ); } try { tmp.erase(tmp.size()+1,999); WY_THROW( WyRet() ); } catch(const Wy_Array<Element>::Reply& e) { if(tmp!=tmp_cpy) { WY_THROW( WyRet() ); } if(e!=Wym_EINVAL) { WY_THROW( WyRet(e) ); } } catch(...) { WY_THROW( WyRet() ); }; tmp.erase(tmp.size(),999); if(tmp!=tmp_cpy) { WY_THROW( WyRet() ); } tmp.erase(3,999); if(tmp.size()!=3) { WY_THROW( WyRet() ); } tmp.erase(0,999); if(tmp.size()!=0) { WY_THROW( WyRet() ); } } // chk swap { Wy_Array<Element> tmp1,tmp2; CHK_DEFAULT(tmp1); CHK_DEFAULT(tmp2); tmp1.push_back(1); tmp1.swap(tmp2); CHK_DEFAULT(tmp1); if(tmp2[0].value()!=1) { WY_THROW( WyRet() ); } tmp2.push_back(2); tmp2[0].swap(tmp2[1]); if(tmp2[0].value()!=2) { WY_THROW( WyRet() ); } if(tmp2[1].value()!=1) { WY_THROW( WyRet() ); } tmp2.reset(tmp1); tmp2.swap(tmp2); // self-swap if(tmp2!=tmp1) { WY_THROW( WyRet() ); } } // chk reset(const Wy_Array&) { Wy_Array<Element> tmp1; CHK_DEFAULT(tmp1); Wy_Array<Element> tmp2; CHK_DEFAULT(tmp2); tmp2.reset(tmp2); CHK_DEFAULT(tmp2); tmp2.reset(tmp1); CHK_DEFAULT(tmp2); tmp1.push_back(8); tmp2.reset(tmp1); if(tmp2!=tmp1) { WY_THROW( WyRet() ); } if(tmp2._capacity()<tmp2.min_capacity()) { WY_THROW( WyRet() ); } tmp1.push_back(7); tmp2.reset(tmp1); if(tmp2==tmp1) { } else { WY_THROW( WyRet() ); } if(tmp2._capacity()<tmp2.min_capacity()) { WY_THROW( WyRet() ); } tmp2.reset(tmp2); // self-reset if(tmp2!=tmp1) { WY_THROW( WyRet() ); } if(tmp2._capacity()<tmp2.min_capacity()) { WY_THROW( WyRet() ); } } // chk reset(const WySeg<EleType>&) { Element arr1[1]={}; Element arr2[2]={}; WySeg<Element> ss1(arr1,1); const WySeg<Element> ss2(arr2,2); Wy_Array<Element> tmp1(ss1); Wy_Array<Element> tmp2(ss2); Wy_Array<Element> tmp3; WY__CK_ARRAY_CHK_DEFAULT(tmp3); if((tmp1.size()!=1)||(tmp2.size()!=2)) { WY_THROW( WyRet() ); } if(tmp1==tmp2) { WY_THROW( WyRet() ); } tmp3.reset(ss1); if(tmp3!=tmp1) { WY_THROW( WyRet() ); } tmp3.reset(ss2); if(tmp3!=tmp2) { WY_THROW( WyRet() ); } tmp2.reset(ss2); // non-overlapped try { tmp2.reset(tmp2.subseg()); // overlapped reset WY_THROW( WyRet() ); } catch(const Wy_Array<Element>::Reply& e) { if(e!=Wym_ELOOP) { WY_THROW( WyRet(e) ); } } catch(...) { throw; }; } // chk reset(size_t,const T&) { Wy_Array<Element> tmp; CHK_DEFAULT(tmp); tmp.reset(0,Element()); CHK_DEFAULT(tmp);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -