stddeque_overload.cpp 4.7 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. // Copyright (C) 2010 Hauke Heibel <hauke.heibel@gmail.com>
  6. //
  7. // This Source Code Form is subject to the terms of the Mozilla
  8. // Public License v. 2.0. If a copy of the MPL was not distributed
  9. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  10. #include "main.h"
  11. #include <Eigen/StdDeque>
  12. #include <Eigen/Geometry>
  13. EIGEN_DEFINE_STL_DEQUE_SPECIALIZATION(Vector4f)
  14. EIGEN_DEFINE_STL_DEQUE_SPECIALIZATION(Matrix2f)
  15. EIGEN_DEFINE_STL_DEQUE_SPECIALIZATION(Matrix4f)
  16. EIGEN_DEFINE_STL_DEQUE_SPECIALIZATION(Matrix4d)
  17. EIGEN_DEFINE_STL_DEQUE_SPECIALIZATION(Affine3f)
  18. EIGEN_DEFINE_STL_DEQUE_SPECIALIZATION(Affine3d)
  19. EIGEN_DEFINE_STL_DEQUE_SPECIALIZATION(Quaternionf)
  20. EIGEN_DEFINE_STL_DEQUE_SPECIALIZATION(Quaterniond)
  21. template<typename MatrixType>
  22. void check_stddeque_matrix(const MatrixType& m)
  23. {
  24. typename MatrixType::Index rows = m.rows();
  25. typename MatrixType::Index cols = m.cols();
  26. MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols);
  27. std::deque<MatrixType> v(10, MatrixType::Zero(rows,cols)), w(20, y);
  28. v[5] = x;
  29. w[6] = v[5];
  30. VERIFY_IS_APPROX(w[6], v[5]);
  31. v = w;
  32. for(int i = 0; i < 20; i++)
  33. {
  34. VERIFY_IS_APPROX(w[i], v[i]);
  35. }
  36. v.resize(21);
  37. v[20] = x;
  38. VERIFY_IS_APPROX(v[20], x);
  39. v.resize(22,y);
  40. VERIFY_IS_APPROX(v[21], y);
  41. v.push_back(x);
  42. VERIFY_IS_APPROX(v[22], x);
  43. // do a lot of push_back such that the deque gets internally resized
  44. // (with memory reallocation)
  45. MatrixType* ref = &w[0];
  46. for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
  47. v.push_back(w[i%w.size()]);
  48. for(unsigned int i=23; i<v.size(); ++i)
  49. {
  50. VERIFY(v[i]==w[(i-23)%w.size()]);
  51. }
  52. }
  53. template<typename TransformType>
  54. void check_stddeque_transform(const TransformType&)
  55. {
  56. typedef typename TransformType::MatrixType MatrixType;
  57. TransformType x(MatrixType::Random()), y(MatrixType::Random()), ti=TransformType::Identity();
  58. std::deque<TransformType> v(10,ti), w(20, y);
  59. v[5] = x;
  60. w[6] = v[5];
  61. VERIFY_IS_APPROX(w[6], v[5]);
  62. v = w;
  63. for(int i = 0; i < 20; i++)
  64. {
  65. VERIFY_IS_APPROX(w[i], v[i]);
  66. }
  67. v.resize(21,ti);
  68. v[20] = x;
  69. VERIFY_IS_APPROX(v[20], x);
  70. v.resize(22,y);
  71. VERIFY_IS_APPROX(v[21], y);
  72. v.push_back(x);
  73. VERIFY_IS_APPROX(v[22], x);
  74. // do a lot of push_back such that the deque gets internally resized
  75. // (with memory reallocation)
  76. TransformType* ref = &w[0];
  77. for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
  78. v.push_back(w[i%w.size()]);
  79. for(unsigned int i=23; i<v.size(); ++i)
  80. {
  81. VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix());
  82. }
  83. }
  84. template<typename QuaternionType>
  85. void check_stddeque_quaternion(const QuaternionType&)
  86. {
  87. typedef typename QuaternionType::Coefficients Coefficients;
  88. QuaternionType x(Coefficients::Random()), y(Coefficients::Random()), qi=QuaternionType::Identity();
  89. std::deque<QuaternionType> v(10,qi), w(20, y);
  90. v[5] = x;
  91. w[6] = v[5];
  92. VERIFY_IS_APPROX(w[6], v[5]);
  93. v = w;
  94. for(int i = 0; i < 20; i++)
  95. {
  96. VERIFY_IS_APPROX(w[i], v[i]);
  97. }
  98. v.resize(21,qi);
  99. v[20] = x;
  100. VERIFY_IS_APPROX(v[20], x);
  101. v.resize(22,y);
  102. VERIFY_IS_APPROX(v[21], y);
  103. v.push_back(x);
  104. VERIFY_IS_APPROX(v[22], x);
  105. // do a lot of push_back such that the deque gets internally resized
  106. // (with memory reallocation)
  107. QuaternionType* ref = &w[0];
  108. for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
  109. v.push_back(w[i%w.size()]);
  110. for(unsigned int i=23; i<v.size(); ++i)
  111. {
  112. VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs());
  113. }
  114. }
  115. void test_stddeque_overload()
  116. {
  117. // some non vectorizable fixed sizes
  118. CALL_SUBTEST_1(check_stddeque_matrix(Vector2f()));
  119. CALL_SUBTEST_1(check_stddeque_matrix(Matrix3f()));
  120. CALL_SUBTEST_2(check_stddeque_matrix(Matrix3d()));
  121. // some vectorizable fixed sizes
  122. CALL_SUBTEST_1(check_stddeque_matrix(Matrix2f()));
  123. CALL_SUBTEST_1(check_stddeque_matrix(Vector4f()));
  124. CALL_SUBTEST_1(check_stddeque_matrix(Matrix4f()));
  125. CALL_SUBTEST_2(check_stddeque_matrix(Matrix4d()));
  126. // some dynamic sizes
  127. CALL_SUBTEST_3(check_stddeque_matrix(MatrixXd(1,1)));
  128. CALL_SUBTEST_3(check_stddeque_matrix(VectorXd(20)));
  129. CALL_SUBTEST_3(check_stddeque_matrix(RowVectorXf(20)));
  130. CALL_SUBTEST_3(check_stddeque_matrix(MatrixXcf(10,10)));
  131. // some Transform
  132. CALL_SUBTEST_4(check_stddeque_transform(Affine2f())); // does not need the specialization (2+1)^2 = 9
  133. CALL_SUBTEST_4(check_stddeque_transform(Affine3f()));
  134. CALL_SUBTEST_4(check_stddeque_transform(Affine3d()));
  135. // some Quaternion
  136. CALL_SUBTEST_5(check_stddeque_quaternion(Quaternionf()));
  137. CALL_SUBTEST_5(check_stddeque_quaternion(Quaterniond()));
  138. }