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.

149 lines
4.5 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008 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. #define EIGEN_USE_NEW_STDVECTOR
  10. #include "main.h"
  11. #include <Eigen/StdVector>
  12. #include <Eigen/Geometry>
  13. template<typename MatrixType>
  14. void check_stdvector_matrix(const MatrixType& m)
  15. {
  16. int rows = m.rows();
  17. int cols = m.cols();
  18. MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols);
  19. std::vector<MatrixType,Eigen::aligned_allocator<MatrixType> > v(10, MatrixType(rows,cols)), w(20, y);
  20. v[5] = x;
  21. w[6] = v[5];
  22. VERIFY_IS_APPROX(w[6], v[5]);
  23. v = w;
  24. for(int i = 0; i < 20; i++)
  25. {
  26. VERIFY_IS_APPROX(w[i], v[i]);
  27. }
  28. v.resize(21);
  29. v[20] = x;
  30. VERIFY_IS_APPROX(v[20], x);
  31. v.resize(22,y);
  32. VERIFY_IS_APPROX(v[21], y);
  33. v.push_back(x);
  34. VERIFY_IS_APPROX(v[22], x);
  35. VERIFY((std::size_t)&(v[22]) == (std::size_t)&(v[21]) + sizeof(MatrixType));
  36. // do a lot of push_back such that the vector gets internally resized
  37. // (with memory reallocation)
  38. MatrixType* ref = &w[0];
  39. for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
  40. v.push_back(w[i%w.size()]);
  41. for(unsigned int i=23; i<v.size(); ++i)
  42. {
  43. VERIFY(v[i]==w[(i-23)%w.size()]);
  44. }
  45. }
  46. template<typename TransformType>
  47. void check_stdvector_transform(const TransformType&)
  48. {
  49. typedef typename TransformType::MatrixType MatrixType;
  50. TransformType x(MatrixType::Random()), y(MatrixType::Random());
  51. std::vector<TransformType,Eigen::aligned_allocator<TransformType> > v(10), w(20, y);
  52. v[5] = x;
  53. w[6] = v[5];
  54. VERIFY_IS_APPROX(w[6], v[5]);
  55. v = w;
  56. for(int i = 0; i < 20; i++)
  57. {
  58. VERIFY_IS_APPROX(w[i], v[i]);
  59. }
  60. v.resize(21);
  61. v[20] = x;
  62. VERIFY_IS_APPROX(v[20], x);
  63. v.resize(22,y);
  64. VERIFY_IS_APPROX(v[21], y);
  65. v.push_back(x);
  66. VERIFY_IS_APPROX(v[22], x);
  67. VERIFY((std::size_t)&(v[22]) == (std::size_t)&(v[21]) + sizeof(TransformType));
  68. // do a lot of push_back such that the vector gets internally resized
  69. // (with memory reallocation)
  70. TransformType* ref = &w[0];
  71. for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
  72. v.push_back(w[i%w.size()]);
  73. for(unsigned int i=23; i<v.size(); ++i)
  74. {
  75. VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix());
  76. }
  77. }
  78. template<typename QuaternionType>
  79. void check_stdvector_quaternion(const QuaternionType&)
  80. {
  81. typedef typename QuaternionType::Coefficients Coefficients;
  82. QuaternionType x(Coefficients::Random()), y(Coefficients::Random());
  83. std::vector<QuaternionType,Eigen::aligned_allocator<QuaternionType> > v(10), w(20, y);
  84. v[5] = x;
  85. w[6] = v[5];
  86. VERIFY_IS_APPROX(w[6], v[5]);
  87. v = w;
  88. for(int i = 0; i < 20; i++)
  89. {
  90. VERIFY_IS_APPROX(w[i], v[i]);
  91. }
  92. v.resize(21);
  93. v[20] = x;
  94. VERIFY_IS_APPROX(v[20], x);
  95. v.resize(22,y);
  96. VERIFY_IS_APPROX(v[21], y);
  97. v.push_back(x);
  98. VERIFY_IS_APPROX(v[22], x);
  99. VERIFY((std::size_t)&(v[22]) == (std::size_t)&(v[21]) + sizeof(QuaternionType));
  100. // do a lot of push_back such that the vector gets internally resized
  101. // (with memory reallocation)
  102. QuaternionType* ref = &w[0];
  103. for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
  104. v.push_back(w[i%w.size()]);
  105. for(unsigned int i=23; i<v.size(); ++i)
  106. {
  107. VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs());
  108. }
  109. }
  110. void test_eigen2_newstdvector()
  111. {
  112. // some non vectorizable fixed sizes
  113. CALL_SUBTEST_1(check_stdvector_matrix(Vector2f()));
  114. CALL_SUBTEST_1(check_stdvector_matrix(Matrix3f()));
  115. CALL_SUBTEST_1(check_stdvector_matrix(Matrix3d()));
  116. // some vectorizable fixed sizes
  117. CALL_SUBTEST_2(check_stdvector_matrix(Matrix2f()));
  118. CALL_SUBTEST_2(check_stdvector_matrix(Vector4f()));
  119. CALL_SUBTEST_2(check_stdvector_matrix(Matrix4f()));
  120. CALL_SUBTEST_2(check_stdvector_matrix(Matrix4d()));
  121. // some dynamic sizes
  122. CALL_SUBTEST_3(check_stdvector_matrix(MatrixXd(1,1)));
  123. CALL_SUBTEST_3(check_stdvector_matrix(VectorXd(20)));
  124. CALL_SUBTEST_3(check_stdvector_matrix(RowVectorXf(20)));
  125. CALL_SUBTEST_3(check_stdvector_matrix(MatrixXcf(10,10)));
  126. // some Transform
  127. CALL_SUBTEST_4(check_stdvector_transform(Transform2f()));
  128. CALL_SUBTEST_4(check_stdvector_transform(Transform3f()));
  129. CALL_SUBTEST_4(check_stdvector_transform(Transform3d()));
  130. //CALL_SUBTEST(check_stdvector_transform(Transform4d()));
  131. // some Quaternion
  132. CALL_SUBTEST_5(check_stdvector_quaternion(Quaternionf()));
  133. CALL_SUBTEST_5(check_stdvector_quaternion(Quaterniond()));
  134. }