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); |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 22 | |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 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); |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 34 | |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 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()); |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 40 | |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 41 | EXPECT_EQ(4, graph_utils::EdgeWeight(graph, make_pair(0, 1))); |
| 42 | } |
| 43 | |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 44 | TEST(GraphUtilsTest, AppendSparseToExtentsTest) { |
| 45 | vector<Extent> extents; |
| 46 | |
| 47 | EXPECT_EQ(0, extents.size()); |
| 48 | graph_utils::AppendBlockToExtents(&extents, kSparseHole); |
| 49 | EXPECT_EQ(1, extents.size()); |
| 50 | graph_utils::AppendBlockToExtents(&extents, 0); |
| 51 | EXPECT_EQ(2, extents.size()); |
| 52 | graph_utils::AppendBlockToExtents(&extents, kSparseHole); |
| 53 | graph_utils::AppendBlockToExtents(&extents, kSparseHole); |
| 54 | |
| 55 | ASSERT_EQ(3, extents.size()); |
| 56 | EXPECT_EQ(kSparseHole, extents[0].start_block()); |
| 57 | EXPECT_EQ(1, extents[0].num_blocks()); |
| 58 | EXPECT_EQ(0, extents[1].start_block()); |
| 59 | EXPECT_EQ(1, extents[1].num_blocks()); |
| 60 | EXPECT_EQ(kSparseHole, extents[2].start_block()); |
| 61 | EXPECT_EQ(2, extents[2].num_blocks()); |
| 62 | } |
| 63 | |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 64 | TEST(GraphUtilsTest, BlocksInExtentsTest) { |
| 65 | { |
| 66 | vector<Extent> extents; |
| 67 | EXPECT_EQ(0, graph_utils::BlocksInExtents(extents)); |
| 68 | extents.push_back(ExtentForRange(0, 1)); |
| 69 | EXPECT_EQ(1, graph_utils::BlocksInExtents(extents)); |
| 70 | extents.push_back(ExtentForRange(23, 55)); |
| 71 | EXPECT_EQ(56, graph_utils::BlocksInExtents(extents)); |
| 72 | extents.push_back(ExtentForRange(1, 2)); |
| 73 | EXPECT_EQ(58, graph_utils::BlocksInExtents(extents)); |
| 74 | } |
| 75 | { |
| 76 | google::protobuf::RepeatedPtrField<Extent> extents; |
| 77 | EXPECT_EQ(0, graph_utils::BlocksInExtents(extents)); |
| 78 | *extents.Add() = ExtentForRange(0, 1); |
| 79 | EXPECT_EQ(1, graph_utils::BlocksInExtents(extents)); |
| 80 | *extents.Add() = ExtentForRange(23, 55); |
| 81 | EXPECT_EQ(56, graph_utils::BlocksInExtents(extents)); |
| 82 | *extents.Add() = ExtentForRange(1, 2); |
| 83 | EXPECT_EQ(58, graph_utils::BlocksInExtents(extents)); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | TEST(GraphUtilsTest, DepsTest) { |
| 88 | Graph graph(3); |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 89 | |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 90 | graph_utils::AddReadBeforeDep(&graph[0], 1, 3); |
| 91 | EXPECT_EQ(1, graph[0].out_edges.size()); |
| 92 | { |
| 93 | Extent& extent = graph[0].out_edges[1].extents[0]; |
| 94 | EXPECT_EQ(3, extent.start_block()); |
| 95 | EXPECT_EQ(1, extent.num_blocks()); |
| 96 | } |
| 97 | graph_utils::AddReadBeforeDep(&graph[0], 1, 4); |
| 98 | EXPECT_EQ(1, graph[0].out_edges.size()); |
| 99 | { |
| 100 | Extent& extent = graph[0].out_edges[1].extents[0]; |
| 101 | EXPECT_EQ(3, extent.start_block()); |
| 102 | EXPECT_EQ(2, extent.num_blocks()); |
| 103 | } |
| 104 | graph_utils::AddReadBeforeDepExtents(&graph[2], 1, |
| 105 | vector<Extent>(1, ExtentForRange(5, 2))); |
| 106 | EXPECT_EQ(1, graph[2].out_edges.size()); |
| 107 | { |
| 108 | Extent& extent = graph[2].out_edges[1].extents[0]; |
| 109 | EXPECT_EQ(5, extent.start_block()); |
| 110 | EXPECT_EQ(2, extent.num_blocks()); |
| 111 | } |
| 112 | // Change most recent edge from read-before to write-before |
| 113 | graph[2].out_edges[1].write_extents.swap(graph[2].out_edges[1].extents); |
| 114 | graph_utils::DropWriteBeforeDeps(&graph[2].out_edges); |
| 115 | EXPECT_EQ(0, graph[2].out_edges.size()); |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 116 | |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 117 | EXPECT_EQ(1, graph[0].out_edges.size()); |
| 118 | graph_utils::DropIncomingEdgesTo(&graph, 1); |
| 119 | EXPECT_EQ(0, graph[0].out_edges.size()); |
| 120 | } |
| 121 | |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 122 | } // namespace chromeos_update_engine |