os/ossrv/stdcpp/tsrc/Boost_test/multi_array/src/slice.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright 2002 The Trustees of Indiana University.
sl@0
     2
sl@0
     3
// Use, modification and distribution is subject to the Boost Software 
sl@0
     4
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
sl@0
     5
// http://www.boost.org/LICENSE_1_0.txt)
sl@0
     6
sl@0
     7
//  Boost.MultiArray Library
sl@0
     8
//  Authors: Ronald Garcia
sl@0
     9
//           Jeremy Siek
sl@0
    10
//           Andrew Lumsdaine
sl@0
    11
//  See http://www.boost.org/libs/multi_array for documentation.
sl@0
    12
sl@0
    13
// 
sl@0
    14
// slice.cpp - testing out slicing on a matrices
sl@0
    15
//
sl@0
    16
/*
sl@0
    17
 * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
sl@0
    18
*/
sl@0
    19
sl@0
    20
#include "generative_tests.hpp"
sl@0
    21
#include "boost/array.hpp"
sl@0
    22
#include "boost/mpl/if.hpp"
sl@0
    23
#include "boost/type_traits/is_same.hpp"
sl@0
    24
sl@0
    25
template <typename Array>
sl@0
    26
struct view_traits_mutable {
sl@0
    27
public:
sl@0
    28
#if 0 // RG - MSVC can't handle templates nested in templates. Use traits
sl@0
    29
  typedef typename Array::template array_view<3>::type array_view3;
sl@0
    30
  typedef typename Array::template array_view<2>::type array_view2;
sl@0
    31
#endif
sl@0
    32
  typedef typename boost::array_view_gen<Array,3>::type array_view3;
sl@0
    33
  typedef typename boost::array_view_gen<Array,2>::type array_view2;
sl@0
    34
};
sl@0
    35
sl@0
    36
template <typename Array>
sl@0
    37
struct view_traits_const {
sl@0
    38
#if 0 // RG - MSVC can't handle templates nested in templates. Use traits
sl@0
    39
  typedef typename Array::template const_array_view<3>::type array_view3;
sl@0
    40
  typedef typename Array::template const_array_view<2>::type array_view2;
sl@0
    41
#endif
sl@0
    42
  typedef typename boost::const_array_view_gen<Array,3>::type array_view3;
sl@0
    43
  typedef typename boost::const_array_view_gen<Array,2>::type array_view2;
sl@0
    44
};
sl@0
    45
sl@0
    46
sl@0
    47
// Meta-program selects the proper view_traits implementation.
sl@0
    48
template <typename Array, typename ConstTag>
sl@0
    49
struct view_traits_generator :
sl@0
    50
  boost::mpl::if_< boost::is_same<ConstTag,const_array_tag>,
sl@0
    51
                   view_traits_const<Array>,
sl@0
    52
                   view_traits_mutable<Array> >
sl@0
    53
{};
sl@0
    54
sl@0
    55
sl@0
    56
template <typename Array, typename ViewTraits>
sl@0
    57
void test_views(Array& A, const ViewTraits&) {
sl@0
    58
  typedef typename Array::index index;
sl@0
    59
  typedef typename Array::index_range range;
sl@0
    60
  typename Array::index_gen indices;
sl@0
    61
sl@0
    62
  const index idx0 = A.index_bases()[0];
sl@0
    63
  const index idx1 = A.index_bases()[1];
sl@0
    64
  const index idx2 = A.index_bases()[2];
sl@0
    65
sl@0
    66
  // Standard View
sl@0
    67
  {
sl@0
    68
    typename ViewTraits::array_view3 B = A[
sl@0
    69
      indices[range(idx0+0,idx0+2)]
sl@0
    70
             [range(idx1+1,idx1+3)]
sl@0
    71
             [range(idx2+0,idx2+4,2)]
sl@0
    72
    ];
sl@0
    73
    
sl@0
    74
    for (index i = 0; i != 2; ++i)
sl@0
    75
      for (index j = 0; j != 2; ++j)
sl@0
    76
        for (index k = 0; k != 2; ++k) {
sl@0
    77
          BOOST_CHECK(B[i][j][k] == A[idx0+i][idx1+j+1][idx2+k*2]);
sl@0
    78
          boost::array<index,3> elmts;
sl@0
    79
          elmts[0]=i; elmts[1]=j; elmts[2]=k;
sl@0
    80
          BOOST_CHECK(B(elmts) == A[idx0+i][idx1+j+1][idx2+k*2]);
sl@0
    81
        }
sl@0
    82
  }
sl@0
    83
  // Degenerate dimensions
sl@0
    84
  {
sl@0
    85
    typename ViewTraits::array_view2 B =
sl@0
    86
      A[indices[range(idx0+0,idx0+2)][idx1+1][range(idx2+0,idx2+4,2)]];
sl@0
    87
    
sl@0
    88
    for (index i = 0; i != 2; ++i)
sl@0
    89
      for (index j = 0; j != 2; ++j) {
sl@0
    90
        BOOST_CHECK(B[i][j] == A[idx0+i][idx1+1][idx2+j*2]);
sl@0
    91
        boost::array<index,2> elmts;
sl@0
    92
        elmts[0]=i; elmts[1]=j;
sl@0
    93
        BOOST_CHECK(B(elmts) == A[idx0+i][idx1+1][idx2+j*2]);
sl@0
    94
      }
sl@0
    95
  }
sl@0
    96
  ++tests_run;
sl@0
    97
}
sl@0
    98
sl@0
    99
sl@0
   100
template <typename Array>
sl@0
   101
void access(Array& A, const mutable_array_tag&) {
sl@0
   102
  assign(A);
sl@0
   103
sl@0
   104
  typedef typename view_traits_generator<Array,mutable_array_tag>::type
sl@0
   105
    m_view_traits;
sl@0
   106
sl@0
   107
  typedef typename view_traits_generator<Array,const_array_tag>::type
sl@0
   108
    c_view_traits;
sl@0
   109
sl@0
   110
  test_views(A,m_view_traits());
sl@0
   111
  test_views(A,c_view_traits());
sl@0
   112
sl@0
   113
  const Array& CA = A;
sl@0
   114
  test_views(CA,c_view_traits());
sl@0
   115
}
sl@0
   116
sl@0
   117
template <typename Array>
sl@0
   118
void access(Array& A, const const_array_tag&) {
sl@0
   119
  typedef typename view_traits_generator<Array,const_array_tag>::type
sl@0
   120
    c_view_traits;
sl@0
   121
  test_views(A,c_view_traits());
sl@0
   122
}
sl@0
   123
sl@0
   124
sl@0
   125
int test_main(int,char*[]) {
sl@0
   126
#ifdef __SYMBIAN32__
sl@0
   127
if(boost::minimal_test::errors_counter() != 0)
sl@0
   128
   assert_failed = true;
sl@0
   129
   	testResultXml("slice");
sl@0
   130
	close_log_file();
sl@0
   131
#endif
sl@0
   132
  return run_generative_tests();
sl@0
   133
}