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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
#ifndef BOOST_MULTI_ARRAY_GENERATIVE_TESTS_HPP
#define BOOST_MULTI_ARRAY_GENERATIVE_TESTS_HPP
// 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.
//
// generative-tests.hpp - Framework for running tests on all the types
// of multi_array
//
// In order to create a set of tests, you must define the following two
// function signatures:
// template <typename Array>
// void access(Array& A, const mutable_array_tag&);
//
// template <typename Array>
// void access(Array& A, const const_array_tag&);
//
// The framework will always pass 2x3x4 arrays into these functions.
// The const_array_tag version of access must NOT attempt to modify
// the array. Assume that the passed array has constness in this case.
//
// The mutable_array_tag version of access should pass the array to the
// assign() function in order to set its values before running tests.
//
// If you wish to write your own code to assign data to the array
// (ie. test the iterators by assigning data with them), you must
// #define MULTIARRAY_TEST_ASSIGN before including this file.
// assign() will call this function.
//
// If you wish to know how many tests were run, you must increment
// the global variable 'tests_run' somewhere in your test code.
//
// Since generative-tests uses the Boost.Test framework, you must
// define at least the following:
//
// int test_main(int,char*[]) { return run_generative_tests(); }
//
#include <boost/multi_array.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp> /* BOOST_NO_SFINAE */
#include <algorithm>
#include <iostream>
#include <vector>
namespace {
unsigned int tests_run = 0;
} // empty namespace
struct mutable_array_tag { };
struct const_array_tag { };
template <typename Array>
void assign_if_not_const(Array&, const const_array_tag&) {
// do nothing
}
template <typename Array>
void assign_if_not_const(Array& A, const mutable_array_tag&);
#ifndef MULTIARRAY_TEST_ASSIGN
template <typename Array>
void assign_if_not_const(Array& A, const mutable_array_tag&) {
typedef typename Array::index index;
const index idx0 = A.index_bases()[0];
const index idx1 = A.index_bases()[1];
const index idx2 = A.index_bases()[2];
int num = 0;
for (index i = idx0; i != idx0 + 2; ++i)
for (index j = idx1; j != idx1 + 3; ++j)
for (index k = idx2; k != idx2 + 4; ++k)
A[i][j][k] = num++;
}
#endif // MULTIARRAY_TEST_ASSIGN
template <typename Array>
void assign(Array& A) {
assign_if_not_const(A,mutable_array_tag());
}
template <typename Array>
void access(Array& A, const mutable_array_tag&);
template <typename Array>
void access(Array& A, const const_array_tag&);
template <typename StorageOrder3,typename StorageOrder4,typename Modifier>
void run_configuration(const StorageOrder3& so3,
const StorageOrder4& so4,
const Modifier& modifier) {
// multi_array
{
typedef boost::multi_array<int,3> array;
typename array::extent_gen extents;
{
array A(extents[2][3][4],so3);
modifier.modify(A);
access(A,mutable_array_tag());
}
}
// multi_array_ref
{
typedef boost::multi_array_ref<int,3> array_ref;
typename array_ref::extent_gen extents;
{
int local[24];
array_ref A(local,extents[2][3][4],so3);
modifier.modify(A);
access(A,mutable_array_tag());
}
}
// const_multi_array_ref
{
typedef boost::multi_array_ref<int,3> array_ref;
typedef boost::const_multi_array_ref<int,3> const_array_ref;
typename array_ref::extent_gen extents;
{
int local[24];
array_ref A(local,extents[2][3][4],so3);
modifier.modify(A);
assign(A);
const_array_ref B = A;
access(B,const_array_tag());
}
}
// sub_array
{
typedef boost::multi_array<int,4> array;
typename array::extent_gen extents;
{
array A(extents[2][2][3][4],so4);
modifier.modify(A);
typename array::template subarray<3>::type B = A[1];
access(B,mutable_array_tag());
}
}
// const_sub_array
{
typedef boost::multi_array<int,4> array;
typename array::extent_gen extents;
{
array A(extents[2][2][3][4],so4);
modifier.modify(A);
typename array::template subarray<3>::type B = A[1];
assign(B);
typename array::template const_subarray<3>::type C = B;
access(C,const_array_tag());
}
}
// array_view
{
typedef boost::multi_array<int,3> array;
typedef typename array::index_range range;
typename array::index_gen indices;
typename array::extent_gen extents;
{
typedef typename array::index index;
array A(extents[4][5][6],so3);
modifier.modify(A);
const index idx0 = A.index_bases()[0];
const index idx1 = A.index_bases()[1];
const index idx2 = A.index_bases()[2];
typename array::template array_view<3>::type B =A[
indices[range(idx0+1,idx0+3)]
[range(idx1+1,idx1+4)]
[range(idx2+1,idx2+5)]
];
access(B,mutable_array_tag());
}
}
// const_array_view
{
typedef boost::multi_array<int,3> array;
typedef typename array::index_range range;
typename array::index_gen indices;
typename array::extent_gen extents;
{
typedef typename array::index index;
array A(extents[4][5][6],so3);
modifier.modify(A);
const index idx0 = A.index_bases()[0];
const index idx1 = A.index_bases()[1];
const index idx2 = A.index_bases()[2];
typename array::template array_view<3>::type B =A[
indices[range(idx0+1,idx0+3)]
[range(idx1+1,idx1+4)]
[range(idx2+1,idx2+5)]
];
assign(B);
typename array::template const_array_view<3>::type C = B;
access(C,const_array_tag());
}
}
}
template <typename ArrayModifier>
void run_storage_tests(const ArrayModifier& modifier) {
run_configuration(boost::c_storage_order(),
boost::c_storage_order(),modifier);
run_configuration(boost::fortran_storage_order(),
boost::fortran_storage_order(),modifier);
std::size_t ordering[] = {2,0,1,3};
bool ascending[] = {false,true,true,true};
run_configuration(boost::general_storage_order<3>(ordering,ascending),
boost::general_storage_order<4>(ordering,ascending),
modifier);
}
struct null_modifier {
template <typename Array>
void modify(Array&) const { }
};
struct set_index_base_modifier {
template <typename Array>
void modify(Array& A) const {
#ifdef BOOST_NO_SFINAE
typedef boost::multi_array_types::index index;
A.reindex(index(1));
#else
A.reindex(1);
#endif
}
};
struct reindex_modifier {
template <typename Array>
void modify(Array& A) const {
boost::array<int,4> bases = {{1,2,3,4}};
A.reindex(bases);
}
};
struct reshape_modifier {
template <typename Array>
void modify(Array& A) const {
typedef typename Array::size_type size_type;
std::vector<size_type> old_shape(A.num_dimensions());
std::vector<size_type> new_shape(A.num_dimensions());
std::copy(A.shape(),A.shape()+A.num_dimensions(),old_shape.begin());
std::copy(old_shape.rbegin(),old_shape.rend(),new_shape.begin());
A.reshape(new_shape);
A.reshape(old_shape);
}
};
int run_generative_tests() {
run_storage_tests(null_modifier());
run_storage_tests(set_index_base_modifier());
run_storage_tests(reindex_modifier());
run_storage_tests(reshape_modifier());
std::cout << "Total Tests Run: " << tests_run << '\n';
return boost::report_errors();
}
#endif
|