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.

148 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. #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. typename MatrixType::Index rows = m.rows();
  16. typename MatrixType::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(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((size_t)&(v[22]) == (size_t)&(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((size_t)&(v[22]) == (size_t)&(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());
  82. std::vector<QuaternionType,Eigen::aligned_allocator<QuaternionType> > v(10), 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((size_t)&(v[22]) == (size_t)&(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. void test_stdvector()
  110. {
  111. // some non vectorizable fixed sizes
  112. CALL_SUBTEST_1(check_stdvector_matrix(Vector2f()));
  113. CALL_SUBTEST_1(check_stdvector_matrix(Matrix3f()));
  114. CALL_SUBTEST_2(check_stdvector_matrix(Matrix3d()));
  115. // some vectorizable fixed sizes
  116. CALL_SUBTEST_1(check_stdvector_matrix(Matrix2f()));
  117. CALL_SUBTEST_1(check_stdvector_matrix(Vector4f()));
  118. CALL_SUBTEST_1(check_stdvector_matrix(Matrix4f()));
  119. CALL_SUBTEST_2(check_stdvector_matrix(Matrix4d()));
  120. // some dynamic sizes
  121. CALL_SUBTEST_3(check_stdvector_matrix(MatrixXd(1,1)));
  122. CALL_SUBTEST_3(check_stdvector_matrix(VectorXd(20)));
  123. CALL_SUBTEST_3(check_stdvector_matrix(RowVectorXf(20)));
  124. CALL_SUBTEST_3(check_stdvector_matrix(MatrixXcf(10,10)));
  125. // some Transform
  126. CALL_SUBTEST_4(check_stdvector_transform(Projective2f()));
  127. CALL_SUBTEST_4(check_stdvector_transform(Projective3f()));
  128. CALL_SUBTEST_4(check_stdvector_transform(Projective3d()));
  129. //CALL_SUBTEST(heck_stdvector_transform(Projective4d()));
  130. // some Quaternion
  131. CALL_SUBTEST_5(check_stdvector_quaternion(Quaternionf()));
  132. CALL_SUBTEST_5(check_stdvector_quaternion(Quaterniond()));
  133. }