Home | History | Annotate | Download | only in test
      1 // This file is part of Eigen, a lightweight C++ template library
      2 // for linear algebra.
      3 //
      4 // Copyright (C) 2011 Benoit Jacob <jacob.benoit.1 (at) gmail.com>
      5 //
      6 // This Source Code Form is subject to the terms of the Mozilla
      7 // Public License v. 2.0. If a copy of the MPL was not distributed
      8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9 
     10 #include "main.h"
     11 
     12 float *ptr;
     13 const float *const_ptr;
     14 
     15 template<typename PlainObjectType,
     16          bool IsDynamicSize = PlainObjectType::SizeAtCompileTime == Dynamic,
     17          bool IsVector = PlainObjectType::IsVectorAtCompileTime
     18 >
     19 struct mapstaticmethods_impl {};
     20 
     21 template<typename PlainObjectType, bool IsVector>
     22 struct mapstaticmethods_impl<PlainObjectType, false, IsVector>
     23 {
     24   static void run(const PlainObjectType& m)
     25   {
     26     mapstaticmethods_impl<PlainObjectType, true, IsVector>::run(m);
     27 
     28     int i = internal::random<int>(2,5), j = internal::random<int>(2,5);
     29 
     30     PlainObjectType::Map(ptr).setZero();
     31     PlainObjectType::MapAligned(ptr).setZero();
     32     PlainObjectType::Map(const_ptr).sum();
     33     PlainObjectType::MapAligned(const_ptr).sum();
     34 
     35     PlainObjectType::Map(ptr, InnerStride<>(i)).setZero();
     36     PlainObjectType::MapAligned(ptr, InnerStride<>(i)).setZero();
     37     PlainObjectType::Map(const_ptr, InnerStride<>(i)).sum();
     38     PlainObjectType::MapAligned(const_ptr, InnerStride<>(i)).sum();
     39 
     40     PlainObjectType::Map(ptr, InnerStride<2>()).setZero();
     41     PlainObjectType::MapAligned(ptr, InnerStride<3>()).setZero();
     42     PlainObjectType::Map(const_ptr, InnerStride<4>()).sum();
     43     PlainObjectType::MapAligned(const_ptr, InnerStride<5>()).sum();
     44 
     45     PlainObjectType::Map(ptr, OuterStride<>(i)).setZero();
     46     PlainObjectType::MapAligned(ptr, OuterStride<>(i)).setZero();
     47     PlainObjectType::Map(const_ptr, OuterStride<>(i)).sum();
     48     PlainObjectType::MapAligned(const_ptr, OuterStride<>(i)).sum();
     49 
     50     PlainObjectType::Map(ptr, OuterStride<2>()).setZero();
     51     PlainObjectType::MapAligned(ptr, OuterStride<3>()).setZero();
     52     PlainObjectType::Map(const_ptr, OuterStride<4>()).sum();
     53     PlainObjectType::MapAligned(const_ptr, OuterStride<5>()).sum();
     54 
     55     PlainObjectType::Map(ptr, Stride<Dynamic, Dynamic>(i,j)).setZero();
     56     PlainObjectType::MapAligned(ptr, Stride<2,Dynamic>(2,i)).setZero();
     57     PlainObjectType::Map(const_ptr, Stride<Dynamic,3>(i,3)).sum();
     58     PlainObjectType::MapAligned(const_ptr, Stride<Dynamic, Dynamic>(i,j)).sum();
     59 
     60     PlainObjectType::Map(ptr, Stride<2,3>()).setZero();
     61     PlainObjectType::MapAligned(ptr, Stride<3,4>()).setZero();
     62     PlainObjectType::Map(const_ptr, Stride<2,4>()).sum();
     63     PlainObjectType::MapAligned(const_ptr, Stride<5,3>()).sum();
     64   }
     65 };
     66 
     67 template<typename PlainObjectType>
     68 struct mapstaticmethods_impl<PlainObjectType, true, false>
     69 {
     70   static void run(const PlainObjectType& m)
     71   {
     72     typedef typename PlainObjectType::Index Index;
     73     Index rows = m.rows(), cols = m.cols();
     74 
     75     int i = internal::random<int>(2,5), j = internal::random<int>(2,5);
     76 
     77     PlainObjectType::Map(ptr, rows, cols).setZero();
     78     PlainObjectType::MapAligned(ptr, rows, cols).setZero();
     79     PlainObjectType::Map(const_ptr, rows, cols).sum();
     80     PlainObjectType::MapAligned(const_ptr, rows, cols).sum();
     81 
     82     PlainObjectType::Map(ptr, rows, cols, InnerStride<>(i)).setZero();
     83     PlainObjectType::MapAligned(ptr, rows, cols, InnerStride<>(i)).setZero();
     84     PlainObjectType::Map(const_ptr, rows, cols, InnerStride<>(i)).sum();
     85     PlainObjectType::MapAligned(const_ptr, rows, cols, InnerStride<>(i)).sum();
     86 
     87     PlainObjectType::Map(ptr, rows, cols, InnerStride<2>()).setZero();
     88     PlainObjectType::MapAligned(ptr, rows, cols, InnerStride<3>()).setZero();
     89     PlainObjectType::Map(const_ptr, rows, cols, InnerStride<4>()).sum();
     90     PlainObjectType::MapAligned(const_ptr, rows, cols, InnerStride<5>()).sum();
     91 
     92     PlainObjectType::Map(ptr, rows, cols, OuterStride<>(i)).setZero();
     93     PlainObjectType::MapAligned(ptr, rows, cols, OuterStride<>(i)).setZero();
     94     PlainObjectType::Map(const_ptr, rows, cols, OuterStride<>(i)).sum();
     95     PlainObjectType::MapAligned(const_ptr, rows, cols, OuterStride<>(i)).sum();
     96 
     97     PlainObjectType::Map(ptr, rows, cols, OuterStride<2>()).setZero();
     98     PlainObjectType::MapAligned(ptr, rows, cols, OuterStride<3>()).setZero();
     99     PlainObjectType::Map(const_ptr, rows, cols, OuterStride<4>()).sum();
    100     PlainObjectType::MapAligned(const_ptr, rows, cols, OuterStride<5>()).sum();
    101 
    102     PlainObjectType::Map(ptr, rows, cols, Stride<Dynamic, Dynamic>(i,j)).setZero();
    103     PlainObjectType::MapAligned(ptr, rows, cols, Stride<2,Dynamic>(2,i)).setZero();
    104     PlainObjectType::Map(const_ptr, rows, cols, Stride<Dynamic,3>(i,3)).sum();
    105     PlainObjectType::MapAligned(const_ptr, rows, cols, Stride<Dynamic, Dynamic>(i,j)).sum();
    106 
    107     PlainObjectType::Map(ptr, rows, cols, Stride<2,3>()).setZero();
    108     PlainObjectType::MapAligned(ptr, rows, cols, Stride<3,4>()).setZero();
    109     PlainObjectType::Map(const_ptr, rows, cols, Stride<2,4>()).sum();
    110     PlainObjectType::MapAligned(const_ptr, rows, cols, Stride<5,3>()).sum();
    111   }
    112 };
    113 
    114 template<typename PlainObjectType>
    115 struct mapstaticmethods_impl<PlainObjectType, true, true>
    116 {
    117   static void run(const PlainObjectType& v)
    118   {
    119     typedef typename PlainObjectType::Index Index;
    120     Index size = v.size();
    121 
    122     int i = internal::random<int>(2,5);
    123 
    124     PlainObjectType::Map(ptr, size).setZero();
    125     PlainObjectType::MapAligned(ptr, size).setZero();
    126     PlainObjectType::Map(const_ptr, size).sum();
    127     PlainObjectType::MapAligned(const_ptr, size).sum();
    128 
    129     PlainObjectType::Map(ptr, size, InnerStride<>(i)).setZero();
    130     PlainObjectType::MapAligned(ptr, size, InnerStride<>(i)).setZero();
    131     PlainObjectType::Map(const_ptr, size, InnerStride<>(i)).sum();
    132     PlainObjectType::MapAligned(const_ptr, size, InnerStride<>(i)).sum();
    133 
    134     PlainObjectType::Map(ptr, size, InnerStride<2>()).setZero();
    135     PlainObjectType::MapAligned(ptr, size, InnerStride<3>()).setZero();
    136     PlainObjectType::Map(const_ptr, size, InnerStride<4>()).sum();
    137     PlainObjectType::MapAligned(const_ptr, size, InnerStride<5>()).sum();
    138   }
    139 };
    140 
    141 template<typename PlainObjectType>
    142 void mapstaticmethods(const PlainObjectType& m)
    143 {
    144   mapstaticmethods_impl<PlainObjectType>::run(m);
    145   VERIFY(true); // just to avoid 'unused function' warning
    146 }
    147 
    148 void test_mapstaticmethods()
    149 {
    150   ptr = internal::aligned_new<float>(1000);
    151   for(int i = 0; i < 1000; i++) ptr[i] = float(i);
    152 
    153   const_ptr = ptr;
    154 
    155   CALL_SUBTEST_1(( mapstaticmethods(Matrix<float, 1, 1>()) ));
    156   CALL_SUBTEST_1(( mapstaticmethods(Vector2f()) ));
    157   CALL_SUBTEST_2(( mapstaticmethods(Vector3f()) ));
    158   CALL_SUBTEST_2(( mapstaticmethods(Matrix2f()) ));
    159   CALL_SUBTEST_3(( mapstaticmethods(Matrix4f()) ));
    160   CALL_SUBTEST_3(( mapstaticmethods(Array4f()) ));
    161   CALL_SUBTEST_4(( mapstaticmethods(Array3f()) ));
    162   CALL_SUBTEST_4(( mapstaticmethods(Array33f()) ));
    163   CALL_SUBTEST_5(( mapstaticmethods(Array44f()) ));
    164   CALL_SUBTEST_5(( mapstaticmethods(VectorXf(1)) ));
    165   CALL_SUBTEST_5(( mapstaticmethods(VectorXf(8)) ));
    166   CALL_SUBTEST_6(( mapstaticmethods(MatrixXf(1,1)) ));
    167   CALL_SUBTEST_6(( mapstaticmethods(MatrixXf(5,7)) ));
    168   CALL_SUBTEST_7(( mapstaticmethods(ArrayXf(1)) ));
    169   CALL_SUBTEST_7(( mapstaticmethods(ArrayXf(5)) ));
    170   CALL_SUBTEST_8(( mapstaticmethods(ArrayXXf(1,1)) ));
    171   CALL_SUBTEST_8(( mapstaticmethods(ArrayXXf(8,6)) ));
    172 
    173   internal::aligned_delete(ptr, 1000);
    174 }
    175 
    176