📄 ck_array.h
字号:
/* Copyright is licensed under GNU LGPL. by IJ. Wang 2006 Basic Wy_Array chk function template. The check with ck_array() is minimal, for it is meant to be common.*/#ifndef WY__CK_ARRAY_H__#define WY__CK_ARRAY_H__#include "wy_array.h"#include <iostream>template<typename T>bool wy__ck_array_chk_default(const T& vec){ if(vec.begin()!=vec.end()) { return(false); } if(vec.size()!=0) { return(false); } if(vec.is_default()==false) { WY_THROW( WyRet() ); } if(vec.begin()!=vec.end()) { WY_THROW( WyRet() ); } if(vec.subseg().size()!=0) { WY_THROW( WyRet() ); } if(vec._capacity()<T::min_capacity()) { WY_THROW( WyRet() ); } { T tmp; if(vec!=tmp) { WY_THROW( WyRet() ); } if(vec==tmp) { } else { WY_THROW( WyRet() ); } } return(true);};#define WY__CK_ARRAY_CHK_DEFAULT(expr) if(wy__ck_array_chk_default(expr)==false) { WY_THROW(WyRet() ); };template<typename EleType>void ck_array(void){ { Wy_Array<EleType> tmp; WY__CK_ARRAY_CHK_DEFAULT(tmp); if(tmp.begin()!=tmp.end()) { WY_THROW( WyRet() ); } if(tmp._capacity()!=Wy_Array<EleType>::min_capacity()) { WY_THROW( WyRet() ); } } // chk Wy_Array(const Wy_Array&) { Wy_Array<EleType> tmp1; WY__CK_ARRAY_CHK_DEFAULT(tmp1); Wy_Array<EleType> tmp2(tmp1); WY__CK_ARRAY_CHK_DEFAULT(tmp2); tmp2.push_back(EleType()); Wy_Array<EleType> tmp3(tmp2); if(tmp2!=tmp3) { WY_THROW( WyRet() ); } if(tmp3.size()!=1) { WY_THROW( WyRet() ); } for(int i=0; i<100; ++i) { tmp2.push_back(EleType()); } Wy_Array<EleType> tmp4(tmp2); if(tmp4!=tmp2) { WY_THROW( WyRet() ); } if(tmp4.size()!=tmp2.size()) { WY_THROW( WyRet() ); } if(tmp4._capacity()<tmp4.size()) { WY_THROW( WyRet() ); } } // chk Wy_Array(const WySeg<T>&), Wy_Array(const WySeg<const T>&) { const EleType ele_arr[3]={EleType(),EleType(),EleType()}; Wy_Array<EleType> tmp( WySeg<const EleType>(ele_arr,3) ); if(tmp._capacity()<tmp.min_capacity()) { WY_THROW( WyRet() ); }; if(tmp.size()!=3) { WY_THROW( WyRet() ); }; EleType ele2_arr[8]; Wy_Array<EleType> tmp2( WySeg<EleType>(ele2_arr,8) ); if(tmp2._capacity()<tmp2.min_capacity()) { WY_THROW( WyRet() ); }; if(tmp2.size()!=8) { WY_THROW( WyRet() ); }; } // chk Wy_Array(size_t,const EleType&) { Wy_Array<EleType> tmp1(40,EleType()),tmp2(tmp1); if(tmp1.size()!=40) { WY_THROW( WyRet() ); } if(tmp2.size()!=tmp1.size()) { WY_THROW( WyRet() ); } try { Wy_Array<EleType> tmp( Wy_Array<EleType>::max_capacity()+1,EleType()); WY_THROW( WyRet() ); } catch(const typename Wy_Array<EleType>::Reply& e) { if(e!=Wym_EFBIG) { throw; } }; } // chk subseg { Wy_Array<EleType> tmp; WY__CK_ARRAY_CHK_DEFAULT(tmp); try { tmp.subseg(1).size(); WY_THROW( WyRet() ); } catch(const typename Wy_Array<EleType>::Reply& e) { if(e!=Wym_EINVAL) { WY_THROW( WyRet(e) ); } } catch(...) { throw; }; try { tmp.subseg(1,1).size(); WY_THROW( WyRet() ); } catch(const typename Wy_Array<EleType>::Reply& e) { if(e!=Wym_EINVAL) { WY_THROW( WyRet(e) ); } } catch(...) { throw; }; tmp.resize(3); if(tmp.size()!=3) { WY_THROW( WyRet() ); } if(tmp.subseg().size()!=tmp.size()) { WY_THROW( WyRet() ); } if(tmp.subseg(0)!=tmp.subseg()) { WY_THROW( WyRet() ); } } // chk reset(void) { Wy_Array<EleType> tmp1; WY__CK_ARRAY_CHK_DEFAULT(tmp1); tmp1.reset(); WY__CK_ARRAY_CHK_DEFAULT(tmp1); Wy_Array<EleType> tmp2(1,EleType()); if(tmp2.size()!=1) { WY_THROW( WyRet() ); } tmp2.reset(); WY__CK_ARRAY_CHK_DEFAULT(tmp2); } // chk reset(const Wy_Array&) { Wy_Array<EleType> tmp1; WY__CK_ARRAY_CHK_DEFAULT(tmp1); Wy_Array<EleType> tmp2; tmp2.reset(tmp1); WY__CK_ARRAY_CHK_DEFAULT(tmp2); Wy_Array<EleType> tmp3(10,EleType()); if(tmp3.size()!=10) { WY_THROW( WyRet() ); } tmp2.reset(tmp3); if(tmp2!=tmp3) { WY_THROW( WyRet() ); } tmp2.reset(tmp2); // self reset if(tmp2==tmp3) { } else { WY_THROW( WyRet() ); } } // chk reset(const WySeg<>&) { EleType arr1[1]={}; EleType arr2[2]={}; WySeg<EleType> ss1(arr1,1); const WySeg<EleType> ss2(arr2,2); Wy_Array<EleType> tmp1(ss1); Wy_Array<EleType> tmp2(ss2); Wy_Array<EleType> 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 typename Wy_Array<EleType>::Reply& e) { if(e!=Wym_ELOOP) { WY_THROW( WyRet(e) ); } } catch(...) { throw; }; } // chk reset(size_t,const T&) { Wy_Array<EleType> tmp; WY__CK_ARRAY_CHK_DEFAULT(tmp); tmp.reset(0,EleType()); WY__CK_ARRAY_CHK_DEFAULT(tmp); int n_elem; for(n_elem=0; n_elem<65; ++n_elem) { tmp.reset(n_elem,EleType()); if(tmp.size()!=size_t(n_elem)) { WY_THROW( WyRet() ); } if(tmp._capacity()<Wy_Array<EleType>::min_capacity()) { WY_THROW( WyRet() ); } if(tmp._capacity()<size_t(n_elem)) { WY_THROW( WyRet() ); } } for(n_elem=65; n_elem>0; --n_elem) { tmp.reset(n_elem,EleType()); if(tmp.size()!=size_t(n_elem)) { WY_THROW( WyRet() ); } if(tmp._capacity()<Wy_Array<EleType>::min_capacity()) { WY_THROW( WyRet() ); } if(tmp._capacity()<size_t(n_elem)) { WY_THROW( WyRet() ); } } try { tmp.reset(Wy_Array<EleType>::max_capacity()+1,EleType()); WY_THROW( WyRet() ); } catch(const typename Wy_Array<EleType>::Reply& e) { if(e!=Wym_EFBIG) { throw; } }; } // chk operator=(const Wy_Array&) { Wy_Array<EleType> tmp1; WY__CK_ARRAY_CHK_DEFAULT(tmp1); Wy_Array<EleType> tmp2; tmp2=tmp1; WY__CK_ARRAY_CHK_DEFAULT(tmp2); Wy_Array<EleType> tmp3(10,EleType()); if(tmp3.size()!=10) { WY_THROW( WyRet() ); } tmp2=tmp3; if(tmp2!=tmp3) { WY_THROW( WyRet() ); } tmp2=tmp2; // self assign if(tmp2==tmp3) { } else { WY_THROW( WyRet() ); } } // chk operator=(const WySeg<>&) { EleType arr1[1]={}; EleType arr2[2]={}; WySeg<EleType> ss1(arr1,1); const WySeg<EleType> ss2(arr2,2); Wy_Array<EleType> tmp1(ss1); Wy_Array<EleType> tmp2(ss2); Wy_Array<EleType> tmp3; WY__CK_ARRAY_CHK_DEFAULT(tmp3); if((tmp1.size()!=1)||(tmp2.size()!=2)) { WY_THROW( WyRet() ); } if(tmp1==tmp2) { WY_THROW( WyRet() ); } if(&(tmp3=ss1)!=&tmp3) { WY_THROW( WyRet() ); } if(tmp3!=tmp1) { WY_THROW( WyRet() ); } tmp3=ss2; if(tmp3!=tmp2) { WY_THROW( WyRet() ); } tmp2=ss2; // non-overlapped if(tmp2.size()!=2) { WY_THROW( WyRet() ); // test precondition failure }; try { tmp2=tmp2.subseg(); // overlapped reset WY_THROW( WyRet() ); } catch(const typename Wy_Array<EleType>::Reply& e) { if(e!=Wym_ELOOP) { WY_THROW( WyRet(e) ); } }; } // chk swap(Wy_Array&) { Wy_Array<EleType> tmp1,tmp2; WY__CK_ARRAY_CHK_DEFAULT(tmp1); WY__CK_ARRAY_CHK_DEFAULT(tmp2); tmp1.swap(tmp1); WY__CK_ARRAY_CHK_DEFAULT(tmp1); tmp1.swap(tmp2); WY__CK_ARRAY_CHK_DEFAULT(tmp1); WY__CK_ARRAY_CHK_DEFAULT(tmp2); Wy_Array<EleType> tmp3(10,EleType()); tmp1.swap(tmp3); if(tmp1.size()!=10) { WY_THROW( WyRet() ); } WY__CK_ARRAY_CHK_DEFAULT(tmp3); } // chk err front() { Wy_Array<EleType> tmp1; WY__CK_ARRAY_CHK_DEFAULT(tmp1); try { tmp1.front(); WY_THROW( WyRet() ); } catch(const typename Wy_Array<EleType>::Reply& e) { WY__CK_ARRAY_CHK_DEFAULT(tmp1); if(e!=Wym_ENOENT) { WY_THROW( WyRet(e) ); } } catch(...) { WY_THROW( WyRet() ); }; } // chk err back() { Wy_Array<EleType> tmp1; WY__CK_ARRAY_CHK_DEFAULT(tmp1); try { tmp1.back(); WY_THROW( WyRet() ); } catch(const typename Wy_Array<EleType>::Reply& e) { if(e!=Wym_ENOENT) { WY_THROW( WyRet(e) ); } } catch(...) { WY_THROW( WyRet() ); }; } // chk err operator[] { Wy_Array<EleType> tmp1; WY__CK_ARRAY_CHK_DEFAULT(tmp1); try { tmp1[0]; WY_THROW( WyRet() ); } catch(const typename Wy_Array<EleType>::Reply& e) { WY__CK_ARRAY_CHK_DEFAULT(tmp1); if(e!=Wym_EINVAL) { WY_THROW( WyRet(e) ); } } catch(...) { WY_THROW( WyRet() ); }; tmp1.push_back( EleType() ); if(tmp1.size()!=1) { WY_THROW( WyRet() ); } try { tmp1[tmp1.size()]; WY_THROW( WyRet() ); } catch(const typename Wy_Array<EleType>::Reply& e) { if(e!=Wym_EINVAL) { WY_THROW( WyRet(e) ); } if(tmp1.size()!=1) { WY_THROW( WyRet() ); } } catch(...) { WY_THROW( WyRet() ); }; } // chk push_back { Wy_Array<EleType> tmp; WY__CK_ARRAY_CHK_DEFAULT(tmp); size_t cap( (tmp._capacity()<<1)+1 ); for(size_t i=0; i<cap; ++i) { tmp.push_back( EleType() ); } if(tmp.size()!=cap) { WY_THROW( WyRet() ); } } // chk pop_back { Wy_Array<EleType> tmp; WY__CK_ARRAY_CHK_DEFAULT(tmp); tmp.pop_back(); WY__CK_ARRAY_CHK_DEFAULT(tmp);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -