queue_test.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright (C) 2011 Tim Blechmann
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/lockfree/lockfree_forward.hpp>
  7. #include <boost/lockfree/queue.hpp>
  8. #include <boost/thread.hpp>
  9. #define BOOST_TEST_MAIN
  10. #ifdef BOOST_LOCKFREE_INCLUDE_TESTS
  11. #include <boost/test/included/unit_test.hpp>
  12. #else
  13. #include <boost/test/unit_test.hpp>
  14. #endif
  15. #include <memory>
  16. #include "test_helpers.hpp"
  17. using namespace boost;
  18. using namespace boost::lockfree;
  19. using namespace std;
  20. BOOST_AUTO_TEST_CASE( simple_queue_test )
  21. {
  22. queue<int> f(64);
  23. BOOST_WARN(f.is_lock_free());
  24. BOOST_REQUIRE(f.empty());
  25. f.push(1);
  26. f.push(2);
  27. int i1(0), i2(0);
  28. BOOST_REQUIRE(f.pop(i1));
  29. BOOST_REQUIRE_EQUAL(i1, 1);
  30. BOOST_REQUIRE(f.pop(i2));
  31. BOOST_REQUIRE_EQUAL(i2, 2);
  32. BOOST_REQUIRE(f.empty());
  33. }
  34. BOOST_AUTO_TEST_CASE( simple_queue_test_capacity )
  35. {
  36. queue<int, capacity<64> > f;
  37. BOOST_WARN(f.is_lock_free());
  38. BOOST_REQUIRE(f.empty());
  39. f.push(1);
  40. f.push(2);
  41. int i1(0), i2(0);
  42. BOOST_REQUIRE(f.pop(i1));
  43. BOOST_REQUIRE_EQUAL(i1, 1);
  44. BOOST_REQUIRE(f.pop(i2));
  45. BOOST_REQUIRE_EQUAL(i2, 2);
  46. BOOST_REQUIRE(f.empty());
  47. }
  48. BOOST_AUTO_TEST_CASE( unsafe_queue_test )
  49. {
  50. queue<int> f(64);
  51. BOOST_WARN(f.is_lock_free());
  52. BOOST_REQUIRE(f.empty());
  53. int i1(0), i2(0);
  54. f.unsynchronized_push(1);
  55. f.unsynchronized_push(2);
  56. BOOST_REQUIRE(f.unsynchronized_pop(i1));
  57. BOOST_REQUIRE_EQUAL(i1, 1);
  58. BOOST_REQUIRE(f.unsynchronized_pop(i2));
  59. BOOST_REQUIRE_EQUAL(i2, 2);
  60. BOOST_REQUIRE(f.empty());
  61. }
  62. BOOST_AUTO_TEST_CASE( queue_consume_one_test )
  63. {
  64. queue<int> f(64);
  65. BOOST_WARN(f.is_lock_free());
  66. BOOST_REQUIRE(f.empty());
  67. f.push(1);
  68. f.push(2);
  69. #ifdef BOOST_NO_CXX11_LAMBDAS
  70. bool success1 = f.consume_one(test_equal(1));
  71. bool success2 = f.consume_one(test_equal(2));
  72. #else
  73. bool success1 = f.consume_one([] (int i) {
  74. BOOST_REQUIRE_EQUAL(i, 1);
  75. });
  76. bool success2 = f.consume_one([] (int i) {
  77. BOOST_REQUIRE_EQUAL(i, 2);
  78. });
  79. #endif
  80. BOOST_REQUIRE(success1);
  81. BOOST_REQUIRE(success2);
  82. BOOST_REQUIRE(f.empty());
  83. }
  84. BOOST_AUTO_TEST_CASE( queue_consume_all_test )
  85. {
  86. queue<int> f(64);
  87. BOOST_WARN(f.is_lock_free());
  88. BOOST_REQUIRE(f.empty());
  89. f.push(1);
  90. f.push(2);
  91. #ifdef BOOST_NO_CXX11_LAMBDAS
  92. size_t consumed = f.consume_all(dummy_functor());
  93. #else
  94. size_t consumed = f.consume_all([] (int i) {
  95. });
  96. #endif
  97. BOOST_REQUIRE_EQUAL(consumed, 2u);
  98. BOOST_REQUIRE(f.empty());
  99. }
  100. BOOST_AUTO_TEST_CASE( queue_convert_pop_test )
  101. {
  102. queue<int*> f(128);
  103. BOOST_REQUIRE(f.empty());
  104. f.push(new int(1));
  105. f.push(new int(2));
  106. f.push(new int(3));
  107. f.push(new int(4));
  108. {
  109. int * i1;
  110. BOOST_REQUIRE(f.pop(i1));
  111. BOOST_REQUIRE_EQUAL(*i1, 1);
  112. delete i1;
  113. }
  114. {
  115. boost::shared_ptr<int> i2;
  116. BOOST_REQUIRE(f.pop(i2));
  117. BOOST_REQUIRE_EQUAL(*i2, 2);
  118. }
  119. {
  120. #ifdef BOOST_NO_AUTO_PTR
  121. unique_ptr<int> i3;
  122. #else
  123. auto_ptr<int> i3;
  124. #endif
  125. BOOST_REQUIRE(f.pop(i3));
  126. BOOST_REQUIRE_EQUAL(*i3, 3);
  127. }
  128. {
  129. boost::shared_ptr<int> i4;
  130. BOOST_REQUIRE(f.pop(i4));
  131. BOOST_REQUIRE_EQUAL(*i4, 4);
  132. }
  133. BOOST_REQUIRE(f.empty());
  134. }
  135. BOOST_AUTO_TEST_CASE( reserve_test )
  136. {
  137. typedef boost::lockfree::queue< void* > memory_queue;
  138. memory_queue ms(1);
  139. ms.reserve(1);
  140. ms.reserve_unsafe(1);
  141. }