summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEvan Martin <martine@danga.com>2012-12-30 20:48:44 (GMT)
committerEvan Martin <martine@danga.com>2013-04-08 21:45:07 (GMT)
commit2f2bacc39ee47b648d6b0aea4c60cb64c41393df (patch)
tree3b41cd8dea016730843118f8da68e986fd283899 /src
parent70fd0f590c3a46a4a9ff9a160f455c5c30f5b90a (diff)
downloadNinja-2f2bacc39ee47b648d6b0aea4c60cb64c41393df.zip
Ninja-2f2bacc39ee47b648d6b0aea4c60cb64c41393df.tar.gz
Ninja-2f2bacc39ee47b648d6b0aea4c60cb64c41393df.tar.bz2
expand DepsLog test, fix two bugs it revealed
Diffstat (limited to 'src')
-rw-r--r--src/deps_log.cc9
-rw-r--r--src/deps_log.h5
-rw-r--r--src/deps_log_test.cc38
3 files changed, 36 insertions, 16 deletions
diff --git a/src/deps_log.cc b/src/deps_log.cc
index 23c9820..5732b6d 100644
--- a/src/deps_log.cc
+++ b/src/deps_log.cc
@@ -63,11 +63,11 @@ bool DepsLog::RecordDeps(Node* node, TimeStamp mtime,
fwrite(&size, 2, 1, file_);
int id = node->id();
fwrite(&id, 4, 1, file_);
- int timestamp = node->mtime();
+ int timestamp = mtime;
fwrite(&timestamp, 4, 1, file_);
for (vector<Node*>::const_iterator i = nodes.begin();
i != nodes.end(); ++i) {
- id = node->id();
+ id = (*i)->id();
fwrite(&id, 4, 1, file_);
}
@@ -89,7 +89,6 @@ bool DepsLog::Load(const string& path, State* state, string* err) {
return false;
}
- int id = 0;
for (;;) {
uint16_t size;
if (fread(&size, 2, 1, f) < 1)
@@ -127,8 +126,8 @@ bool DepsLog::Load(const string& path, State* state, string* err) {
StringPiece path(buf, size);
Node* node = state->GetNode(path);
assert(node->id() < 0);
- node->set_id(id);
- ++id;
+ node->set_id(nodes_.size());
+ nodes_.push_back(node);
}
}
if (ferror(f)) {
diff --git a/src/deps_log.h b/src/deps_log.h
index e11cca3..5ddc7bd 100644
--- a/src/deps_log.h
+++ b/src/deps_log.h
@@ -77,12 +77,17 @@ struct DepsLog {
bool Load(const string& path, State* state, string* err);
Deps* GetDeps(Node* node);
+ /// Used for tests.
+ const vector<Node*>& nodes() const { return nodes_; }
+
private:
// Write a node name record, assigning it an id.
bool RecordId(Node* node);
FILE* file_;
+ /// Maps id -> Node.
vector<Node*> nodes_;
+ /// Maps id -> deps of that id.
vector<Deps*> deps_;
friend struct DepsLogTest;
diff --git a/src/deps_log_test.cc b/src/deps_log_test.cc
index 540865b..3f47fef 100644
--- a/src/deps_log_test.cc
+++ b/src/deps_log_test.cc
@@ -39,25 +39,41 @@ TEST_F(DepsLogTest, WriteRead) {
EXPECT_TRUE(log1.OpenForWrite(kTestFilename, &err));
ASSERT_EQ("", err);
- vector<Node*> deps;
- deps.push_back(state1.GetNode("foo.h"));
- deps.push_back(state1.GetNode("bar.h"));
- log1.RecordDeps(state1.GetNode("out.o"), 1, deps);
+ {
+ vector<Node*> deps;
+ deps.push_back(state1.GetNode("foo.h"));
+ deps.push_back(state1.GetNode("bar.h"));
+ log1.RecordDeps(state1.GetNode("out.o"), 1, deps);
- deps.clear();
- deps.push_back(state1.GetNode("foo.h"));
- deps.push_back(state1.GetNode("bar2.h"));
- log1.RecordDeps(state1.GetNode("out2.o"), 2, deps);
+ deps.clear();
+ deps.push_back(state1.GetNode("foo.h"));
+ deps.push_back(state1.GetNode("bar2.h"));
+ log1.RecordDeps(state1.GetNode("out2.o"), 2, deps);
+ }
log1.Close();
State state2;
DepsLog log2;
- EXPECT_TRUE(log1.Load(kTestFilename, &state2, &err));
+ EXPECT_TRUE(log2.Load(kTestFilename, &state2, &err));
ASSERT_EQ("", err);
- state2.Dump();
- state2.GetNode("out2.o")->Dump();
+ ASSERT_EQ(log1.nodes().size(), log2.nodes().size());
+ for (int i = 0; i < (int)log1.nodes().size(); ++i) {
+ Node* node1 = log1.nodes()[i];
+ Node* node2 = log2.nodes()[i];
+ ASSERT_EQ(i, node1->id());
+ ASSERT_EQ(node1->id(), node2->id());
+ }
+
+ // log1 has no deps entries, as it was only used for writing.
+ // Manually check the entries in log2.
+ DepsLog::Deps* deps = log2.GetDeps(state2.GetNode("out.o"));
+ ASSERT_TRUE(deps);
+ ASSERT_EQ(1, deps->mtime);
+ ASSERT_EQ(2, deps->node_count);
+ ASSERT_EQ("foo.h", deps->nodes[0]->path());
+ ASSERT_EQ("bar.h", deps->nodes[1]->path());
}
} // anonymous namespace