resize.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2002 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Boost.MultiArray Library
  6. // Authors: Ronald Garcia
  7. // Jeremy Siek
  8. // Andrew Lumsdaine
  9. // See http://www.boost.org/libs/multi_array for documentation.
  10. //
  11. // resize.cpp - Test of resizing multi_arrays
  12. //
  13. #include <boost/core/lightweight_test.hpp>
  14. #include <boost/multi_array.hpp>
  15. #include <iostream>
  16. using namespace std;
  17. int main() {
  18. typedef boost::multi_array<int,3> marray;
  19. int A_data[] = {
  20. 0,1,2,3,
  21. 4,5,6,7,
  22. 8,9,10,11,
  23. 12,13,14,15,
  24. 16,17,18,19,
  25. 20,21,22,23
  26. };
  27. int A_resize[] = {
  28. 0,1,
  29. 4,5,
  30. 8,9,
  31. 12,13,
  32. 16,17,
  33. 20,21,
  34. 0,0,
  35. 0,0,
  36. 0,0,
  37. 0,0,
  38. 0,0,
  39. 0,0
  40. };
  41. // resize through the extent_gen interface
  42. {
  43. marray A(boost::extents[2][3][4]);
  44. A.assign(A_data,A_data+(2*3*4));
  45. A.resize(boost::extents[4][3][2]);
  46. BOOST_TEST(std::equal(A_resize,A_resize+(4*3*2),A.data()));
  47. }
  48. // resize through the Collection
  49. {
  50. marray A(boost::extents[2][3][4]);
  51. A.assign(A_data,A_data+(2*3*4));
  52. boost::array<int,3> new_extents = {{4,3,2}};
  53. A.resize(new_extents);
  54. BOOST_TEST(std::equal(A_resize,A_resize+(4*3*2),A.data()));
  55. }
  56. // default construct all the new elements (in this case, all elements)
  57. {
  58. marray defaultA;
  59. defaultA.resize(boost::extents[2][3][4]);
  60. BOOST_TEST(std::accumulate(defaultA.data(),
  61. defaultA.data()+(2*3*4),0) == 0);
  62. }
  63. // verify the preservation of storage order
  64. {
  65. int tiling_graph_storage_order[] = {2, 0, 1};
  66. bool tiling_graph_index_order[] = {true, true, true};
  67. marray A(boost::extents[3][4][2],
  68. boost::general_storage_order<3>(tiling_graph_storage_order,
  69. tiling_graph_index_order));
  70. int value = 0;
  71. for (int i = 0; i < 3; i++) {
  72. for (int j = 0; j < 4; j++) {
  73. for (int k = 0; k < 2; k++) {
  74. *(A.data() + value) = value;
  75. ++value;
  76. }
  77. }
  78. }
  79. // "Resize" to the same size
  80. A.resize(boost::extents[3][4][2]);
  81. int check = 0;
  82. for (int x = 0; x < 3; x++) {
  83. for (int y = 0; y < 4; y++) {
  84. for (int z = 0; z < 2; z++) {
  85. BOOST_TEST(*(A.data() + check) == check);
  86. ++check;
  87. }
  88. }
  89. }
  90. }
  91. // Resizing that changes index bases too (impl bug caused an assert)
  92. {
  93. typedef boost::multi_array<int, 1> ar_t;
  94. typedef ar_t::extent_range range;
  95. ar_t ar;
  96. ar.resize(boost::extents[range(-3, 3)]);
  97. }
  98. return boost::report_errors();
  99. }