Home | History | Annotate | Download | only in func.wrap.func.con
      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<class F, class A> function(allocator_arg_t, const A&, F);
     15 
     16 #include <functional>
     17 #include <cassert>
     18 
     19 #include "min_allocator.h"
     20 #include "test_allocator.h"
     21 #include "count_new.hpp"
     22 #include "../function_types.h"
     23 
     24 class DummyClass {};
     25 
     26 template <class FuncType, class AllocType>
     27 void test_FunctionObject(AllocType& alloc)
     28 {
     29     assert(globalMemCounter.checkOutstandingNewEq(0));
     30     {
     31     FunctionObject target;
     32     assert(FunctionObject::count == 1);
     33     assert(globalMemCounter.checkOutstandingNewEq(0));
     34     std::function<FuncType> f2(std::allocator_arg, alloc, target);
     35     assert(FunctionObject::count == 2);
     36     assert(globalMemCounter.checkOutstandingNewEq(1));
     37     assert(f2.template target<FunctionObject>());
     38     assert(f2.template target<FuncType>() == 0);
     39     assert(f2.template target<FuncType*>() == 0);
     40     }
     41     assert(FunctionObject::count == 0);
     42     assert(globalMemCounter.checkOutstandingNewEq(0));
     43 }
     44 
     45 
     46 template <class FuncType, class AllocType>
     47 void test_FreeFunction(AllocType& alloc)
     48 {
     49     assert(globalMemCounter.checkOutstandingNewEq(0));
     50     {
     51     FuncType* target = &FreeFunction;
     52     assert(globalMemCounter.checkOutstandingNewEq(0));
     53     std::function<FuncType> f2(std::allocator_arg, alloc, target);
     54     assert(globalMemCounter.checkOutstandingNewEq(0));
     55     assert(f2.template target<FuncType*>());
     56     assert(*f2.template target<FuncType*>() == target);
     57     assert(f2.template target<FuncType>() == 0);
     58     assert(f2.template target<DummyClass>() == 0);
     59     }
     60     assert(globalMemCounter.checkOutstandingNewEq(0));
     61 }
     62 
     63 template <class TargetType, class FuncType, class AllocType>
     64 void test_MemFunClass(AllocType& alloc)
     65 {
     66     assert(globalMemCounter.checkOutstandingNewEq(0));
     67     {
     68     TargetType target = &MemFunClass::foo;
     69     assert(globalMemCounter.checkOutstandingNewEq(0));
     70     std::function<FuncType> f2(std::allocator_arg, alloc, target);
     71     assert(globalMemCounter.checkOutstandingNewEq(0));
     72     assert(f2.template target<TargetType>());
     73     assert(*f2.template target<TargetType>() == target);
     74     assert(f2.template target<FuncType*>() == 0);
     75     }
     76     assert(globalMemCounter.checkOutstandingNewEq(0));
     77 }
     78 
     79 template <class Alloc>
     80 void test_for_alloc(Alloc& alloc) {
     81     test_FunctionObject<int()>(alloc);
     82     test_FunctionObject<int(int)>(alloc);
     83     test_FunctionObject<int(int, int)>(alloc);
     84     test_FunctionObject<int(int, int, int)>(alloc);
     85 
     86     test_FreeFunction<int()>(alloc);
     87     test_FreeFunction<int(int)>(alloc);
     88     test_FreeFunction<int(int, int)>(alloc);
     89     test_FreeFunction<int(int, int, int)>(alloc);
     90 
     91     test_MemFunClass<int(MemFunClass::*)() const, int(MemFunClass&)>(alloc);
     92     test_MemFunClass<int(MemFunClass::*)(int) const, int(MemFunClass&, int)>(alloc);
     93     test_MemFunClass<int(MemFunClass::*)(int, int) const, int(MemFunClass&, int, int)>(alloc);
     94 }
     95 
     96 int main()
     97 {
     98     {
     99         bare_allocator<DummyClass> bare_alloc;
    100         test_for_alloc(bare_alloc);
    101     }
    102     {
    103         non_default_test_allocator<DummyClass> non_default_alloc(42);
    104         test_for_alloc(non_default_alloc);
    105     }
    106 }
    107