blob: dddf8150827c05b979b7c8ffd866117c6de9c76b [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Reyes0ce161b2010-02-22 15:27:01 -080016
Alex Deymoaab50e32014-11-10 19:55:35 -080017#include "update_engine/payload_generator/graph_utils.h"
18
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080019#include <utility>
20#include <vector>
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070021
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080022#include <gtest/gtest.h>
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070023
Alex Deymo39910dc2015-11-09 17:04:30 -080024#include "update_engine/payload_consumer/payload_constants.h"
Alex Deymo1beda782015-06-07 23:01:25 +020025#include "update_engine/payload_generator/extent_ranges.h"
Alex Deymo5c6c6552015-06-03 19:06:50 +020026#include "update_engine/payload_generator/extent_utils.h"
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080027
28using std::make_pair;
29using std::vector;
30
31namespace chromeos_update_engine {
32
33class GraphUtilsTest : public ::testing::Test {};
34
35TEST(GraphUtilsTest, SimpleTest) {
36 Graph graph(2);
Darin Petkov94817cb2013-05-08 14:33:24 +020037
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080038 graph[0].out_edges.insert(make_pair(1, EdgeProperties()));
39
40 vector<Extent>& extents = graph[0].out_edges[1].extents;
41
Alex Deymo80f70ff2016-02-10 16:08:11 -080042 EXPECT_EQ(0U, extents.size());
Alex Deymo5c6c6552015-06-03 19:06:50 +020043 AppendBlockToExtents(&extents, 0);
Alex Deymo80f70ff2016-02-10 16:08:11 -080044 EXPECT_EQ(1U, extents.size());
Alex Deymo5c6c6552015-06-03 19:06:50 +020045 AppendBlockToExtents(&extents, 1);
46 AppendBlockToExtents(&extents, 2);
Alex Deymo80f70ff2016-02-10 16:08:11 -080047 EXPECT_EQ(1U, extents.size());
Alex Deymo5c6c6552015-06-03 19:06:50 +020048 AppendBlockToExtents(&extents, 4);
Darin Petkov94817cb2013-05-08 14:33:24 +020049
Alex Deymo80f70ff2016-02-10 16:08:11 -080050 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 Petkov94817cb2013-05-08 14:33:24 +020055
Alex Deymo80f70ff2016-02-10 16:08:11 -080056 EXPECT_EQ(4U, graph_utils::EdgeWeight(graph, make_pair(0, 1)));
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080057}
58
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070059
60TEST(GraphUtilsTest, DepsTest) {
61 Graph graph(3);
Darin Petkov94817cb2013-05-08 14:33:24 +020062
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070063 graph_utils::AddReadBeforeDep(&graph[0], 1, 3);
Alex Deymo80f70ff2016-02-10 16:08:11 -080064 EXPECT_EQ(1U, graph[0].out_edges.size());
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070065 {
66 Extent& extent = graph[0].out_edges[1].extents[0];
Alex Deymo80f70ff2016-02-10 16:08:11 -080067 EXPECT_EQ(3U, extent.start_block());
68 EXPECT_EQ(1U, extent.num_blocks());
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070069 }
70 graph_utils::AddReadBeforeDep(&graph[0], 1, 4);
Alex Deymo80f70ff2016-02-10 16:08:11 -080071 EXPECT_EQ(1U, graph[0].out_edges.size());
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070072 {
73 Extent& extent = graph[0].out_edges[1].extents[0];
Alex Deymo80f70ff2016-02-10 16:08:11 -080074 EXPECT_EQ(3U, extent.start_block());
75 EXPECT_EQ(2U, extent.num_blocks());
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070076 }
77 graph_utils::AddReadBeforeDepExtents(&graph[2], 1,
78 vector<Extent>(1, ExtentForRange(5, 2)));
Alex Deymo80f70ff2016-02-10 16:08:11 -080079 EXPECT_EQ(1U, graph[2].out_edges.size());
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070080 {
81 Extent& extent = graph[2].out_edges[1].extents[0];
Alex Deymo80f70ff2016-02-10 16:08:11 -080082 EXPECT_EQ(5U, extent.start_block());
83 EXPECT_EQ(2U, extent.num_blocks());
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070084 }
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 Deymo80f70ff2016-02-10 16:08:11 -080088 EXPECT_EQ(0U, graph[2].out_edges.size());
Darin Petkov94817cb2013-05-08 14:33:24 +020089
Alex Deymo80f70ff2016-02-10 16:08:11 -080090 EXPECT_EQ(1U, graph[0].out_edges.size());
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070091 graph_utils::DropIncomingEdgesTo(&graph, 1);
Alex Deymo80f70ff2016-02-10 16:08:11 -080092 EXPECT_EQ(0U, graph[0].out_edges.size());
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070093}
94
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080095} // namespace chromeos_update_engine