📄 stl_memory测试代码.cpp
字号:
//////////////////////////////////////////////////////////////////
//*test for library of <memory>
//@author 付彦伟
//
/////////////////////////////////////////////////////////////////
#include"stdafx.h"
#include<iostream>
#include<assert.h>
#include<memory>
#include<new>
using namespace std;
static size_t cnt;
class Myint{
public:
Myint(int value)
:val(value){++cnt;}
Myint(const Myint & x)
:val(x.val){++cnt;}
~Myint(){--cnt;}
int get_val() const
{ return(val);}
private:
int val;
};
typedef allocator<float> Myal;
Myal get_al()
{ return(Myal());}
void test_alloc()
{
float f1;
Myal::size_type *p_size=(size_t *)0;
Myal::difference_type *p_val=(ptrdiff_t *)0;
Myal::pointer *p_ptr=(float **)0;
Myal::const_pointer *p_cptr=(const float **)0;
Myal::reference p_ref=f1;
Myal::const_reference p_cref=(const float &)f1;
Myal::value_type *p_dist=(float *)0;
// Myal::rebind<int>::other *p_iptr=(int **)0;
Myal al0(get_al()),al(al0);
allocator<void>::pointer *pv_ptr=(void **)0;
allocator<void>::value_type *pv_dist=(void *)0;
allocator<void>::const_pointer *pv_cptr=(const void **)0;
// Myal::rebind<int>::other *pv_iptr=(int **)0;
allocator<void> alv0,alv(alv0);
alv=alv0;
float *pf1=al0.address(f1);
assert(pf1==&f1);
pf1=al.allocate(3,0);
al.construct(&pf1[2],2.0F);
assert(pf1[2]==2.0F);
al.destroy(pf1);
al.deallocate(pf1,1);
assert(0<al.max_size());
assert(al0==al);
assert(!(al0!=al));
al.destroy(pf1);
al.deallocate(pf1,1);
}
void test_unint()
{
cnt=0;
Myint *p=(Myint *)operator new(6* sizeof(Myint));
uninitialized_fill(p,p+2,3);
assert(p[1].get_val()==5&&cnt==2);
uninitialized_fill_n(p+2,2,5);
assert(p[3].get_val()==5&&cnt==4);
assert(uninitialized_copy(p+1,p+3,p+4)==p+6);
assert(p[4].get_val()==3&&cnt==6);
assert(p[5].get_val()==5);
operator delete(p);
}
void test_tempbuf()
{
/////////////////////////////////////////////////////////////
//这个语句的用法一定要注意!!
pair<short* ,ptrdiff_t> tbuf=get_temporary_buffer<short>(5);
////////////////////////////////////////////////////////////
assert(tbuf.first!=0&&tbuf.second==5);
typedef raw_storage_iterator<short *,short> Rit;
Rit::iter_type *p_iter=(short **)0;
Rit::element_type *p_elem=(short *)0;
Rit it(tbuf.first);
for(int i=0;i<5;i++)
{
*it++=i;
assert(tbuf.first[i]==i);
}
return_temporary_buffer(tbuf.first);
}
void test_autoptr()
{
cnt=0;
typedef auto_ptr<Myint> Myptr;
Myptr::element_type *p_elem=(Myint *)0;
Myptr p0;
assert(p0.get()==0);
{
Myptr p1(new Myint(3));
Myint *p=p1.get();
assert(cnt==1);
assert(p->get_val()==3
&&(*p1).get_val()==3
&& p1.release()->get_val()==3
&&p1.get()==0);
delete(p);
assert(cnt==0);
}
assert(cnt==0);
{
Myptr p2(new Myint(5));
assert(cnt==1);
}
assert(cnt==0);
{
Myptr p3(new Myint(7)),p4(p3);
assert(cnt==1);
assert(p3.get()==0&&p4.get()->get_val()==7);
p3=p4;
assert(p4.get()==NULL&&p3.get()->get_val()==7);
p3.reset();
assert(p3.get()==NULL&& cnt==0);
p4.reset();
assert(p4.get()==0);
}
assert(cnt==0);
}
int _tmain()
{
test_alloc();
test_autoptr();
test_tempbuf();
test_unint();
cout<<"OK!"<<endl;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -