graph_utils_unittest.cc 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // Copyright (C) 2009 The Android Open Source Project
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #include "update_engine/payload_generator/graph_utils.h"
  17. #include <utility>
  18. #include <vector>
  19. #include <gtest/gtest.h>
  20. #include "update_engine/payload_consumer/payload_constants.h"
  21. #include "update_engine/payload_generator/extent_ranges.h"
  22. #include "update_engine/payload_generator/extent_utils.h"
  23. using std::make_pair;
  24. using std::vector;
  25. namespace chromeos_update_engine {
  26. class GraphUtilsTest : public ::testing::Test {};
  27. TEST(GraphUtilsTest, SimpleTest) {
  28. Graph graph(2);
  29. graph[0].out_edges.insert(make_pair(1, EdgeProperties()));
  30. vector<Extent>& extents = graph[0].out_edges[1].extents;
  31. EXPECT_EQ(0U, extents.size());
  32. AppendBlockToExtents(&extents, 0);
  33. EXPECT_EQ(1U, extents.size());
  34. AppendBlockToExtents(&extents, 1);
  35. AppendBlockToExtents(&extents, 2);
  36. EXPECT_EQ(1U, extents.size());
  37. AppendBlockToExtents(&extents, 4);
  38. EXPECT_EQ(2U, extents.size());
  39. EXPECT_EQ(0U, extents[0].start_block());
  40. EXPECT_EQ(3U, extents[0].num_blocks());
  41. EXPECT_EQ(4U, extents[1].start_block());
  42. EXPECT_EQ(1U, extents[1].num_blocks());
  43. EXPECT_EQ(4U, graph_utils::EdgeWeight(graph, make_pair(0, 1)));
  44. }
  45. TEST(GraphUtilsTest, DepsTest) {
  46. Graph graph(3);
  47. graph_utils::AddReadBeforeDep(&graph[0], 1, 3);
  48. EXPECT_EQ(1U, graph[0].out_edges.size());
  49. {
  50. Extent& extent = graph[0].out_edges[1].extents[0];
  51. EXPECT_EQ(3U, extent.start_block());
  52. EXPECT_EQ(1U, extent.num_blocks());
  53. }
  54. graph_utils::AddReadBeforeDep(&graph[0], 1, 4);
  55. EXPECT_EQ(1U, graph[0].out_edges.size());
  56. {
  57. Extent& extent = graph[0].out_edges[1].extents[0];
  58. EXPECT_EQ(3U, extent.start_block());
  59. EXPECT_EQ(2U, extent.num_blocks());
  60. }
  61. graph_utils::AddReadBeforeDepExtents(
  62. &graph[2], 1, vector<Extent>(1, ExtentForRange(5, 2)));
  63. EXPECT_EQ(1U, graph[2].out_edges.size());
  64. {
  65. Extent& extent = graph[2].out_edges[1].extents[0];
  66. EXPECT_EQ(5U, extent.start_block());
  67. EXPECT_EQ(2U, extent.num_blocks());
  68. }
  69. // Change most recent edge from read-before to write-before
  70. graph[2].out_edges[1].write_extents.swap(graph[2].out_edges[1].extents);
  71. graph_utils::DropWriteBeforeDeps(&graph[2].out_edges);
  72. EXPECT_EQ(0U, graph[2].out_edges.size());
  73. EXPECT_EQ(1U, graph[0].out_edges.size());
  74. graph_utils::DropIncomingEdgesTo(&graph, 1);
  75. EXPECT_EQ(0U, graph[0].out_edges.size());
  76. }
  77. } // namespace chromeos_update_engine