LCOV - code coverage report
Current view: top level - tests/owner - owner_test.cpp (source / functions) Hit Total Coverage
Test: iow-coverage.info Lines: 170 170 100.0 %
Date: 2019-09-16 Functions: 60 60 100.0 %

          Line data    Source code
       1             : #include <fas/testing.hpp>
       2             : #include <iow/owner/owner.hpp>
       3             : #include <boost/concept_check.hpp>
       4             : 
       5             : class foo
       6             : {
       7             : public:
       8           2 :   foo() {++ctor;}
       9          18 :   foo(const foo& ) {++cctor;}
      10          11 :   foo(foo&& ) {++mctor;}
      11             : public:
      12             :   static int ctor ;
      13             :   static int cctor ;
      14             :   static int mctor ;
      15             : };
      16             : 
      17             : int foo::ctor = 0;
      18             : int foo::cctor = 0;
      19             : int foo::mctor = 0;
      20             : 
      21             : int inc(int i);
      22             : int dec(int i);
      23             : 
      24           4 : int inc(int i)
      25             : {
      26           4 :   return i+1;
      27             : }
      28             : 
      29             : 
      30           2 : int dec(int i)
      31             : {
      32           2 :   return i-1;
      33             : }
      34             : 
      35           2 : struct finc{ int operator()(int val) const { return val+1;}};
      36           2 : struct fdec{ int operator()(int dec) const { return dec-1;}};
      37             : 
      38             : template<typename Handle>
      39          42 : void rec(Handle&& h, int i)
      40             : {
      41          42 :   if ( i == 0 ) h();
      42          38 :   else rec( std::forward<Handle>(h), i-1);
      43          42 : }
      44             : 
      45             : template<typename Handle>
      46           2 : void rec2(Handle h, int i)
      47             : {
      48           2 :   if ( i == 0 ) h();
      49           2 :   else rec( h, i-1);
      50           2 : }
      51             : 
      52           3 : UNIT(wrap, "")
      53             : {
      54             :   using namespace fas::testing;
      55             : 
      56           1 :   foo::ctor = 0;
      57           1 :   foo::cctor = 0;
      58           1 :   foo::mctor = 0;
      59             : 
      60           1 :   ::iow::owner own;
      61           2 :   auto test1 = own.wrap(&inc, nullptr);
      62           1 :   int val = 0;
      63           1 :   val = test1(1);
      64           1 :   t << equal<expect>(val, 2) << FAS_FL;
      65           1 :   own.reset();
      66           1 :   val = test1(1);
      67           1 :   t << equal<expect>(val, 0) << FAS_FL;
      68             :   
      69           2 :   auto test2 = own.wrap(&inc, &dec);
      70           1 :   val = test2(2);
      71           1 :   t << equal<expect>(val, 3) << FAS_FL;
      72           1 :   own.reset();
      73           1 :   val = test2(2);
      74           1 :   t << equal<expect>(val, 1) << FAS_FL;
      75             :   
      76           1 :   foo f;
      77           4 :   auto test3 = own.wrap([f](int v)->int { return v + 1;}, nullptr);
      78           1 :   val = test3(1);
      79           1 :   t << equal<expect>(val, 2) << FAS_FL;
      80           1 :   t << equal<expect>(foo::ctor, 1) << FAS_FL;
      81           1 :   t << equal<expect>(foo::cctor, 1) << foo::cctor << FAS_FL;
      82           1 :   t << equal<expect>(foo::mctor, 1) << FAS_FL;
      83           1 :   own.reset();
      84           1 :   val = test3(1);
      85           1 :   t << equal<expect>(val, 0) << FAS_FL;
      86             : 
      87             : 
      88           9 :   auto test4 = own.wrap([f](int v)->int { return v + 1;}, [f](int v)->int { return v - 1;});
      89           1 :   val = test4(2);
      90           1 :   t << equal<expect>(val, 3) << FAS_FL;
      91           1 :   own.reset();
      92           1 :   val = test4(2);
      93           1 :   t << equal<expect>(val, 1) << FAS_FL;
      94             : 
      95           1 :   t << equal<expect>(foo::ctor, 1) << FAS_FL;
      96           1 :   t << equal<expect>(foo::cctor, 3) << foo::cctor << FAS_FL;
      97           1 :   t << equal<expect>(foo::mctor, 3) << foo::mctor << FAS_FL;
      98             :   
      99           2 :   rec([f](){}, 10);
     100           1 :   t << equal<expect>(foo::cctor, 4) << foo::cctor << FAS_FL;
     101           1 :   t << equal<expect>(foo::mctor, 3) << foo::mctor << FAS_FL;
     102             : 
     103           2 :   rec2([f](){}, 10);
     104           1 :   t << equal<expect>(foo::cctor, 5) << foo::cctor << FAS_FL;
     105           1 :   t << equal<expect>(foo::mctor, 3) << foo::mctor << FAS_FL;
     106             :   
     107           2 :   auto test5 = test4;
     108           1 :   val = test5(2);
     109           1 :   t << equal<expect>(val, 1) << FAS_FL;
     110             : 
     111           5 :   auto test6 = own.wrap([f](int v)->int { return v + 1;}, nullptr);
     112           2 :   auto test7 = test6;
     113           1 :   val = test7(2);
     114           1 :   t << equal<expect>(val, 3) << FAS_FL;
     115           1 :   own.reset();
     116           1 :   val = test7(2);
     117           1 :   t << equal<expect>(val, 0) << FAS_FL;
     118             :   
     119           2 :   auto test8 = own.wrap(finc(), fdec());
     120             :   //auto test8 = own.wrap(&inc, &dec);
     121             :   //auto test8 = own.wrap([f](int val)->int { return val + 1;}, [f](int val)->int { return val - 1;});
     122           2 :   auto test9 = test8;
     123           1 :   val = test9(2);
     124           1 :   t << equal<expect>(val, 3) << val << FAS_FL;
     125           1 :   own.reset();
     126           1 :   val = test9(2);
     127           2 :   t << equal<expect>(val, 1) << val << FAS_FL;
     128           1 : }
     129             : 
     130           3 : UNIT(wrap_callback, "")
     131             : {
     132             :   using namespace fas::testing;
     133           1 :   foo::ctor = 0;
     134           1 :   foo::cctor = 0;
     135           1 :   foo::mctor = 0;
     136             : 
     137           1 :   ::iow::owner own;
     138           2 :   auto test1 =own.wrap(  own.callback( &inc), nullptr );
     139           1 :   int val = 0;
     140           1 :   val = test1(1);
     141           1 :   t << equal<expect>(val, 2) << FAS_FL;
     142           1 :   own.reset();
     143           1 :   val = test1(1);
     144           1 :   t << equal<expect>(val, 0) << FAS_FL;
     145             :   
     146           2 :   auto test2 =own.wrap(  own.callback( &inc), &dec );
     147           1 :   val = test2(2);
     148           1 :   t << equal<expect>(val, 3) << FAS_FL;
     149           1 :   own.reset();
     150           1 :   val = test2(2);
     151           1 :   t << equal<expect>(val, 1) << FAS_FL;
     152             :   
     153           1 :   foo f;
     154           5 :   auto test3 = own.wrap( own.callback([f](int v)->int { return v + 1;}), nullptr);
     155           1 :   val = test3(1);
     156           1 :   t << equal<expect>(val, 2) << FAS_FL;
     157           1 :   t << equal<expect>(foo::ctor, 1) << FAS_FL;
     158           1 :   t << equal<expect>(foo::cctor, 1) <<  FAS_FL;
     159           1 :   t << equal<expect>(foo::mctor, 2) << FAS_FL;
     160           1 :   own.reset();
     161           1 :   val = test3(1);
     162           1 :   t << equal<expect>(val, 0) << FAS_FL;
     163             : 
     164             : 
     165          10 :   auto test4 = own.wrap( own.callback([f](int v)->int { return v + 1;}), [f](int v)->int { return v - 1;});
     166           1 :   val = test4(2);
     167           1 :   t << equal<expect>(val, 3) << FAS_FL;
     168           1 :   own.reset();
     169           1 :   val = test4(2);
     170           1 :   t << equal<expect>(val, 1) << FAS_FL;
     171             : 
     172           1 :   t << equal<expect>(foo::ctor, 1) << FAS_FL;
     173           1 :   t << equal<expect>(foo::cctor, 3) << foo::cctor << FAS_FL;
     174           1 :   t << equal<expect>(foo::mctor, 5) << foo::mctor << FAS_FL;
     175             :   
     176           2 :   rec([f](){}, 10);
     177           1 :   t << equal<expect>(foo::cctor, 4) << foo::cctor << FAS_FL;
     178           1 :   t << equal<expect>(foo::mctor, 5) << foo::mctor << FAS_FL;
     179             : 
     180           2 :   rec2([f](){}, 10);
     181           1 :   t << equal<expect>(foo::cctor, 5) << foo::cctor << FAS_FL;
     182           1 :   t << equal<expect>(foo::mctor, 5) << foo::mctor << FAS_FL;
     183             :   
     184           2 :   auto test5 = test4;
     185           1 :   val = test5(2);
     186           1 :   t << equal<expect>(val, 1) << FAS_FL;
     187             : 
     188           6 :   auto test6 = own.wrap( own.callback([f](int v)->int { return v + 1;}), nullptr);
     189           2 :   auto test7 = test6;
     190           1 :   val = test7(2);
     191           1 :   t << equal<expect>(val, 3) << FAS_FL;
     192           1 :   own.reset();
     193           1 :   val = test7(2);
     194           1 :   t << equal<expect>(val, 0) << FAS_FL;
     195             :   
     196           2 :   auto test8 = own.wrap( own.callback(finc()), fdec());
     197             :   //auto test8 = own.wrap(&inc, &dec);
     198             :   //auto test8 = own.wrap([f](int val)->int { return val + 1;}, [f](int val)->int { return val - 1;});
     199           2 :   auto test9 = test8;
     200           1 :   val = test9(2);
     201           1 :   t << equal<expect>(val, 3) << val << FAS_FL;
     202           1 :   own.reset();
     203           1 :   val = test9(2);
     204           2 :   t << equal<expect>(val, 1) << val << FAS_FL;
     205           1 : }
     206             : 
     207           3 : UNIT(callback1, "")
     208             : {
     209             :   using namespace fas::testing;
     210           1 :   int count = 0;
     211           1 :   int dcount = 0;
     212           1 :   int ncount = 0;
     213             :   
     214           1 :   ::iow::owner own;
     215           3 :   auto cb1 = own.callback([&count](){count++;});
     216           1 :   cb1();
     217           1 :   t << equal<expect>(count, 1) << FAS_FL;
     218           1 :   cb1();
     219           1 :   t << equal<expect>(count, 1) << FAS_FL;
     220             :   
     221           2 :   own.set_double_call_handler([&dcount](){++dcount;});
     222           2 :   own.set_no_call_handler([&ncount](){++ncount;});
     223             : 
     224           3 :   auto cb2 = own.callback([&count](){count++;});
     225           1 :   cb1();
     226           1 :   t << equal<expect>(count, 1) << FAS_FL;
     227           1 :   cb2();
     228           1 :   t << equal<expect>(count, 2) << FAS_FL;
     229           1 :   cb2();
     230           1 :   t << equal<expect>(count, 2) << FAS_FL;
     231           1 :   t << equal<expect>(dcount, 1) << FAS_FL;
     232           1 :   t << equal<expect>(ncount, 0) << FAS_FL;
     233             :   {
     234           1 :     auto cb3 = own.callback([&count](){count++;});
     235             :   }
     236           1 :   t << equal<expect>(count, 2) << FAS_FL;
     237           1 :   t << equal<expect>(dcount, 1) << FAS_FL;
     238           2 :   t << equal<expect>(ncount, 1) << FAS_FL;
     239             : 
     240           1 : }
     241             : 
     242           1 : BEGIN_SUITE(owner, "")
     243             :   ADD_UNIT(wrap)
     244             :   ADD_UNIT(wrap_callback)  
     245             :   ADD_UNIT(callback1)  
     246           4 : END_SUITE(owner)
     247             : 
     248             : 
     249           1 : BEGIN_TEST
     250           1 :   RUN_SUITE(owner)
     251           4 : END_TEST

Generated by: LCOV version 1.10