#define BOOST_TEST_MODULE $Id: test_fill.cc 890 2010-01-06 03:56:22Z rhys $ #include #include #include #include #include template struct fill_functor { template< class Array, class V > void operator()(Array x, const V &v) { fill_functor f; for (typename Array::iterator i = x.begin(); i != x.end(); ++i) { typename Array::iterator::reference ri = *i; f(ri,v); } } }; template<> struct fill_functor<1> { template< class Array, class V > void operator()(Array x, const V &v) { std::fill(x.begin(), x.end(), v); } }; template<> struct fill_functor<0> { /* error on zero dimensional */ }; template void fill(Array &x, const V &v) { fill_functor()(x,v); } template void fill(boost::multi_array &x, const V2 &v) { std::fill_n(x.data(), x.num_elements(), v); } template void fill(boost::multi_array_ref &x, const V2 &v) { std::fill_n(x.data(), x.num_elements(), v); } typedef boost::mpl::list test_types; BOOST_AUTO_TEST_CASE_TEMPLATE(fill_multiarray_1D, T, test_types) { typedef boost::multi_array_types::index_range range; boost::multi_array foo(boost::extents[3]); fill(foo,123); for (boost::multi_array_types::index i = 0; i < foo.shape()[0]; ++i) { BOOST_CHECK_EQUAL(123, foo[i]); } typename boost::array_view_gen,1>::type bar = foo[ boost::indices[range()] ]; fill(bar,456); for (boost::multi_array_types::index i = 0; i < bar.shape()[0]; ++i) { BOOST_CHECK_EQUAL(456, bar[i]); } boost::scoped_array raw(new T[3]); boost::multi_array_ref baz(raw.get(),boost::extents[3]); fill(baz,123); for (boost::multi_array_types::index i = 0; i < baz.shape()[0]; ++i) { BOOST_CHECK_EQUAL(123, baz[i]); } typename boost::array_view_gen,1>::type qux = bar[ boost::indices[range()] ]; fill(qux,456); for (boost::multi_array_types::index i = 0; i < qux.shape()[0]; ++i) { BOOST_CHECK_EQUAL(456, qux[i]); } } BOOST_AUTO_TEST_CASE_TEMPLATE(fill_multiarray_2D, T, test_types) { typedef boost::multi_array_types::index_range range; boost::multi_array foo(boost::extents[3][4]); fill(foo,123); for (boost::multi_array_types::index i = 0; i < foo.shape()[0]; ++i) { for (boost::multi_array_types::index j = 0; j < foo.shape()[1]; ++j) { BOOST_CHECK_EQUAL(123, foo[i][j]); } } typename boost::array_view_gen,2>::type bar = foo[ boost::indices[range()][range()] ]; fill(bar,456); for (boost::multi_array_types::index i = 0; i < bar.shape()[0]; ++i) { for (boost::multi_array_types::index j = 0; j < bar.shape()[1]; ++j) { BOOST_CHECK_EQUAL(456, bar[i][j]); } } boost::scoped_array raw(new T[3*4]); boost::multi_array_ref baz(raw.get(),boost::extents[3][4]); fill(baz,123); for (boost::multi_array_types::index i = 0; i < baz.shape()[0]; ++i) { for (boost::multi_array_types::index j = 0; j < baz.shape()[1]; ++j) { BOOST_CHECK_EQUAL(123, baz[i][j]); } } typename boost::array_view_gen,2>::type qux = bar[ boost::indices[range()][range()] ]; fill(qux,456); for (boost::multi_array_types::index i = 0; i < qux.shape()[0]; ++i) { for (boost::multi_array_types::index j = 0; j < qux.shape()[0]; ++j) { BOOST_CHECK_EQUAL(456, qux[i][j]); } } }