blob: 3bd9d16ae39485bc750932aa58943ae28d307560 [file] [log] [blame]
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -08001// 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 Reyes1bc16ab2010-10-06 15:07:21 -07007
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -08008#include <gtest/gtest.h>
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -07009
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080010#include "update_engine/graph_utils.h"
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070011#include "update_engine/extent_ranges.h"
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080012
13using std::make_pair;
14using std::vector;
15
16namespace chromeos_update_engine {
17
18class GraphUtilsTest : public ::testing::Test {};
19
20TEST(GraphUtilsTest, SimpleTest) {
21 Graph graph(2);
Darin Petkov94817cb2013-05-08 14:33:24 +020022
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080023 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 Petkov94817cb2013-05-08 14:33:24 +020034
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080035 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 Petkov94817cb2013-05-08 14:33:24 +020040
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080041 EXPECT_EQ(4, graph_utils::EdgeWeight(graph, make_pair(0, 1)));
42}
43
Darin Petkov94817cb2013-05-08 14:33:24 +020044TEST(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 Reyes1bc16ab2010-10-06 15:07:21 -070064TEST(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
87TEST(GraphUtilsTest, DepsTest) {
88 Graph graph(3);
Darin Petkov94817cb2013-05-08 14:33:24 +020089
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -070090 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 Petkov94817cb2013-05-08 14:33:24 +0200116
Andrew de los Reyes1bc16ab2010-10-06 15:07:21 -0700117 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 Reyes0ce161b2010-02-22 15:27:01 -0800122} // namespace chromeos_update_engine