huangcm
2025-08-25 f350412dc55c15118d0a7925d1071877498e5e24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// Copyright (C) 2010 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
 
#include "update_engine/payload_generator/tarjan.h"
 
#include <string>
#include <utility>
 
#include <base/logging.h>
#include <base/stl_util.h>
#include <gtest/gtest.h>
 
#include "update_engine/payload_generator/graph_types.h"
 
using std::make_pair;
using std::string;
using std::vector;
 
namespace chromeos_update_engine {
 
class TarjanAlgorithmTest : public ::testing::Test {};
 
TEST(TarjanAlgorithmTest, SimpleTest) {
  const Vertex::Index n_a = 0;
  const Vertex::Index n_b = 1;
  const Vertex::Index n_c = 2;
  const Vertex::Index n_d = 3;
  const Vertex::Index n_e = 4;
  const Vertex::Index n_f = 5;
  const Vertex::Index n_g = 6;
  const Vertex::Index n_h = 7;
  const Graph::size_type kNodeCount = 8;
 
  Graph graph(kNodeCount);
 
  graph[n_a].out_edges.insert(make_pair(n_e, EdgeProperties()));
  graph[n_a].out_edges.insert(make_pair(n_f, EdgeProperties()));
  graph[n_b].out_edges.insert(make_pair(n_a, EdgeProperties()));
  graph[n_c].out_edges.insert(make_pair(n_d, EdgeProperties()));
  graph[n_d].out_edges.insert(make_pair(n_e, EdgeProperties()));
  graph[n_d].out_edges.insert(make_pair(n_f, EdgeProperties()));
  graph[n_e].out_edges.insert(make_pair(n_b, EdgeProperties()));
  graph[n_e].out_edges.insert(make_pair(n_c, EdgeProperties()));
  graph[n_e].out_edges.insert(make_pair(n_f, EdgeProperties()));
  graph[n_f].out_edges.insert(make_pair(n_g, EdgeProperties()));
  graph[n_g].out_edges.insert(make_pair(n_h, EdgeProperties()));
  graph[n_h].out_edges.insert(make_pair(n_g, EdgeProperties()));
 
  TarjanAlgorithm tarjan;
 
  for (Vertex::Index i = n_a; i <= n_e; i++) {
    vector<Vertex::Index> vertex_indexes;
    tarjan.Execute(i, &graph, &vertex_indexes);
 
    EXPECT_EQ(5U, vertex_indexes.size());
    EXPECT_TRUE(base::ContainsValue(vertex_indexes, n_a));
    EXPECT_TRUE(base::ContainsValue(vertex_indexes, n_b));
    EXPECT_TRUE(base::ContainsValue(vertex_indexes, n_c));
    EXPECT_TRUE(base::ContainsValue(vertex_indexes, n_d));
    EXPECT_TRUE(base::ContainsValue(vertex_indexes, n_e));
  }
 
  {
    vector<Vertex::Index> vertex_indexes;
    tarjan.Execute(n_f, &graph, &vertex_indexes);
 
    EXPECT_EQ(1U, vertex_indexes.size());
    EXPECT_TRUE(base::ContainsValue(vertex_indexes, n_f));
  }
 
  for (Vertex::Index i = n_g; i <= n_h; i++) {
    vector<Vertex::Index> vertex_indexes;
    tarjan.Execute(i, &graph, &vertex_indexes);
 
    EXPECT_EQ(2U, vertex_indexes.size());
    EXPECT_TRUE(base::ContainsValue(vertex_indexes, n_g));
    EXPECT_TRUE(base::ContainsValue(vertex_indexes, n_h));
  }
}
 
}  // namespace chromeos_update_engine