stdvector.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. #include "main.h"
  10. #include <Eigen/StdVector>
  11. #include <Eigen/Geometry>
  12. template<typename MatrixType>
  13. void check_stdvector_matrix(const MatrixType& m)
  14. {
  15. Index rows = m.rows();
  16. Index cols = m.cols();
  17. MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols);
  18. std::vector<MatrixType,Eigen::aligned_allocator<MatrixType> > v(10, MatrixType::Zero(rows,cols)), w(20, y);
  19. v[5] = x;
  20. w[6] = v[5];
  21. VERIFY_IS_APPROX(w[6], v[5]);
  22. v = w;
  23. for(int i = 0; i < 20; i++)
  24. {
  25. VERIFY_IS_APPROX(w[i], v[i]);
  26. }
  27. v.resize(21);
  28. v[20] = x;
  29. VERIFY_IS_APPROX(v[20], x);
  30. v.resize(22,y);
  31. VERIFY_IS_APPROX(v[21], y);
  32. v.push_back(x);
  33. VERIFY_IS_APPROX(v[22], x);
  34. VERIFY((internal::UIntPtr)&(v[22]) == (internal::UIntPtr)&(v[21]) + sizeof(MatrixType));
  35. // do a lot of push_back such that the vector gets internally resized
  36. // (with memory reallocation)
  37. MatrixType* ref = &w[0];
  38. for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
  39. v.push_back(w[i%w.size()]);
  40. for(unsigned int i=23; i<v.size(); ++i)
  41. {
  42. VERIFY(v[i]==w[(i-23)%w.size()]);
  43. }
  44. }
  45. template<typename TransformType>
  46. void check_stdvector_transform(const TransformType&)
  47. {
  48. typedef typename TransformType::MatrixType MatrixType;
  49. TransformType x(MatrixType::Random()), y(MatrixType::Random());
  50. std::vector<TransformType,Eigen::aligned_allocator<TransformType> > v(10), w(20, y);
  51. v[5] = x;
  52. w[6] = v[5];
  53. VERIFY_IS_APPROX(w[6], v[5]);
  54. v = w;
  55. for(int i = 0; i < 20; i++)
  56. {
  57. VERIFY_IS_APPROX(w[i], v[i]);
  58. }
  59. v.resize(21);
  60. v[20] = x;
  61. VERIFY_IS_APPROX(v[20], x);
  62. v.resize(22,y);
  63. VERIFY_IS_APPROX(v[21], y);
  64. v.push_back(x);
  65. VERIFY_IS_APPROX(v[22], x);
  66. VERIFY((internal::UIntPtr)&(v[22]) == (internal::UIntPtr)&(v[21]) + sizeof(TransformType));
  67. // do a lot of push_back such that the vector gets internally resized
  68. // (with memory reallocation)
  69. TransformType* ref = &w[0];
  70. for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
  71. v.push_back(w[i%w.size()]);
  72. for(unsigned int i=23; i<v.size(); ++i)
  73. {
  74. VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix());
  75. }
  76. }
  77. template<typename QuaternionType>
  78. void check_stdvector_quaternion(const QuaternionType&)
  79. {
  80. typedef typename QuaternionType::Coefficients Coefficients;
  81. QuaternionType x(Coefficients::Random()), y(Coefficients::Random()), qi=QuaternionType::Identity();
  82. std::vector<QuaternionType,Eigen::aligned_allocator<QuaternionType> > v(10,qi), w(20, y);
  83. v[5] = x;
  84. w[6] = v[5];
  85. VERIFY_IS_APPROX(w[6], v[5]);
  86. v = w;
  87. for(int i = 0; i < 20; i++)
  88. {
  89. VERIFY_IS_APPROX(w[i], v[i]);
  90. }
  91. v.resize(21);
  92. v[20] = x;
  93. VERIFY_IS_APPROX(v[20], x);
  94. v.resize(22,y);
  95. VERIFY_IS_APPROX(v[21], y);
  96. v.push_back(x);
  97. VERIFY_IS_APPROX(v[22], x);
  98. VERIFY((internal::UIntPtr)&(v[22]) == (internal::UIntPtr)&(v[21]) + sizeof(QuaternionType));
  99. // do a lot of push_back such that the vector gets internally resized
  100. // (with memory reallocation)
  101. QuaternionType* ref = &w[0];
  102. for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
  103. v.push_back(w[i%w.size()]);
  104. for(unsigned int i=23; i<v.size(); ++i)
  105. {
  106. VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs());
  107. }
  108. }
  109. // the code below triggered an invalid warning with gcc >= 7
  110. // eigen/Eigen/src/Core/util/Memory.h:189:12: warning: argument 1 value '18446744073709551612' exceeds maximum object size 9223372036854775807
  111. // This has been reported to gcc there: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87544
  112. void std_vector_gcc_warning()
  113. {
  114. typedef Eigen::Vector3f T;
  115. std::vector<T, Eigen::aligned_allocator<T> > v;
  116. v.push_back(T());
  117. }
  118. EIGEN_DECLARE_TEST(stdvector)
  119. {
  120. // some non vectorizable fixed sizes
  121. CALL_SUBTEST_1(check_stdvector_matrix(Vector2f()));
  122. CALL_SUBTEST_1(check_stdvector_matrix(Matrix3f()));
  123. CALL_SUBTEST_2(check_stdvector_matrix(Matrix3d()));
  124. // some vectorizable fixed sizes
  125. CALL_SUBTEST_1(check_stdvector_matrix(Matrix2f()));
  126. CALL_SUBTEST_1(check_stdvector_matrix(Vector4f()));
  127. CALL_SUBTEST_1(check_stdvector_matrix(Matrix4f()));
  128. CALL_SUBTEST_2(check_stdvector_matrix(Matrix4d()));
  129. // some dynamic sizes
  130. CALL_SUBTEST_3(check_stdvector_matrix(MatrixXd(1,1)));
  131. CALL_SUBTEST_3(check_stdvector_matrix(VectorXd(20)));
  132. CALL_SUBTEST_3(check_stdvector_matrix(RowVectorXf(20)));
  133. CALL_SUBTEST_3(check_stdvector_matrix(MatrixXcf(10,10)));
  134. // some Transform
  135. CALL_SUBTEST_4(check_stdvector_transform(Projective2f()));
  136. CALL_SUBTEST_4(check_stdvector_transform(Projective3f()));
  137. CALL_SUBTEST_4(check_stdvector_transform(Projective3d()));
  138. //CALL_SUBTEST(heck_stdvector_transform(Projective4d()));
  139. // some Quaternion
  140. CALL_SUBTEST_5(check_stdvector_quaternion(Quaternionf()));
  141. CALL_SUBTEST_5(check_stdvector_quaternion(Quaterniond()));
  142. }