array_utils_test.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/array_utils.h"
  31. #include <cmath>
  32. #include <limits>
  33. #include <vector>
  34. #include "gtest/gtest.h"
  35. namespace ceres::internal {
  36. TEST(ArrayUtils, IsArrayValid) {
  37. double x[3];
  38. x[0] = 0.0;
  39. x[1] = 1.0;
  40. x[2] = 2.0;
  41. EXPECT_TRUE(IsArrayValid(3, x));
  42. x[1] = std::numeric_limits<double>::infinity();
  43. EXPECT_FALSE(IsArrayValid(3, x));
  44. x[1] = std::numeric_limits<double>::quiet_NaN();
  45. EXPECT_FALSE(IsArrayValid(3, x));
  46. x[1] = std::numeric_limits<double>::signaling_NaN();
  47. EXPECT_FALSE(IsArrayValid(3, x));
  48. EXPECT_TRUE(IsArrayValid(1, nullptr));
  49. InvalidateArray(3, x);
  50. EXPECT_FALSE(IsArrayValid(3, x));
  51. }
  52. TEST(ArrayUtils, FindInvalidIndex) {
  53. double x[3];
  54. x[0] = 0.0;
  55. x[1] = 1.0;
  56. x[2] = 2.0;
  57. EXPECT_EQ(FindInvalidValue(3, x), 3);
  58. x[1] = std::numeric_limits<double>::infinity();
  59. EXPECT_EQ(FindInvalidValue(3, x), 1);
  60. x[1] = std::numeric_limits<double>::quiet_NaN();
  61. EXPECT_EQ(FindInvalidValue(3, x), 1);
  62. x[1] = std::numeric_limits<double>::signaling_NaN();
  63. EXPECT_EQ(FindInvalidValue(3, x), 1);
  64. EXPECT_EQ(FindInvalidValue(1, nullptr), 1);
  65. InvalidateArray(3, x);
  66. EXPECT_EQ(FindInvalidValue(3, x), 0);
  67. }
  68. TEST(MapValuesToContiguousRange, ContiguousEntries) {
  69. std::vector<int> array;
  70. array.push_back(0);
  71. array.push_back(1);
  72. std::vector<int> expected = array;
  73. MapValuesToContiguousRange(array.size(), &array[0]);
  74. EXPECT_EQ(array, expected);
  75. array.clear();
  76. array.push_back(1);
  77. array.push_back(0);
  78. expected = array;
  79. MapValuesToContiguousRange(array.size(), &array[0]);
  80. EXPECT_EQ(array, expected);
  81. }
  82. TEST(MapValuesToContiguousRange, NonContiguousEntries) {
  83. std::vector<int> array;
  84. array.push_back(0);
  85. array.push_back(2);
  86. std::vector<int> expected;
  87. expected.push_back(0);
  88. expected.push_back(1);
  89. MapValuesToContiguousRange(array.size(), &array[0]);
  90. EXPECT_EQ(array, expected);
  91. }
  92. TEST(MapValuesToContiguousRange, NonContiguousRepeatingEntries) {
  93. std::vector<int> array;
  94. array.push_back(3);
  95. array.push_back(1);
  96. array.push_back(0);
  97. array.push_back(0);
  98. array.push_back(0);
  99. array.push_back(5);
  100. std::vector<int> expected;
  101. expected.push_back(2);
  102. expected.push_back(1);
  103. expected.push_back(0);
  104. expected.push_back(0);
  105. expected.push_back(0);
  106. expected.push_back(3);
  107. MapValuesToContiguousRange(array.size(), &array[0]);
  108. EXPECT_EQ(array, expected);
  109. }
  110. } // namespace ceres::internal