You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
7.8 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@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. #ifndef EIGEN_NO_STATIC_ASSERT
  10. #define EIGEN_NO_STATIC_ASSERT // turn static asserts into runtime asserts in order to check them
  11. #endif
  12. #include "main.h"
  13. #define EIGEN_TESTMAP_MAX_SIZE 256
  14. template<typename VectorType> void map_class_vector(const VectorType& m)
  15. {
  16. typedef typename VectorType::Index Index;
  17. typedef typename VectorType::Scalar Scalar;
  18. Index size = m.size();
  19. Scalar* array1 = internal::aligned_new<Scalar>(size);
  20. Scalar* array2 = internal::aligned_new<Scalar>(size);
  21. Scalar* array3 = new Scalar[size+1];
  22. Scalar* array3unaligned = (std::size_t(array3)%EIGEN_MAX_ALIGN_BYTES) == 0 ? array3+1 : array3;
  23. Scalar array4[EIGEN_TESTMAP_MAX_SIZE];
  24. Map<VectorType, AlignedMax>(array1, size) = VectorType::Random(size);
  25. Map<VectorType, AlignedMax>(array2, size) = Map<VectorType,AlignedMax>(array1, size);
  26. Map<VectorType>(array3unaligned, size) = Map<VectorType>(array1, size);
  27. Map<VectorType>(array4, size) = Map<VectorType,AlignedMax>(array1, size);
  28. VectorType ma1 = Map<VectorType, AlignedMax>(array1, size);
  29. VectorType ma2 = Map<VectorType, AlignedMax>(array2, size);
  30. VectorType ma3 = Map<VectorType>(array3unaligned, size);
  31. VectorType ma4 = Map<VectorType>(array4, size);
  32. VERIFY_IS_EQUAL(ma1, ma2);
  33. VERIFY_IS_EQUAL(ma1, ma3);
  34. VERIFY_IS_EQUAL(ma1, ma4);
  35. #ifdef EIGEN_VECTORIZE
  36. if(internal::packet_traits<Scalar>::Vectorizable)
  37. VERIFY_RAISES_ASSERT((Map<VectorType,AlignedMax>(array3unaligned, size)))
  38. #endif
  39. internal::aligned_delete(array1, size);
  40. internal::aligned_delete(array2, size);
  41. delete[] array3;
  42. }
  43. template<typename MatrixType> void map_class_matrix(const MatrixType& m)
  44. {
  45. typedef typename MatrixType::Index Index;
  46. typedef typename MatrixType::Scalar Scalar;
  47. Index rows = m.rows(), cols = m.cols(), size = rows*cols;
  48. Scalar s1 = internal::random<Scalar>();
  49. // array1 and array2 -> aligned heap allocation
  50. Scalar* array1 = internal::aligned_new<Scalar>(size);
  51. for(int i = 0; i < size; i++) array1[i] = Scalar(1);
  52. Scalar* array2 = internal::aligned_new<Scalar>(size);
  53. for(int i = 0; i < size; i++) array2[i] = Scalar(1);
  54. // array3unaligned -> unaligned pointer to heap
  55. Scalar* array3 = new Scalar[size+1];
  56. for(int i = 0; i < size+1; i++) array3[i] = Scalar(1);
  57. Scalar* array3unaligned = size_t(array3)%EIGEN_MAX_ALIGN_BYTES == 0 ? array3+1 : array3;
  58. Scalar array4[256];
  59. if(size<=256)
  60. for(int i = 0; i < size; i++) array4[i] = Scalar(1);
  61. Map<MatrixType> map1(array1, rows, cols);
  62. Map<MatrixType, AlignedMax> map2(array2, rows, cols);
  63. Map<MatrixType> map3(array3unaligned, rows, cols);
  64. Map<MatrixType> map4(array4, rows, cols);
  65. VERIFY_IS_EQUAL(map1, MatrixType::Ones(rows,cols));
  66. VERIFY_IS_EQUAL(map2, MatrixType::Ones(rows,cols));
  67. VERIFY_IS_EQUAL(map3, MatrixType::Ones(rows,cols));
  68. map1 = MatrixType::Random(rows,cols);
  69. map2 = map1;
  70. map3 = map1;
  71. MatrixType ma1 = map1;
  72. MatrixType ma2 = map2;
  73. MatrixType ma3 = map3;
  74. VERIFY_IS_EQUAL(map1, map2);
  75. VERIFY_IS_EQUAL(map1, map3);
  76. VERIFY_IS_EQUAL(ma1, ma2);
  77. VERIFY_IS_EQUAL(ma1, ma3);
  78. VERIFY_IS_EQUAL(ma1, map3);
  79. VERIFY_IS_APPROX(s1*map1, s1*map2);
  80. VERIFY_IS_APPROX(s1*ma1, s1*ma2);
  81. VERIFY_IS_EQUAL(s1*ma1, s1*ma3);
  82. VERIFY_IS_APPROX(s1*map1, s1*map3);
  83. map2 *= s1;
  84. map3 *= s1;
  85. VERIFY_IS_APPROX(s1*map1, map2);
  86. VERIFY_IS_APPROX(s1*map1, map3);
  87. if(size<=256)
  88. {
  89. VERIFY_IS_EQUAL(map4, MatrixType::Ones(rows,cols));
  90. map4 = map1;
  91. MatrixType ma4 = map4;
  92. VERIFY_IS_EQUAL(map1, map4);
  93. VERIFY_IS_EQUAL(ma1, map4);
  94. VERIFY_IS_EQUAL(ma1, ma4);
  95. VERIFY_IS_APPROX(s1*map1, s1*map4);
  96. map4 *= s1;
  97. VERIFY_IS_APPROX(s1*map1, map4);
  98. }
  99. internal::aligned_delete(array1, size);
  100. internal::aligned_delete(array2, size);
  101. delete[] array3;
  102. }
  103. template<typename VectorType> void map_static_methods(const VectorType& m)
  104. {
  105. typedef typename VectorType::Index Index;
  106. typedef typename VectorType::Scalar Scalar;
  107. Index size = m.size();
  108. Scalar* array1 = internal::aligned_new<Scalar>(size);
  109. Scalar* array2 = internal::aligned_new<Scalar>(size);
  110. Scalar* array3 = new Scalar[size+1];
  111. Scalar* array3unaligned = size_t(array3)%EIGEN_MAX_ALIGN_BYTES == 0 ? array3+1 : array3;
  112. VectorType::MapAligned(array1, size) = VectorType::Random(size);
  113. VectorType::Map(array2, size) = VectorType::Map(array1, size);
  114. VectorType::Map(array3unaligned, size) = VectorType::Map(array1, size);
  115. VectorType ma1 = VectorType::Map(array1, size);
  116. VectorType ma2 = VectorType::MapAligned(array2, size);
  117. VectorType ma3 = VectorType::Map(array3unaligned, size);
  118. VERIFY_IS_EQUAL(ma1, ma2);
  119. VERIFY_IS_EQUAL(ma1, ma3);
  120. internal::aligned_delete(array1, size);
  121. internal::aligned_delete(array2, size);
  122. delete[] array3;
  123. }
  124. template<typename PlainObjectType> void check_const_correctness(const PlainObjectType&)
  125. {
  126. // there's a lot that we can't test here while still having this test compile!
  127. // the only possible approach would be to run a script trying to compile stuff and checking that it fails.
  128. // CMake can help with that.
  129. // verify that map-to-const don't have LvalueBit
  130. typedef typename internal::add_const<PlainObjectType>::type ConstPlainObjectType;
  131. VERIFY( !(internal::traits<Map<ConstPlainObjectType> >::Flags & LvalueBit) );
  132. VERIFY( !(internal::traits<Map<ConstPlainObjectType, AlignedMax> >::Flags & LvalueBit) );
  133. VERIFY( !(Map<ConstPlainObjectType>::Flags & LvalueBit) );
  134. VERIFY( !(Map<ConstPlainObjectType, AlignedMax>::Flags & LvalueBit) );
  135. }
  136. template<typename Scalar>
  137. void map_not_aligned_on_scalar()
  138. {
  139. typedef Matrix<Scalar,Dynamic,Dynamic> MatrixType;
  140. typedef typename MatrixType::Index Index;
  141. Index size = 11;
  142. Scalar* array1 = internal::aligned_new<Scalar>((size+1)*(size+1)+1);
  143. Scalar* array2 = reinterpret_cast<Scalar*>(sizeof(Scalar)/2+std::size_t(array1));
  144. Map<MatrixType,0,OuterStride<> > map2(array2, size, size, OuterStride<>(size+1));
  145. MatrixType m2 = MatrixType::Random(size,size);
  146. map2 = m2;
  147. VERIFY_IS_EQUAL(m2, map2);
  148. typedef Matrix<Scalar,Dynamic,1> VectorType;
  149. Map<VectorType> map3(array2, size);
  150. MatrixType v3 = VectorType::Random(size);
  151. map3 = v3;
  152. VERIFY_IS_EQUAL(v3, map3);
  153. internal::aligned_delete(array1, (size+1)*(size+1)+1);
  154. }
  155. void test_mapped_matrix()
  156. {
  157. for(int i = 0; i < g_repeat; i++) {
  158. CALL_SUBTEST_1( map_class_vector(Matrix<float, 1, 1>()) );
  159. CALL_SUBTEST_1( check_const_correctness(Matrix<float, 1, 1>()) );
  160. CALL_SUBTEST_2( map_class_vector(Vector4d()) );
  161. CALL_SUBTEST_2( map_class_vector(VectorXd(13)) );
  162. CALL_SUBTEST_2( check_const_correctness(Matrix4d()) );
  163. CALL_SUBTEST_3( map_class_vector(RowVector4f()) );
  164. CALL_SUBTEST_4( map_class_vector(VectorXcf(8)) );
  165. CALL_SUBTEST_5( map_class_vector(VectorXi(12)) );
  166. CALL_SUBTEST_5( check_const_correctness(VectorXi(12)) );
  167. CALL_SUBTEST_1( map_class_matrix(Matrix<float, 1, 1>()) );
  168. CALL_SUBTEST_2( map_class_matrix(Matrix4d()) );
  169. CALL_SUBTEST_11( map_class_matrix(Matrix<float,3,5>()) );
  170. CALL_SUBTEST_4( map_class_matrix(MatrixXcf(internal::random<int>(1,10),internal::random<int>(1,10))) );
  171. CALL_SUBTEST_5( map_class_matrix(MatrixXi(internal::random<int>(1,10),internal::random<int>(1,10))) );
  172. CALL_SUBTEST_6( map_static_methods(Matrix<double, 1, 1>()) );
  173. CALL_SUBTEST_7( map_static_methods(Vector3f()) );
  174. CALL_SUBTEST_8( map_static_methods(RowVector3d()) );
  175. CALL_SUBTEST_9( map_static_methods(VectorXcd(8)) );
  176. CALL_SUBTEST_10( map_static_methods(VectorXf(12)) );
  177. CALL_SUBTEST_11( map_not_aligned_on_scalar<double>() );
  178. }
  179. }