reshape.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // reshape.cpp - testing reshaping functionality
  12. //
  13. #include <boost/multi_array.hpp>
  14. #include <boost/core/lightweight_test.hpp>
  15. #include <boost/array.hpp>
  16. #include <boost/type.hpp>
  17. int
  18. main()
  19. {
  20. const int ndims=3;
  21. typedef boost::multi_array<int,ndims> array;
  22. typedef boost::multi_array_ref<int,ndims> array_ref;
  23. typedef boost::const_multi_array_ref<int,ndims> const_array_ref;
  24. boost::array<array::size_type,ndims> dims = {{2,3,4}};
  25. boost::array<array::size_type,ndims> new_dims = {{4,3,2}};
  26. int data[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,
  27. 14,15,16,17,18,19,20,21,22,23};
  28. const int data_size=24;
  29. // Basic reshape test
  30. {
  31. array A(dims);
  32. A.assign(data,data+data_size);
  33. array_ref B(data,dims);
  34. const_array_ref C(data,dims);
  35. A.reshape(new_dims);
  36. B.reshape(new_dims);
  37. C.reshape(new_dims);
  38. int* ptr = data;
  39. for (array::index i = 0; i != 4; ++i)
  40. for (array::index j = 0; j != 3; ++j)
  41. for (array::index k = 0; k != 2; ++k) {
  42. BOOST_TEST(A[i][j][k] == *ptr);
  43. BOOST_TEST(B[i][j][k] == *ptr);
  44. BOOST_TEST(C[i][j][k] == *ptr++);
  45. }
  46. }
  47. // Ensure that index bases are preserved over reshape
  48. {
  49. boost::array<array::index,ndims> bases = {{0, 1, -1}};
  50. array A(dims);
  51. A.assign(data,data+data_size);
  52. array_ref B(data,dims);
  53. const_array_ref C(data,dims);
  54. A.reindex(bases);
  55. B.reindex(bases);
  56. C.reindex(bases);
  57. A.reshape(new_dims);
  58. B.reshape(new_dims);
  59. C.reshape(new_dims);
  60. int* ptr = data;
  61. for (array::index i = 0; i != 4; ++i)
  62. for (array::index j = 1; j != 4; ++j)
  63. for (array::index k = -1; k != 1; ++k) {
  64. BOOST_TEST(A[i][j][k] == *ptr);
  65. BOOST_TEST(B[i][j][k] == *ptr);
  66. BOOST_TEST(C[i][j][k] == *ptr++);
  67. }
  68. }
  69. return boost::report_errors();
  70. }