regression_test.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // thread_safe_signals library
  2. // Some assorted tests to expose various bugs that existed at some point,
  3. // to make sure they stay fixed
  4. // Copyright Frank Mori Hess 2008
  5. // Use, modification and
  6. // distribution is subject to the Boost Software License, Version
  7. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. // For more information, see http://www.boost.org
  10. #include <boost/test/minimal.hpp>
  11. #include <boost/signals2.hpp>
  12. typedef boost::signals2::signal<void ()> sig0_type;
  13. // combiner that returns the number of slots invoked
  14. struct slot_counter {
  15. typedef unsigned result_type;
  16. template<typename InputIterator>
  17. unsigned operator()(InputIterator first, InputIterator last) const
  18. {
  19. unsigned count = 0;
  20. for (; first != last; ++first)
  21. {
  22. try
  23. {
  24. *first;
  25. ++count;
  26. }
  27. catch(const boost::bad_weak_ptr &)
  28. {}
  29. }
  30. return count;
  31. }
  32. };
  33. void my_slot()
  34. {
  35. }
  36. void my_connecting_slot(sig0_type &sig)
  37. {
  38. sig.connect(&my_slot);
  39. }
  40. void slot_connect_test()
  41. {
  42. sig0_type sig;
  43. sig.connect(sig0_type::slot_type(&my_connecting_slot, boost::ref(sig)).track(sig));
  44. /* 2008-02-28: the following signal invocation triggered a (bogus) failed assertion of _shared_state.unique()
  45. at detail/signal_template.hpp:285 */
  46. sig();
  47. BOOST_CHECK(sig.num_slots() == 2);
  48. sig.disconnect(&my_slot);
  49. BOOST_CHECK(sig.num_slots() == 1);
  50. /* 2008-03-11: checked iterator barfed on next line, due to bad semantics of copy construction
  51. for boost::signals2::detail::grouped_list */
  52. sig();
  53. BOOST_CHECK(sig.num_slots() == 2);
  54. }
  55. /* 2008-03-10: we weren't disconnecting old connection in scoped_connection assignment operator */
  56. void scoped_connection_test()
  57. {
  58. typedef boost::signals2::signal<void (), slot_counter> signal_type;
  59. signal_type sig;
  60. {
  61. boost::signals2::scoped_connection conn(sig.connect(&my_slot));
  62. BOOST_CHECK(sig() == 1);
  63. conn = sig.connect(&my_slot);
  64. BOOST_CHECK(sig() == 1);
  65. }
  66. BOOST_CHECK(sig() == 0);
  67. }
  68. // testsignal that returns a reference type
  69. struct ref_returner
  70. {
  71. static int i;
  72. int& ref_return_slot()
  73. {
  74. return i;
  75. }
  76. };
  77. int ref_returner::i = 0;
  78. void reference_return_test()
  79. {
  80. boost::signals2::signal<int& ()> rTest;
  81. ref_returner rr;
  82. rTest.connect(boost::bind(&ref_returner::ref_return_slot, &rr));
  83. int& r = *rTest();
  84. BOOST_CHECK(ref_returner::i == 0);
  85. r = 1;
  86. BOOST_CHECK(ref_returner::i == 1);
  87. }
  88. int test_main(int, char*[])
  89. {
  90. slot_connect_test();
  91. scoped_connection_test();
  92. reference_return_test();
  93. return 0;
  94. }