ordered_groups_test.cc 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: sameeragarwal@google.com (Sameer Agarwal)
  30. #include "ceres/ordered_groups.h"
  31. #include <cstddef>
  32. #include <vector>
  33. #include "gtest/gtest.h"
  34. namespace ceres::internal {
  35. TEST(OrderedGroups, EmptyOrderedGroupBehavesCorrectly) {
  36. ParameterBlockOrdering ordering;
  37. EXPECT_EQ(ordering.NumGroups(), 0);
  38. EXPECT_EQ(ordering.NumElements(), 0);
  39. EXPECT_EQ(ordering.GroupSize(1), 0);
  40. double x;
  41. EXPECT_EQ(ordering.GroupId(&x), -1);
  42. EXPECT_FALSE(ordering.Remove(&x));
  43. }
  44. TEST(OrderedGroups, EverythingInOneGroup) {
  45. ParameterBlockOrdering ordering;
  46. double x[3];
  47. ordering.AddElementToGroup(x, 1);
  48. ordering.AddElementToGroup(x + 1, 1);
  49. ordering.AddElementToGroup(x + 2, 1);
  50. ordering.AddElementToGroup(x, 1);
  51. EXPECT_EQ(ordering.NumGroups(), 1);
  52. EXPECT_EQ(ordering.NumElements(), 3);
  53. EXPECT_EQ(ordering.GroupSize(1), 3);
  54. EXPECT_EQ(ordering.GroupSize(0), 0);
  55. EXPECT_EQ(ordering.GroupId(x), 1);
  56. EXPECT_EQ(ordering.GroupId(x + 1), 1);
  57. EXPECT_EQ(ordering.GroupId(x + 2), 1);
  58. ordering.Remove(x);
  59. EXPECT_EQ(ordering.NumGroups(), 1);
  60. EXPECT_EQ(ordering.NumElements(), 2);
  61. EXPECT_EQ(ordering.GroupSize(1), 2);
  62. EXPECT_EQ(ordering.GroupSize(0), 0);
  63. EXPECT_EQ(ordering.GroupId(x), -1);
  64. EXPECT_EQ(ordering.GroupId(x + 1), 1);
  65. EXPECT_EQ(ordering.GroupId(x + 2), 1);
  66. }
  67. TEST(OrderedGroups, StartInOneGroupAndThenSplit) {
  68. ParameterBlockOrdering ordering;
  69. double x[3];
  70. ordering.AddElementToGroup(x, 1);
  71. ordering.AddElementToGroup(x + 1, 1);
  72. ordering.AddElementToGroup(x + 2, 1);
  73. ordering.AddElementToGroup(x, 1);
  74. EXPECT_EQ(ordering.NumGroups(), 1);
  75. EXPECT_EQ(ordering.NumElements(), 3);
  76. EXPECT_EQ(ordering.GroupSize(1), 3);
  77. EXPECT_EQ(ordering.GroupSize(0), 0);
  78. EXPECT_EQ(ordering.GroupId(x), 1);
  79. EXPECT_EQ(ordering.GroupId(x + 1), 1);
  80. EXPECT_EQ(ordering.GroupId(x + 2), 1);
  81. ordering.AddElementToGroup(x, 5);
  82. EXPECT_EQ(ordering.NumGroups(), 2);
  83. EXPECT_EQ(ordering.NumElements(), 3);
  84. EXPECT_EQ(ordering.GroupSize(1), 2);
  85. EXPECT_EQ(ordering.GroupSize(5), 1);
  86. EXPECT_EQ(ordering.GroupSize(0), 0);
  87. EXPECT_EQ(ordering.GroupId(x), 5);
  88. EXPECT_EQ(ordering.GroupId(x + 1), 1);
  89. EXPECT_EQ(ordering.GroupId(x + 2), 1);
  90. }
  91. TEST(OrderedGroups, AddAndRemoveEveryThingFromOneGroup) {
  92. ParameterBlockOrdering ordering;
  93. double x[3];
  94. ordering.AddElementToGroup(x, 1);
  95. ordering.AddElementToGroup(x + 1, 1);
  96. ordering.AddElementToGroup(x + 2, 1);
  97. ordering.AddElementToGroup(x, 1);
  98. EXPECT_EQ(ordering.NumGroups(), 1);
  99. EXPECT_EQ(ordering.NumElements(), 3);
  100. EXPECT_EQ(ordering.GroupSize(1), 3);
  101. EXPECT_EQ(ordering.GroupSize(0), 0);
  102. EXPECT_EQ(ordering.GroupId(x), 1);
  103. EXPECT_EQ(ordering.GroupId(x + 1), 1);
  104. EXPECT_EQ(ordering.GroupId(x + 2), 1);
  105. ordering.AddElementToGroup(x, 5);
  106. ordering.AddElementToGroup(x + 1, 5);
  107. ordering.AddElementToGroup(x + 2, 5);
  108. EXPECT_EQ(ordering.NumGroups(), 1);
  109. EXPECT_EQ(ordering.NumElements(), 3);
  110. EXPECT_EQ(ordering.GroupSize(1), 0);
  111. EXPECT_EQ(ordering.GroupSize(5), 3);
  112. EXPECT_EQ(ordering.GroupSize(0), 0);
  113. EXPECT_EQ(ordering.GroupId(x), 5);
  114. EXPECT_EQ(ordering.GroupId(x + 1), 5);
  115. EXPECT_EQ(ordering.GroupId(x + 2), 5);
  116. }
  117. TEST(OrderedGroups, ReverseOrdering) {
  118. ParameterBlockOrdering ordering;
  119. double x[3];
  120. ordering.AddElementToGroup(x, 1);
  121. ordering.AddElementToGroup(x + 1, 2);
  122. ordering.AddElementToGroup(x + 2, 2);
  123. EXPECT_EQ(ordering.NumGroups(), 2);
  124. EXPECT_EQ(ordering.NumElements(), 3);
  125. EXPECT_EQ(ordering.GroupSize(1), 1);
  126. EXPECT_EQ(ordering.GroupSize(2), 2);
  127. EXPECT_EQ(ordering.GroupId(x), 1);
  128. EXPECT_EQ(ordering.GroupId(x + 1), 2);
  129. EXPECT_EQ(ordering.GroupId(x + 2), 2);
  130. ordering.Reverse();
  131. EXPECT_EQ(ordering.NumGroups(), 2);
  132. EXPECT_EQ(ordering.NumElements(), 3);
  133. EXPECT_EQ(ordering.GroupSize(3), 1);
  134. EXPECT_EQ(ordering.GroupSize(2), 2);
  135. EXPECT_EQ(ordering.GroupId(x), 3);
  136. EXPECT_EQ(ordering.GroupId(x + 1), 2);
  137. EXPECT_EQ(ordering.GroupId(x + 2), 2);
  138. }
  139. TEST(OrderedGroups, ReverseOrderingWithEmptyOrderedGroups) {
  140. ParameterBlockOrdering ordering;
  141. // This should be a no-op.
  142. ordering.Reverse();
  143. // Ensure the properties of an empty OrderedGroups still hold after Reverse().
  144. EXPECT_EQ(ordering.NumGroups(), 0);
  145. EXPECT_EQ(ordering.NumElements(), 0);
  146. EXPECT_EQ(ordering.GroupSize(1), 0);
  147. double x;
  148. EXPECT_EQ(ordering.GroupId(&x), -1);
  149. EXPECT_FALSE(ordering.Remove(&x));
  150. }
  151. TEST(OrderedGroups, BulkRemove) {
  152. ParameterBlockOrdering ordering;
  153. double x[3];
  154. ordering.AddElementToGroup(x, 1);
  155. ordering.AddElementToGroup(x + 1, 2);
  156. ordering.AddElementToGroup(x + 2, 2);
  157. std::vector<double*> elements_to_remove;
  158. elements_to_remove.push_back(x);
  159. elements_to_remove.push_back(x + 2);
  160. EXPECT_EQ(ordering.Remove(elements_to_remove), 2);
  161. EXPECT_EQ(ordering.NumElements(), 1);
  162. EXPECT_EQ(ordering.GroupId(x), -1);
  163. EXPECT_EQ(ordering.GroupId(x + 1), 2);
  164. EXPECT_EQ(ordering.GroupId(x + 2), -1);
  165. }
  166. TEST(OrderedGroups, BulkRemoveWithNoElements) {
  167. ParameterBlockOrdering ordering;
  168. double x[3];
  169. std::vector<double*> elements_to_remove;
  170. elements_to_remove.push_back(x);
  171. elements_to_remove.push_back(x + 2);
  172. EXPECT_EQ(ordering.Remove(elements_to_remove), 0);
  173. ordering.AddElementToGroup(x, 1);
  174. ordering.AddElementToGroup(x + 1, 2);
  175. ordering.AddElementToGroup(x + 2, 2);
  176. elements_to_remove.clear();
  177. EXPECT_EQ(ordering.Remove(elements_to_remove), 0);
  178. }
  179. TEST(OrderedGroups, MinNonZeroGroup) {
  180. ParameterBlockOrdering ordering;
  181. double x[3];
  182. ordering.AddElementToGroup(x, 1);
  183. ordering.AddElementToGroup(x + 1, 1);
  184. ordering.AddElementToGroup(x + 2, 2);
  185. EXPECT_EQ(ordering.MinNonZeroGroup(), 1);
  186. ordering.Remove(x);
  187. EXPECT_EQ(ordering.MinNonZeroGroup(), 1);
  188. ordering.Remove(x + 1);
  189. EXPECT_EQ(ordering.MinNonZeroGroup(), 2);
  190. ordering.Remove(x + 2);
  191. // No non-zero groups left.
  192. EXPECT_DEATH_IF_SUPPORTED(ordering.MinNonZeroGroup(), "NumGroups");
  193. }
  194. } // namespace ceres::internal