Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <set> |
| 6 | #include <string> |
| 7 | #include <utility> |
| 8 | #include <vector> |
| 9 | #include <gtest/gtest.h> |
| 10 | #include "chromeos/obsolete_logging.h" |
| 11 | #include "update_engine/cycle_breaker.h" |
| 12 | #include "update_engine/graph_types.h" |
| 13 | #include "update_engine/utils.h" |
| 14 | |
| 15 | using std::make_pair; |
| 16 | using std::pair; |
| 17 | using std::set; |
| 18 | using std::string; |
| 19 | using std::vector; |
| 20 | |
| 21 | namespace chromeos_update_engine { |
| 22 | |
| 23 | class CycleBreakerTest : public ::testing::Test {}; |
| 24 | |
| 25 | TEST(CycleBreakerTest, SimpleTest) { |
| 26 | int counter = 0; |
| 27 | const Vertex::Index n_a = counter++; |
| 28 | const Vertex::Index n_b = counter++; |
| 29 | const Vertex::Index n_c = counter++; |
| 30 | const Vertex::Index n_d = counter++; |
| 31 | const Vertex::Index n_e = counter++; |
| 32 | const Vertex::Index n_f = counter++; |
| 33 | const Vertex::Index n_g = counter++; |
| 34 | const Vertex::Index n_h = counter++; |
| 35 | const Graph::size_type kNodeCount = counter++; |
| 36 | |
| 37 | Graph graph(kNodeCount); |
| 38 | |
| 39 | graph[n_a].out_edges.insert(make_pair(n_e, EdgeProperties())); |
| 40 | graph[n_a].out_edges.insert(make_pair(n_f, EdgeProperties())); |
| 41 | graph[n_b].out_edges.insert(make_pair(n_a, EdgeProperties())); |
| 42 | graph[n_c].out_edges.insert(make_pair(n_d, EdgeProperties())); |
| 43 | graph[n_d].out_edges.insert(make_pair(n_e, EdgeProperties())); |
| 44 | graph[n_d].out_edges.insert(make_pair(n_f, EdgeProperties())); |
| 45 | graph[n_e].out_edges.insert(make_pair(n_b, EdgeProperties())); |
| 46 | graph[n_e].out_edges.insert(make_pair(n_c, EdgeProperties())); |
| 47 | graph[n_e].out_edges.insert(make_pair(n_f, EdgeProperties())); |
| 48 | graph[n_f].out_edges.insert(make_pair(n_g, EdgeProperties())); |
| 49 | graph[n_g].out_edges.insert(make_pair(n_h, EdgeProperties())); |
| 50 | graph[n_h].out_edges.insert(make_pair(n_g, EdgeProperties())); |
| 51 | |
| 52 | CycleBreaker breaker; |
| 53 | |
| 54 | set<Edge> broken_edges; |
| 55 | breaker.BreakCycles(graph, &broken_edges); |
| 56 | |
| 57 | // The following cycles must be cut: |
| 58 | // A->E->B |
| 59 | // C->D->E |
| 60 | // G->H |
| 61 | |
| 62 | EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_a, n_e)) || |
| 63 | utils::SetContainsKey(broken_edges, make_pair(n_e, n_b)) || |
| 64 | utils::SetContainsKey(broken_edges, make_pair(n_b, n_a))); |
| 65 | EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_c, n_d)) || |
| 66 | utils::SetContainsKey(broken_edges, make_pair(n_d, n_e)) || |
| 67 | utils::SetContainsKey(broken_edges, make_pair(n_e, n_c))); |
| 68 | EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_g, n_h)) || |
| 69 | utils::SetContainsKey(broken_edges, make_pair(n_h, n_g))); |
| 70 | EXPECT_EQ(3, broken_edges.size()); |
| 71 | } |
| 72 | |
| 73 | namespace { |
| 74 | pair<Vertex::Index, EdgeProperties> EdgeWithWeight(Vertex::Index dest, |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 75 | uint64_t weight) { |
Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 76 | EdgeProperties props; |
| 77 | props.extents.resize(1); |
| 78 | props.extents[0].set_num_blocks(weight); |
| 79 | return make_pair(dest, props); |
| 80 | } |
| 81 | } // namespace {} |
| 82 | |
| 83 | TEST(CycleBreakerTest, WeightTest) { |
| 84 | int counter = 0; |
| 85 | const Vertex::Index n_a = counter++; |
| 86 | const Vertex::Index n_b = counter++; |
| 87 | const Vertex::Index n_c = counter++; |
| 88 | const Vertex::Index n_d = counter++; |
| 89 | const Vertex::Index n_e = counter++; |
| 90 | const Vertex::Index n_f = counter++; |
| 91 | const Vertex::Index n_g = counter++; |
| 92 | const Vertex::Index n_h = counter++; |
| 93 | const Vertex::Index n_i = counter++; |
| 94 | const Vertex::Index n_j = counter++; |
| 95 | const Graph::size_type kNodeCount = counter++; |
| 96 | |
| 97 | Graph graph(kNodeCount); |
| 98 | |
| 99 | graph[n_a].out_edges.insert(EdgeWithWeight(n_b, 4)); |
| 100 | graph[n_a].out_edges.insert(EdgeWithWeight(n_f, 3)); |
| 101 | graph[n_a].out_edges.insert(EdgeWithWeight(n_h, 2)); |
| 102 | graph[n_b].out_edges.insert(EdgeWithWeight(n_a, 3)); |
| 103 | graph[n_b].out_edges.insert(EdgeWithWeight(n_c, 4)); |
| 104 | graph[n_c].out_edges.insert(EdgeWithWeight(n_b, 5)); |
| 105 | graph[n_c].out_edges.insert(EdgeWithWeight(n_d, 3)); |
| 106 | graph[n_d].out_edges.insert(EdgeWithWeight(n_a, 6)); |
| 107 | graph[n_d].out_edges.insert(EdgeWithWeight(n_e, 3)); |
| 108 | graph[n_e].out_edges.insert(EdgeWithWeight(n_d, 4)); |
| 109 | graph[n_e].out_edges.insert(EdgeWithWeight(n_g, 5)); |
| 110 | graph[n_f].out_edges.insert(EdgeWithWeight(n_g, 2)); |
| 111 | graph[n_g].out_edges.insert(EdgeWithWeight(n_f, 3)); |
| 112 | graph[n_g].out_edges.insert(EdgeWithWeight(n_d, 5)); |
| 113 | graph[n_h].out_edges.insert(EdgeWithWeight(n_i, 8)); |
| 114 | graph[n_i].out_edges.insert(EdgeWithWeight(n_e, 4)); |
| 115 | graph[n_i].out_edges.insert(EdgeWithWeight(n_h, 9)); |
| 116 | graph[n_i].out_edges.insert(EdgeWithWeight(n_j, 6)); |
| 117 | |
| 118 | CycleBreaker breaker; |
| 119 | |
| 120 | set<Edge> broken_edges; |
| 121 | breaker.BreakCycles(graph, &broken_edges); |
| 122 | |
| 123 | // These are required to be broken: |
| 124 | EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_b, n_a))); |
| 125 | EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_b, n_c))); |
| 126 | EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_d, n_e))); |
| 127 | EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_f, n_g))); |
| 128 | EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_h, n_i))); |
| 129 | } |
| 130 | |
Andrew de los Reyes | 8a51e5c | 2010-07-26 13:40:03 -0700 | [diff] [blame^] | 131 | TEST(CycleBreakerTest, UnblockGraphTest) { |
| 132 | int counter = 0; |
| 133 | const Vertex::Index n_a = counter++; |
| 134 | const Vertex::Index n_b = counter++; |
| 135 | const Vertex::Index n_c = counter++; |
| 136 | const Vertex::Index n_d = counter++; |
| 137 | const Graph::size_type kNodeCount = counter++; |
| 138 | |
| 139 | Graph graph(kNodeCount); |
| 140 | |
| 141 | graph[n_a].out_edges.insert(EdgeWithWeight(n_b, 1)); |
| 142 | graph[n_a].out_edges.insert(EdgeWithWeight(n_c, 1)); |
| 143 | graph[n_b].out_edges.insert(EdgeWithWeight(n_c, 2)); |
| 144 | graph[n_c].out_edges.insert(EdgeWithWeight(n_b, 2)); |
| 145 | graph[n_b].out_edges.insert(EdgeWithWeight(n_d, 2)); |
| 146 | graph[n_d].out_edges.insert(EdgeWithWeight(n_a, 2)); |
| 147 | |
| 148 | CycleBreaker breaker; |
| 149 | |
| 150 | set<Edge> broken_edges; |
| 151 | breaker.BreakCycles(graph, &broken_edges); |
| 152 | |
| 153 | // These are required to be broken: |
| 154 | EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_a, n_b))); |
| 155 | EXPECT_TRUE(utils::SetContainsKey(broken_edges, make_pair(n_a, n_c))); |
| 156 | } |
| 157 | |
Andrew de los Reyes | 35a7af1 | 2010-03-10 16:33:26 -0800 | [diff] [blame] | 158 | } // namespace chromeos_update_engine |