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