expand_bwd_test_template.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006. 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/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_CONTAINER_TEST_ALLOCATION_TEST_TEMPLATE_HEADER
  11. #define BOOST_CONTAINER_TEST_ALLOCATION_TEST_TEMPLATE_HEADER
  12. #include <boost/container/detail/config_begin.hpp>
  13. #include <vector>
  14. #include <typeinfo>
  15. #include <iostream>
  16. #include "expand_bwd_test_allocator.hpp"
  17. #include <boost/container/detail/algorithm.hpp> //equal()
  18. #include "movable_int.hpp"
  19. #include <boost/move/make_unique.hpp>
  20. namespace boost { namespace container { namespace test {
  21. //Function to check if both sets are equal
  22. template <class Vector1, class Vector2>
  23. bool CheckEqualVector(const Vector1 &vector1, const Vector2 &vector2)
  24. {
  25. if(vector1.size() != vector2.size())
  26. return false;
  27. return boost::container::algo_equal(vector1.begin(), vector1.end(), vector2.begin());
  28. }
  29. template<class Vector>
  30. bool CheckUninitializedIsZero(const Vector & v)
  31. {
  32. typedef typename Vector::value_type value_type;
  33. typename Vector::size_type sz = v.size();
  34. typename Vector::size_type extra = v.capacity() - v.size();
  35. value_type comp(0);
  36. const value_type *holder = &v[0] + sz;
  37. while(extra--){
  38. if(*holder++ != comp)
  39. return false;
  40. }
  41. return true;
  42. }
  43. //This function tests all the possible combinations when
  44. //inserting data in a vector and expanding backwards
  45. template<class VectorWithExpandBwdAllocator>
  46. bool test_insert_with_expand_bwd()
  47. {
  48. typedef typename VectorWithExpandBwdAllocator::value_type value_type;
  49. typedef std::vector<value_type> Vect;
  50. const unsigned int MemorySize = 1000;
  51. //Distance old and new buffer
  52. const unsigned int Offset[] =
  53. { 350, 300, 250, 200, 150, 100, 150, 100,
  54. 150, 50, 50, 50 };
  55. //Initial vector size
  56. const unsigned int InitialSize[] =
  57. { 200, 200, 200, 200, 200, 200, 200, 200,
  58. 200, 200, 200, 200 };
  59. //Size of the data to insert
  60. const unsigned int InsertSize[] =
  61. { 100, 100, 100, 100, 100, 100, 200, 200,
  62. 300, 25, 100, 200 };
  63. //Number of tests
  64. const unsigned int Iterations = sizeof(InsertSize)/sizeof(int);
  65. //Insert position
  66. const int Position[] =
  67. { 0, 100, 200 };
  68. for(unsigned int pos = 0; pos < sizeof(Position)/sizeof(Position[0]); ++pos){
  69. if(!life_count<value_type>::check(0))
  70. return false;
  71. for(unsigned int iteration = 0; iteration < Iterations; ++iteration)
  72. {
  73. boost::movelib::unique_ptr<char[]> memptr =
  74. boost::movelib::make_unique_definit<char[]>(MemorySize*sizeof(value_type));
  75. value_type *memory = (value_type*)memptr.get();
  76. std::vector<value_type> initial_data;
  77. initial_data.resize(InitialSize[iteration]);
  78. for(unsigned int i = 0; i < InitialSize[iteration]; ++i){
  79. initial_data[i] = i;
  80. }
  81. if(!life_count<value_type>::check(InitialSize[iteration]))
  82. return false;
  83. Vect data_to_insert;
  84. data_to_insert.resize(InsertSize[iteration]);
  85. for(unsigned int i = 0; i < InsertSize[iteration]; ++i){
  86. data_to_insert[i] = -i;
  87. }
  88. if(!life_count<value_type>::check(InitialSize[iteration]+InsertSize[iteration]))
  89. return false;
  90. expand_bwd_test_allocator<value_type> alloc
  91. (memory, MemorySize, Offset[iteration]);
  92. VectorWithExpandBwdAllocator vector(alloc);
  93. vector.insert( vector.begin()
  94. , initial_data.begin(), initial_data.end());
  95. vector.insert( vector.begin() + Position[pos]
  96. , data_to_insert.begin(), data_to_insert.end());
  97. if(!life_count<value_type>::check(InitialSize[iteration]*2+InsertSize[iteration]*2))
  98. return false;
  99. initial_data.insert(initial_data.begin() + Position[pos]
  100. , data_to_insert.begin(), data_to_insert.end());
  101. //Now check that values are equal
  102. if(!CheckEqualVector(vector, initial_data)){
  103. std::cout << "test_assign_with_expand_bwd::CheckEqualVector failed." << std::endl
  104. << " Class: " << typeid(VectorWithExpandBwdAllocator).name() << std::endl
  105. << " Iteration: " << iteration << std::endl;
  106. return false;
  107. }
  108. }
  109. if(!life_count<value_type>::check(0))
  110. return false;
  111. }
  112. return true;
  113. }
  114. //This function tests all the possible combinations when
  115. //inserting data in a vector and expanding backwards
  116. template<class VectorWithExpandBwdAllocator>
  117. bool test_assign_with_expand_bwd()
  118. {
  119. typedef typename VectorWithExpandBwdAllocator::value_type value_type;
  120. const unsigned int MemorySize = 200;
  121. const unsigned int Offset[] = { 50, 50, 50};
  122. const unsigned int InitialSize[] = { 25, 25, 25};
  123. const unsigned int InsertSize[] = { 15, 35, 55};
  124. const unsigned int Iterations = sizeof(InsertSize)/sizeof(int);
  125. for(unsigned int iteration = 0; iteration <Iterations; ++iteration)
  126. {
  127. boost::movelib::unique_ptr<char[]> memptr =
  128. boost::movelib::make_unique_definit<char[]>(MemorySize*sizeof(value_type));
  129. value_type *memory = (value_type*)memptr.get();
  130. //Create initial data
  131. std::vector<value_type> initial_data;
  132. initial_data.resize(InitialSize[iteration]);
  133. for(unsigned int i = 0; i < InitialSize[iteration]; ++i){
  134. initial_data[i] = i;
  135. }
  136. //Create data to assign
  137. std::vector<value_type> data_to_insert;
  138. data_to_insert.resize(InsertSize[iteration]);
  139. for(unsigned int i = 0; i < InsertSize[iteration]; ++i){
  140. data_to_insert[i] = -i;
  141. }
  142. //Insert initial data to the vector to test
  143. expand_bwd_test_allocator<value_type> alloc
  144. (memory, MemorySize, Offset[iteration]);
  145. VectorWithExpandBwdAllocator vector(alloc);
  146. vector.insert( vector.begin()
  147. , initial_data.begin(), initial_data.end());
  148. //Assign data
  149. vector.insert(vector.cbegin(), data_to_insert.begin(), data_to_insert.end());
  150. initial_data.insert(initial_data.begin(), data_to_insert.begin(), data_to_insert.end());
  151. //Now check that values are equal
  152. if(!CheckEqualVector(vector, initial_data)){
  153. std::cout << "test_assign_with_expand_bwd::CheckEqualVector failed." << std::endl
  154. << " Class: " << typeid(VectorWithExpandBwdAllocator).name() << std::endl
  155. << " Iteration: " << iteration << std::endl;
  156. return false;
  157. }
  158. }
  159. return true;
  160. }
  161. //This function calls all tests
  162. template<class VectorWithExpandBwdAllocator>
  163. bool test_all_expand_bwd()
  164. {
  165. std::cout << "Starting test_insert_with_expand_bwd." << std::endl << " Class: "
  166. << typeid(VectorWithExpandBwdAllocator).name() << std::endl;
  167. if(!test_insert_with_expand_bwd<VectorWithExpandBwdAllocator>()){
  168. std::cout << "test_allocation_direct_deallocation failed. Class: "
  169. << typeid(VectorWithExpandBwdAllocator).name() << std::endl;
  170. return false;
  171. }
  172. std::cout << "Starting test_assign_with_expand_bwd." << std::endl << " Class: "
  173. << typeid(VectorWithExpandBwdAllocator).name() << std::endl;
  174. if(!test_assign_with_expand_bwd<VectorWithExpandBwdAllocator>()){
  175. std::cout << "test_allocation_direct_deallocation failed. Class: "
  176. << typeid(VectorWithExpandBwdAllocator).name() << std::endl;
  177. return false;
  178. }
  179. return true;
  180. }
  181. }}} //namespace boost { namespace container { namespace test {
  182. #include <boost/container/detail/config_end.hpp>
  183. #endif //BOOST_CONTAINER_TEST_ALLOCATION_TEST_TEMPLATE_HEADER