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 
17 #include "update_engine/payload_generator/graph_utils.h"
18 
19 #include <utility>
20 #include <vector>
21 
22 #include <gtest/gtest.h>
23 
24 #include "update_engine/payload_consumer/payload_constants.h"
25 #include "update_engine/payload_generator/extent_ranges.h"
26 #include "update_engine/payload_generator/extent_utils.h"
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);
37 
38   graph[0].out_edges.insert(make_pair(1, EdgeProperties()));
39 
40   vector<Extent>& extents = graph[0].out_edges[1].extents;
41 
42   EXPECT_EQ(0U, extents.size());
43   AppendBlockToExtents(&extents, 0);
44   EXPECT_EQ(1U, extents.size());
45   AppendBlockToExtents(&extents, 1);
46   AppendBlockToExtents(&extents, 2);
47   EXPECT_EQ(1U, extents.size());
48   AppendBlockToExtents(&extents, 4);
49 
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());
55 
56   EXPECT_EQ(4U, graph_utils::EdgeWeight(graph, make_pair(0, 1)));
57 }
58 
59 TEST(GraphUtilsTest, DepsTest) {
60   Graph graph(3);
61 
62   graph_utils::AddReadBeforeDep(&graph[0], 1, 3);
63   EXPECT_EQ(1U, graph[0].out_edges.size());
64   {
65     Extent& extent = graph[0].out_edges[1].extents[0];
66     EXPECT_EQ(3U, extent.start_block());
67     EXPECT_EQ(1U, extent.num_blocks());
68   }
69   graph_utils::AddReadBeforeDep(&graph[0], 1, 4);
70   EXPECT_EQ(1U, graph[0].out_edges.size());
71   {
72     Extent& extent = graph[0].out_edges[1].extents[0];
73     EXPECT_EQ(3U, extent.start_block());
74     EXPECT_EQ(2U, extent.num_blocks());
75   }
76   graph_utils::AddReadBeforeDepExtents(
77       &graph[2], 1, vector<Extent>(1, ExtentForRange(5, 2)));
78   EXPECT_EQ(1U, graph[2].out_edges.size());
79   {
80     Extent& extent = graph[2].out_edges[1].extents[0];
81     EXPECT_EQ(5U, extent.start_block());
82     EXPECT_EQ(2U, extent.num_blocks());
83   }
84   // Change most recent edge from read-before to write-before
85   graph[2].out_edges[1].write_extents.swap(graph[2].out_edges[1].extents);
86   graph_utils::DropWriteBeforeDeps(&graph[2].out_edges);
87   EXPECT_EQ(0U, graph[2].out_edges.size());
88 
89   EXPECT_EQ(1U, graph[0].out_edges.size());
90   graph_utils::DropIncomingEdgesTo(&graph, 1);
91   EXPECT_EQ(0U, graph[0].out_edges.size());
92 }
93 
94 }  // namespace chromeos_update_engine
95