access.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. // access.cpp - operator[] and operator() tests with various arrays
  12. // The tests assume that they are working on an Array of shape 2x3x4
  13. //
  14. #include "generative_tests.hpp"
  15. #include <boost/static_assert.hpp>
  16. template <typename Array>
  17. void access(Array& A, const mutable_array_tag&) {
  18. assign(A);
  19. access(A,const_array_tag());
  20. const Array& CA = A;
  21. access(CA,const_array_tag());
  22. }
  23. template <typename Array>
  24. void access(Array& A, const const_array_tag&) {
  25. const unsigned int ndims = 3;
  26. BOOST_STATIC_ASSERT((Array::dimensionality == ndims));
  27. typedef typename Array::index index;
  28. const index idx0 = A.index_bases()[0];
  29. const index idx1 = A.index_bases()[1];
  30. const index idx2 = A.index_bases()[2];
  31. // operator[]
  32. int cnum = 0;
  33. const Array& CA = A;
  34. for (index i = idx0; i != idx0+2; ++i)
  35. for (index j = idx1; j != idx1+3; ++j)
  36. for (index k = idx2; k != idx2+4; ++k) {
  37. BOOST_TEST(A[i][j][k] == cnum++);
  38. BOOST_TEST(CA[i][j][k] == A[i][j][k]);
  39. }
  40. // operator()
  41. for (index i2 = idx0; i2 != idx0+2; ++i2)
  42. for (index j2 = idx1; j2 != idx1+3; ++j2)
  43. for (index k2 = idx2; k2 != idx2+4; ++k2) {
  44. boost::array<index,ndims> indices;
  45. indices[0] = i2; indices[1] = j2; indices[2] = k2;
  46. BOOST_TEST(A(indices) == A[i2][j2][k2]);
  47. BOOST_TEST(CA(indices) == A(indices));
  48. }
  49. ++tests_run;
  50. }
  51. int main() {
  52. return run_generative_tests();
  53. }