diff options
author | Evan Martin <martine@danga.com> | 2011-05-01 02:30:30 (GMT) |
---|---|---|
committer | Evan Martin <martine@danga.com> | 2011-05-01 02:51:45 (GMT) |
commit | b95343a7745c7b717d039fc532f857acd5796874 (patch) | |
tree | 6ce8a091430dfb6f0f22cc3f2517ff5f0327eb5e | |
parent | a3aa545229420e8e816ef5983a0983ab590d0ca4 (diff) | |
download | Ninja-b95343a7745c7b717d039fc532f857acd5796874.zip Ninja-b95343a7745c7b717d039fc532f857acd5796874.tar.gz Ninja-b95343a7745c7b717d039fc532f857acd5796874.tar.bz2 |
add test for RootNodes(); fix comment
-rw-r--r-- | src/graph_test.cc | 16 | ||||
-rw-r--r-- | src/ninja.h | 2 | ||||
-rw-r--r-- | src/ninja_jumble.cc | 22 |
3 files changed, 28 insertions, 12 deletions
diff --git a/src/graph_test.cc b/src/graph_test.cc index 7e0eac0..7b28e87 100644 --- a/src/graph_test.cc +++ b/src/graph_test.cc @@ -97,3 +97,19 @@ TEST_F(GraphTest, PathWithCurrentDirectory) { EXPECT_FALSE(GetNode("out.o")->dirty_); } + +TEST_F(GraphTest, RootNodes) { + ASSERT_NO_FATAL_FAILURE(AssertParse(&state_, +"build out1: cat in1\n" +"build mid1: cat in1\n" +"build out2: cat mid1\n" +"build out3 out4: cat mid1\n")); + + string err; + vector<Node*> root_nodes = state_.RootNodes(&err); + EXPECT_EQ(4, root_nodes.size()); + for (size_t i = 0; i < root_nodes.size(); ++i) { + string name = root_nodes[i]->file_->path_; + EXPECT_EQ("out", name.substr(0, 3)); + } +} diff --git a/src/ninja.h b/src/ninja.h index 1ff4e32..180f90c 100644 --- a/src/ninja.h +++ b/src/ninja.h @@ -88,7 +88,7 @@ struct State { Node* LookupNode(const string& path); void AddIn(Edge* edge, const string& path); void AddOut(Edge* edge, const string& path); - /// @return the root node(s) of the graph. (Root nodes have no input edges). + /// @return the root node(s) of the graph. (Root nodes have no output edges). /// @param error where to write the error message if somethings went wrong. vector<Node*> RootNodes(string* error); diff --git a/src/ninja_jumble.cc b/src/ninja_jumble.cc index e6a0de6..cb0ca5e 100644 --- a/src/ninja_jumble.cc +++ b/src/ninja_jumble.cc @@ -183,20 +183,20 @@ void State::AddOut(Edge* edge, const string& path) { node->in_edge_ = edge; } -vector<Node*> State::RootNodes(string* error) -{ - assert(error); +vector<Node*> State::RootNodes(string* err) { vector<Node*> root_nodes; // Search for nodes with no output. - for (vector<Edge*>::iterator e = edges_.begin(); e != edges_.end(); ++e) - for (vector<Node*>::iterator outs = (*e)->outputs_.begin(); - outs != (*e)->outputs_.end(); - ++outs) - if ((*outs)->out_edges_.size() == 0) - root_nodes.push_back(*outs); - if (!edges_.empty() && root_nodes.empty()) { - *error = "could not determine root nodes of build graph"; + for (vector<Edge*>::iterator e = edges_.begin(); e != edges_.end(); ++e) { + for (vector<Node*>::iterator out = (*e)->outputs_.begin(); + out != (*e)->outputs_.end(); ++out) { + if ((*out)->out_edges_.empty()) + root_nodes.push_back(*out); + } } + + if (!edges_.empty() && root_nodes.empty()) + *err = "could not determine root nodes of build graph"; + assert(edges_.empty() || !root_nodes.empty()); return root_nodes; } |