integer_sequence_algorithm_test.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2022 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: jodebo_beck@gmx.de (Johannes Beck)
  30. // sergiu.deitsch@gmail.com (Sergiu Deitsch)
  31. #include "ceres/internal/integer_sequence_algorithm.h"
  32. #include <type_traits>
  33. #include <utility>
  34. #include "ceres/internal/jet_traits.h"
  35. namespace ceres::internal {
  36. // Unit tests for exclusive scan of integer sequence.
  37. static_assert(std::is_same<ExclusiveScan<std::integer_sequence<int>>,
  38. std::integer_sequence<int>>::value,
  39. "Unit test of calculating the exclusive scan of an integer "
  40. "sequence failed.");
  41. static_assert(std::is_same<ExclusiveScan<std::integer_sequence<int, 2>>,
  42. std::integer_sequence<int, 0>>::value,
  43. "Unit test of calculating the exclusive scan of an integer "
  44. "sequence failed.");
  45. static_assert(std::is_same<ExclusiveScan<std::integer_sequence<int, 2, 1>>,
  46. std::integer_sequence<int, 0, 2>>::value,
  47. "Unit test of calculating the exclusive scan of an integer "
  48. "sequence failed.");
  49. static_assert(std::is_same<ExclusiveScan<std::integer_sequence<int, 2, 1, 10>>,
  50. std::integer_sequence<int, 0, 2, 3>>::value,
  51. "Unit test of calculating the exclusive scan of an integer "
  52. "sequence failed.");
  53. using Ranks001 = Ranks_t<Jet<double, 0>, double, Jet<double, 1>>;
  54. using Ranks1 = Ranks_t<Jet<double, 1>>;
  55. using Ranks110 = Ranks_t<Jet<double, 1>, Jet<double, 1>, double>;
  56. using Ranks023 = Ranks_t<double, Jet<double, 2>, Jet<double, 3>>;
  57. using EmptyRanks = Ranks_t<>;
  58. // Remove zero from the ranks integer sequence
  59. using NonZeroRanks001 = RemoveValue_t<Ranks001, 0>;
  60. using NonZeroRanks1 = RemoveValue_t<Ranks1, 0>;
  61. using NonZeroRanks110 = RemoveValue_t<Ranks110, 0>;
  62. using NonZeroRanks023 = RemoveValue_t<Ranks023, 0>;
  63. static_assert(std::is_same<RemoveValue_t<EmptyRanks, 0>,
  64. std::integer_sequence<int>>::value,
  65. "filtered sequence does not match an empty one");
  66. static_assert(std::is_same<RemoveValue_t<std::integer_sequence<int, 2, 2>, 2>,
  67. std::integer_sequence<int>>::value,
  68. "filtered sequence does not match an empty one");
  69. static_assert(
  70. std::is_same<RemoveValue_t<std::integer_sequence<int, 0, 0, 2>, 2>,
  71. std::integer_sequence<int, 0, 0>>::value,
  72. "filtered sequence does not match the expected one");
  73. static_assert(
  74. std::is_same<RemoveValue_t<std::make_integer_sequence<int, 6>, 7>,
  75. std::make_integer_sequence<int, 6>>::value,
  76. "sequence not containing the element to remove must not be transformed");
  77. static_assert(
  78. std::is_same<NonZeroRanks001, std::integer_sequence<int, 1>>::value,
  79. "sequences do not match");
  80. static_assert(std::is_same<NonZeroRanks1, std::integer_sequence<int, 1>>::value,
  81. "sequences do not match");
  82. static_assert(
  83. std::is_same<NonZeroRanks110, std::integer_sequence<int, 1, 1>>::value,
  84. "sequences do not match");
  85. static_assert(
  86. std::is_same<NonZeroRanks023, std::integer_sequence<int, 2, 3>>::value,
  87. "sequences do not match");
  88. static_assert(std::is_same<RemoveValue_t<std::integer_sequence<long>, -1>,
  89. std::integer_sequence<long>>::value,
  90. "sequences do not match");
  91. static_assert(
  92. std::is_same<RemoveValue_t<std::integer_sequence<short, -2, -3, -1>, -1>,
  93. std::integer_sequence<short, -2, -3>>::value,
  94. "sequences do not match");
  95. using J = Jet<double, 2>;
  96. template <typename T>
  97. using J0 = Jet<T, 0>;
  98. using J0d = J0<double>;
  99. // Ensure all types match
  100. static_assert(AreAllSame_v<int, int>, "types must be the same");
  101. static_assert(AreAllSame_v<long, long, long>, "types must be the same");
  102. static_assert(AreAllSame_v<J0d, J0d, J0d>, "types must be the same");
  103. static_assert(!AreAllSame_v<double, int>, "types must not be the same");
  104. static_assert(!AreAllSame_v<int, short, char>, "types must not be the same");
  105. // Ensure all values in the integer sequence match
  106. static_assert(AreAllEqual_v<int, 1, 1>,
  107. "integer sequence must contain same values");
  108. static_assert(AreAllEqual_v<long, 2>,
  109. "integer sequence must contain one value");
  110. static_assert(!AreAllEqual_v<short, 3, 4>,
  111. "integer sequence must not contain the same values");
  112. static_assert(!AreAllEqual_v<unsigned, 3, 4, 3>,
  113. "integer sequence must not contain the same values");
  114. static_assert(!AreAllEqual_v<int, 4, 4, 3>,
  115. "integer sequence must not contain the same values");
  116. static_assert(IsEmptyOrAreAllEqual_v<std::integer_sequence<short>>,
  117. "expected empty sequence is not");
  118. static_assert(IsEmptyOrAreAllEqual_v<std::integer_sequence<unsigned, 7, 7, 7>>,
  119. "expected all equal sequence is not");
  120. static_assert(IsEmptyOrAreAllEqual_v<std::integer_sequence<int, 1>>,
  121. "expected all equal sequence is not");
  122. static_assert(
  123. IsEmptyOrAreAllEqual_v<std::integer_sequence<long, 111, 111, 111, 111>>,
  124. "expected all equal sequence is not");
  125. } // namespace ceres::internal