expand_bwd_test_template.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006-2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_INTERPROCESS_TEST_ALLOCATION_TEST_TEMPLATE_HEADER
  11. #define BOOST_INTERPROCESS_TEST_ALLOCATION_TEST_TEMPLATE_HEADER
  12. #include <boost/interprocess/detail/config_begin.hpp>
  13. #include "expand_bwd_test_allocator.hpp"
  14. #include <boost/interprocess/detail/type_traits.hpp>
  15. #include <algorithm> //std::equal
  16. #include <vector>
  17. #include <iostream>
  18. namespace boost { namespace interprocess { namespace test {
  19. template<class T>
  20. struct value_holder
  21. {
  22. value_holder(T val) : m_value(val){}
  23. value_holder(): m_value(0){}
  24. ~value_holder(){ m_value = 0; }
  25. bool operator == (const value_holder &other) const
  26. { return m_value == other.m_value; }
  27. bool operator != (const value_holder &other) const
  28. { return m_value != other.m_value; }
  29. T m_value;
  30. };
  31. template<class T>
  32. struct triple_value_holder
  33. {
  34. triple_value_holder(T val)
  35. : m_value1(val)
  36. , m_value2(val)
  37. , m_value3(val)
  38. {}
  39. triple_value_holder()
  40. : m_value1(0)
  41. , m_value2(0)
  42. , m_value3(0)
  43. {}
  44. ~triple_value_holder()
  45. { m_value1 = m_value2 = m_value3 = 0; }
  46. bool operator == (const triple_value_holder &other) const
  47. {
  48. return m_value1 == other.m_value1
  49. && m_value2 == other.m_value2
  50. && m_value3 == other.m_value3;
  51. }
  52. bool operator != (const triple_value_holder &other) const
  53. {
  54. return m_value1 != other.m_value1
  55. || m_value2 != other.m_value2
  56. || m_value3 != other.m_value3;
  57. }
  58. T m_value1;
  59. T m_value2;
  60. T m_value3;
  61. };
  62. typedef value_holder<int> int_holder;
  63. typedef triple_value_holder<int> triple_int_holder;
  64. //Function to check if both sets are equal
  65. template <class Vector1, class Vector2>
  66. bool CheckEqualVector(const Vector1 &vector1, const Vector2 &vector2)
  67. {
  68. if(vector1.size() != vector2.size())
  69. return false;
  70. return std::equal(vector1.begin(), vector1.end(), vector2.begin());
  71. }
  72. template<class Vector>
  73. bool CheckUninitializedIsZero(const Vector & v)
  74. {
  75. typedef typename Vector::value_type value_type;
  76. typename Vector::size_type sz = v.size();
  77. typename Vector::size_type extra = v.capacity() - v.size();
  78. value_type comp(0);
  79. const value_type *holder = &v[0] + sz;
  80. while(extra--){
  81. if(*holder++ != comp)
  82. return false;
  83. }
  84. return true;
  85. }
  86. //This function tests all the possible combinations when
  87. //inserting data in a vector and expanding backwards
  88. template<class VectorWithExpandBwdAllocator>
  89. bool test_insert_with_expand_bwd()
  90. {
  91. typedef typename VectorWithExpandBwdAllocator::value_type value_type;
  92. typedef typename boost::interprocess::ipcdetail::remove_volatile<value_type>::type non_volatile_value_type;
  93. typedef std::vector<non_volatile_value_type> Vect;
  94. const int MemorySize = 1000;
  95. //Distance old and new buffer
  96. const int Offset[] =
  97. { 350, 250, 150, 150,
  98. 150, 50, 50, 50 };
  99. //Insert position
  100. const int Position[] =
  101. { 100, 100, 100, 100,
  102. 100, 100, 100, 100 };
  103. //Initial vector size
  104. const int InitialSize[] =
  105. { 200, 200, 200, 200,
  106. 200, 200, 200, 200 };
  107. //Size of the data to insert
  108. const int InsertSize[] =
  109. { 100, 100, 100, 200,
  110. 300, 25, 100, 200 };
  111. //Number of tests
  112. const int Iterations = sizeof(InsertSize)/sizeof(int);
  113. for(int iteration = 0; iteration < Iterations; ++iteration)
  114. {
  115. value_type *memory = new value_type[MemorySize];
  116. try {
  117. std::vector<non_volatile_value_type> initial_data;
  118. initial_data.resize(InitialSize[iteration]);
  119. for(int i = 0; i < InitialSize[iteration]; ++i){
  120. initial_data[i] = i;
  121. }
  122. Vect data_to_insert;
  123. data_to_insert.resize(InsertSize[iteration]);
  124. for(int i = 0; i < InsertSize[iteration]; ++i){
  125. data_to_insert[i] = -i;
  126. }
  127. expand_bwd_test_allocator<value_type> alloc
  128. (&memory[0], MemorySize, Offset[iteration]);
  129. VectorWithExpandBwdAllocator vector(alloc);
  130. vector.insert( vector.begin()
  131. , initial_data.begin(), initial_data.end());
  132. vector.insert( vector.begin() + Position[iteration]
  133. , data_to_insert.begin(), data_to_insert.end());
  134. initial_data.insert(initial_data.begin() + Position[iteration]
  135. , data_to_insert.begin(), data_to_insert.end());
  136. //Now check that values are equal
  137. if(!CheckEqualVector(vector, initial_data)){
  138. std::cout << "test_assign_with_expand_bwd::CheckEqualVector failed." << std::endl
  139. << " Class: " << typeid(VectorWithExpandBwdAllocator).name() << std::endl
  140. << " Iteration: " << iteration << std::endl;
  141. return false;
  142. }
  143. }
  144. catch(...){
  145. delete [](const_cast<non_volatile_value_type*>(memory));
  146. throw;
  147. }
  148. delete [](const_cast<non_volatile_value_type*>(memory));
  149. }
  150. return true;
  151. }
  152. //This function tests all the possible combinations when
  153. //inserting data in a vector and expanding backwards
  154. template<class VectorWithExpandBwdAllocator>
  155. bool test_assign_with_expand_bwd()
  156. {
  157. typedef typename VectorWithExpandBwdAllocator::value_type value_type;
  158. typedef typename boost::interprocess::ipcdetail::remove_volatile<value_type>::type non_volatile_value_type;
  159. const int MemorySize = 200;
  160. const int Offset[] = { 50, 50, 50};
  161. const int InitialSize[] = { 25, 25, 25};
  162. const int InsertSize[] = { 15, 35, 55};
  163. const int Iterations = sizeof(InsertSize)/sizeof(int);
  164. for(int iteration = 0; iteration <Iterations; ++iteration)
  165. {
  166. value_type *memory = new value_type[MemorySize];
  167. try {
  168. //Create initial data
  169. std::vector<non_volatile_value_type> initial_data;
  170. initial_data.resize(InitialSize[iteration]);
  171. for(int i = 0; i < InitialSize[iteration]; ++i){
  172. initial_data[i] = i;
  173. }
  174. //Create data to insert
  175. std::vector<non_volatile_value_type> data_to_insert;
  176. data_to_insert.resize(InsertSize[iteration]);
  177. for(int i = 0; i < InsertSize[iteration]; ++i){
  178. data_to_insert[i] = -i;
  179. }
  180. //Insert initial data to the vector to test
  181. expand_bwd_test_allocator<value_type> alloc
  182. (&memory[0], MemorySize, Offset[iteration]);
  183. VectorWithExpandBwdAllocator vector(alloc);
  184. vector.insert( vector.begin()
  185. , initial_data.begin(), initial_data.end());
  186. //Insert data
  187. vector.insert(vector.cbegin(), data_to_insert.begin(), data_to_insert.end());
  188. initial_data.insert(initial_data.begin(), data_to_insert.begin(), data_to_insert.end());
  189. //Now check that values are equal
  190. if(!CheckEqualVector(vector, initial_data)){
  191. std::cout << "test_insert_with_expand_bwd::CheckEqualVector failed." << std::endl
  192. << " Class: " << typeid(VectorWithExpandBwdAllocator).name() << std::endl
  193. << " Iteration: " << iteration << std::endl;
  194. return false;
  195. }
  196. }
  197. catch(...){
  198. delete [](const_cast<typename boost::interprocess::ipcdetail::remove_volatile<value_type>::type*>(memory));
  199. throw;
  200. }
  201. delete [](const_cast<typename boost::interprocess::ipcdetail::remove_volatile<value_type>::type*>(memory));
  202. }
  203. return true;
  204. }
  205. //This function calls all tests
  206. template<class VectorWithExpandBwdAllocator>
  207. bool test_all_expand_bwd()
  208. {
  209. std::cout << "Starting test_insert_with_expand_bwd." << std::endl << " Class: "
  210. << typeid(VectorWithExpandBwdAllocator).name() << std::endl;
  211. if(!test_insert_with_expand_bwd<VectorWithExpandBwdAllocator>()){
  212. std::cout << "test_allocation_direct_deallocation failed. Class: "
  213. << typeid(VectorWithExpandBwdAllocator).name() << std::endl;
  214. return false;
  215. }
  216. std::cout << "Starting test_assign_with_expand_bwd." << std::endl << " Class: "
  217. << typeid(VectorWithExpandBwdAllocator).name() << std::endl;
  218. if(!test_assign_with_expand_bwd<VectorWithExpandBwdAllocator>()){
  219. std::cout << "test_allocation_direct_deallocation failed. Class: "
  220. << typeid(VectorWithExpandBwdAllocator).name() << std::endl;
  221. return false;
  222. }
  223. return true;
  224. }
  225. }}} //namespace boost { namespace interprocess { namespace test {
  226. #include <boost/interprocess/detail/config_end.hpp>
  227. #endif //BOOST_INTERPROCESS_TEST_ALLOCATION_TEST_TEMPLATE_HEADER