//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <functional>
// class function<R(ArgTypes...)>
// template <MoveConstructible R, MoveConstructible ... ArgTypes>
// void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&);
#include<functional>#include<new>#include<cstdlib>#include<cassert>intnew_called=0;void*operatornew(std::size_ts)throw(std::bad_alloc){++new_called;returnstd::malloc(s);}voidoperatordelete(void*p)throw(){--new_called;std::free(p);}classA{intdata_[10];public:staticintcount;explicitA(intj){++count;data_[0]=j;}A(constA&a){++count;for(inti=0;i<10;++i)data_[i]=a.data_[i];}~A(){--count;}intoperator()(inti)const{for(intj=0;j<10;++j)i+=data_[j];returni;}intid()const{returndata_[0];}};intA::count=0;intg(int){return0;}inth(int){return1;}intmain(){assert(new_called==0);{std::function<int(int)>f1=A(1);std::function<int(int)>f2=A(2);assert(A::count==2);assert(new_called==2);assert(f1.target<A>()->id()==1);assert(f2.target<A>()->id()==2);swap(f1,f2);assert(A::count==2);assert(new_called==2);assert(f1.target<A>()->id()==2);assert(f2.target<A>()->id()==1);}assert(A::count==0);assert(new_called==0);{std::function<int(int)>f1=A(1);std::function<int(int)>f2=g;assert(A::count==1);assert(new_called==1);assert(f1.target<A>()->id()==1);assert(*f2.target<int(*)(int)>()==g);swap(f1,f2);assert(A::count==1);assert(new_called==1);assert(*f1.target<int(*)(int)>()==g);assert(f2.target<A>()->id()==1);}assert(A::count==0);assert(new_called==0);{std::function<int(int)>f1=g;std::function<int(int)>f2=A(1);assert(A::count==1);assert(new_called==1);assert(*f1.target<int(*)(int)>()==g);assert(f2.target<A>()->id()==1);swap(f1,f2);assert(A::count==1);assert(new_called==1);assert(f1.target<A>()->id()==1);assert(*f2.target<int(*)(int)>()==g);}assert(A::count==0);assert(new_called==0);{std::function<int(int)>f1=g;std::function<int(int)>f2=h;assert(A::count==0);assert(new_called==0);assert(*f1.target<int(*)(int)>()==g);assert(*f2.target<int(*)(int)>()==h);swap(f1,f2);assert(A::count==0);assert(new_called==0);assert(*f1.target<int(*)(int)>()==h);assert(*f2.target<int(*)(int)>()==g);}assert(A::count==0);assert(new_called==0);}