summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/multi_array/test/index_bases.cpp
blob: 159e39420d1b0f079d4b0267b09bd93e157176a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2002 The Trustees of Indiana University.

// Use, modification and distribution is subject to the Boost Software 
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

//  Boost.MultiArray Library
//  Authors: Ronald Garcia
//           Jeremy Siek
//           Andrew Lumsdaine
//  See http://www.boost.org/libs/multi_array for documentation.

// 
// index_bases - test of the index_base modifying facilities.
//

#include <boost/multi_array.hpp>

#include <boost/core/lightweight_test.hpp>

#include <boost/array.hpp>
#include <vector>
#include <iostream>
int
main()
{
  typedef boost::multi_array<double, 3> array;
  typedef boost::multi_array_ref<double, 3> array_ref;
  typedef boost::const_multi_array_ref<double, 3> const_array_ref;
  typedef array::array_view<3>::type array_view;

  typedef array::size_type size_type;
  typedef array::extent_range range;
  typedef array::index_range irange;

  array::extent_gen extents;
  array::index_gen indices;

  // Construct with nonzero bases
  {

    array A(extents[range(1,4)][range(2,5)][range(3,6)]);
    array B(extents[3][3][3]);

    double ptr[27];
    array_ref
      C(ptr,extents[range(1,4)][range(2,5)][range(3,6)]);

    const_array_ref
      D(ptr,extents[range(1,4)][range(2,5)][range(3,6)]);

    array_view E = A[indices[irange()][irange()][irange()]];

    std::vector<double> vals;
    for (int i = 0; i < 27; ++i)
      vals.push_back(i);

    A.assign(vals.begin(),vals.end());
    B.assign(vals.begin(),vals.end());
    C.assign(vals.begin(),vals.end());

    boost::array<int,3> bases = { { 1, 2, 3 } };
    for (size_type a = 0; a < A.shape()[0]; ++a)
      for (size_type b = 0; b < A.shape()[1]; ++b)
        for (size_type c = 0; c < A.shape()[2]; ++c) {
          BOOST_TEST(A[a+bases[0]][b+bases[1]][c+bases[2]] == B[a][b][c]);
          BOOST_TEST(C[a+bases[0]][b+bases[1]][c+bases[2]] == B[a][b][c]);
          BOOST_TEST(D[a+bases[0]][b+bases[1]][c+bases[2]] == B[a][b][c]);
          // Test that E does not inherit A's index_base
          BOOST_TEST(E[a][b][c] == B[a][b][c]);
        }
  }

  // Reindex
  {
    typedef array::size_type size_type;
    array A(extents[3][3][3]), B(extents[3][3][3]);

    double ptr[27];
    array_ref C(ptr,extents[3][3][3]);
    const_array_ref D(ptr,extents[3][3][3]);

    array_view E = B[indices[irange()][irange()][irange()]];

    std::vector<double> vals;
    for (int i = 0; i < 27; ++i)
      vals.push_back(i);

    A.assign(vals.begin(),vals.end());
    B.assign(vals.begin(),vals.end());
    C.assign(vals.begin(),vals.end());

    boost::array<int,3> bases = { { 1, 2, 3 } };

    A.reindex(bases);
    C.reindex(bases);
    D.reindex(bases);
    E.reindex(bases);

    for (size_type a = 0; a < A.shape()[0]; ++a)
      for (size_type b = 0; b < A.shape()[1]; ++b)
        for (size_type c = 0; c < A.shape()[2]; ++c) {
          BOOST_TEST(A[a+bases[0]][b+bases[1]][c+bases[2]] == B[a][b][c]);
          BOOST_TEST(C[a+bases[0]][b+bases[1]][c+bases[2]] == B[a][b][c]);
          BOOST_TEST(D[a+bases[0]][b+bases[1]][c+bases[2]] == B[a][b][c]);
          BOOST_TEST(E[a+bases[0]][b+bases[1]][c+bases[2]] == B[a][b][c]);
        }
  }

  // Set Index Base
  {
    typedef array::size_type size_type;
    array A(extents[3][3][3]), B(extents[3][3][3]);

    double ptr[27];
    array_ref C(ptr,extents[3][3][3]);
    const_array_ref D(ptr,extents[3][3][3]);

    array_view E = B[indices[irange()][irange()][irange()]];

    std::vector<double> vals;
    for (int i = 0; i < 27; ++i)
      vals.push_back(i);

    A.assign(vals.begin(),vals.end());
    B.assign(vals.begin(),vals.end());
    C.assign(vals.begin(),vals.end());

#ifdef BOOST_NO_SFINAE
    typedef boost::multi_array_types::index index;
    A.reindex(index(1));
    C.reindex(index(1));
    D.reindex(index(1));
    E.reindex(index(1));
#else
    A.reindex(1);
    C.reindex(1);
    D.reindex(1);
    E.reindex(1);
#endif

    for (size_type a = 0; a < A.shape()[0]; ++a)
      for (size_type b = 0; b < A.shape()[1]; ++b)
        for (size_type c = 0; c < A.shape()[2]; ++c) {
          BOOST_TEST(A[a+1][b+1][c+1] == B[a][b][c]);
          BOOST_TEST(C[a+1][b+1][c+1] == B[a][b][c]);
          BOOST_TEST(D[a+1][b+1][c+1] == B[a][b][c]);
          BOOST_TEST(E[a+1][b+1][c+1] == B[a][b][c]);
        }
  }

  return boost::report_errors();
}