3f47feff37aae3f6a0856f2bff7916b73ab64d7a
[platform/upstream/ninja.git] / src / deps_log_test.cc
1 // Copyright 2012 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "deps_log.h"
16
17 #include "graph.h"
18 #include "util.h"
19 #include "test.h"
20
21 namespace {
22
23 const char kTestFilename[] = "DepsLogTest-tempfile";
24
25 struct DepsLogTest : public testing::Test {
26   virtual void SetUp() {
27     // In case a crashing test left a stale file behind.
28     unlink(kTestFilename);
29   }
30   virtual void TearDown() {
31     //unlink(kTestFilename);
32   }
33 };
34
35 TEST_F(DepsLogTest, WriteRead) {
36   State state1;
37   DepsLog log1;
38   string err;
39   EXPECT_TRUE(log1.OpenForWrite(kTestFilename, &err));
40   ASSERT_EQ("", err);
41
42   {
43     vector<Node*> deps;
44     deps.push_back(state1.GetNode("foo.h"));
45     deps.push_back(state1.GetNode("bar.h"));
46     log1.RecordDeps(state1.GetNode("out.o"), 1, deps);
47
48     deps.clear();
49     deps.push_back(state1.GetNode("foo.h"));
50     deps.push_back(state1.GetNode("bar2.h"));
51     log1.RecordDeps(state1.GetNode("out2.o"), 2, deps);
52   }
53
54   log1.Close();
55
56   State state2;
57   DepsLog log2;
58   EXPECT_TRUE(log2.Load(kTestFilename, &state2, &err));
59   ASSERT_EQ("", err);
60
61   ASSERT_EQ(log1.nodes().size(), log2.nodes().size());
62   for (int i = 0; i < (int)log1.nodes().size(); ++i) {
63     Node* node1 = log1.nodes()[i];
64     Node* node2 = log2.nodes()[i];
65     ASSERT_EQ(i, node1->id());
66     ASSERT_EQ(node1->id(), node2->id());
67   }
68
69   // log1 has no deps entries, as it was only used for writing.
70   // Manually check the entries in log2.
71   DepsLog::Deps* deps = log2.GetDeps(state2.GetNode("out.o"));
72   ASSERT_TRUE(deps);
73   ASSERT_EQ(1, deps->mtime);
74   ASSERT_EQ(2, deps->node_count);
75   ASSERT_EQ("foo.h", deps->nodes[0]->path());
76   ASSERT_EQ("bar.h", deps->nodes[1]->path());
77 }
78
79 }  // anonymous namespace