Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 28, 2015, 6:57:26 PM (9 years ago)
Author:
landauf
Message:

use the existing class 'FunctorPointer' to store the callable object. functors with callable objects (e.g. lambdas) are now treated as static functors.
this also fixes an issue on gcc: because of a bug, gcc was not able to store the function pointer of a lambda's operator() https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51048. storing the whole object is apparently a workaround for this issue.

Note: tests currently fail on MSVC because it doesn't accept std::function as a callable object.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/cpp11_v2/test/core/command/FunctorTest.cc

    r10984 r10986  
    7171        int f4a(int arg) { return arg*2; }
    7272        int f4b(int arg) { return arg*3; }
     73        int multiply(int a, int b) { return a*b; }
    7374
    7475        struct CallableStruct
     
    634635    ////////////////////////////// Various tests //////////////////////////////
    635636
     637    TEST_F(FunctorTest, CreateFunctorWithVariousFunctionTypes)
     638    {
     639        FunctorPtr c1 = createFunctor(&f4a);
     640        FunctorPtr c2 = createFunctor(&f4b);
     641        FunctorPtr c3 = createFunctor([] (int arg) { return arg*4; });
     642        auto lambda = [] (int arg1, int arg2) { return arg1*arg2; };
     643        FunctorPtr c4 = createFunctor(lambda);
     644
     645        std::function<int()> bind0 = std::bind(multiply, 4, 6);
     646        std::function<int(int)> bind1 = std::bind(multiply, std::placeholders::_1, 7);
     647        std::function<int(int, int)> bind2 = std::bind(multiply, std::placeholders::_1, std::placeholders::_2);
     648        std::function<int(int)> function = f4a;
     649
     650        FunctorPtr c5 = createFunctor(bind0);
     651        FunctorPtr c6 = createFunctor(bind1);
     652        FunctorPtr c7 = createFunctor(bind2);
     653        FunctorPtr c8 = createFunctor(function);
     654
     655
     656        EXPECT_EQ(8,  (*c1)(4).get<int>());
     657        EXPECT_EQ(12, (*c2)(4).get<int>());
     658        EXPECT_EQ(16, (*c3)(4).get<int>());
     659        EXPECT_EQ(20, (*c4)(4, 5).get<int>());
     660        EXPECT_EQ(24, (*c5)(4).get<int>());
     661        EXPECT_EQ(28, (*c6)(4).get<int>());
     662        EXPECT_EQ(32, (*c7)(4, 8).get<int>());
     663        EXPECT_EQ(8,  (*c8)(4).get<int>());
     664
     665        EXPECT_EQ(1, c1->getParamCount());
     666        EXPECT_EQ(1, c2->getParamCount());
     667        EXPECT_EQ(1, c3->getParamCount());
     668        EXPECT_EQ(2, c4->getParamCount());
     669        EXPECT_EQ(0, c5->getParamCount());
     670        EXPECT_EQ(1, c6->getParamCount());
     671        EXPECT_EQ(2, c7->getParamCount());
     672        EXPECT_EQ(1, c8->getParamCount());
     673
     674        EXPECT_EQ(typeid(int(*)(int)), c1->getFullIdentifier());
     675        EXPECT_EQ(typeid(int(*)(int)), c2->getFullIdentifier());
     676        EXPECT_EQ(typeid(int(*)(int)), c3->getFullIdentifier());
     677        EXPECT_EQ(typeid(int(*)(int, int)), c4->getFullIdentifier());
     678        EXPECT_EQ(typeid(int(*)()), c5->getFullIdentifier());
     679        EXPECT_EQ(typeid(int(*)(int)), c6->getFullIdentifier());
     680        EXPECT_EQ(typeid(int(*)(int, int)), c7->getFullIdentifier());
     681        EXPECT_EQ(typeid(int(*)(int)), c8->getFullIdentifier());
     682
     683        EXPECT_EQ(typeid(int(*)(int)), c1->getHeaderIdentifier());
     684        EXPECT_EQ(typeid(int(*)(int)), c2->getHeaderIdentifier());
     685        EXPECT_EQ(typeid(int(*)(int)), c3->getHeaderIdentifier());
     686        EXPECT_EQ(typeid(int(*)(int, int)), c4->getHeaderIdentifier());
     687        EXPECT_EQ(typeid(int(*)()), c5->getHeaderIdentifier());
     688        EXPECT_EQ(typeid(int(*)(int)), c6->getHeaderIdentifier());
     689        EXPECT_EQ(typeid(int(*)(int, int)), c7->getHeaderIdentifier());
     690        EXPECT_EQ(typeid(int(*)(int)), c8->getHeaderIdentifier());
     691
     692        EXPECT_EQ(typeid(int(*)(int)), c1->getHeaderIdentifier(1));
     693        EXPECT_EQ(typeid(int(*)(int)), c2->getHeaderIdentifier(1));
     694        EXPECT_EQ(typeid(int(*)(int)), c3->getHeaderIdentifier(1));
     695        EXPECT_EQ(typeid(int(*)(int)), c4->getHeaderIdentifier(1));
     696        EXPECT_EQ(typeid(int(*)()), c5->getHeaderIdentifier(1));
     697        EXPECT_EQ(typeid(int(*)(int)), c6->getHeaderIdentifier(1));
     698        EXPECT_EQ(typeid(int(*)(int)), c7->getHeaderIdentifier(1));
     699        EXPECT_EQ(typeid(int(*)(int)), c8->getHeaderIdentifier(1));
     700
     701        EXPECT_EQ(typeid(int(*)()), c1->getHeaderIdentifier(0));
     702        EXPECT_EQ(typeid(int(*)()), c2->getHeaderIdentifier(0));
     703        EXPECT_EQ(typeid(int(*)()), c3->getHeaderIdentifier(0));
     704        EXPECT_EQ(typeid(int(*)()), c4->getHeaderIdentifier(0));
     705        EXPECT_EQ(typeid(int(*)()), c5->getHeaderIdentifier(0));
     706        EXPECT_EQ(typeid(int(*)()), c6->getHeaderIdentifier(0));
     707        EXPECT_EQ(typeid(int(*)()), c7->getHeaderIdentifier(0));
     708        EXPECT_EQ(typeid(int(*)()), c8->getHeaderIdentifier(0));
     709    }
     710
     711    TEST_F(FunctorTest, CanCallFunctorWithLambdaWichIsAlreadyOutOfScope)
     712    {
     713        int var1 = 1;
     714        FunctorPtr fp1a = createFunctor([&var1] () { return var1++; });
     715        FunctorPtr fp1b = createFunctor([&var1] () { return var1; });
     716        FunctorPtr fp2;
     717        FunctorPtr fp3;
     718
     719        {
     720            int var2 = 2;
     721            fp2 = createFunctor([var2] () { return var2; });
     722        }
     723        {
     724            int var3 = 3;
     725            fp3 = createFunctor([var3] () { return var3; });
     726        }
     727
     728        EXPECT_EQ(1, var1);
     729        EXPECT_EQ(1, (*fp1a)().get<int>());
     730        EXPECT_EQ(2, var1);
     731        EXPECT_EQ(2, (*fp1b)().get<int>());
     732
     733        EXPECT_EQ(2, (*fp2)().get<int>());
     734        EXPECT_EQ(3, (*fp3)().get<int>());
     735    }
     736
    636737    TEST_F(FunctorTest, canCompareHeaderIdentifiers)
    637738    {
Note: See TracChangeset for help on using the changeset viewer.