Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 1 | // Copyright (c) 2009 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 <utility> |
| 6 | #include <vector> |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 7 | |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 8 | #include <gtest/gtest.h> |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 9 | |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 10 | #include "update_engine/graph_utils.h" |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 11 | #include "update_engine/extent_ranges.h" |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 12 | |
| 13 | using std::make_pair; |
| 14 | using std::vector; |
| 15 | |
| 16 | namespace chromeos_update_engine { |
| 17 | |
| 18 | class GraphUtilsTest : public ::testing::Test {}; |
| 19 | |
| 20 | TEST(GraphUtilsTest, SimpleTest) { |
| 21 | Graph graph(2); |
| 22 | |
| 23 | graph[0].out_edges.insert(make_pair(1, EdgeProperties())); |
| 24 | |
| 25 | vector<Extent>& extents = graph[0].out_edges[1].extents; |
| 26 | |
| 27 | EXPECT_EQ(0, extents.size()); |
| 28 | graph_utils::AppendBlockToExtents(&extents, 0); |
| 29 | EXPECT_EQ(1, extents.size()); |
| 30 | graph_utils::AppendBlockToExtents(&extents, 1); |
| 31 | graph_utils::AppendBlockToExtents(&extents, 2); |
| 32 | EXPECT_EQ(1, extents.size()); |
| 33 | graph_utils::AppendBlockToExtents(&extents, 4); |
| 34 | |
| 35 | EXPECT_EQ(2, extents.size()); |
| 36 | EXPECT_EQ(0, extents[0].start_block()); |
| 37 | EXPECT_EQ(3, extents[0].num_blocks()); |
| 38 | EXPECT_EQ(4, extents[1].start_block()); |
| 39 | EXPECT_EQ(1, extents[1].num_blocks()); |
| 40 | |
| 41 | EXPECT_EQ(4, graph_utils::EdgeWeight(graph, make_pair(0, 1))); |
| 42 | } |
| 43 | |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 44 | TEST(GraphUtilsTest, BlocksInExtentsTest) { |
| 45 | { |
| 46 | vector<Extent> extents; |
| 47 | EXPECT_EQ(0, graph_utils::BlocksInExtents(extents)); |
| 48 | extents.push_back(ExtentForRange(0, 1)); |
| 49 | EXPECT_EQ(1, graph_utils::BlocksInExtents(extents)); |
| 50 | extents.push_back(ExtentForRange(23, 55)); |
| 51 | EXPECT_EQ(56, graph_utils::BlocksInExtents(extents)); |
| 52 | extents.push_back(ExtentForRange(1, 2)); |
| 53 | EXPECT_EQ(58, graph_utils::BlocksInExtents(extents)); |
| 54 | } |
| 55 | { |
| 56 | google::protobuf::RepeatedPtrField<Extent> extents; |
| 57 | EXPECT_EQ(0, graph_utils::BlocksInExtents(extents)); |
| 58 | *extents.Add() = ExtentForRange(0, 1); |
| 59 | EXPECT_EQ(1, graph_utils::BlocksInExtents(extents)); |
| 60 | *extents.Add() = ExtentForRange(23, 55); |
| 61 | EXPECT_EQ(56, graph_utils::BlocksInExtents(extents)); |
| 62 | *extents.Add() = ExtentForRange(1, 2); |
| 63 | EXPECT_EQ(58, graph_utils::BlocksInExtents(extents)); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | TEST(GraphUtilsTest, DepsTest) { |
| 68 | Graph graph(3); |
| 69 | |
| 70 | graph_utils::AddReadBeforeDep(&graph[0], 1, 3); |
| 71 | EXPECT_EQ(1, graph[0].out_edges.size()); |
| 72 | { |
| 73 | Extent& extent = graph[0].out_edges[1].extents[0]; |
| 74 | EXPECT_EQ(3, extent.start_block()); |
| 75 | EXPECT_EQ(1, extent.num_blocks()); |
| 76 | } |
| 77 | graph_utils::AddReadBeforeDep(&graph[0], 1, 4); |
| 78 | EXPECT_EQ(1, graph[0].out_edges.size()); |
| 79 | { |
| 80 | Extent& extent = graph[0].out_edges[1].extents[0]; |
| 81 | EXPECT_EQ(3, extent.start_block()); |
| 82 | EXPECT_EQ(2, extent.num_blocks()); |
| 83 | } |
| 84 | graph_utils::AddReadBeforeDepExtents(&graph[2], 1, |
| 85 | vector<Extent>(1, ExtentForRange(5, 2))); |
| 86 | EXPECT_EQ(1, graph[2].out_edges.size()); |
| 87 | { |
| 88 | Extent& extent = graph[2].out_edges[1].extents[0]; |
| 89 | EXPECT_EQ(5, extent.start_block()); |
| 90 | EXPECT_EQ(2, extent.num_blocks()); |
| 91 | } |
| 92 | // Change most recent edge from read-before to write-before |
| 93 | graph[2].out_edges[1].write_extents.swap(graph[2].out_edges[1].extents); |
| 94 | graph_utils::DropWriteBeforeDeps(&graph[2].out_edges); |
| 95 | EXPECT_EQ(0, graph[2].out_edges.size()); |
| 96 | |
| 97 | EXPECT_EQ(1, graph[0].out_edges.size()); |
| 98 | graph_utils::DropIncomingEdgesTo(&graph, 1); |
| 99 | EXPECT_EQ(0, graph[0].out_edges.size()); |
| 100 | } |
| 101 | |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 102 | } // namespace chromeos_update_engine |