Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 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 | // |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 16 | |
Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_generator/graph_utils.h" |
| 18 | |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 19 | #include <utility> |
| 20 | #include <vector> |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 21 | |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 22 | #include <gtest/gtest.h> |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 23 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 24 | #include "update_engine/payload_consumer/payload_constants.h" |
Alex Deymo | 1beda78 | 2015-06-07 23:01:25 +0200 | [diff] [blame] | 25 | #include "update_engine/payload_generator/extent_ranges.h" |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 26 | #include "update_engine/payload_generator/extent_utils.h" |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 27 | |
| 28 | using std::make_pair; |
| 29 | using std::vector; |
| 30 | |
| 31 | namespace chromeos_update_engine { |
| 32 | |
| 33 | class GraphUtilsTest : public ::testing::Test {}; |
| 34 | |
| 35 | TEST(GraphUtilsTest, SimpleTest) { |
| 36 | Graph graph(2); |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 37 | |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 38 | graph[0].out_edges.insert(make_pair(1, EdgeProperties())); |
| 39 | |
| 40 | vector<Extent>& extents = graph[0].out_edges[1].extents; |
| 41 | |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 42 | EXPECT_EQ(0U, extents.size()); |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 43 | AppendBlockToExtents(&extents, 0); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 44 | EXPECT_EQ(1U, extents.size()); |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 45 | AppendBlockToExtents(&extents, 1); |
| 46 | AppendBlockToExtents(&extents, 2); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 47 | EXPECT_EQ(1U, extents.size()); |
Alex Deymo | 5c6c655 | 2015-06-03 19:06:50 +0200 | [diff] [blame] | 48 | AppendBlockToExtents(&extents, 4); |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 49 | |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 50 | EXPECT_EQ(2U, extents.size()); |
| 51 | EXPECT_EQ(0U, extents[0].start_block()); |
| 52 | EXPECT_EQ(3U, extents[0].num_blocks()); |
| 53 | EXPECT_EQ(4U, extents[1].start_block()); |
| 54 | EXPECT_EQ(1U, extents[1].num_blocks()); |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 55 | |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 56 | EXPECT_EQ(4U, graph_utils::EdgeWeight(graph, make_pair(0, 1))); |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 59 | |
| 60 | TEST(GraphUtilsTest, DepsTest) { |
| 61 | Graph graph(3); |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 62 | |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 63 | graph_utils::AddReadBeforeDep(&graph[0], 1, 3); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 64 | EXPECT_EQ(1U, graph[0].out_edges.size()); |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 65 | { |
| 66 | Extent& extent = graph[0].out_edges[1].extents[0]; |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 67 | EXPECT_EQ(3U, extent.start_block()); |
| 68 | EXPECT_EQ(1U, extent.num_blocks()); |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 69 | } |
| 70 | graph_utils::AddReadBeforeDep(&graph[0], 1, 4); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 71 | EXPECT_EQ(1U, graph[0].out_edges.size()); |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 72 | { |
| 73 | Extent& extent = graph[0].out_edges[1].extents[0]; |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 74 | EXPECT_EQ(3U, extent.start_block()); |
| 75 | EXPECT_EQ(2U, extent.num_blocks()); |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 76 | } |
| 77 | graph_utils::AddReadBeforeDepExtents(&graph[2], 1, |
| 78 | vector<Extent>(1, ExtentForRange(5, 2))); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 79 | EXPECT_EQ(1U, graph[2].out_edges.size()); |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 80 | { |
| 81 | Extent& extent = graph[2].out_edges[1].extents[0]; |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 82 | EXPECT_EQ(5U, extent.start_block()); |
| 83 | EXPECT_EQ(2U, extent.num_blocks()); |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 84 | } |
| 85 | // Change most recent edge from read-before to write-before |
| 86 | graph[2].out_edges[1].write_extents.swap(graph[2].out_edges[1].extents); |
| 87 | graph_utils::DropWriteBeforeDeps(&graph[2].out_edges); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 88 | EXPECT_EQ(0U, graph[2].out_edges.size()); |
Darin Petkov | 94817cb | 2013-05-08 14:33:24 +0200 | [diff] [blame] | 89 | |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 90 | EXPECT_EQ(1U, graph[0].out_edges.size()); |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 91 | graph_utils::DropIncomingEdgesTo(&graph, 1); |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 92 | EXPECT_EQ(0U, graph[0].out_edges.size()); |
Andrew de los Reyes | 1bc16ab | 2010-10-06 15:07:21 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Andrew de los Reyes | 0ce161b | 2010-02-22 15:27:01 -0800 | [diff] [blame] | 95 | } // namespace chromeos_update_engine |