bind_void_test.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <boost/config.hpp>
  2. #if defined(BOOST_MSVC)
  3. #pragma warning(disable: 4786) // identifier truncated in debug info
  4. #pragma warning(disable: 4710) // function not inlined
  5. #pragma warning(disable: 4711) // function selected for automatic inline expansion
  6. #pragma warning(disable: 4514) // unreferenced inline removed
  7. #endif
  8. //
  9. // bind_void_test.cpp - test for bind<void>
  10. //
  11. // Copyright (c) 2008 Peter Dimov
  12. // Copyright (c) 2014 Agustin Berge
  13. //
  14. // Distributed under the Boost Software License, Version 1.0. (See
  15. // accompanying file LICENSE_1_0.txt or copy at
  16. // http://www.boost.org/LICENSE_1_0.txt)
  17. //
  18. #include <boost/bind.hpp>
  19. #include <boost/ref.hpp>
  20. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  21. #pragma warning(push, 3)
  22. #endif
  23. #include <iostream>
  24. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  25. #pragma warning(pop)
  26. #endif
  27. #include <boost/detail/lightweight_test.hpp>
  28. //
  29. long global_result;
  30. long f_0()
  31. {
  32. return global_result = 17041L;
  33. }
  34. long f_1(long a)
  35. {
  36. return global_result = a;
  37. }
  38. long f_2(long a, long b)
  39. {
  40. return global_result = a + 10 * b;
  41. }
  42. long f_3(long a, long b, long c)
  43. {
  44. return global_result = a + 10 * b + 100 * c;
  45. }
  46. long f_4(long a, long b, long c, long d)
  47. {
  48. return global_result = a + 10 * b + 100 * c + 1000 * d;
  49. }
  50. long f_5(long a, long b, long c, long d, long e)
  51. {
  52. return global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e;
  53. }
  54. long f_6(long a, long b, long c, long d, long e, long f)
  55. {
  56. return global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f;
  57. }
  58. long f_7(long a, long b, long c, long d, long e, long f, long g)
  59. {
  60. return global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g;
  61. }
  62. long f_8(long a, long b, long c, long d, long e, long f, long g, long h)
  63. {
  64. return global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h;
  65. }
  66. long f_9(long a, long b, long c, long d, long e, long f, long g, long h, long i)
  67. {
  68. return global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h + 100000000 * i;
  69. }
  70. void function_test()
  71. {
  72. using namespace boost;
  73. int const i = 1;
  74. BOOST_TEST( (bind<void>(f_0)(i), (global_result == 17041L)) );
  75. BOOST_TEST( (bind<void>(f_1, _1)(i), (global_result == 1L)) );
  76. BOOST_TEST( (bind<void>(f_2, _1, 2)(i), (global_result == 21L)) );
  77. BOOST_TEST( (bind<void>(f_3, _1, 2, 3)(i), (global_result == 321L)) );
  78. BOOST_TEST( (bind<void>(f_4, _1, 2, 3, 4)(i), (global_result == 4321L)) );
  79. BOOST_TEST( (bind<void>(f_5, _1, 2, 3, 4, 5)(i), (global_result == 54321L)) );
  80. BOOST_TEST( (bind<void>(f_6, _1, 2, 3, 4, 5, 6)(i), (global_result == 654321L)) );
  81. BOOST_TEST( (bind<void>(f_7, _1, 2, 3, 4, 5, 6, 7)(i), (global_result == 7654321L)) );
  82. BOOST_TEST( (bind<void>(f_8, _1, 2, 3, 4, 5, 6, 7, 8)(i), (global_result == 87654321L)) );
  83. BOOST_TEST( (bind<void>(f_9, _1, 2, 3, 4, 5, 6, 7, 8, 9)(i), (global_result == 987654321L)) );
  84. }
  85. //
  86. struct Y
  87. {
  88. short operator()(short & r) const { return global_result = ++r; }
  89. int operator()(int a, int b) const { return global_result = a + 10 * b; }
  90. long operator() (long a, long b, long c) const { return global_result = a + 10 * b + 100 * c; }
  91. void operator() (long a, long b, long c, long d) const { global_result = a + 10 * b + 100 * c + 1000 * d; }
  92. };
  93. void function_object_test()
  94. {
  95. using namespace boost;
  96. short i(6);
  97. int const k = 3;
  98. BOOST_TEST( (bind<void>(Y(), ref(i))(), (global_result == 7)) );
  99. BOOST_TEST( (bind<void>(Y(), ref(i))(), (global_result == 8)) );
  100. BOOST_TEST( (bind<void>(Y(), i, _1)(k), (global_result == 38)) );
  101. BOOST_TEST( (bind<void>(Y(), i, _1, 9)(k), (global_result == 938)) );
  102. BOOST_TEST( (bind<void>(Y(), i, _1, 9, 4)(k), (global_result == 4938)) );
  103. }
  104. int main()
  105. {
  106. function_test();
  107. function_object_test();
  108. return boost::report_errors();
  109. }