graph_test.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/graph.h"
  31. #include <unordered_set>
  32. #include "gtest/gtest.h"
  33. namespace ceres::internal {
  34. TEST(Graph, EmptyGraph) {
  35. Graph<int> graph;
  36. EXPECT_EQ(graph.vertices().size(), 0);
  37. }
  38. TEST(Graph, AddVertexAndEdge) {
  39. Graph<int> graph;
  40. graph.AddVertex(0);
  41. graph.AddVertex(1);
  42. graph.AddEdge(0, 1);
  43. const std::unordered_set<int>& vertices = graph.vertices();
  44. EXPECT_EQ(vertices.size(), 2);
  45. EXPECT_EQ(graph.Neighbors(0).size(), 1);
  46. EXPECT_EQ(graph.Neighbors(1).size(), 1);
  47. }
  48. TEST(Graph, AddVertexIdempotence) {
  49. Graph<int> graph;
  50. graph.AddVertex(0);
  51. graph.AddVertex(1);
  52. graph.AddEdge(0, 1);
  53. const std::unordered_set<int>& vertices = graph.vertices();
  54. EXPECT_EQ(vertices.size(), 2);
  55. // Try adding the vertex again with a new weight.
  56. graph.AddVertex(0);
  57. EXPECT_EQ(vertices.size(), 2);
  58. // Rest of the graph remains the same.
  59. EXPECT_EQ(graph.Neighbors(0).size(), 1);
  60. EXPECT_EQ(graph.Neighbors(1).size(), 1);
  61. }
  62. TEST(Graph, DieOnNonExistentVertex) {
  63. Graph<int> graph;
  64. graph.AddVertex(0);
  65. graph.AddVertex(1);
  66. graph.AddEdge(0, 1);
  67. EXPECT_DEATH_IF_SUPPORTED(graph.Neighbors(2), "key not found");
  68. }
  69. TEST(WeightedGraph, EmptyGraph) {
  70. WeightedGraph<int> graph;
  71. EXPECT_EQ(graph.vertices().size(), 0);
  72. }
  73. TEST(WeightedGraph, AddVertexAndEdge) {
  74. WeightedGraph<int> graph;
  75. graph.AddVertex(0, 1.0);
  76. graph.AddVertex(1, 2.0);
  77. graph.AddEdge(0, 1, 0.5);
  78. const std::unordered_set<int>& vertices = graph.vertices();
  79. EXPECT_EQ(vertices.size(), 2);
  80. EXPECT_EQ(graph.VertexWeight(0), 1.0);
  81. EXPECT_EQ(graph.VertexWeight(1), 2.0);
  82. EXPECT_EQ(graph.Neighbors(0).size(), 1);
  83. EXPECT_EQ(graph.Neighbors(1).size(), 1);
  84. EXPECT_EQ(graph.EdgeWeight(0, 1), 0.5);
  85. EXPECT_EQ(graph.EdgeWeight(1, 0), 0.5);
  86. }
  87. TEST(WeightedGraph, AddVertexIdempotence) {
  88. WeightedGraph<int> graph;
  89. graph.AddVertex(0, 1.0);
  90. graph.AddVertex(1, 2.0);
  91. graph.AddEdge(0, 1, 0.5);
  92. const std::unordered_set<int>& vertices = graph.vertices();
  93. EXPECT_EQ(vertices.size(), 2);
  94. // Try adding the vertex again with a new weight.
  95. graph.AddVertex(0, 3.0);
  96. EXPECT_EQ(vertices.size(), 2);
  97. // The vertex weight is reset.
  98. EXPECT_EQ(graph.VertexWeight(0), 3.0);
  99. // Rest of the graph remains the same.
  100. EXPECT_EQ(graph.VertexWeight(1), 2.0);
  101. EXPECT_EQ(graph.Neighbors(0).size(), 1);
  102. EXPECT_EQ(graph.Neighbors(1).size(), 1);
  103. EXPECT_EQ(graph.EdgeWeight(0, 1), 0.5);
  104. EXPECT_EQ(graph.EdgeWeight(1, 0), 0.5);
  105. }
  106. TEST(WeightedGraph, DieOnNonExistentVertex) {
  107. WeightedGraph<int> graph;
  108. graph.AddVertex(0, 1.0);
  109. graph.AddVertex(1, 2.0);
  110. graph.AddEdge(0, 1, 0.5);
  111. EXPECT_DEATH_IF_SUPPORTED(graph.VertexWeight(2), "key not found");
  112. EXPECT_DEATH_IF_SUPPORTED(graph.Neighbors(2), "key not found");
  113. }
  114. TEST(WeightedGraph, NonExistentEdge) {
  115. WeightedGraph<int> graph;
  116. graph.AddVertex(0, 1.0);
  117. graph.AddVertex(1, 2.0);
  118. graph.AddEdge(0, 1, 0.5);
  119. // Default value for non-existent edges is 0.
  120. EXPECT_EQ(graph.EdgeWeight(2, 3), 0);
  121. }
  122. } // namespace ceres::internal