Home | History | Annotate | Download | only in func.wrap.func.alg
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // <functional>
     11 
     12 // class function<R(ArgTypes...)>
     13 
     14 // template <MoveConstructible  R, MoveConstructible ... ArgTypes>
     15 //   void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;
     16 
     17 
     18 #include <functional>
     19 #include <cstdlib>
     20 #include <cassert>
     21 
     22 #include "test_macros.h"
     23 #include "count_new.hpp"
     24 
     25 class A
     26 {
     27     int data_[10];
     28 public:
     29     static int count;
     30 
     31     explicit A(int j)
     32     {
     33         ++count;
     34         data_[0] = j;
     35     }
     36 
     37     A(const A& a)
     38     {
     39         ++count;
     40         for (int i = 0; i < 10; ++i)
     41             data_[i] = a.data_[i];
     42     }
     43 
     44     ~A() {--count;}
     45 
     46     int operator()(int i) const
     47     {
     48         for (int j = 0; j < 10; ++j)
     49             i += data_[j];
     50         return i;
     51     }
     52 
     53     int id() const {return data_[0];}
     54 };
     55 
     56 int A::count = 0;
     57 
     58 int g(int) {return 0;}
     59 int h(int) {return 1;}
     60 
     61 int main()
     62 {
     63     assert(globalMemCounter.checkOutstandingNewEq(0));
     64     {
     65     std::function<int(int)> f1 = A(1);
     66     std::function<int(int)> f2 = A(2);
     67 #if TEST_STD_VER >= 11
     68     static_assert(noexcept(swap(f1, f2)), "" );
     69 #endif
     70     assert(A::count == 2);
     71     assert(globalMemCounter.checkOutstandingNewEq(2));
     72     assert(f1.target<A>()->id() == 1);
     73     assert(f2.target<A>()->id() == 2);
     74     swap(f1, f2);
     75     assert(A::count == 2);
     76     assert(globalMemCounter.checkOutstandingNewEq(2));
     77     assert(f1.target<A>()->id() == 2);
     78     assert(f2.target<A>()->id() == 1);
     79     }
     80     assert(A::count == 0);
     81     assert(globalMemCounter.checkOutstandingNewEq(0));
     82     {
     83     std::function<int(int)> f1 = A(1);
     84     std::function<int(int)> f2 = g;
     85 #if TEST_STD_VER >= 11
     86     static_assert(noexcept(swap(f1, f2)), "" );
     87 #endif
     88     assert(A::count == 1);
     89     assert(globalMemCounter.checkOutstandingNewEq(1));
     90     assert(f1.target<A>()->id() == 1);
     91     assert(*f2.target<int(*)(int)>() == g);
     92     swap(f1, f2);
     93     assert(A::count == 1);
     94     assert(globalMemCounter.checkOutstandingNewEq(1));
     95     assert(*f1.target<int(*)(int)>() == g);
     96     assert(f2.target<A>()->id() == 1);
     97     }
     98     assert(A::count == 0);
     99     assert(globalMemCounter.checkOutstandingNewEq(0));
    100     {
    101     std::function<int(int)> f1 = g;
    102     std::function<int(int)> f2 = A(1);
    103 #if TEST_STD_VER >= 11
    104     static_assert(noexcept(swap(f1, f2)), "" );
    105 #endif
    106     assert(A::count == 1);
    107     assert(globalMemCounter.checkOutstandingNewEq(1));
    108     assert(*f1.target<int(*)(int)>() == g);
    109     assert(f2.target<A>()->id() == 1);
    110     swap(f1, f2);
    111     assert(A::count == 1);
    112     assert(globalMemCounter.checkOutstandingNewEq(1));
    113     assert(f1.target<A>()->id() == 1);
    114     assert(*f2.target<int(*)(int)>() == g);
    115     }
    116     assert(A::count == 0);
    117     assert(globalMemCounter.checkOutstandingNewEq(0));
    118     {
    119     std::function<int(int)> f1 = g;
    120     std::function<int(int)> f2 = h;
    121 #if TEST_STD_VER >= 11
    122     static_assert(noexcept(swap(f1, f2)), "" );
    123 #endif
    124     assert(A::count == 0);
    125     assert(globalMemCounter.checkOutstandingNewEq(0));
    126     assert(*f1.target<int(*)(int)>() == g);
    127     assert(*f2.target<int(*)(int)>() == h);
    128     swap(f1, f2);
    129     assert(A::count == 0);
    130     assert(globalMemCounter.checkOutstandingNewEq(0));
    131     assert(*f1.target<int(*)(int)>() == h);
    132     assert(*f2.target<int(*)(int)>() == g);
    133     }
    134     assert(A::count == 0);
    135     assert(globalMemCounter.checkOutstandingNewEq(0));
    136 }
    137