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.

144 lines
5.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. template<typename VectorType> void map_class_vector(const VectorType& m)
  14. {
  15. typedef typename VectorType::Index Index;
  16. typedef typename VectorType::Scalar Scalar;
  17. Index size = m.size();
  18. // test Map.h
  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 = size_t(array3)%16 == 0 ? array3+1 : array3;
  23. Map<VectorType, Aligned>(array1, size) = VectorType::Random(size);
  24. Map<VectorType, Aligned>(array2, size) = Map<VectorType,Aligned>(array1, size);
  25. Map<VectorType>(array3unaligned, size) = Map<VectorType>(array1, size);
  26. VectorType ma1 = Map<VectorType, Aligned>(array1, size);
  27. VectorType ma2 = Map<VectorType, Aligned>(array2, size);
  28. VectorType ma3 = Map<VectorType>(array3unaligned, size);
  29. VERIFY_IS_EQUAL(ma1, ma2);
  30. VERIFY_IS_EQUAL(ma1, ma3);
  31. #ifdef EIGEN_VECTORIZE
  32. if(internal::packet_traits<Scalar>::Vectorizable)
  33. VERIFY_RAISES_ASSERT((Map<VectorType,Aligned>(array3unaligned, size)))
  34. #endif
  35. internal::aligned_delete(array1, size);
  36. internal::aligned_delete(array2, size);
  37. delete[] array3;
  38. }
  39. template<typename MatrixType> void map_class_matrix(const MatrixType& m)
  40. {
  41. typedef typename MatrixType::Index Index;
  42. typedef typename MatrixType::Scalar Scalar;
  43. Index rows = m.rows(), cols = m.cols(), size = rows*cols;
  44. // test Map.h
  45. Scalar* array1 = internal::aligned_new<Scalar>(size);
  46. for(int i = 0; i < size; i++) array1[i] = Scalar(1);
  47. Scalar* array2 = internal::aligned_new<Scalar>(size);
  48. for(int i = 0; i < size; i++) array2[i] = Scalar(1);
  49. Scalar* array3 = new Scalar[size+1];
  50. for(int i = 0; i < size+1; i++) array3[i] = Scalar(1);
  51. Scalar* array3unaligned = size_t(array3)%16 == 0 ? array3+1 : array3;
  52. Map<MatrixType, Aligned>(array1, rows, cols) = MatrixType::Ones(rows,cols);
  53. Map<MatrixType>(array2, rows, cols) = Map<MatrixType>(array1, rows, cols);
  54. Map<MatrixType>(array3unaligned, rows, cols) = Map<MatrixType>(array1, rows, cols);
  55. MatrixType ma1 = Map<MatrixType>(array1, rows, cols);
  56. MatrixType ma2 = Map<MatrixType, Aligned>(array2, rows, cols);
  57. VERIFY_IS_EQUAL(ma1, ma2);
  58. MatrixType ma3 = Map<MatrixType>(array3unaligned, rows, cols);
  59. VERIFY_IS_EQUAL(ma1, ma3);
  60. internal::aligned_delete(array1, size);
  61. internal::aligned_delete(array2, size);
  62. delete[] array3;
  63. }
  64. template<typename VectorType> void map_static_methods(const VectorType& m)
  65. {
  66. typedef typename VectorType::Index Index;
  67. typedef typename VectorType::Scalar Scalar;
  68. Index size = m.size();
  69. // test Map.h
  70. Scalar* array1 = internal::aligned_new<Scalar>(size);
  71. Scalar* array2 = internal::aligned_new<Scalar>(size);
  72. Scalar* array3 = new Scalar[size+1];
  73. Scalar* array3unaligned = size_t(array3)%16 == 0 ? array3+1 : array3;
  74. VectorType::MapAligned(array1, size) = VectorType::Random(size);
  75. VectorType::Map(array2, size) = VectorType::Map(array1, size);
  76. VectorType::Map(array3unaligned, size) = VectorType::Map(array1, size);
  77. VectorType ma1 = VectorType::Map(array1, size);
  78. VectorType ma2 = VectorType::MapAligned(array2, size);
  79. VectorType ma3 = VectorType::Map(array3unaligned, size);
  80. VERIFY_IS_EQUAL(ma1, ma2);
  81. VERIFY_IS_EQUAL(ma1, ma3);
  82. internal::aligned_delete(array1, size);
  83. internal::aligned_delete(array2, size);
  84. delete[] array3;
  85. }
  86. template<typename PlainObjectType> void check_const_correctness(const PlainObjectType&)
  87. {
  88. typedef typename PlainObjectType::Index Index;
  89. typedef typename PlainObjectType::Scalar Scalar;
  90. // there's a lot that we can't test here while still having this test compile!
  91. // the only possible approach would be to run a script trying to compile stuff and checking that it fails.
  92. // CMake can help with that.
  93. // verify that map-to-const don't have LvalueBit
  94. typedef typename internal::add_const<PlainObjectType>::type ConstPlainObjectType;
  95. VERIFY( !(internal::traits<Map<ConstPlainObjectType> >::Flags & LvalueBit) );
  96. VERIFY( !(internal::traits<Map<ConstPlainObjectType, Aligned> >::Flags & LvalueBit) );
  97. VERIFY( !(Map<ConstPlainObjectType>::Flags & LvalueBit) );
  98. VERIFY( !(Map<ConstPlainObjectType, Aligned>::Flags & LvalueBit) );
  99. }
  100. void test_map()
  101. {
  102. for(int i = 0; i < g_repeat; i++) {
  103. CALL_SUBTEST_1( map_class_vector(Matrix<float, 1, 1>()) );
  104. CALL_SUBTEST_1( check_const_correctness(Matrix<float, 1, 1>()) );
  105. CALL_SUBTEST_2( map_class_vector(Vector4d()) );
  106. CALL_SUBTEST_2( check_const_correctness(Matrix4d()) );
  107. CALL_SUBTEST_3( map_class_vector(RowVector4f()) );
  108. CALL_SUBTEST_4( map_class_vector(VectorXcf(8)) );
  109. CALL_SUBTEST_5( map_class_vector(VectorXi(12)) );
  110. CALL_SUBTEST_5( check_const_correctness(VectorXi(12)) );
  111. CALL_SUBTEST_1( map_class_matrix(Matrix<float, 1, 1>()) );
  112. CALL_SUBTEST_2( map_class_matrix(Matrix4d()) );
  113. CALL_SUBTEST_11( map_class_matrix(Matrix<float,3,5>()) );
  114. CALL_SUBTEST_4( map_class_matrix(MatrixXcf(internal::random<int>(1,10),internal::random<int>(1,10))) );
  115. CALL_SUBTEST_5( map_class_matrix(MatrixXi(internal::random<int>(1,10),internal::random<int>(1,10))) );
  116. CALL_SUBTEST_6( map_static_methods(Matrix<double, 1, 1>()) );
  117. CALL_SUBTEST_7( map_static_methods(Vector3f()) );
  118. CALL_SUBTEST_8( map_static_methods(RowVector3d()) );
  119. CALL_SUBTEST_9( map_static_methods(VectorXcd(8)) );
  120. CALL_SUBTEST_10( map_static_methods(VectorXf(12)) );
  121. }
  122. }