Ticket #7330: function_test.cpp.diff

File function_test.cpp.diff, 2.3 KB (added by Antony Polukhin, 10 years ago)
  • function/test/function_test.cpp

     
    690690  test_call_cref(std::plus<int>());
    691691}
    692692
     693struct big_aggregating_structure {
     694  int disable_small_objects_optimizations[32];
     695   
     696  big_aggregating_structure()
     697  {
     698    ++ global_int;
     699  }
     700   
     701  big_aggregating_structure(const big_aggregating_structure&)
     702  {
     703    ++ global_int;
     704  }
     705
     706  ~big_aggregating_structure()
     707  {
     708    -- global_int;
     709  }
     710   
     711  void operator()()
     712  {
     713    ++ global_int;
     714  }
     715
     716  void operator()(int)
     717  {
     718    ++ global_int;
     719  }
     720};
     721
     722template <class FunctionT>
     723static void test_move_semantics()
     724{
     725  typedef FunctionT f1_type;
     726
     727  big_aggregating_structure obj;
     728 
     729  f1_type f1 = obj;
     730  global_int = 0;
     731  f1();
     732 
     733  BOOST_CHECK(!f1.empty());
     734  BOOST_CHECK(global_int == 1);
     735 
     736#ifndef BOOST_NO_RVALUE_REFERENCES
     737  // Testing rvalue constructors
     738  f1_type f2(static_cast<f1_type&&>(f1));
     739  BOOST_CHECK(f1.empty());
     740  BOOST_CHECK(!f2.empty());
     741  BOOST_CHECK(global_int == 1);
     742  f2();
     743  BOOST_CHECK(global_int == 2);
     744 
     745  f1_type f3(static_cast<f1_type&&>(f2));
     746  BOOST_CHECK(f1.empty());
     747  BOOST_CHECK(f2.empty());
     748  BOOST_CHECK(!f3.empty());
     749  BOOST_CHECK(global_int == 2);
     750  f3();
     751  BOOST_CHECK(global_int == 3);
     752   
     753  // Testing move assignment
     754  f1_type f4;
     755  BOOST_CHECK(f4.empty());
     756  f4 = static_cast<f1_type&&>(f3);
     757  BOOST_CHECK(f1.empty());
     758  BOOST_CHECK(f2.empty());
     759  BOOST_CHECK(f3.empty());
     760  BOOST_CHECK(!f4.empty());
     761  BOOST_CHECK(global_int == 3);
     762  f4();
     763  BOOST_CHECK(global_int == 4);
     764 
     765  // Testing self move assignment
     766  f4 = static_cast<f1_type&&>(f4);
     767  BOOST_CHECK(!f4.empty());
     768  BOOST_CHECK(global_int == 4);
     769
     770  // Testing, that no memory leaked when assigning to nonempty function
     771  f4 = obj;
     772  BOOST_CHECK(!f4.empty());
     773  BOOST_CHECK(global_int == 4);
     774  f1_type f5 = obj;
     775  BOOST_CHECK(global_int == 5);
     776  f4 = static_cast<f1_type&&>(f5);
     777  BOOST_CHECK(global_int == 4);
     778 
     779#endif 
     780}
     781
    693782int test_main(int, char* [])
    694783{
    695784  test_zero_args();
     
    702791  test_exception();
    703792  test_implicit();
    704793  test_call();
     794  test_move_semantics<function<void()> >();
     795  test_move_semantics<boost::function0<void> >();
    705796
    706797  return 0;
    707798}
     799